层sequential_10的Keras LSTM Input 0与层不兼容 [英] Keras LSTM Input 0 of layer sequential_10 is incompatible with the layer

查看:54
本文介绍了层sequential_10的Keras LSTM Input 0与层不兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 LSTM 代码如下:

My code for the LSTM is as follows:

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()

我收到以下错误:

ValueError: Input 0 of layer lstm is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 20)

这个错误让我很困惑,因为我觉得 3 维形状被传递到 LSTM,但它表明检测到了 2 维形状.

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.

我的数据维度如下:x_train 形状是 (207, 20),y_train 形状是 (207, 5),x_test 形状是 (24, 20),y_test 形状为 (24, 5),

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),

我也在为一个分类用例运行这个 LSTM,正如你在我的代码中看到的那样.

I'm also running this LSTM for a classification use case, as you can see in my code.

推荐答案

正如@Andrey 提到的,LSTM 希望有一个 3D 形状数据 [batch_size, time_steps, feature_size]

As @Andrey mention that, LSTM expects to have a 3D shape data [batch_size, time_steps, feature_size]

例如,如果我们为 32 个批次样本中的每一个,对于 10 个时间步中的每一个,提供一个 8 维向量:输入数据形状应该类似于,

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,

X_train = tf.random.normal([32, 10, 8])

这篇关于层sequential_10的Keras LSTM Input 0与层不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆