Hi,
I wrote a snippet as follow:
model = Net()
old_params = {}

for name, params in model.named_parameters():
    old_params[name] = params.clone()

# do some modification here
old_params['conv1.weight'][0] = 0.

random_input = torch.randn(1,3,3,3)
random_target = torch.randn(3,3,3)
criterion = nn.MSELoss()
opt = torch.optim.SGD(model.parameters(), lr=0.1)

for i in range(3):
    opt.zero_grad()
    random_out = model(random_input)
    loss = criterion(random_out, random_target)
    loss.backward()
    opt.step()

# leaf variable with requires_grad=True can not used inplace operation
for name, params in model.named_parameters():
    params.data.copy_(old_params[name])

# do another predict

model is a convnet with three layers stack.