tensorflow - 在 tensorflow 中实现两个图像之间的交叉熵损失

标签 tensorflow deep-learning

我正在尝试为完全转换网络实现两个图像之间的交叉熵损失。我的训练图像和输入图像都在 0-1 范围内。现在,我尝试仅针对一类图像实现此功能。为了说明这一点,我有不同的橙色图片,但只有橙色图片。我已经构建了模型并实现了交叉熵损失函数。

def loss_func_entropy(logits,y):
    logits=tf.reshape(logits,[BATCH_SIZE*480*640,1])
    y=tf.reshape(y,[BATCH_SIZE*480*640,1])
    print (logits.get_shape(),y.get_shape())
    return tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits,labels=y,dim=0) )

显然我做得不对,因为我的损失函数不断增加。需要注意的是 logits 和 y 都是二维的。我将它们 reshape 为单个向量并尝试进行交叉熵。

最佳答案

首先, reshape 实际上应该是(对于这个特定问题,它也可以在没有它的情况下运行,但这并不是 TF 真正期望看到的模型输出):

logits=tf.reshape(logits,[BATCH_SIZE,480*640])
y=tf.reshape(y,[BATCH_SIZE,480*640])

那么唯一的错误在于 TF has a "jungle" of cross-entropy functions 。您正在寻找的是sigmoid_cross_entropy_with_logits,而不是softmax。您使用的规范化了整个图像,因此像素之和为一(这显然是不正确的)。您想要的是将每个像素视为一个单独的“软分类”问题,其中其归一化强度被视为概率,这正是 sigmoid_cross_entropy_with_logits 所做的。换句话说 - 这只是一个具有软目标的多标签分类。

特别是,通过之前的 reshape ,softmax_cross_entropy_with_logits 的行为会更加奇怪 - 因为你有 一个 输出,但仍然应用了 softmax,它应该始终产生输出“1”,就像 >标量 x:

softmax(x) = exp(x) / SUM exp(x) = 1

关于tensorflow - 在 tensorflow 中实现两个图像之间的交叉熵损失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45909114/

相关文章:

tensorflow - Keras VGG16 preprocess_input 模式

machine-learning - 如何在fastai学习器上进行grid_search?

python - 为什么不这样训练 GAN?

python - ValueError : Dimensions must be equal,,但对于 'loss/output_1_loss/mul' 来说是 3 和 3072 (op : 'Mul' ) with input shapes: [? ,3], [?,3072]

machine-learning - 如何使用Keras的ModelCheckpoint继续训练模型

python - TensorFlow/Keras : Why do I get "ValueError: Incompatible conversion from float32 to uint8" when calling fit?

image - 在 Keras 中检查验证结果显示只有 50% 是正确的。明显随机

tensorflow - TensorFlow 中的这种单热编码速度快吗?或者出于任何原因有缺陷?

python - 属性错误 : 'Sequential' object has no attribute 'output_names'

Tensorflow 2.0 数据集和数据加载器