我知道两种从梯度计算中排除计算元素的方法 backward
方法一:使用with torch.no_grad()
with torch.no_grad():
y = reward + gamma * torch.max(net.forward(x))
loss = criterion(net.forward(torch.from_numpy(o)), y)
loss.backward();
Run Code Online (Sandbox Code Playgroud)
方法二:使用.detach()
y = reward + gamma * torch.max(net.forward(x))
loss = criterion(net.forward(torch.from_numpy(o)), y.detach())
loss.backward();
Run Code Online (Sandbox Code Playgroud)
这两者有区别吗?两者都有好处/坏处吗?