TensorFlow tf.keras.losses.SparseCategoricalCrossentropy

下面的例子,y_true的形状是[batch_size],y_pred的形状是[batch_size,num_class].
也就是说y_pred经过softmax层之后,是one-hot编码,SparseCategoricalCrossentropyy_true变成了one-hot编码.

cce = tf.keras.losses.SparseCategoricalCrossentropy()
loss = cce(
  [0, 1, 2],
  [[.9, .05, .05], [.5, .89, .6], [.05, .01, .94]])
print('Loss: ', loss.numpy())  # Loss: 0.3239

init

__init__(
    from_logits=False,
    reduction=losses_utils.ReductionV2.AUTO,
    name=None
)

call

__call__(
    y_true,
    y_pred,
    sample_weight=None
)

参考:
官网

你可能感兴趣的:(TensorFlow)