ValueError:lstm_1 层的输入 0 与该层不兼容:预期 ndim=3,发现 ndim=2。收到完整形状:(无,64)

Hey*_*ate 1 python lstm keras tensorflow

我一直很难理解这个错误消息的含义。我看过很多帖子,比如

Keras LSTM 层的 4D 输入

ValueError: 层顺序的输入 0 与层不兼容: : 预期 min_ndim=4, 发现 ndim=3

ValueError:输入 0 与层 lstm_13 不兼容:预期 ndim=3,发现 ndim=4

层顺序的输入0与期望ndim=3的层不兼容,发现ndim=2。收到完整形状:[无,1]

预期 ndim=3,发现 ndim=2

但他们似乎都没有解决我的问题。

我有

batch_train_dataset = tf.data.Dataset.from_tensor_slices((train_features, train_labels)).shuffle(512).batch(batch_size)

for i,x in enumerate(batch_train_dataset):
  print("x[0].ndim: ", x[0].ndim)
  print("x[0].shape: ", x[0].shape)
  print("x[1].shape: ", x[1].shape)
  if i==0:
    break
##########OUTPUT###########
x[0].ndim:  3
x[0].shape:  (64, 32, 1000)
x[1].shape:  (64,)
Run Code Online (Sandbox Code Playgroud)

我的个人数据具有一个形状,(64,32,1000)其中64是batch_size,32是时间步长,1000是许多特征。

这是我的模型。

num_classes = len(index_to_label)

lstm_model = tf.keras.Sequential([
    tf.keras.layers.Masking(mask_value=0.0), # DO NOT REMOVE THIS LAYER

    # TODO: Define a recurrent neural network to recognize one of `num_classes` actions from the given video
    ### START CODE HERE ###
    tf.keras.layers.LSTM(64, input_shape=(batch_size,32,1000)),
    tf.keras.layers.LSTM(32),
    tf.keras.layers.Dense(32, activation='relu'),
    tf.keras.layers.Dense(32, activation='relu'),
    tf.keras.layers.Dense(num_classes, activation='softmax')
    ### END CODE HERE ###
])
Run Code Online (Sandbox Code Playgroud)

我认为我的input_shape设置是正确的,并且根据上面列出的这些问题,我不知道要解决什么问题。每当我尝试拟合模型时,它仍然会打印错误

ValueError: Input 0 of layer lstm_1 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 64)
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮我解决这个问题吗?

====================编辑==================

感谢一些评论,我已将 input_shape 更改为 (32,1000) 但它仍然打印出完全相同的错误。我仍然想知道原因可能是什么,因此任何其他想法将不胜感激。

小智 6

在堆叠的 LSTM 层中,LSTM 层不会返回序列,即它们将返回 2D 输出。这意味着第二个 LSTM 层将没有所需的 3D 输入。要解决这个问题,您需要设置 return_sequences=True:

tf.keras.layers.LSTM(64, input_shape=(32,1000),return_sequences=True),
tf.keras.layers.LSTM(32),
Run Code Online (Sandbox Code Playgroud)