我正在尝试测试由Kelly于2015年提出的非侵入式负载监控算法。拟议的架构为
现在,我将在TensorFlow中测试此架构。我有一些休闲方面的初始训练,
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]
答案 0 :(得分:0)
当使用堆叠的 LSTM 层时,使用 @Ather Cheema 提到的 return_sequences=True
。 LSTM 期望输入形状为 [batch, timesteps, feature]
的形状 3D 张量。强烈建议设置 return_sequences=True
return_sequences:布尔值。是否返回最后的输出。在输出序列或完整序列中。默认值:假。
工作示例代码片段 没有 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)