Hi Prerna and Animesh!



 Prerna_Dhareshwar:

I think you’re looking for BCEloss which can be found here.


You don’t want – in the typical case – BCELoss for classification
problems.  This is because BCELoss requires the predictions fed
into it to be numbers in (0, 1) (probability-like numbers).
For  example, Animesh’s output layer is a Linear
        self.fc_out = nn.Linear(100, 4)

that will, in general, output numbers ranging from -infinity to
+infinity.  You also don’t want to pass these outputs through
a Sigmoid (or Softmax) layer to map them to (0, 1) because
of the risk of overflow.
You want, instead, a loss function that takes logit-like
predictions (that run from -infinity to +infinity), such as
BCEWithLogitsLoss.
MultiLabelSoftMarginLoss and BCEWithLogitsLoss
are essentially the same function, as Peter explains here:

  
    
    
    What is the difference between BCEWithLogitsLoss and MultiLabelSoftMarginLoss 
  
  
    You are right. Both loss functions seem to return the same loss values: 
x = Variable(torch.randn(10, 3))
y = Variable(torch.FloatTensor(10, 3).random_(2))

# double the loss for class 1
class_weight = torch.FloatTensor([1.0, 2.0, 1.0])
# double the loss for last sample
element_weight = torch.FloatTensor([1.0]*9 + [2.0]).view(-1, 1)
element_weight = element_weight.repeat(1, 3)

bce_criterion = nn.BCEWithLogitsLoss(weight=None, reduce=False)
multi_criterion = nn.MultiLabelSoftMarginLoss(weight=No…
  


Good luck.
K. Frank