Cyan's Blog

Search

Search IconIcon to open search

Cross_Entropy_Loss_Input_Format-交叉熵损失函数输入格式

Last updated Unknown Edit Source

# PyTorch

标签不需要变成独热编码:

# Keras

Kears有两种形式:

Use this crossentropy loss function when there are two or more label classes. We expect labels to be provided in a one_hot representation.

1
2
3
4
5
6
>>> 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

Use this crossentropy loss function when there are two or more label classes. We expect labels to be provided as integers.

1
2
3
4
5
6
>>> 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

python - What is the difference between sparse_categorical_crossentropy and categorical_crossentropy? - Stack Overflow