AssertionError: nn criterions don‘t compute the gradient w.r.t. targets
生活随笔
收集整理的這篇文章主要介紹了
AssertionError: nn criterions don‘t compute the gradient w.r.t. targets
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
AssertionError:
## nn criterions don’t compute the gradient w.r.t. targets - please mark these tensors as not requiring gradients
讀錯誤
可以看到這是一個斷言錯誤,就是我們設置的東西和系統設置的東西不太兼容,所以我們要再次設置一下。
我們看一下后面說的東西,nn的原則告訴我們這里是不需要計算梯度的,所以請將這些張量定義為不需要計算梯度。
所以是我們將某些需要計算梯度的東西傳到了不計算梯度的地方。
解決錯誤
我們看錯誤的位置,
48 target = torch.randn_like(output)49 #這里可能產生一個報錯在下面詳細敘述—> 50 loss = loss_fnc(target, output)
51 #loss = loss_fnc(output, target)52 loss.backward()傳入的東西就兩個一個target一個output。顯然,需要計算梯度的是output,所以是他的位置不對。
參考https://discuss.pytorch.org/t/nn-criterions-dont-compute-the-gradient-w-r-t-targets/3693/4
我們知道在進行反向傳播的時候,損失函數可以計算梯度的只有前一位的輸入,后一位不能計算梯度。
所以我們調換一下順序就行了。
總結
以上是生活随笔為你收集整理的AssertionError: nn criterions don‘t compute the gradient w.r.t. targets的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: anaconda不同虚拟环境下使用jup
- 下一篇: FCN全连接卷积网络(2)--读论文的过