KERAS交叉熵损失在多目标训练中缺少标签

发布于 2025-01-24 09:15:48 字数 1110 浏览 0 评论 0原文

我使用功能API有一个KERAS神经网络,该函数API具有多个输出和多个损失函数(某些回归,某些多类分类)。我将始终为训练中至少一个输出提供标签,但通常至少会丢失一个标签。

我正在尝试编写一个自定义的分类交叉熵损失函数:

def custom_error_function(y_true, y_pred):
    bool_finite = y_true != -1
    
    loss = keras.losses.CategoricalCrossentropy(from_logits=True)
    one_hotted = one_hot(np.int(boolean_mask(y_true, bool_finite)), depth=5)
    return loss(one_hotted, boolean_mask(y_pred, bool_finite, axis=1))

y_pred和y_true应该具有相同的形状([[n_samples_in_batch,n_classes(5)])和一个值> -1 y_true表示y_true指示缺失的值。

但是当我运行这个时,我会

ValueError: in user code:

    File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/keras/engine/training.py", line 1021, in train_function  *
        return step_function(self, iterator)
    File "/var/folders/pn/c0hwfk8n7q9442628b1g_p1r0000gp/T/ipykernel_13239/802342025.py", line 12, in custom_error_function  *
        return loss(one_hotted, boolean_mask(y_pred, bool_finite, axis=1))

    ValueError: Shapes (5,) and (None, 1) are incompatible

有些疲惫不堪,并感谢任何帮助。谢谢!

I have a Keras neural network, using the Functional API, that has multiple outputs and multiple loss functions (some regression, some multi-class classification). I will always have a label for at least one of the outputs in training but commonly at least one will be missing.

I'm trying to write a custom categorical cross entropy loss function:

def custom_error_function(y_true, y_pred):
    bool_finite = y_true != -1
    
    loss = keras.losses.CategoricalCrossentropy(from_logits=True)
    one_hotted = one_hot(np.int(boolean_mask(y_true, bool_finite)), depth=5)
    return loss(one_hotted, boolean_mask(y_pred, bool_finite, axis=1))

where y_pred and y_true should have the same shape ([n_samples_in_batch, n_classes (5)]) and a value of -1 for y_true indicates missing.

But when I run this, I get

ValueError: in user code:

    File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/keras/engine/training.py", line 1021, in train_function  *
        return step_function(self, iterator)
    File "/var/folders/pn/c0hwfk8n7q9442628b1g_p1r0000gp/T/ipykernel_13239/802342025.py", line 12, in custom_error_function  *
        return loss(one_hotted, boolean_mask(y_pred, bool_finite, axis=1))

    ValueError: Shapes (5,) and (None, 1) are incompatible

I'm a bit flummoxed and would appreciate any assistance. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

向地狱狂奔 2025-01-31 09:15:48

该问题来自axis = 1在丢失调用中,以下内容应起作用:

def custom_error_function(y_true, y_pred):
    bool_finite = y_true != -1
    
    loss = keras.losses.CategoricalCrossentropy(from_logits=True)
    return loss(tf.boolean_mask(y_true, bool_finite), tf.boolean_mask(y_pred, bool_finite))

The problem comes from axis=1 in the loss call, the following should work:

def custom_error_function(y_true, y_pred):
    bool_finite = y_true != -1
    
    loss = keras.losses.CategoricalCrossentropy(from_logits=True)
    return loss(tf.boolean_mask(y_true, bool_finite), tf.boolean_mask(y_pred, bool_finite))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文