machine-learning - Tensorflow - 使用 tf.losses.hinge_loss 导致形状不兼容错误

标签 machine-learning tensorflow cross-entropy

我当前使用sparse_softmax_cross_entropy的代码工作正常。

loss_normal = (
    tf.reduce_mean(tf.losses
                   .sparse_softmax_cross_entropy(labels=labels,
                                                 logits=logits,
                                                 weights=class_weights))
    )

但是,当我尝试使用hinge_loss时:

loss_normal = (
    tf.reduce_mean(tf.losses
                   .hinge_loss(labels=labels,
                               logits=logits,
                               weights=class_weights))
    )

它报告了一个错误:

ValueError: Shapes (1024, 2) and (1024,) are incompatible

该错误似乎源自 losses_impl.py 文件中的此函数:

  with ops.name_scope(scope, "hinge_loss", (logits, labels)) as scope:
    ...
    logits.get_shape().assert_is_compatible_with(labels.get_shape())
    ...

我修改了代码如下,只提取 logits 张量的 1 列:

loss_normal = (
    tf.reduce_mean(tf.losses
                   .hinge_loss(labels=labels,
                               logits=logits[:,1:],
                               weights=class_weights
                               ))
    )

但是还是报类似的错误:

ValueError: Shapes (1024, 1) and (1024,) are incompatible.

有人可以帮忙指出为什么我的代码可以在 sparse_softmax_cross_entropy 损失下正常工作,但在 hinge_loss 上却不能正常工作吗?

最佳答案

张量labels的形状为[1024],张量logits的形状为[1024, 2]形状。这适用于 tf.nn.sparse_softmax_cross_entropy_with_logits :

  • labels: Tensor of shape [d_0, d_1, ..., d_{r-1}] (where r is rank of labels and result) and dtype int32 or int64. Each entry in labels must be an index in [0, num_classes). Other values will raise an exception when this op is run on CPU, and return NaN for corresponding loss and gradient rows on GPU.
  • logits: Unscaled log probabilities of shape [d_0, d_1, ..., d_{r-1}, num_classes] and dtype float32 or float64.

但是tf.hinge_loss要求不同:

  • labels: The ground truth output tensor. Its shape should match the shape of logits. The values of the tensor are expected to be 0.0 or 1.0.
  • logits: The logits, a float tensor.

您可以通过两种方式解决此问题:

  • 将标签 reshape 为 [1024, 1] 并仅使用一行 logits,就像您所做的那样 - logits[:,1: ]:

    labels = tf.reshape(labels, [-1, 1])
    hinge_loss = (
        tf.reduce_mean(tf.losses.hinge_loss(labels=labels,
                                            logits=logits[:,1:],
                                            weights=class_weights))
        )
    

    我认为您还需要以同样的方式 reshape class_weights

  • 通过tf.reduce_sum使用所有学习的logits特征,这将生成一个平坦的(1024,)张量:

    logits = tf.reduce_sum(logits, axis=1)
    hinge_loss = (
        tf.reduce_mean(tf.losses.hinge_loss(labels=labels,
                                            logits=logits,
                                            weights=class_weights))
        )
    

    这样您就不需要 reshape 标签class_weights

关于machine-learning - Tensorflow - 使用 tf.losses.hinge_loss 导致形状不兼容错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47172754/

相关文章:

python - 在tensorflow中加载多个DNN模型并多次使用它们

python - VS Code/Pylance/Pylint 无法解析导入

python - 如何将时间分布式层与卷积层一起使用?

tensorflow - 如何在 python 中加载 tf.saved_model 后获取输入和输出张量

c++ - Caffe 中的交叉熵实现

matlab - 如何提高matlab中决策树的准确性

spring - 使用 Spring Boot 进行机器学习

python - TensorFlow 模型获得零损失

machine-learning - 我们如何使用 LSTM 对序列进行分类?

python - 加权稀疏分类交叉熵