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)
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
,包括:
小智 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
.
目标的表示是唯一的区别,结果应该是相同的,因为它们都在计算分类交叉熵。
归档时间: |
|
查看次数: |
31207 次 |
最近记录: |