This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pytorch function to replicate tensorflow's tf.nn.softmax_cross_entropy_with_logits | |
# works for soft targets or one-hot encodings | |
import torch | |
import torch.nn.functional as F | |
logits = model(input) | |
loss = torch.sum(- target * F.log_softmax(logits, -1), -1) | |
mean_loss = loss.mean() |