gpt4 book ai didi

python - Keras LSTM 训练。如何调整我的输入数据?

转载 作者:行者123 更新时间:2023-12-01 01:47:20 25 4
gpt4 key购买 nike

我有一个包含 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:检查模型输入时出错:您传递给模型的 Numpy 数组列表不是模型预期的大小。预计会看到 3 个数组,但得到的却是以下 1 个数组的列表...

我应该如何 reshape 我的数据来解决问题?

最佳答案

您神奇地假设具有 3 个时间序列的单个输入 X_train 将分为 4 个 channel 并分配给不同的输入。好吧,这并没有发生,这就是错误所提示的。您有 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

关于python - Keras LSTM 训练。如何调整我的输入数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51139888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com