【问题标题】:Python - Keras: LSTM data structure ValueErrorPython - Keras:LSTM 数据结构 ValueError
【发布时间】:2020-08-10 02:01:41
【问题描述】:

我正在尝试使用 return_sequence 训练 LSTM 模型以返回每个输入时间步的隐藏状态输出,从而解决回归问题。

我的数据形状是:(31, 2720, 16),即 31 批 2720 个样本,具有 16 个特征。
我的目标形状是: (31, 2720, 1) 即 31 批 2720 行包含 1 个值。

我已经建立了以下模型:

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
_________________________________________________________________

尝试拟合模型时,出现以下错误:


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.')

我正在尝试掌握构建数据的正确方法,我缺少什么?

【问题讨论】:

    标签: python tensorflow keras


    【解决方案1】:

    您的目标形状为(31, 2720, 1),您当前模型的输出形状为(31, 1)。这种情况下的错误是自我解释的。

    您可以通过以下两种方式之一解决此问题:

    1. 看看你的模型,我猜你只想要关于最后一个序列的损失。这种情况下,可以拨打model.fit如下:

      model.fit(train_x, train_y[:, -1, :], ...) 
      
    2. 如果要计算所有时间步的损失,请将return_sequences=True 添加到第二个 LSTM 层:

      model.add(LSTM(16, return_sequences=True, stateful=False,  activation='tanh'))
      

    【讨论】:

    • 谢谢,我的错误是我忘记设置 return_sequences=True
    【解决方案2】:

    您想要的目标形状与模型输出不一致。

    改变这一行

    model.add(LSTM(16, return_sequences=False, stateful=False, activation='tanh'))

    model.add(LSTM(16, return_sequences=True, stateful=False, activation='tanh'))

    所以时间维度就在那里。

    from tensorflow.keras.layers import *
    from tensorflow.keras.models import Model, Sequential
    from tensorflow.keras.optimizers import Adam
    import tensorflow as tf
    import numpy as np
    
    from tensorflow.keras.models import Sequential
    from tensorflow.keras.layers import Dense
    
    from tensorflow.keras import metrics
    import tensorflow as tf
    import numpy as np 
    
    model = Sequential()
    opt = Adam(learning_rate=0.0001, clipnorm=1)
    
    num_samples = 2720
    num_features = 16
    
    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=True, 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])
    
    model.summary()
    
    Model: "sequential_3"
    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    masking_1 (Masking)          (None, 2720, 16)          0         
    _________________________________________________________________
    lstm_2 (LSTM)                (None, 2720, 32)          6272      
    _________________________________________________________________
    dropout_2 (Dropout)          (None, 2720, 32)          0         
    _________________________________________________________________
    lstm_3 (LSTM)                (None, 2720, 16)          3136      
    _________________________________________________________________
    dropout_3 (Dropout)          (None, 2720, 16)          0         
    _________________________________________________________________
    dense_3 (Dense)              (None, 2720, 16)          272       
    _________________________________________________________________
    dense_4 (Dense)              (None, 2720, 8)           136       
    _________________________________________________________________
    dense_5 (Dense)              (None, 2720, 1)           9         
    =================================================================
    Total params: 9,825
    Trainable params: 9,825
    Non-trainable params: 0
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    • 2018-01-22
    • 2021-01-12
    • 2014-06-14
    • 2016-02-23
    • 1970-01-01
    相关资源
    最近更新 更多