sparse_categorical_crossentropy 和 categorical_crossentropy 有什么区别?

xpe*_*dev 48 python machine-learning deep-learning keras tensorflow

sparse_categorical_crossentropy和 和有categorical_crossentropy什么区别?什么时候应该使用一种损失而不是另一种?例如,这些损失是否适合线性回归?

Bit*_*sky 66

我也对这个感到困惑。幸运的是,优秀的 keras 文档拯救了我们。两者具有相同的损失函数,并且最终做相同的事情,唯一的区别在于真实标签的表示。

当有两个或多个标签类时,使用此交叉熵损失函数。我们希望以 one_hot 表示形式提供标签。

>>> y_true = [[0, 1, 0], [0, 0, 1]]
>>> y_pred = [[0.05, 0.95, 0], [0.1, 0.8, 0.1]]
>>> # Using 'auto'/'sum_over_batch_size' reduction type.  
>>> cce = tf.keras.losses.CategoricalCrossentropy()
>>> cce(y_true, y_pred).numpy()
1.177
Run Code Online (Sandbox Code Playgroud)
  • 稀疏分类交叉熵 [文档]:

当有两个或多个标签类时,使用此交叉熵损失函数。我们希望标签以整数形式提供。

>>> y_true = [1, 2]
>>> y_pred = [[0.05, 0.95, 0], [0.1, 0.8, 0.1]]
>>> # Using 'auto'/'sum_over_batch_size' reduction type.  
>>> scce = tf.keras.losses.SparseCategoricalCrossentropy()
>>> scce(y_true, y_pred).numpy()
1.177
Run Code Online (Sandbox Code Playgroud)

稀疏分类交叉熵的一个很好的例子是 fasion-mnist 数据集。

import tensorflow as tf
from tensorflow import keras

fashion_mnist = keras.datasets.fashion_mnist
(X_train_full, y_train_full), (X_test, y_test) = fashion_mnist.load_data()

print(y_train_full.shape) # (60000,)
print(y_train_full.dtype) # uint8

y_train_full[:10]
# array([9, 0, 0, 3, 0, 2, 7, 2, 5, 5], dtype=uint8)
Run Code Online (Sandbox Code Playgroud)

  • 这应该是公认的答案。 (10认同)

dtu*_*ene 60

简单地:

  • categorical_crossentropy( cce) 生成一个单热数组,其中包含每个类别的可能匹配项,
  • sparse_categorical_crossentropy( scce) 生成最可能匹配类别的类别索引。

考虑一个有 5 个类别(或类)的分类问题。

  • 在 的情况下cce,one-hot target 可能是[0, 1, 0, 0, 0]并且模型可以预测[.2, .5, .1, .1, .1](可能是对的)

  • 在 的情况下scce,目标索引可能是 [1],模型可能会预测:[.5]。

现在考虑一个有 3 个类的分类问题。

  • 在 的情况下cce,one-hot 目标可能是[0, 0, 1]并且模型可能会预测[.5, .1, .4](可能不准确,因为它为第一类提供了更多的概率)
  • 在 的情况下scce,目标索引可能是[0],并且模型可能会预测[.5]

许多分类模型产生scce输出是因为您节省了空间,但会丢失很多信息(例如,在第二个示例中,索引 2 也非常接近。)我通常更喜欢cce输出以提​​高模型可靠性。

有多种情况可以使用scce,包括:

  • 当您的课程相互排斥时,即您根本不关心其他足够接近的预测,
  • 类别数量大到预测输出变得势不可挡。

  • 这个答案是完全错误的。这是如何被投票和接受的?模型输出仍然是热编码的;它只是存储为索引的真实值。 (5认同)
  • 我认为这是不正确的:sparse_categorical_crossentropy 仍然期望使用 one-hot 编码进行预测。 (4认同)
  • “[.5]”是什么意思?这是最令人困惑的部分。 (3认同)

小智 11

从 TensorFlow 源代码中sparse_categorical_crossentropy定义为categorical crossentropy整数目标:

def sparse_categorical_crossentropy(target, output, from_logits=False, axis=-1):
  """Categorical crossentropy with integer targets.
  Arguments:
      target: An integer tensor.
      output: A tensor resulting from a softmax
          (unless `from_logits` is True, in which
          case `output` is expected to be the logits).
      from_logits: Boolean, whether `output` is the
          result of a softmax, or is a tensor of logits.
      axis: Int specifying the channels axis. `axis=-1` corresponds to data
          format `channels_last', and `axis=1` corresponds to data format
          `channels_first`.
  Returns:
      Output tensor.
  Raises:
      ValueError: if `axis` is neither -1 nor one of the axes of `output`.
  """
Run Code Online (Sandbox Code Playgroud)

从 TensorFlow 源代码中categorical_crossentropy被定义为输出张量和目标张量之间的分类交叉熵。

def categorical_crossentropy(target, output, from_logits=False, axis=-1):
  """Categorical crossentropy between an output tensor and a target tensor.
  Arguments:
      target: A tensor of the same shape as `output`.
      output: A tensor resulting from a softmax
          (unless `from_logits` is True, in which
          case `output` is expected to be the logits).
      from_logits: Boolean, whether `output` is the
          result of a softmax, or is a tensor of logits.
      axis: Int specifying the channels axis. `axis=-1` corresponds to data
          format `channels_last', and `axis=1` corresponds to data format
          `channels_first`.
  Returns:
      Output tensor.
  Raises:
      ValueError: if `axis` is neither -1 nor one of the axes of `output`.
  """

Run Code Online (Sandbox Code Playgroud)

整数目标的含义是目标标签应该是显示类索引的整数列表的形式,例如:

  • 对于sparse_categorical_crossentropy, 对于 1 类和 2 类目标,在 5 类分类问题中,列表应该是 [1,2]。基本上,目标应该是整数形式以便调用sparse_categorical_crossentropy. 这被称为稀疏,因为目标表示比单热编码需要的空间少得多。例如,具有b目标和k类的批次需要b * k以单热表示的空间,而具有b目标和k类的批次需要b以整数形式表示的空间。

  • 对于categorical_crossentropy,对于 1 类和 2 类目标,在 5 类分类问题中,列表应该是[[0,1,0,0,0], [0,0,1,0,0]]。基本上,目标应该是 one-hot 形式以便调用categorical_crossentropy.

目标的表示是唯一的区别,结果应该是相同的,因为它们都在计算分类交叉熵。