python - 目标不匹配的张量流形状(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) 不兼容。
我不确定如何解决这个错误。
解决方案
发生这种情况是因为 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 编码之前使用标签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)
现在标签的形状应该对应于模型的输出形状。
推荐阅读
- javascript - 在 ReactJS 中按预期转换 JSON 数据响应
- ruby-on-rails - 使用 validates_each 进行自定义验证
- python - 从 pyinstaller 创建 exe 文件时出现 ModuleNotFoundError
- elasticsearch - 如何将存储桶选择器聚合与日期范围聚合一起使用?
- javascript - 递归异步函数不返回值
- rust - 为什么return语句后面的分号是可选的?
- c++ - 将输入参数作为右值引用传递?
- validation - 卷积神经网络:防止在测试阶段作弊
- selenium - (Pytest + Selenium)从 excel 表中读取数据并使用单选按钮和下拉菜单提交网页
- android - Android / Koin - 动态加载功能的数据模块