for batch_idx, (inputs, targets) in enumerate(testloader):
optimizer.zero_grad()
inputs = inputs.float().cuda()
inputs, targets = inputs.to(device), targets.to(device)
outputs = im_net(inputs)
ce_loss = criterion(outputs, targets)
loss = criterion(outputs, targets)
loss.backward()
im_net.features[25].weight.grad[temp_5.values] = 0
print(im_net.features[25].weight.grad[temp_5.values])
optimizer.step()以上是我的代码,试图修复第25卷积层滤波器的子集(temp_5.values)。我尝试将子集过滤器的梯度设置为0,这样就不会更新。当我试图打印梯度时,它们都是零,但是权重已经改变了。
所以有两个问题:
发布于 2020-04-09 14:34:01
在移相器中这样做的方法是将requires_grad设置为False。
这一行应该能做到这一点:
im_net.features[25].weight.requires_grad = Falsehttps://stackoverflow.com/questions/61122757
复制相似问题