python - 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...
我应该如何重塑我的数据来解决这个问题?
解决方案
您神奇地假设具有 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这样的非线性激活函数。
推荐阅读
- matlab - 3D 表面颜色图的平滑/插值
- c# - 字典中的重复键
- php - 尝试使用 curl 获取信息但收到 http 错误
- python - Discord Python - 如何从用户 ID 中获取用户名?
- sql-server-2012 - 左连接视图在 SQL Server 中花费了太多时间
- android - 如何移动(翻译动画)具有可点击区域的按钮?
- php - 多人使用时,我的项目目录中是否有生成的二维码会混在一起?
- c - 如何将值从第一个数组复制到另一个数组?
- reactjs - 未找到模块:无法解析“C:\Users\USER\Desktop\test\src”中的“react”
- python - 如何包络非周期信号?