目标不匹配的Tensorflow形状(cifar10)



我目前正在使用tensorflow和cifar10开发一个模型。

输入尺寸是从cifar10加载的。

(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.cifar10.load_data()
train_images, test_images = train_images / 255.0, test_images / 255.0
nb_classes = len(numpy.unique(train_labels))
train_labels = tf.one_hot(train_labels, nb_classes)
test_labels = tf.one_hot(test_labels, nb_classes) 

输入形状为(10000,32,32,3),测试形状为(10000,1,10)。

我收到一个错误与我的模型的层的最后一行。

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to
==================================================================================================
input_1 (InputLayer)            [(None, 32, 32, 3)]  0
__________________________________________________________________________________________________
conv2d (Conv2D)                 (None, 32, 32, 32)   2432        input_1[0][0]
__________________________________________________________________________________________________
conv2d_1 (Conv2D)               (None, 32, 32, 32)   25632       conv2d[0][0]
__________________________________________________________________________________________________
conv2d_2 (Conv2D)               (None, 32, 32, 32)   25632       conv2d_1[0][0]
__________________________________________________________________________________________________
add (Add)                       (None, 32, 32, 32)   0           conv2d_2[0][0]
conv2d[0][0]
__________________________________________________________________________________________________
conv2d_4 (Conv2D)               (None, 32, 32, 64)   51264       add[0][0]
__________________________________________________________________________________________________
conv2d_5 (Conv2D)               (None, 32, 32, 64)   102464      conv2d_4[0][0]
__________________________________________________________________________________________________
conv2d_3 (Conv2D)               (None, 32, 32, 64)   51264       conv2d[0][0]
__________________________________________________________________________________________________
add_1 (Add)                     (None, 32, 32, 64)   0           conv2d_5[0][0]
conv2d_3[0][0]
__________________________________________________________________________________________________
flatten (Flatten)               (None, 65536)        0           add_1[0][0]
__________________________________________________________________________________________________
dense (Dense)                   (None, 512)          33554944    flatten[0][0]
__________________________________________________________________________________________________
dense_1 (Dense)                 (None, 10)           5130        dense[0][0]
==================================================================================================

我得到错误信息"ValueError:形状(None, 1,10)和(None, 10)是不兼容的。

我不知道如何解决这个错误。

这是因为CIFAR-10标签嵌套在形状(None, 1)而不是(None)中。

>>> a = [[0], [1], [2], [3]]
>>> tf.one_hot(a, 4)
<tf.Tensor: shape=(4, 1, 4), dtype=float32, numpy=
array([[[1., 0., 0., 0.]],
[[0., 1., 0., 0.]],
[[0., 0., 1., 0.]],
[[0., 0., 0., 1.]]], dtype=float32)>
>>> b = [0, 1, 2, 3]
>>> tf.one_hot(a, 4)
<tf.Tensor: shape=(4, 4), dtype=float32, numpy=
array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]], dtype=float32)>

您可以看到嵌套标签生成嵌套的单热编码标签。在您的情况下,您可以只在单热编码前flattenreshape标记:

...
nb_classes = len(numpy.unique(train_labels))
train_labels = train_labels.flatten()
test_labels = test_labels.flatten()
train_labels = tf.one_hot(train_labels, nb_classes)
test_labels = tf.one_hot(test_labels, nb_classes)

现在标签的形状应该对应于模型的输出形状。

你的train_labels的原始形状是(50000,1),你需要删除1使用numpy.squeeze),这将使形状(50000)相同的处理test_labels

(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.cifar10.load_data()
train_images, test_images = train_images / 255.0, test_images / 255.0
nb_classes = len(numpy.unique(train_labels))
test_labels=numpy.squeeze(test_labels)
train_labels=numpy.squeeze(train_labels)
train_labels = tf.one_hot(train_labels, nb_classes)
test_labels = tf.one_hot(test_labels, nb_classes) 

相关内容

  • 没有找到相关文章

最新更新