您必须clone
使用参数,否则只需复制引用即可。
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]))
这使
False
False
True
True