Python-Keras:LSTM数据结构ValueError [英] Python - Keras: LSTM data structure ValueError
问题描述
我正在尝试使用return_sequence训练LSTM模型以返回每个输入时间步长的隐藏状态输出,以解决回归问题.
I'm trying to train an LSTM model using return_sequence to return the hidden state output for each input time step, solving a regression problem.
我的数据形状为:(31、2720、16),即31批具有16个特征的2720个样本.
我的目标形状是:(31,2720,1),即31批2720行包含1个值.
My Data shape is: (31, 2720, 16) i.e 31 batches of 2720 samples with 16 features.
My target shape is: (31, 2720, 1) i.e 31 batches of 2720 rows containing 1 value.
我建立了以下模型:
model = Sequential()
opt = Adam(learning_rate=0.0001, clipnorm=1)
num_samples = train_x.shape[1]
num_features = train_x.shape[2]
model.add(Masking(mask_value=-10., input_shape=(num_samples, num_features)))
model.add(LSTM(32, return_sequences=True, stateful=False, activation='tanh'))
model.add(Dropout(0.3))
#this is the last LSTM layer, use return_sequences=False
model.add(LSTM(16, return_sequences=False, stateful=False, activation='tanh'))
model.add(Dropout(0.3))
model.add(Dense(16, activation='tanh'))
model.add(Dense(8, activation='tanh'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='mse', optimizer='adam' ,metrics=[metrics.mean_absolute_error, metrics.mean_squared_error])
logdir = os.path.join(logs_base_dir, datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))
tensorboard_callback = TensorBoard(log_dir=logdir, update_freq=1)
model.summary()
摘要:
Model: "sequential_33"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
masking_24 (Masking) (None, 2720, 16) 0
_________________________________________________________________
lstm_61 (LSTM) (None, 2720, 32) 6272
_________________________________________________________________
dropout_51 (Dropout) (None, 2720, 32) 0
_________________________________________________________________
lstm_62 (LSTM) (None, 16) 3136
_________________________________________________________________
dropout_52 (Dropout) (None, 16) 0
_________________________________________________________________
dense_67 (Dense) (None, 16) 272
_________________________________________________________________
dense_68 (Dense) (None, 8) 136
_________________________________________________________________
dense_69 (Dense) (None, 1) 9
=================================================================
Total params: 9,825
Trainable params: 9,825
Non-trainable params: 0
_________________________________________________________________
尝试拟合模型时,出现以下错误:
When trying to fit the model, I get the following error:
ValueError Traceback (most recent call last)
<ipython-input-354-afdba8dea179> in <module>()
----> 1 model.fit(train_x, train_y, epochs=1000, batch_size=128,validation_split = 0.2, callbacks=[tensorboard_callback,checkpoint])
5 frames
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/training_utils.py in check_loss_and_target_compatibility(targets, loss_fns, output_shapes)
808 raise ValueError('A target array with shape ' + str(y.shape) +
809 ' was passed for an output of shape ' + str(shape) +
--> 810 ' while using as loss `' + loss_name + '`. '
811 'This loss expects targets to have the same shape '
812 'as the output.')
我正在尝试掌握正确的数据结构方式,我想念的是什么?
I'm trying to grasp the right way to structure the data, what am I missing?
推荐答案
您的目标的形状为(31, 2720, 1)
,当前模型的输出结果的形状为(31, 1)
.这种情况下的错误是自我解释.
Your target is of shape (31, 2720, 1)
and the output of your current model will be of shape (31, 1)
. The error in this case is self explainatory.
您可以通过以下两种方式之一解决此问题:
You can solve this in one of two ways:
-
查看您的模型,我猜您只希望相对于最后一个序列损失.在这种情况下,您可以按以下方式调用
model.fit
:
model.fit(train_x, train_y[:, -1, :], ...)
如果要计算所有时间步长的损耗,请将return_sequences=True
添加到第二个LSTM层:
If you want to compute the loss across all timesteps, add return_sequences=True
to the second LSTM layer:
model.add(LSTM(16, return_sequences=True, stateful=False, activation='tanh'))
这篇关于Python-Keras:LSTM数据结构ValueError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!