python - tensorflow : "logits and labels must be the same size"

标签 python tensorflow deep-learning conv-neural-network

我已经根据一篇研究论文构建了一个深度 CNN,现在我正在尝试训练它。在执行完所有卷积和反卷积之后,我得到了一个名为最终的结果。

final = tf.add(add1,add2)
print(final)

Tensor("Add_35:0", shape=(1, 32, 32, 7, 1), dtype=float32)

在我的模型中,我有一个尺寸为 32x32x7 的图像,其中每个像素都有相应的密度。模型的输出将是每个像素的标签。因此,我声明了两个占位符,其中“x”代表输入,“y_”代表输出。

x = tf.placeholder(tf.float32, shape=[None, 7168])
y_ = tf.placeholder(tf.float32, shape=[None, 7168])

现在我正在尝试训练模型,我有这条线

cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=final))

当模型正在训练时,我收到错误:logits 和标签必须具有相同的大小:logits_size=[7168,1] labels_size=[1,7168] 标签采用这种尺寸是有道理的,因为这就是我声明它的方式。 但是,我不明白为什么当打印出“final”时 logits 的大小为 [7168,1] 形状为 (1, 32, 32, 7, 1)。

最佳答案

tf.reshape您的最终:

final = tf.reshape(final, [None, 7168])

虽然我不确定为什么当你调用softmax_cross_entropy_with_logits时它会自动展平...

关于python - tensorflow : "logits and labels must be the same size",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47058909/

相关文章:

machine-learning - 深度学习中可能/也许的类别

Python正则表达式不捕获单个字符串

python - 从文本文件中解析/提取数据。无法使其工作

python - rabbitMQ 重启时 celery 撤销丢失

python - 使用 Tor 代理时的多线程爬虫

python - 使用 tf.range 附加到循环中的列表

javascript - 如何获取tensorflowjs中tf.toPixels()返回值的PromiseValue

python - 为什么我的线性回归得到的是 nan 值而不是学习?

python - Keras 嵌入层掩蔽。为什么 input_dim 需要是 |vocabulary| + 2?

tensorflow - 构建我们自己的图像检测模型的过程