Do you have any idea of how to make it work for the following scenario?
import torch

class Network(nn.Module):
    def __init__(self):
        super(Network, self).__init__()
        self.l1 = nn.Linear(20, 15)
        self.l2 = nn.Linear(15, 10)
    
    def forward(self, input):
        out = self.l1(input).detach()
        return self.l2(out)

target = torch.randn(1, 10)
input = torch.randn(1, 20)
optimizer = torch.optim.SGD(network.parameters(), lr=0.01)
optimizer.zero_grad()
network = Network()
loss = ((network(input)-target)**2).sum()
loss.backward()
print(network.l1.weight[0, 0])
optimizer.step()
print(network.l1.weight[0, 0]) #doesn't work(it is not changed)
network.l1.weight.grad = torch.randn(15, 20)
optimizer.step()
print(network.l1.weight[0, 0]) #doesn't work(it is not changed)