我正在测试 tf.keras.losses.CategoricalCrossentRopy
的结果,它为我提供了与定义不同的值。
我对跨熵的理解是:
def ce_loss_def(y_true, y_pred):
return tf.reduce_sum(-tf.math.multiply(y_true, tf.math.log(y_pred)))
我有这样的价值:
pred = [0.1, 0.1, 0.1, 0.7]
target = [0, 0, 0, 1]
pred = tf.constant(pred, dtype = tf.float32)
target = tf.constant(target, dtype = tf.float32)
pred_2 = [0.1, 0.3, 0.1, 0.7]
target = [0, 0, 0, 1]
pred_2 = tf.constant(pred_2, dtype = tf.float32)
target = tf.constant(target, dtype = tf.float32)
根据定义,我认为它应该无视非目标类中的概率,例如:
ce_loss_def(y_true = target, y_pred = pred), ce_loss_def(y_true = target, y_pred = pred_2)
(<tf.Tensor: shape=(), dtype=float32, numpy=0.35667497>,
<tf.Tensor: shape=(), dtype=float32, numpy=0.35667497>)
但是 tf.keras.losses.losses.categoricalcrossentropy
不给我相同的结果:
ce_loss_keras = tf.keras.losses.CategoricalCrossentropy()
ce_loss_keras(y_true = target, y_pred = pred), ce_loss_keras(y_true = target, y_pred = pred_2)
输出:
(<tf.Tensor: shape=(), dtype=float32, numpy=0.35667497>,
<tf.Tensor: shape=(), dtype=float32, numpy=0.5389965>)
我缺少什么?
这是我用来获得此结果的笔记本的链接:
I am testing outcomes of tf.keras.losses.CategoricalCrossEntropy
, and it gives me values different from the definition.
My understanding of cross entropy is:
def ce_loss_def(y_true, y_pred):
return tf.reduce_sum(-tf.math.multiply(y_true, tf.math.log(y_pred)))
And lets say I have values like this:
pred = [0.1, 0.1, 0.1, 0.7]
target = [0, 0, 0, 1]
pred = tf.constant(pred, dtype = tf.float32)
target = tf.constant(target, dtype = tf.float32)
pred_2 = [0.1, 0.3, 0.1, 0.7]
target = [0, 0, 0, 1]
pred_2 = tf.constant(pred_2, dtype = tf.float32)
target = tf.constant(target, dtype = tf.float32)
By the definition I think it should disregard the probabilities in the non-target classes, like this:
ce_loss_def(y_true = target, y_pred = pred), ce_loss_def(y_true = target, y_pred = pred_2)
(<tf.Tensor: shape=(), dtype=float32, numpy=0.35667497>,
<tf.Tensor: shape=(), dtype=float32, numpy=0.35667497>)
But tf.keras.losses.CategoricalCrossEntropy
doesn't give me the same results:
ce_loss_keras = tf.keras.losses.CategoricalCrossentropy()
ce_loss_keras(y_true = target, y_pred = pred), ce_loss_keras(y_true = target, y_pred = pred_2)
outputs:
(<tf.Tensor: shape=(), dtype=float32, numpy=0.35667497>,
<tf.Tensor: shape=(), dtype=float32, numpy=0.5389965>)
What am I missing?
Here is the link to the notebook I used to get this result:
https://colab.research.google.com/drive/1T69vn7MCGMSQ8hlRkyve6_EPxIZC1IKb#scrollTo=dHZruq-PGyzO
发布评论
评论(1)
我发现问题是什么。矢量元素会以某种方式自动缩放,以总结为1,因为这些值是概率。
给出
预期的。
I found out what the problem was. The vector elements get scaled automatically somehow, to sum up to 1 because the values are probabilities.
gives
As intended.