Your approach could work, but I don’t think there is a guarantee to keep other data, such as the gradients, after loading a new state_dict, so something like this should work:
model = models.resnet18()
out = model(torch.randn(1, 3, 224, 224))
loss = out.mean()
loss.backward()

grads = {}
for name, param in model.named_parameters():
    print(param.grad.sum())
    grads[name] = param.grad.clone()
  
# change the parameters here
sd = models.resnet18().state_dict()
sd['fc.weight'] = torch.rand_like(sd['fc.weight'])
model.load_state_dict(sd)

# reload grads
for name, param in model.named_parameters():
    print(param.grad.sum())
    param.grad = grads[name]
    print(param.grad.sum())