Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当适合我的模型时,我获得ValueError:层顺序的输入0与层不兼容_Python_Tensorflow_Machine Learning_Keras_Deep Learning - Fatal编程技术网

Python 当适合我的模型时,我获得ValueError:层顺序的输入0与层不兼容

Python 当适合我的模型时,我获得ValueError:层顺序的输入0与层不兼容,python,tensorflow,machine-learning,keras,deep-learning,Python,Tensorflow,Machine Learning,Keras,Deep Learning,我尝试使用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 a

我尝试使用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,但接收到形状的输入(无、28、28、1)

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

我想也许我必须把我的标签转换成这样的分类

from tensorflow.keras.utils import to_categorical

train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
但是,同样的错误再次出现

ValueError:layer sequential_1的输入0与层不兼容:输入形状的轴-1应具有值3,但接收到带形状的输入(无、28、28、1)

有人能帮我理解一下吗


多谢各位

代码中有两个问题。比如

  • 第1期)您在模型中设置了
    input_shpae=(32,32,3)
    ,而mnist示例是
    (28,28,1)
    。如果检查示例形状,您将看到:
但您在模型定义中将输入形状定义为不同

# 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)输入标签是整数(而不是一个热编码),请检查
    训练标签[:5]
    ,但您可以设置
    分类交叉熵
    ,而整数目标的输入标签应该是
    稀疏分类交叉熵

现在,正如您在后面提到的,您已经尝试了对目标标签进行热编码,在这种情况下,您可以使用
category\u crossentropy
作为损失函数。

看看这个:input\u shape=(32,32,3),与您的错误消息相比,您在这里看到了什么错误吗?Fok,这是因为我也尝试过使用Cifar10数据集,以查看是否存在灰度问题…谢谢!第一个问题是,当我试图解决第二个问题时,使用cifar10数据集时出现了一个错误,第二个问题是因为我忘记了对验证标签进行分类,而且我对分类交叉熵和稀疏分类企业中心的了解非常少。现在,我将阅读有关它们的文章,以了解它们的有趣之处(使用其中一种!)
# 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))) 
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'])