How to reassign modified model.parameters() back to the model?


(Cagri Kaplan) #1

Hello, I trained a network and played on its weights by making a list using model.parameters(). Now I want to reassign new weights to the model and see new prediction values in case of classification. How can I do that?


#2

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.


(Cagri Kaplan) #3

Thanks but why did you train the network again after cloning and modifying parameters? I just need to clone, modify, reassign and predict. My aim is to see how modifying some of the weights alters the accuracy of the network? Or am I missing something in your snippet? Or can you say your answer is the the part of snippet in which random data generation and training parts are excluded?


#4

Oh, it seems the snippet is in a wrong order. You can change the train phase before modification or omit the train phase.