【问题标题】:Keras LSTM training. How to shape my input data?Keras LSTM 训练。如何塑造我的输入数据?
【发布时间】:2018-12-10 21:37:17
【问题描述】:

我有一个包含 3000 个观察值的数据集。每个观察由 3 个长度为 200 个样本的时间序列组成。作为输出,我有 5 个类标签。

所以我构建训练作为测试集如下:

test_split = round(num_samples * 3 / 4)
X_train = X_all[:test_split, :, :] # Start upto just before test_split
y_train = y_all[:test_split]
X_test = X_all[test_split:, :, :] # From test_split to end
y_test = y_all[test_split:]

# Print shapes and class labels
print(X_train.shape)
print(y_train.shape)


> (2250, 200, 3)
> (22250, 5)

我使用 Keras 功能 API 构建我的网络:

from keras.models import  Model
from keras.layers import Dense, Activation, Input, Dropout, concatenate
from keras.layers.recurrent import LSTM
from keras.constraints import maxnorm
from keras.optimizers import SGD
from keras.callbacks import EarlyStopping

series_len = 200
num_RNN_neurons = 64
ch1 = Input(shape=(series_len, 1), name='ch1')
ch2 = Input(shape=(series_len, 1), name='ch2')
ch3 = Input(shape=(series_len, 1), name='ch3')

ch1_layer = LSTM(num_RNN_neurons, return_sequences=False)(ch1)
ch2_layer = LSTM(num_RNN_neurons, return_sequences=False)(ch2)
ch3_layer = LSTM(num_RNN_neurons, return_sequences=False)(ch3)


visible = concatenate([
    ch1_layer,
    ch2_layer,
    ch3_layer])


hidden1 = Dense(30, activation='linear', name='weighted_average_channels')(visible)
output = Dense(num_classes, activation='softmax')(hidden1)

model = Model(inputs= [ch1, ch2, ch3], outputs=output)

# Compile model
model.compile(loss='categorical_crossentropy', optimizer=SGD(), metrics=['accuracy'])
monitor = EarlyStopping(monitor='val_loss', min_delta=1e-4, patience=5, verbose=1, mode='auto')

然后,我尝试拟合模型:

# Fit the model
model.fit(X_train, y_train, 
          epochs=epochs, 
          batch_size=batch_size,
          validation_data=(X_test, y_test),
          callbacks=[monitor],
          verbose=1)

我收到以下错误:

ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 3 array(s), but instead got the following list of 1 arrays...

我应该如何重塑我的数据以解决问题?

【问题讨论】:

    标签: python keras time-series lstm datashape


    【解决方案1】:

    您神奇地假设具有 3 个时间序列的单个输入 X_train 将分成 4 个通道并分配给不同的输入。好吧,这不会发生,这就是错误所抱怨的。您有 1 个输入:

    ch123_in = Input(shape=(series_len, 3), name='ch123')
    latent = LSTM(num_RNN_neurons)(ch123_in)
    hidden1 = Dense(30, activation='linear', name='weighted_average_channels')(latent)
    

    通过将系列合并到单个 LSTM 中,该模型也可以拾取跨时间序列的关系。现在你的目标形状必须是y_train.shape == (2250, 5),第一个维度必须匹配X_train.shape[0]

    另一点是Dense 层具有线性激活,这几乎没有用,因为它不提供任何非线性。您可能想要使用像 relu 这样的非线性激活函数。

    【讨论】:

    • 感谢您的帮助。我在复制/粘贴代码时出错,因为我想将 3 个时间序列跨越到 3 个通道(我已经编辑了代码 sn-p)。您的解决方案有效。但是我仍然不明白如何训练 3 个独立的 LSTM。
    • 您可以对ch1_in = ch123_in[:,:,0] 进行切片,这将为您提供第一个形状为 (batch_size, series_len, 1) 的通道。然后你可以像以前一样为每个通道运行独立的 LSTM。