gpt4 book ai didi

python - LSTM 和 CNN : ValueError: Error when checking target: expected time_distributed_1 to have 3 dimensions, 但得到形状为 (400, 256) 的数组

转载 作者:行者123 更新时间:2023-11-28 22:25:19 24 4
gpt4 key购买 nike

我想在我的数据上应用CNNLSTM,我只选择了一小部分数据;我的训练数据大小是 (400,50),我的测试数据是 (200,50)。只有 CNN 模型,它没有任何错误,我只是在添加 LSTM 模型时有很多错误:

model = Sequential()
model.add(Conv1D(filters=8,
kernel_size=16,
padding='valid',
activation='relu',
strides=1, input_shape=(50,1)))
model.add(MaxPooling1D(pool_size=2,strides=None, padding='valid', input_shape=(50,1))) # strides=None means strides=pool_size
model.add(Conv1D(filters=8,
kernel_size=8,
padding='valid',
activation='relu',
strides=1))
model.add(MaxPooling1D(pool_size=2,strides=None, padding='valid',input_shape=(50,1)))
model.add(LSTM(32, return_sequences=True,
activation='tanh', recurrent_activation='hard_sigmoid',
dropout=0.2,recurrent_dropout=0.2)) # 100 num of LSTM units
model.add(LSTM(32, return_sequences=True,
activation='tanh', recurrent_activation='hard_sigmoid',
dropout=0.2,recurrent_dropout=0.2))
model.add(LSTM(32, return_sequences=True,
activation='tanh', recurrent_activation='hard_sigmoid',
dropout=0.2,recurrent_dropout=0.2))
model.add(LSTM(32, return_sequences=True,
activation='tanh', recurrent_activation='hard_sigmoid',
dropout=0.2,recurrent_dropout=0.2))
model.add(LSTM(32, return_sequences=True,
activation='tanh', recurrent_activation='hard_sigmoid',
dropout=0.2,recurrent_dropout=0.2))
model.add(TimeDistributed(Dense(256, activation='softmax')))

# # # 4. Compile model
print('########################### Compilation of the model ######################################')
model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
print(model.summary())
print('###########################Fitting the model ######################################')
# # # # # 5. Fit model on training data
x_train = x_train.reshape((400,50,1))
print(x_train.shape) # (400,50,1)
x_test = x_test.reshape((200,50,1))
print(x_test.shape) # (200,50,1)
model.fit(x_train, y_train, batch_size=100, epochs=100,verbose=0)
print(model.summary())
# # # # # 6. Evaluate model on test data
score = model.evaluate(x_test, y_test, verbose=0)
print (score)

这是错误:

Traceback (most recent call last):
File "CNN_LSTM_Based_Attack.py", line 156, in <module>
model.fit(x_train, y_train, batch_size=100, epochs=100,verbose=0)
File "/home/doc/.local/lib/python2.7/site-packages/keras/models.py", line 853, in fit
initial_epoch=initial_epoch)
File "/home/doc/.local/lib/python2.7/site-packages/keras/engine/training.py", line 1424, in fit
batch_size=batch_size)
File "/home/doc/.local/lib/python2.7/site-packages/keras/engine/training.py", line 1304, in _standardize_user_data
exception_prefix='target')
File "/home/doc/.local/lib/python2.7/site-packages/keras/engine/training.py", line 127, in _standardize_input_data
str(array.shape))
ValueError: Error when checking target: expected time_distributed_1 to have 3 dimensions, but got array with shape (400, 256)

您可以在这里找到该模型的完整摘要:(我是 LSTM 的新手,这是我第一次使用它)。

_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv1d_1 (Conv1D) (None, 35, 8) 136
_________________________________________________________________
max_pooling1d_1 (MaxPooling1 (None, 17, 8) 0
_________________________________________________________________
dropout_1 (Dropout) (None, 17, 8) 0
_________________________________________________________________
conv1d_2 (Conv1D) (None, 10, 8) 520
_________________________________________________________________
max_pooling1d_2 (MaxPooling1 (None, 5, 8) 0
_________________________________________________________________
dropout_2 (Dropout) (None, 5, 8) 0
_________________________________________________________________
lstm_1 (LSTM) (None, 5, 32) 5248
_________________________________________________________________
lstm_2 (LSTM) (None, 5, 32) 8320
_________________________________________________________________
lstm_3 (LSTM) (None, 5, 32) 8320
_________________________________________________________________
lstm_4 (LSTM) (None, 5, 32) 8320
_________________________________________________________________
lstm_5 (LSTM) (None, 5, 32) 8320
_________________________________________________________________
time_distributed_1 (TimeDist (None, 5, 256) 8448
=================================================================
Total params: 47,632
Trainable params: 47,632
Non-trainable params: 0
_________________________________________________________________

当我替换这行代码时:

model.add(LSTM(32, return_sequences=True,
activation='tanh', recurrent_activation='hard_sigmoid',
dropout=0.2,recurrent_dropout=0.2)) # 100 num of LSTM units
model.add(LSTM(32, return_sequences=True,
activation='tanh', recurrent_activation='hard_sigmoid',
dropout=0.2,recurrent_dropout=0.2))
model.add(LSTM(32, return_sequences=True,
activation='tanh', recurrent_activation='hard_sigmoid',
dropout=0.2,recurrent_dropout=0.2))
model.add(LSTM(32, return_sequences=True,
activation='tanh', recurrent_activation='hard_sigmoid',
dropout=0.2,recurrent_dropout=0.2))
model.add(LSTM(32, return_sequences=True,
activation='tanh', recurrent_activation='hard_sigmoid',
dropout=0.2,recurrent_dropout=0.2))
model.add(TimeDistributed(Dense(256, activation='softmax')))

只有这一行:

model.add(LSTM(26, activation='tanh'))

比它工作得很好。

如果你能帮助我,我将不胜感激。

最佳答案

因此 LSTM 层期望输入形状(样本、时间步长、特征)。堆叠 LSTM 时,您应该 return_sequences = True。这将给出形状(样本、时间步长、单位)的输出,从而允许堆栈组合在一起——如果您只想提前预测一步(即下一个值),则应在最后一个 LSTM 层上设置 return_sequences = False在序列/时间序列中) - 如果你不这样做,它将预测与输入中相同的时间步数。您当然也可以预测不同的数字(例如,给定 50 个过去的观察结果预测接下来的 10 个,但这在 Keras 中有点棘手)。

在您的情况下,Conv/MaxPool-layers 输出 5 个“时间步长”,并且您在最后一个 LSTM 层上有 return_sequences = True - 因此您的“y”必须具有形状 (Samples, 5, 256) - 否则转 return_sequences = False 在最后一层并且不使用 TimeDistributed,因为您只预测一个时间步长。

关于python - LSTM 和 CNN : ValueError: Error when checking target: expected time_distributed_1 to have 3 dimensions, 但得到形状为 (400, 256) 的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45590240/

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