pytorch交叉熵
import torch
# CrossEntropyLoss由LogSoftmax和Nllloss组成。
# 输入和输出
input_x = torch.tensor([[0.05, 0.95, 0], [0.1, 0.8, 0.1]])
target_y = torch.tensor([1, 2])
logsoftmax = torch.nn.LogSoftmax(dim = 1)
logsoftmax_output = logsoftmax(input_x)
nllloss = torch.nn.NLLLoss()
nllloss_output = nllloss(logsoftmax_output, target_y)
#
crossentropy = torch.nn.CrossEntropyLoss()
crossentropy_output = crossentropy(input_x, target_y)
print("logsoftmax_nllloss: ", nllloss_output)
keras交叉熵:
import tensorflow as tf
import numpy as np
y_true_np = np.array([1, 2])
flag = 1
if flag == 1:
y_true = tf.constant([1, 2])
y_pred = tf.constant([[0.05, 0.95, 0], [0.1, 0.8, 0.1]])
# sparse_categorical_crossentropy等价于pytorch中的torch.nn.Nllloss()(torch.log(input_x), target)
loss_total = tf.keras.losses.sparse_categorical_crossentropy(y_true, y_pred)
loss = tf.reduce_mean(loss_total)
y_true_np = np.array([1, 2])
y_pred_np = np.array([[0.05, 0.95, 0], [0.1, 0.8, 0.1]])
np_crossentropy = -(np.log(y_pred_np[0][y_true_np[0]]) + np.log(np.log(y_pred_np[1][y_true_np[1]])))
print(loss.numpy())
print(np_crossentropy)
总结:
在keras中使用了sparse_categorical_crossentropy,则输出需要使用softmax(x);
在pytorch中达到相同的数值,使用CrossEntropyLoss时,网络输出不需要softmax(x)/log_softmax();或者使用Nllloss(),网络输出则为torch.nn.log_softmax()