【发布时间】:2019-02-22 14:57:26
【问题描述】:
我正在尝试了解如何正确地将数据输入到我的 keras 模型中,以使用 LSTM 神经网络将多元时间序列数据分为三类。
我已经查看了不同的资源 - 主要是 Jason Brownlee post1、post2、post3 的这三篇优秀博客文章,其他 SO questions 和不同的 papers,但那里没有提供任何信息完全符合我的问题案例,我无法确定我的数据预处理/将其输入模型是否正确,所以我想如果我在这里指定我的确切条件,我可能会得到一些帮助。
我要做的是对多元时间序列数据进行分类,其原始形式的结构如下:
我有 200 个样本
一个样本就是一个 csv 文件。
一个样本可以有 1 到 50 个特征(即 csv 文件有 1 到 50 列)。
在固定的时间内“跟踪”每个功能的值 步数,假设为 100(即每个 csv 文件正好有 100 行)。
每个 csv 文件都有三个类别之一(“好”、“太小”、“太大”)
所以我现在的状态如下:
我有一个具有以下结构的 numpy 数组 "samples":
# array holding all samples
[
# sample 1
[
# feature 1 of sample 1
[ 0.1, 0.2, 0.3, 0.2, 0.3, 0.1, 0.2, 0.4, 0.5, 0.1, ... ], # "time series" of feature 1
# feature 2 of sample 1
[ 0.5, 0.6, 0.7, 0.6, 0.4, 0.3, 0.2, 0.1, -0.1, -0.2, ... ], # "time series" of feature 2
... # up to 50 features
],
# sample 2
[
# feature 1 of sample 2
[ 0.1, 0.2, 0.3, 0.2, 0.3, 0.1, 0.2, 0.4, 0.5, 0.1, ... ], # "time series" of feature 1
# feature 2 of sample 2
[ 0.5, 0.6, 0.7, 0.6, 0.4, 0.3, 0.2, 0.1, -0.1, -0.2, ... ], # "time series" of feature 2
... # up to 50 features
],
... # up to sample no. 200
]
我还有一个与 "samples" 数组长度相同的 numpy 数组 "labels"(即 200)。标签的编码方式如下:
- “好”= 0
- “太小” = 1
- “太大”= 2
[0, 2, 2, 1, 0, 1, 2, 0, 0, 0, 1, 2, ... ] # up to label no. 200
这个 "labels" 数组然后用 keras 的 to_categorical 函数编码
to_categorical(labels, len(np.unique(labels)))
我的模型定义目前如下所示:
max_nb_features = 50
nb_time_steps = 100
model = Sequential()
model.add(LSTM(5, input_shape=(max_nb_features, nb_time_steps)))
model.add(Dense(3, activation='softmax'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
- LSTM 层中的 5 个单元暂时只是随机挑选的
- 我的三个类的密集层中的 3 个输出神经元
然后我将数据拆分为训练/测试数据:
samples_train, samples_test, labels_train, labels_test = train_test_split(samples, labels, test_size=0.33)
这给我们留下了 134 个用于训练的样本和 66 个用于测试的样本。
我目前遇到的问题是以下代码不起作用:
model.fit(samples_train, labels_train, epochs=1, batch_size=1)
错误如下:
Traceback (most recent call last):
File "lstm_test.py", line 152, in <module>
model.fit(samples_train, labels_train, epochs=1, batch_size=1)
File "C:\Program Files\Python36\lib\site-packages\keras\models.py", line 1002, in fit
validation_steps=validation_steps)
File "C:\Program Files\Python36\lib\site-packages\keras\engine\training.py", line 1630, in fit
batch_size=batch_size)
File "C:\Program Files\Python36\lib\site-packages\keras\engine\training.py", line 1476, in _standardize_user_data
exception_prefix='input')
File "C:\Program Files\Python36\lib\site-packages\keras\engine\training.py", line 113, in _standardize_input_data
'with shape ' + str(data_shape))
ValueError: Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (134, 1)
对我来说,这似乎不起作用,因为我的样本可以具有可变数量的功能。 如果我使用“假”(生成)数据,其中所有参数都相同,除了每个样本具有完全相同数量的特征(50),代码可以工作。
现在我想了解的是:
- 我对如何为 LSTM 输入构建数据的一般假设是否正确?参数(
batch_size、input_shape)是否正确/合理? - keras LSTM 模型通常能够处理具有不同特征量的样本吗?
- 如果是,我必须如何调整我的代码才能使用不同数量的功能?
- 如果不是,“零填充”(填充)样本中少于 50 个特征的列是否有效?还有其他首选方法可以实现我的目标吗?
【问题讨论】:
标签: python machine-learning keras time-series lstm