[英]Tensorflow shapes for target not matching (cifar10)
I am currently using tensorflow and cifar10 to develop a model.我目前正在使用 tensorflow 和 cifar10 开发 model。
the input dimensions are loaded from 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)
The input shapes are (10000, 32, 32, 3) and test shapes (10000, 1, 10).输入形状是 (10000, 32, 32, 3) 和测试形状 (10000, 1, 10)。
I am receiving an error with my final line of the model's layers.我收到模型图层的最后一行错误。
__________________________________________________________________________________________________
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]
==================================================================================================
I'm getting the error message "ValueError: Shapes (None, 1, 10) and (None, 10) are incompatible.我收到错误消息“ValueError:Shapes (None, 1, 10) 和 (None, 10) 不兼容。
I'm unsure how to solve this error.我不确定如何解决这个错误。
This happens because the CIFAR-10 labels are nested in the shape (None, 1)
instead of just (None)
.发生这种情况是因为 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)>
You can see that nested labels produces nested one-hot encoded labels.您可以看到嵌套标签生成嵌套的 one-hot 编码标签。 In your case, you can just flatten
or reshape
the labels before one-hot encoding:在您的情况下,您可以在 one-hot 编码之前flatten
或reshape
标签:
...
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)
Now the labels' shape should correspond to the model's output shape.现在标签的形状应该对应于模型的 output 形状。
your train_labels original shape is (50000,1) you need to remove the 1 using numpy.squeeze) which will make the shape(50000) same deal for test_labels您的 train_labels 原始形状是 (50000,1) 您需要使用 numpy.squeeze 删除 1 ,这将使形状 (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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.