Hi trypag,
Thanks a lot for support. I have developed this code for dice similarity measure following your code.

def dice_loss(self,output, target, weights=None, ignore_index=None):
    # output : NxCxHxW Variable of float tensor
    # target :  NxHxW long tensor
    # weights : C float tensor
    # ignore_index : int value to ignore from loss
    smooth = 1.
    loss = 0.

    output = output.exp()   # computes the exponential of each element ie. for 0 it finds 10
    encoded_target = output.data.clone().zero_() # make output size array and initialize with zeros
    #ignore_index=1

    if ignore_index is not None:
        mask = target == ignore_index
        target = target.clone()
        target[mask] = 0
        encoded_target.scatter_(1, target.unsqueeze(1), 1)
        mask = mask.unsqueeze(1).expand_as(encoded_target)
        encoded_target[mask] = 0
    else:
      unseq=target.long()  # here
      unseq=unseq.data   # here
      encoded_target.scatter_(1, unseq, 1)
     

    encoded_target = Variable(encoded_target)

    if weights is None:
        weights = Variable(torch.ones(output.size(1)).type_as(output.data))

    intersection = output * encoded_target
    numerator = 2 * intersection.sum(3).sum(2).sum(0) + smooth
    denominator = (output + encoded_target).sum(3).sum(2).sum(0) + smooth
    loss_per_channel = weights * (1 - (numerator / denominator)) # weights may be directly multiplied

    return loss_per_channel.sum() / output.size(1)


The code seems to working fine, here are two things to consider. 1) what is the purpose of getting exponential of target variable and 2) i had to change few lines as shown in bold as without them i was getting errors. Can you have a look on this code. Thirdly as given in “Generalized Dice overlap as a deep learning loss function for highly unbalanced segmentation”  paper, the weights should be multiplied as (1-weights*(numerator/denominator)) and it is also given here https://cmiclab.cs.ucl.ac.uk/CMIC/NiftyNet/blob/dev/niftynet/layer/loss_segmentation.py 72 in generalized dice loss function.
Best