【问题标题】:Keras: ValueError: Input 0 of layer sequential_1 is incompatible with the layer: expected ndim=3, found ndim=2Keras:ValueError:层序贯_1的输入0与层不兼容:预期ndim = 3,发现ndim = 2
【发布时间】:2020-10-09 16:24:51
【问题描述】:

我有一个形状为 X: (1146165, 19, 22)Y: (1146165,) 的数据集。这是我的模型代码:

import tensorflow as tf

train_data = tf.data.Dataset.from_tensor_slices((x_train, y_train))
valid_data = tf.data.Dataset.from_tensor_slices((x_valid, y_valid))

def create_model(shape=(19, 22)):
    tfkl = tf.keras.layers
    model = tf.keras.Sequential([
        tfkl.LSTM(128, return_sequences=True, input_shape=shape),
        tfkl.LSTM(64),
        tfkl.Dropout(0.3),
        tfkl.Dense(64, activation="linear"),
        tfkl.Dense(1)
    ])
    
    model.compile(loss='mean_absolute_error', optimizer="adam")
    return model

model = create_model()
model.summary()

你可以看到input_shape(19, 22),这是正确的,但是当我使用fit 时,我得到了错误ValueError: Input 0 of layer sequential_15 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [19, 22]
我在 Stack 上搜索了一些答案,但其中大部分是因为输入维度是 (a, b) 而不是 (a,b,c)。任何帮助表示赞赏。

【问题讨论】:

    标签: python tensorflow keras


    【解决方案1】:

    如果您想使用tf.data.Dataset 来拟合您的模型,则需要确保在将其用于model.fit 之前对其进行批处理。对于您选择的batch_size,请尝试

    train_data = train_data.batch(batch_size)
    model.fit(train_data)
    

    【讨论】:

      猜你喜欢
      • 2020-10-05
      • 2022-08-15
      • 2020-06-12
      • 1970-01-01
      • 1970-01-01
      • 2021-08-25
      • 1970-01-01
      • 2019-06-04
      • 2019-08-14
      相关资源
      最近更新 更多