Created
July 14, 2018 22:51
-
-
Save tejaskhot/cf3d087ce4708c422e68b3b747494b9f to your computer and use it in GitHub Desktop.
Pytorch softmax cross entropy with logits
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() |
exm, it seems you want to replicate tensorflow's tf.nn.soft,ax_cross_entropy_with_logits, but you use F.log_softmax rather than F.softmax?
Here log is for computing cross entropy.
Will this work if my logits shape is (32, 1, 128, 128) and target is also of same shape but with all values zero and one [x,y] = 1.0
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
exm, it seems you want to replicate tensorflow's tf.nn.soft,ax_cross_entropy_with_logits, but you use F.log_softmax rather than F.softmax?