[英]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.