【问题标题】:Merging LSTM and 1D CNN layers合并 LSTM 和 1D CNN 层
【发布时间】:2020-11-04 09:42:17
【问题描述】:

我正在尝试测试 Kelly 在 2015 年提出的非侵入式负载监控算法。提出的架构是,

  1. 输入(长度由设备持续时间决定)
  2. 1D conv(过滤器大小=4,步幅=1,过滤器数量=16,激活函数=线性,边框模式=相同)
  3. 双向 LSTM(N=128,带窥视孔)
  4. 双向 LSTM(N=256,带有窥视孔)
  5. 全连接(N=128,激活函数=TanH)
  6. 全连接(N=1,激活函数=线性)

现在我要在 TensorFlow 中测试这个架构。我有一个像 fallows 这样的初始训练集,

x_train_temp = (243127,) y_train_temp = (243127,)

然后我将这些数据转换为如下所示的窗口,其中每个窗口都有 250 x1 数组。

x_train = (972, 250, 1) y_train = (972, 250, 1)

当我实现我的模型时,它给出了一个错误。你能帮我理解错误在哪里吗?

型号

input_shape=x_train.shape[1:]
model = Sequential()
model.add(tf.keras.layers.Conv1D(16,4, strides=1,activation='linear',input_shape=(x_train.shape[1:])))
model.add(tf.keras.layers.LSTM(128))
model.add(tf.keras.layers.LSTM(256))
model.add(tf.keras.layers.LSTM(128, activation='relu'))
model.add(tf.keras.layers.LSTM(128, activation='linear'))
print(model.summary())

错误

ValueError                                Traceback (most recent call last)
<ipython-input-47-4f5e2441909e> in <module>()
      4 model.add(tf.keras.layers.Conv1D(16,4, strides=1,activation='linear',input_shape=(x_train.shape[1:])))
      5 model.add(tf.keras.layers.LSTM(128))
----> 6 model.add(tf.keras.layers.LSTM(256))
      7 model.add(tf.keras.layers.LSTM(128, activation='relu'))
      8 model.add(tf.keras.layers.LSTM(128, activation='linear'))

5 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
    178                          'expected ndim=' + str(spec.ndim) + ', found ndim=' +
    179                          str(ndim) + '. Full shape received: ' +
--> 180                          str(x.shape.as_list()))
    181     if spec.max_ndim is not None:
    182       ndim = x.shape.ndims

ValueError: Input 0 of layer lstm_1 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 128]

【问题讨论】:

  • 这可能是您使用激活函数relu 而不是tanh 的原因吗?
  • 当你使用背靠背 LSTM 时,你应该为第一个 LSTM 使用return_sequences=True,以便它返回形状为batch_size, lookback, input_features 的输出。
  • @AtherCheema 感谢您的宝贵意见。

标签: python tensorflow keras lstm recurrent-neural-network


【解决方案1】:

使用堆叠的 LSTM 层时,请使用@Ather Cheema 提到的return_sequences=True。 LSTM 期望输入形状为 [batch, timesteps, feature] 的 3D 张量。强烈建议设置return_sequences=True

return_sequences:布尔值。是否返回最后的输出。在输出序列或完整序列中。默认值:假。

工作示例代码 sn-p 没有 retuen_sequences

import tensorflow as tf
inputs = tf.random.normal([32, 10, 8])
lstm = tf.keras.layers.LSTM(4)
output = lstm(inputs)
print(output.shape)

输出

(32, 4)

#With return_sequences=True

import tensorflow as tf
inputs = tf.random.normal([32, 10, 8])
lstm = tf.keras.layers.LSTM(4, return_sequences=True, return_state=True)
whole_seq_output, final_memory_state, final_carry_state = lstm(inputs)
print(whole_seq_output.shape)

输出

(32, 10, 4)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-31
    • 2021-08-03
    • 1970-01-01
    • 2020-07-14
    • 2020-01-20
    • 1970-01-01
    • 2020-04-04
    • 1970-01-01
    相关资源
    最近更新 更多