keras张量整形(lstm输入形状错误)

时间:2019-02-17 10:04:21

标签: python tensorflow keras lstm

我在keras上使用LSTM并使用了重塑层,希望我不必为LSTM层指定形状。

输入为84600 x 6

2个月

84600秒。 整个2个月中有6种度量标准/ [标签]即时测量

到目前为止我有

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Reshape((86400,1,6), input_shape=(84600, 6)))
model.add(tf.keras.layers.LSTM(128,  activation='relu', input_shape= 
(x_train.shape), return_sequences=True))
model.add(tf.keras.layers.Dense(10, activation='softmax'))

这会引发错误:

ValueError: Input 0 of layer lstm is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: [None, 86400, 1, 6]

这是可以理解的。批处理大小加上3层等于4。 但是,当我重塑

model.add(tf.keras.layers.Reshape((86400,1,6), input_shape=(84600, 6)))
vvvvvvv
model.add(tf.keras.layers.Reshape((86400,6), input_shape=(84600, 6)))

它抛出

ValueError: Error when checking input: expected reshape_input to have 3 dimensions, but got array with shape (86400, 6)

似乎忽略批处理大小作为数组元素。并将其视为2个索引。它从4维跳到2维。

问题在于LSTM将3维作为输入,但我似乎无法理解。理想情况下,我想要86400 x 1 x 6阵列/张量。这样就变成了84600个1x6数据示例。

非常感谢您!

1 个答案:

答案 0 :(得分:0)

问题在于,重塑输入的方式与LSTM层不兼容。 LSTM层期望输入3个维度:(batch_size, timesteps, features)。但是,您正在向其输入形状为(batch_size, 84600, 1, 6)的输入。

在您的情况下,似乎时间步长为84600,每个时间步长为6。因此,省略Reshape层并为LSTM层简单使用input_shape (84600, 6)更为有意义:

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.LSTM(128,  activation='relu', input_shape=(84600, 6), return_sequences=True))
model.add(tf.keras.layers.Dense(10, activation='softmax'))
相关问题