简体   繁体   English

Keras 自定义损失 function - 尽管返回与分类交叉熵相同的形状,但形状不匹配

[英]Keras custom loss function - shape mismatch despite returning same shape as categorical crossentropy

I've created a custom loss function based on cosine:我创建了一个基于余弦的自定义损失 function:

def cos_loss(y_true, y_pred):
    norm_pred = tf.math.l2_normalize(y_pred)
    dprod = tf.tensordot(
        a=y_true,
        b=norm_pred,
        axes=1
    )
    return 1 - dprod

However, training a model with this custom loss results in the error In[0] mismatch In[1] shape: 2 vs. 8: [8,2] [8,2] 0 0 .但是,使用此自定义损失训练 model 会导致错误In[0] mismatch In[1] shape: 2 vs. 8: [8,2] [8,2] 0 0 If I use a built-in loss function like categorical cross-entropy, the model trains without issue.如果我使用像分类交叉熵这样的内置损失 function,则 model 训练没有问题。

This is despite my custom loss and categorical crossentropy returning values that are exactly the same type and shape.尽管我的自定义损失和分类交叉熵返回值的类型和形状完全相同。 For example, I create testing y_true and y_pred and run them through both:例如,我创建了测试y_truey_pred并运行它们:

test_true = np.asarray([1.0, 0.0])
test_pred = np.asarray([0.9, 0.2])
print(cos_loss(test_true, test_pred))
print(tf.keras.losses.categorical_crossentropy(test_true, test_pred))

which returns:返回:

> tf.Tensor(0.023812939816047263, shape=(), dtype=float64)
  tf.Tensor(0.20067069546215124, shape=(), dtype=float64)

So both give TF tensors with a single float-64 value and no shape.因此,两者都给出了具有单个 float-64 值且没有形状的 TF 张量。 So why am I getting a shape mismatch error on one but not the other if the shape outputs are the same please?那么,如果形状输出相同,为什么我会在一个上出现形状不匹配错误,而在另一个上却没有呢? Thanks.谢谢。

Your loss function should be able to take in a batch of predictions and ground truth and return a batch of loss values.您的损失 function 应该能够接受一批预测和基本事实并返回一批损失值。 At the moment, that's not the case, as a tensordot with axis=1 is a matrix multiplication, and you have a conflict of dimensions when you start to introduce a batch dimension.目前,情况并非如此,因为tensordot axis=1的张量点是矩阵乘法,当您开始引入批量维度时,您会遇到维度冲突。

You can probably use the following instead:您可能可以改用以下内容:

def cos_loss(y_true, y_pred):
    norm_pred = tf.math.l2_normalize(y_pred)
    dprod = tf.reduce_sum(y_true*norm_pred, axis=-1)
    return 1 - dprod

暂无
暂无

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

相关问题 keras sparse_categorical_crossentropy损失函数输出形状不匹配 - keras sparse_categorical_crossentropy loss function output shape didn't match `categorical_crossentropy` 损失中的 ValueError function:形状问题 - ValueError in `categorical_crossentropy` loss function: shape issue keras自定义生成器categorical_crossentropy修复输出形状问题 - keras custom generator categorical_crossentropy fix output shape issue Keras 的稀疏分类交叉熵形状问题 - Sparse Categorical CrossEntropy shape problem with Keras 创建 keras 张量,其形状与 model output 用于自定义损失 ZC1C425268E68385D1ABZA77 - Create keras tensor with shape as same as model output for custom loss function 用于多类分类交叉熵损失函数的 Keras CNN - Keras CNN for multiclass categorical crossentropy loss function CNN 值错误输入形状,同时用作损失“categorical_crossentropy”。 此损失期望目标与输出具有相同的形状 - CNN value error input shape while using as loss `categorical_crossentropy`. This loss expects targets to have the same shape as the output Keras logits 和 label 必须具有相同的第一维,得到 logits 形状 [10240,151] 和标签形状 [1],sparse_categorical_crossentropy - Keras logits and labels must have the same first dimension, got logits shape [10240,151] and labels shape [1], sparse_categorical_crossentropy keras sparse_categorical_crossentropy 损失 function 中的错误 - Error in keras sparse_categorical_crossentropy loss function 形状为 (32, 3) 的目标数组被传递给形状为 (None, 15, 15, 3) 的 output,同时用作损失`categorical_crossentropy` - A target array with shape (32, 3) was passed for an output of shape (None, 15, 15, 3) while using as loss `categorical_crossentropy`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM