Skip to content Skip to sidebar Skip to footer

Why Is It In Pytorch When I Make A Copy Of A Network's Weight It Would Be Automatically Updated After Back-propagation?

I wrote the following code as a test because in my original network I use a ModuleDict and depends on what index I feed it would slice and train only parts of that network. I wante

Solution 1:

You have to clone the parameters, otherwise you just copy the reference.

weights = []

for param in model.parameters():
    weights.append(param.clone())

criterion = nn.BCELoss() # criterion and optimizer setup
optimizer = optim.Adam(model.parameters(), lr=0.001)

foo = torch.randn(3, 10) # fake input
target = torch.randn(3, 5) # fake target

result = model(foo) # predictions and comparison and backprop
loss = criterion(result, target)
optimizer.zero_grad()
loss.backward()
optimizer.step()


weights_after_backprop = [] # weights after backprop
for param in model.parameters():
    weights_after_backprop.append(param.clone()) # only layer1's weight should update, layer2 is not used

for i in zip(weights, weights_after_backprop):
    print(torch.equal(i[0], i[1]))

which gives

FalseFalseTrueTrue

Post a Comment for "Why Is It In Pytorch When I Make A Copy Of A Network's Weight It Would Be Automatically Updated After Back-propagation?"