Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.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:TensorFlow2输入0与图层模型不兼容_Python_Tensorflow_Conv Neural Network_Resnet - Fatal编程技术网

Python ValueError:TensorFlow2输入0与图层模型不兼容

Python ValueError:TensorFlow2输入0与图层模型不兼容,python,tensorflow,conv-neural-network,resnet,Python,Tensorflow,Conv Neural Network,Resnet,我正试图通过使用Python3、TensorFlow2和CIFAR-10数据集来编写一个基于的ResNet CNN体系结构。您可以访问Jupyter笔记本 在使用“model.fit()”训练模型的过程中,经过一个历元的训练,我得到以下错误: ValueError:输入0与图层模型不兼容:应为 形状=(无,32,32,3),找到的形状=(32,32,3) 使用batch_size=128对训练图像进行批处理,因此训练循环给出TF Conv2D期望的以下4-d张量-(128、32、32、3) 这个

我正试图通过使用Python3、TensorFlow2和CIFAR-10数据集来编写一个基于的ResNet CNN体系结构。您可以访问Jupyter笔记本

在使用“model.fit()”训练模型的过程中,经过一个历元的训练,我得到以下错误:

ValueError:输入0与图层模型不兼容:应为 形状=(无,32,32,3),找到的形状=(32,32,3)

使用batch_size=128对训练图像进行批处理,因此训练循环给出TF Conv2D期望的以下4-d张量-(128、32、32、3)


这个错误的来源是什么?

好的,我在您的代码中发现了一个小问题。问题发生在测试数据集中。你忘了正确地变换它。那么现在你有这样的

images, labels = next(iter(test_dataset))
images.shape, labels.shape

(TensorShape([32, 32, 3]), TensorShape([10]))
您需要在测试中执行与在火车组上相同的转换。但当然,你要考虑的事情是:没有洗牌,没有扩充

def testaugmentation(x, y):
    x = tf.image.resize_with_crop_or_pad(x, HEIGHT + 8, WIDTH + 8)
    x = tf.image.random_crop(x, [HEIGHT, WIDTH, NUM_CHANNELS])
    return x, y

def normalize(x, y):
    x = tf.image.per_image_standardization(x)
    return x, y

test_dataset = (test_dataset
        .map(testaugmentation)
        .map(normalize)
        .batch(batch_size = batch_size, drop_remainder = True))

images, labels = next(iter(test_dataset))
images.shape, labels.shape
(TensorShape([128, 32, 32, 3]), TensorShape([128, 10]))

请在这里包含代码,不要在其他网站上。我在colab上运行了您的代码。我从你的笔记本上取下了密码。这里运转良好。你测试过了吗?