简体   繁体   English

Keras:ValueError:输入 0 层序列_1 与层不兼容:预期 ndim=3,发现 ndim=2

[英]Keras: ValueError: Input 0 of layer sequential_1 is incompatible with the layer: expected ndim=3, found ndim=2

I have a dataset that has shape X: (1146165, 19, 22) and Y: (1146165,) .我有一个形状为X: (1146165, 19, 22)Y: (1146165,) This is my model code:这是我的模型代码:

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

As you can see the input_shape is (19, 22) , which is right, but when I use fit I get the error ValueError: Input 0 of layer sequential_15 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [19, 22]正如您所看到的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] ValueError: Input 0 of layer sequential_15 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [19, 22]
I search some answers on Stack, but most of them is because the input dimension is (a, b) instead of (a,b,c) .我在 Stack 上搜索了一些答案,但其中大部分是因为输入维度是(a, b)而不是(a,b,c) Any help is appreciated.任何帮助表示赞赏。

If you want to fit your model with a tf.data.Dataset , you'll need to make sure it is batched before using it in model.fit .如果你想用tf.data.Dataset拟合你的模型,你需要确保它在model.fit使用之前是批处理的。 For a batch_size of your choice, try对于您选择的batch_size ,请尝试

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:layersequential_1 的输入 0 与 layer 不兼容::预期 min_ndim=4,发现 ndim=3。 收到的完整形状:[无、256、256] - ValueError: Input 0 of layer sequential_1 is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [None, 256, 256] ValueError: 层序贯_1 的输入 0 与层不兼容: : 预期 min_ndim=4,发现 ndim=2。 收到的完整形状:(32768, 1) - ValueError: Input 0 of layer sequential_1 is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: (32768, 1) Keras Conv2D - ValueError:层序的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3 - Keras Conv2D - ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3 二进制信号数据:keras ValueError:层顺序的输入 0 与层不兼容::预期 min_ndim=3,发现 ndim=2 - binary signal data: keras ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=3, found ndim=2 Keras Conv1D ValueError:层顺序的输入 0 与层不兼容::预期 min_ndim=3,发现 ndim=2 - Keras Conv1D ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=3, found ndim=2 TF Keras:ValueError:层顺序的输入0与层不兼容:预期ndim = 3,发现ndim = 2 - TF Keras: ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=3, found ndim=2 Conv1D:ValueError:层序列_1 的输入 0 与层不兼容::预期 min_ndim=3,发现 ndim=2。 收到完整形状:(无,2) - Conv1D: ValueError: Input 0 of layer sequential_1 is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: (None, 2) ValueError:层顺序的输入 0 与层不兼容:预期 ndim=4,发现 ndim=3。 收到的完整形状:[32, 64, 3] - ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [32, 64, 3] ValueError: 层序列 10 的输入 0 与层不兼容:预期 ndim=5,发现 ndim=4。 Alexnet(cnn) + LSTM - ValueError: Input 0 of layer sequential_10 is incompatible with the layer: expected ndim=5, found ndim=4. Alexnet(cnn) + LSTM ValueError: 层序的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2。 收到的完整形状:(57, 1) - ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (57, 1)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM