[英]When fit my model I obtain ValueError: Input 0 of layer sequential is incompatible with the layer
I'm trying to emulate LeNet using mnist dataset,我正在尝试使用 mnist 数据集来模拟 LeNet,
I'm doing the following,我正在做以下事情,
import tensorflow as tf
import tensorflow_datasets as tfds
from tensorflow.keras import layers, models
from tensorflow.keras.utils import plot_model
from tensorflow.keras.optimizers import SGD
import matplotlib.pyplot as plt
import numpy as np
# Import dataset
(train_ds, test_ds), info_ds = tfds.load('mnist', split=['train','test'],
as_supervised = True,
with_info=True,
batch_size = -1)
train_images, train_labels = tfds.as_numpy(train_ds)
test_images, test_labels = tfds.as_numpy(test_ds)
# Split test to obtain validation dataset
val_size = int(len(test_images) * 0.8)
val_images, test_images = test_images[:val_size], test_images[val_size:]
val_labels, test_labels = test_labels[:val_size], test_labels[val_size:]
# Normalizing images between 0 to 1
train_images, test_images = train_images / 255.0, test_images / 255.0
# Create the model
model = models.Sequential()
model.add(layers.Conv2D(filters=6, kernel_size=(5,5), activation='relu', input_shape=(32,32,3)))
model.add(layers.MaxPooling2D(pool_size=(2,2)))
model.add(layers.Conv2D(filters=16, kernel_size=(5,5), activation='relu'))
model.add(layers.MaxPooling2D(pool_size=(2,2)))
model.add(layers.Flatten())
model.add(layers.Dense(120,activation='relu'))
model.add(layers.Dense(84,activation='relu'))
model.add(layers.Dense(10,activation='softmax'))
# Compile
opt = SGD(learning_rate=0.1)
model.compile(optimizer=opt,
loss='categorical_crossentropy',
metrics=['accuracy'])
# Fit
history = model.fit(train_images, train_labels,
epochs=10, batch_size=128,
validation_data=(val_images, val_labels),
verbose=2)
When fit, I obtain this error:合适时,我收到此错误:
ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input with shape (None, 28, 28, 1) ValueError:层顺序的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 3,但接收到的输入形状为 (None, 28, 28, 1)
This means I have to reshape mi images?这意味着我必须重塑 mi 图像?
I thought maybe I had to convert my labels to categorical like this我想也许我必须像这样将标签转换为分类
from tensorflow.keras.utils import to_categorical
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
But then the same error appears again,但随后同样的错误再次出现,
ValueError: Input 0 of layer sequential_1 is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input with shape (None, 28, 28, 1) ValueError: 层序 1 的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 3,但接收到的输入形状为 (None, 28, 28, 1)
Can somebody help me to undertand it?有人可以帮我理解吗?
Thank you very much!非常感谢!
There are two issues in your code.您的代码中有两个问题。 Such as如
input_shpae = (32,32,3)
in your model whereas the mnist samples are (28, 28, 1)
. (问题 1 )您在 model 中设置input_shpae = (32,32,3)
而 mnist 样本为(28, 28, 1)
。 If you check your samples shapes, you will see:如果您检查样品形状,您将看到:train_images.shape, train_labels.shape
((60000, 28, 28, 1), (60000,))
But you define your input shape as not the same in the model definition.但是您在 model 定义中将输入形状定义为不相同。
# current: and not OK, according to the sample shapes
...kernel_size=(5,5), activation='relu', input_shape=(32,32,3)))
# should be, otherwise resize your input
...kernel_size=(5,5), activation='relu', input_shape=(28,28,3)))
train_labels[:5]
but you set categorical_crossentropy
, whereas it should be sparse_categorical_crossentropy
for integer target. (问题 2 )输入标签是 integer (而不是单热编码),检查train_labels[:5]
但你设置了categorical_crossentropy
,而它应该是sparse_categorical_crossentropy
用于 integer 目标。current
model.compile(optimizer=opt,
loss='categorical_crossentropy', # when labels are one-hot encoded
metrics=['accuracy'])
should be
model.compile(optimizer=opt,
loss='sparse_categorical_crossentropy', # when label are integers
metrics=['accuracy'])
Now, as you mentioned later that you have tried to_categorical
to one-hot encoded the target label, in that case, you can use categorical_crossentropy
as a loss function.现在,正如您稍后提到的,您已尝试to_categorical
对目标 label 进行一次热编码,在这种情况下,您可以使用categorical_crossentropy
作为损失 function。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.