You are right.
i changed that ; (i specified the place with “**”)
class Dice_Loss(torch.nn.Module):
    
    **def __init__(self,x,y):**
**        super(Dice_Loss,self).__init__()**
**        self.x = x**
**        self.y = y**
    def forward(self,x,y):
        self.x = x.view(-1)
        self.y = y.view(-1)
        square_x = torch.sqrt(torch.square(self.x))
        square_y = torch.sqrt(torch.square(self.y))
        intersection = 2*torch.mul(square_x,square_y)
        #multipication = torch.sum(torch.sqrt(torch.square(x)),torch.sqrt(torch.square(y)))
        multipication = square_x.add(square_y)
        return (intersection/multipication)

and
loss_dice = Dice_Loss(labels,output)
loss_dice.backward()

new trace
ModuleAttributeError                      Traceback (most recent call last)
<ipython-input-16-41ead3ef64fb> in <module>()
     15     print(labels.size(),type(labels))
     16     loss_dice = Dice_Loss(labels,output)
---> 17     loss_dice.backward()
     18     optim.step()
     19     running_loss += (loss.item()/len(images))

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in __getattr__(self, name)
    777                 return modules[name]
    778         raise ModuleAttributeError("'{}' object has no attribute '{}'".format(
--> 779             type(self).__name__, name))
    780 
    781     def __setattr__(self, name: str, value: Union[Tensor, 'Module']) -> None:

ModuleAttributeError: 'Dice_Loss' object has no attribute 'backward'

i directly used Dice_Loss function in traning process but now that Dice_Loss object has no attribute backward. That is true but should i use backward method in new version of pytorch ? (i have seen a few comments without backward method  ) i can’t find example about this method .  Do you have an example about custom loss function backward method ?