Hi,
Thank you. You code gives the right answer, but if you do it my way, it doesn’t give the correct answer. Please run this code to see what I mean. Thanks a lot.
x = torch.rand(10, 1, requires_grad=False)#simulate x
y = 2 + 3 * x + .1 * torch.rand(10, 1)#simulate y from linear regression

Run linear regression
class net(torch.nn.Module):
    def __init__(self):
        super(net, self).__init__()

        self.linear = torch.nn.Linear(1, 1, bias=True)

    def forward(self, x):
        out = self.linear(x)
        return(out)


x.requires_grad_()
# training
model = net()

model.train()

loss = torch.nn.MSELoss(size_average=True)
optimizer  = torch.optim.SGD(model.parameters(), lr = 0.01)
 

for epoch in range(10):
    
    y_pred = model(x)

    l = loss(y_pred, y)

    optimizer.zero_grad()
    l.backward()
    optimizer.step()

print(x.grad)