【问题标题】:When fit my model I obtain ValueError: Input 0 of layer sequential is incompatible with the layer当适合我的模型时,我得到 ValueError: Input 0 of layer sequence is incompatible with the layer
【发布时间】:2021-08-15 01:30:08
【问题描述】:

我正在尝试使用 mnist 数据集来模拟 LeNet,

我正在做以下事情,

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)

合适的时候,我得到这个错误:

ValueError:层顺序的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 3,但接收到形状为 (None, 28, 28, 1) 的输入

这意味着我必须重塑 mi 图像?

我想也许我必须像这样将标签转换为分类标签

from tensorflow.keras.utils import to_categorical

train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)

但随后又出现同样的错误,

ValueError: 层序贯_1 的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 3,但接收到形状为 (None, 28, 28, 1) 的输入

有人可以帮我理解吗?

非常感谢!

【问题讨论】:

  • 看看这个:input_shape=(32,32,3),和你的错误信息相比,你发现这里有什么问题吗?
  • Fok,这是因为我也尝试过 Cifar10 数据集,看看是不是灰度的问题...

标签: python tensorflow machine-learning keras deep-learning


【解决方案1】:

您的代码中有两个问题。比如

  • 问题 1)您在模型中设置了 input_shpae = (32,32,3),而 mnist 样本为 (28, 28, 1)。如果您检查样品形状,您会看到:
train_images.shape, train_labels.shape
((60000, 28, 28, 1), (60000,))

但是您将输入形状定义为与模型定义中的不同。

# 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))) 
  • 问题 2)输入标签是整数(而不是 one-hot 编码),检查 train_labels[:5] 但您设置了 categorical_crossentropy,而它应该是sparse_categorical_crossentropy 用于整数目标。
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'])

现在,正如您稍后提到的,您已尝试使用to_categorical 对目标标签进行 one-hot 编码,在这种情况下,您可以使用 categorical_crossentropy 作为损失函数。

【讨论】:

  • 谢谢!第一个问题是我在尝试解决第二个问题时使用 cifar10 数据集时出错,第二个问题出现是因为我忘记对验证标签进行分类,而且我对 categorical_crossentropy 和 sparse_categorical_corssentropy 的了解非常低。现在我将阅读它们以了解何时使用其中一种有趣! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-29
  • 1970-01-01
  • 2019-09-05
  • 2022-11-06
  • 2021-08-01
  • 2022-07-15
  • 2022-09-24
相关资源
最近更新 更多