首页 > 解决方案 > 目标不匹配的张量流形状(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:Shapes (None, 1, 10) 和 (None, 10) 不兼容。

我不确定如何解决这个错误。

标签: pythontensorflowkeras

解决方案


发生这种情况是因为 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)>

您可以看到嵌套标签生成嵌套的 one-hot 编码标签。在您的情况下,您可以在 one-hot 编码之前使用标签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)

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


推荐阅读