Hi Deep!



 dllacer:

Which is the equivalent TF softmax_cross_entropy() function in PyTorch?


In short, there is no direct equivalent, so you have to write your own.
A quick glance at the tensorflow documentation suggests that
tf.losses.softmax_cross_entropy() has been deprecated
in favor of tf.nn.softmax_cross_entropy_with_logits(),
but that both of these take labels of shape [nBatch, nClass]
that are probabilities (sometimes called “soft labels”).
In contrast, pytorch’s torch.nn.CrossEntropyLoss (or its function
version torch.nn.functional.cross_entropy()) takes integer
class labels of shape [nBatch].
(Both the tensorflow and pytorch versions take logits, rather than
probabilities, for the predictions you pass is, so that part’s the same.)
If your problem uses, in fact, integer class labels, just use pytorch’s
CrossEntropyLoss.  But if your labels are probabilities, then you
can write your own “soft-label” version, as described here:

  
    
    
    Soft Cross Entropy Loss (TF has it does Pytorch have it) 
  
  
    Hello Raaj! 

I do not believe that pytorch has a “soft” cross-entropy function built in. 
But you can implement it using pytorch tensor operations, so you should 
get the full benefit of autograd and gpu acceleration. 
See this (pytorch version 0.3.0) script: 
import torch
torch.__version__

# define "soft" cross-entropy with pytorch tensor operations
def softXEnt (input, target):
    logprobs = torch.nn.functional.log_softmax (input, dim = 1)
    return  -(target * logprobs).sum() / input.shap…
  


Good luck.
K. Frank