简体   繁体   English

Keras Categorical_crossentropy 损失实现

[英]Keras Categorical_crossentropy loss implementation

I'm trying to reimplement the Categorical Cross Entropy loss from Keras so that I can customize it.我正在尝试从 Keras 重新实现分类交叉熵损失,以便我可以自定义它。 I got the following我得到了以下

def CustomCrossEntropy(output, target, axis=-1):
    target = ops.convert_to_tensor_v2_with_dispatch(target)
    output = ops.convert_to_tensor_v2_with_dispatch(output)
    target.shape.assert_is_compatible_with(output.shape)
    # scale preds so that the class probas of each sample sum to 1
    output = output / math_ops.reduce_sum(output, axis, True)
    # Compute cross entropy from probabilities.
    epsilon_ = _constant_to_tensor(epsilon(), output.dtype.base_dtype)
    output = clip_ops.clip_by_value(output, epsilon_, 1. - epsilon_)
    return -math_ops.reduce_sum(target * math_ops.log(output), axis)

It produces different results than the internal function which confuses me, as I just copied the code from github so far.它产生的结果与内部 function 不同,这让我感到困惑,因为到目前为止我只是从github复制代码。 What am I missing here?我在这里想念什么?

Prove:证明:

y_true = [[0., 1., 0.], [0., 0., 1.]]
y_pred = [[0.05, 0.95, 0], [0.1, 0.8, 0.1]]
loss = tf.keras.losses.categorical_crossentropy(y_true, y_pred)
customLoss = CustomCrossEntropy(y_true, y_pred)
assert loss.shape == (2,)
print(loss)
print(customLoss)
>>tf.Tensor([0.05129331 2.3025851 ], shape=(2,), dtype=float32)
>>tf.Tensor([ 0.8059049 14.506287 ], shape=(2,), dtype=float32)

You have inverted the arguments of the function in your definition of CustomCrossEntropy , if you double check the source code in GitHub you will see that the first argument is target and the second one is output . You have inverted the arguments of the function in your definition of CustomCrossEntropy , if you double check the source code in GitHub you will see that the first argument is target and the second one is output . If you switch them back you will get the same results as expected.如果您将它们切换回来,您将获得与预期相同的结果。

import tensorflow as tf
from tensorflow.keras.backend import categorical_crossentropy as CustomCrossEntropy

y_true = [[0., 1., 0.], [0., 0., 1.]]
y_pred = [[0.05, 0.95, 0], [0.1, 0.8, 0.1]]

y_true = tf.convert_to_tensor(y_true)
y_pred = tf.convert_to_tensor(y_pred)

loss = tf.keras.losses.categorical_crossentropy(y_true, y_pred)
print(loss)
# tf.Tensor([0.05129331 2.3025851 ], shape=(2,), dtype=float32)

loss = CustomCrossEntropy(y_true, y_pred)
print(loss)
# tf.Tensor([0.05129331 2.3025851 ], shape=(2,), dtype=float32)

loss = CustomCrossEntropy(y_pred, y_true)
print(loss)
# tf.Tensor([ 0.8059049 14.506287 ], shape=(2,), dtype=float32)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在keras中实现categorical_crossentropy? - How is the categorical_crossentropy implemented in keras? 在 Keras 中为“categorical_crossentropy”选择验证指标 - Selecting validation metric for `categorical_crossentropy` in Keras 即使在keras中精度为1.00,categorical_crossentropy也会返回较小的损失值 - categorical_crossentropy returns small loss value even if accuracy is 1.00 in keras Keras:binary_crossentropy 和 categorical_crossentropy 混淆 - Keras: binary_crossentropy & categorical_crossentropy confusion 损失为 NaN,使用激活 softmax 和损失 function categorical_crossentropy - Loss is NaN using activation softmax and loss function categorical_crossentropy `categorical_crossentropy` 损失中的 ValueError function:形状问题 - ValueError in `categorical_crossentropy` loss function: shape issue keras自定义生成器categorical_crossentropy修复输出形状问题 - keras custom generator categorical_crossentropy fix output shape issue python 神经网络损失 = 'categorical_crossentropy' vs 'binary_crossentropy' isse - python neural network loss = 'categorical_crossentropy' vs 'binary_crossentropy' isse TensorFlow 'categorical_crossentropy' 中的 ValueError - ValueError in TensorFlow 'categorical_crossentropy' 用于多类分类交叉熵损失函数的 Keras CNN - Keras CNN for multiclass categorical crossentropy loss function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM