Replace trained layer weights with modified weights

Hi all,

I’m working on a project and have extracted the trained layer weights, manipulated them, and am trying to figure out how to replace the trained weights with the manipulated weights I have. Can anyone please help?


Do you still have the state_dict and could load it or do you have the parameters as tensors after your manipulation?
In the former case, you could just try model.load_state_dict(manipulated_state_dict),
while this code might help setting the weights for the latter case:

lin = nn.Linear(in_features=10, out_features=2, bias=False)
x = torch.ones(1, 10)
output = lin(x)

with torch.no_grad():
    # Manipulate your weight here
    lin.weight += 1 # or e.g. lin.weight = nn.Parameter(lin.weight / 2. + 1)

output = lin(x)
1 Like

Thanks very much, I’ll have to give this a try!