var.detach().numpy() instead. pytorch Tensor 轉 numpy 報錯

    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "F:/GPAI/secondTimes/CatchYou/Train.py", line 49, in <module>
    pre_rect = out[0].numpy()
RuntimeError: Can't call numpy() on Variable that requires grad. Use var.detach().numpy() instead.

問題代碼:

 

按報錯提醒修改後,成功運行。。。在有求導要求的參數上不能直接用numpy()轉,要用var.detach().numpy()

直接打印出來可以看到, 第一個tensor可以直接numpy(),第二個要轉

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章