ValueError: Input 0 is in compatible with layer model: expected shape=(None, 14999, 7), found shape=
Posted
技术标签:
【中文标题】ValueError: Input 0 is in compatible with layer model: expected shape=(None, 14999, 7), found shape=(None, 7)【英文标题】:ValueError: Input 0 is incompatible with layer model: expected shape=(None, 14999, 7), found shape=(None, 7) 【发布时间】:2021-07-06 20:44:00 【问题描述】:我正在尝试将 Conv1D 层应用于具有数字数据集的分类模型。我的模型的神经网络如下:
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Conv1D(8,kernel_size = 3, strides = 1,padding = 'valid', activation = 'relu',input_shape = (14999,7)))
model.add(tf.keras.layers.Conv1D(16,kernel_size = 3, strides = 1,padding = 'valid', activation = 'relu'))
model.add(tf.keras.layers.MaxPooling1D(2))
model.add(tf.keras.layers.Dropout(0.2))
model.add(tf.keras.layers.Conv1D(32,kernel_size = 3, strides = 1,padding = 'valid', activation = 'relu'))
model.add(tf.keras.layers.Conv1D(64,kernel_size = 3, strides = 1,padding = 'valid', activation = 'relu'))
model.add(tf.keras.layers.MaxPooling1D(2))
model.add(tf.keras.layers.Dropout(0.2))
model.add(tf.keras.layers.Conv1D(128,kernel_size = 3, strides = 1,padding = 'valid', activation = 'relu'))
model.add(tf.keras.layers.Conv1D(256,kernel_size = 3, strides = 1,padding = 'valid', activation = 'relu'))
model.add(tf.keras.layers.MaxPooling1D(2))
model.add(tf.keras.layers.Dropout(0.2))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(512,activation = 'relu'))
model.add(tf.keras.layers.Dense(128,activation = 'relu'))
model.add(tf.keras.layers.Dense(32,activation = 'relu'))
model.add(tf.keras.layers.Dense(3, activation = 'softmax'))
而模型的输入形状为 (14999, 7)。
model.summary() 给出以下输出
Model: "sequential_8"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv1d_24 (Conv1D) (None, 14997, 8) 176
_________________________________________________________________
conv1d_25 (Conv1D) (None, 14995, 16) 400
_________________________________________________________________
max_pooling1d_10 (MaxPooling (None, 7497, 16) 0
_________________________________________________________________
dropout_9 (Dropout) (None, 7497, 16) 0
_________________________________________________________________
conv1d_26 (Conv1D) (None, 7495, 32) 1568
_________________________________________________________________
conv1d_27 (Conv1D) (None, 7493, 64) 6208
_________________________________________________________________
max_pooling1d_11 (MaxPooling (None, 3746, 64) 0
_________________________________________________________________
dropout_10 (Dropout) (None, 3746, 64) 0
_________________________________________________________________
conv1d_28 (Conv1D) (None, 3744, 128) 24704
_________________________________________________________________
conv1d_29 (Conv1D) (None, 3742, 256) 98560
_________________________________________________________________
max_pooling1d_12 (MaxPooling (None, 1871, 256) 0
_________________________________________________________________
dropout_11 (Dropout) (None, 1871, 256) 0
_________________________________________________________________
flatten_3 (Flatten) (None, 478976) 0
_________________________________________________________________
dense_14 (Dense) (None, 512) 245236224
_________________________________________________________________
dense_15 (Dense) (None, 128) 65664
_________________________________________________________________
dense_16 (Dense) (None, 32) 4128
_________________________________________________________________
dense_17 (Dense) (None, 3) 99
=================================================================
Total params: 245,437,731
Trainable params: 245,437,731
Non-trainable params: 0
模型拟合代码为:
model.compile(loss = 'sparse_categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
history = model.fit(xtrain_scaled, ytrain_scaled, epochs = 30, batch_size = 5, validation_data = (xval_scaled, yval_scaled))
在执行时,我遇到以下错误:
ValueError: Input 0 is incompatible with layer model: expected shape=(None, 14999, 7), found shape=(None, 7)
谁能帮忙解决这个问题?
【问题讨论】:
【参考方案1】:TL;DR:
改变
model.add(tf.keras.layers.Conv1D(8,kernel_size = 3, strides = 1,padding = 'valid', activation = 'relu',input_shape = (14999,7)))
到
model.add(tf.keras.layers.Conv1D(8,kernel_size = 3, strides = 1,padding = 'valid', activation = 'relu',input_shape = (7)))
完整答案:
假设:我猜您选择在输入形状中写入 14999 的原因是因为这是您的批量大小/训练数据的总大小
这个问题:
Tensorflow 假设输入形状不包括批量大小 通过指定 2Dinput_shape
,您可以让 Tensorflow 期待形状为 (Batch_size, 14999, 7)
的 3D 输入。但是你的输入显然是大小(Batch_size, 7)
解决方案:
将形状从 (14999, 7)
更改为 (7)
PS:不要担心告诉你的模型你在数据集中有多少训练示例。 TF Keras 假设它可以显示无限的训练示例。
【讨论】:
以上是关于ValueError: Input 0 is in compatible with layer model: expected shape=(None, 14999, 7), found shape=的主要内容,如果未能解决你的问题,请参考以下文章
ValueError: Input 0 is in compatible with layer conv_1: expected ndim=3, found ndim=4
ValueError: Input 0 is in compatible with layer vggface_resnet50: expected shape=(None, 224, 224, 3)
如何修复:ValueError: Input 0 is in compatible with layer lstm_2: expected ndim=3, found ndim=2
TensorFlow ValueError: Input 0 is in compatible with layer model_1: expected shape=(None, 32, 32, 1)
Keras LSTM ValueError: Input 0 of layer "sequential" is in compatible with the layer: expe
Keras ValueError: Input 0 is in compatible with layer conv2d_1: expected ndim=4, found ndim=5