github.com
  
  
    mattmacy/torchbiomed/blob/master/torchbiomed/loss.py#L32 27

result_ = torch.squeeze(result_)
if input.is_cuda:
    result = torch.cuda.FloatTensor(result_.size())
    self.target_ = torch.cuda.FloatTensor(target.size())
else:
    result = torch.FloatTensor(result_.size())
    self.target_ = torch.FloatTensor(target.size())
result.copy_(result_)
self.target_.copy_(target)
target = self.target_
#       print(input)
intersect = torch.dot(result, target)
# binary values so sum the same as sum of squares
result_sum = torch.sum(result)
target_sum = torch.sum(target)
union = result_sum + target_sum + (2*eps)


# the target volume can be empty - so we still want to
# end up with a score of 1 if the result is 0/0
IoU = intersect / union
print('union: {:.3f}\t intersect: {:.6f}\t target_sum: {:.0f} IoU: result_sum: {:.0f} IoU {:.7f}'.format(



  
  
    
    
  
  


I’ve implemented the Dice IoU function as a loss function as suggested by the Vnet paper. I’ve copied the last couple of lines from the backward function as generated by  _make_function_class_criterion and I’ve implemented a separate loss function as a wrapper around the call to forward.
Since I’m just imitating what I see elsewhere there’s a few things I don’t understand about loss functions.
In class Function  you set __call__ = _C._FunctionBase._do_forward - presumably that just calls down in to whatever forward function that a derived class implements. Given a call down in to the forward function in the training code that returns a tensor, how does the torch know what the corresponding backward function is when you call backward() on that tensor?
e.g.
loss = F.nll_loss(output, target)
loss.backward()
My second question is what the last two lines are doing. What does the view(*repeat(… do? And what does mul_(tensor.expand_as(…) do?
Last question is, does the DiceLoss class make sense? When I first started looking at adding a new loss function a few days ago it seemed overwhelming, but this seems like a rather trivial piece of code.
Thanks in advance.