KERAS交叉熵损失在多目标训练中缺少标签
我使用功能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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该问题来自
axis = 1
在丢失调用中,以下内容应起作用:The problem comes from
axis=1
in the loss call, the following should work: