Python records the error of None when tensor seeks the gradient

To learn against samples today, you need to find the gradient of tensor,

x1, x2, label = data
x1.requires_grad = True
x2.requires_grad = True
if use_gpu:
    x1 = x1.cuda()
    x2 = x2.cuda()
    label = label.cuda()
output1, output2 = net(x1, x2)
# 计算损失
loss = criterion(output1, output2, label)
# 将所有现有的渐变归零
net.zero_grad()
loss.backward()
# 收集grad
x1_grad = x1.grad
x2_grad = x2.grad

The obtained gradient is None and
it is found that the position of requires_grad is wrong. It should be that after the tensor is put into cuda, requires_grad is changed to False by default.

if use_gpu:
    x1 = x1.cuda()
    x2 = x2.cuda()
    label = label.cuda()
x1.requires_grad = True
x2.requires_grad = True 

solve

Guess you like

Origin blog.csdn.net/weixin_47681965/article/details/125800343