I am calling it from forward as mentioned in code but it gives error “my_hook” is not defined. I make changes mentioned in code 2 but in that also it gives the same error.
import torch.nn as nn
class Net(torch.nn.Module):

    def __init__(self):       
        super(Net, self).__init__()
        self.w=torch.nn.Parameter(torch.ones(2))
       
    def my_hook(grad):
      grad = grad.clone()
      grad[0][0] = 50
      print("new grad: ", grad)
      return grad

    def forward(self, x):
        out1= 2 * x
        out1.register_hook(my_hook)
        out = torch.mul(out1,self.w)
        return out

   
learning_rate = 0.2
model=Net()
criterion=torch.nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)

x=4*torch.ones((2),requires_grad=True)
target=torch.FloatTensor([30,25])

for i in range (0,2):
    optimizer.zero_grad()
    out=model(x)
    loss= criterion(target,out)
    print("output and loss is ",out,loss)
    loss.backward(retain_graph=True)
    optimizer.step()

Code:2
import torch

import torch.nn as nn

class Net(torch.nn.Module):

    def __init__(self):       

        super(Net, self).__init__()

        self.w=torch.nn.Parameter(torch.ones(2))

        self.out1=torch.ones(2)

       

    def my_hook(self,grad):

      grad = grad.clone()

      grad[0][0] = 50

      print("new grad: ", grad)

      return grad

    def forward(self, x):

        self.out1= 2 * x

        # out1.register_hook(my_hook)

        out = torch.mul(self.out1,self.w)

        return out

   

learning_rate = 0.2

model=Net()

criterion=torch.nn.MSELoss()

optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)

x=4*torch.ones((2),requires_grad=True)

target=torch.FloatTensor([30,25])

for i in range (0,2):

    optimizer.zero_grad()

    out=model(x)

    loss= criterion(target,out)

    print("output and loss is ",out,loss)

    loss.backward(retain_graph=True)

    model.out1.register_hook(my_hook)

    optimizer.step()