[英]Keras LSTM Input 0 of layer sequential_10 is incompatible with the layer
My code for the LSTM is as follows:我的 LSTM 代码如下:
def myLSTM(i_shape, o_shape):
input = keras.layers.Input(i_shape)
model = Sequential()
x = keras.layers.LSTM(128, return_sequences = True, input_shape = (x_train.shape[1], 1))(input)
x = keras.layers.Dropout(0.2)(x)
x = keras.layers.LSTM(128, return_sequences = True)(x)
x = keras.layers.Dropout(0.2)(x)
x = keras.layers.LSTM(64, return_sequences = True)(x)
x = keras.layers.Dropout(0.2)(x)
output = layers.Dense(units = 1, activation='softmax')(x)
return Model(input, output)
my_lstm = myLSTM(x_train.shape[1:], y_train.shape[1:])
my_lstm.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc'])
my_lstm.summary()
I am getting the following error:我收到以下错误:
ValueError: Input 0 of layer lstm is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 20)
This error confuses me because I feel like a 3-dimensional shape is passed into the LSTM but it shows that a 2-dimensional shape is detected.这个错误让我感到困惑,因为我觉得一个 3 维形状被传递到 LSTM 中,但它表明检测到一个 2 维形状。
The dimensions of my data are as follows: x_train shape is (207, 20), y_train shape is (207, 5), x_test shape is (24, 20), y_test shape is (24, 5),我的数据的维度如下:x_train 形状是(207, 20),y_train 形状是(207, 5),x_test 形状是(24, 20),y_test 形状是(24, 5),
I'm also running this LSTM for a classification use case, as you can see in my code.正如您在我的代码中看到的那样,我还在为分类用例运行此 LSTM。
As @Andrey mention that, LSTM expects to have a 3D shape data [batch_size, time_steps, feature_size]
正如@Andrey 提到的那样,LSTM 期望有一个 3D 形状数据
[batch_size, time_steps, feature_size]
Example,If we provide for each of the 32 batch samples, for each of the 10 time steps, a 8 dimensional vector: Input data shape should be something like,例如,如果我们为 32 个批次样本中的每一个提供 10 个时间步长中的每一个,一个 8 维向量: 输入数据形状应该类似于,
X_train = tf.random.normal([32, 10, 8])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.