首页 > 解决方案 > Keras LSTM 训练。如何塑造我的输入数据?

问题描述

我有一个包含 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...

我应该如何重塑我的数据来解决这个问题?

标签: pythonkerastime-serieslstmdatashape

解决方案


您神奇地假设具有 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这样的非线性激活函数。


推荐阅读