pytorch--(MisMatch in shape & invalid index of a 0-dim tensor)


在嘗試運行CVPR2019一篇行為識別論文的代碼時,遇到了兩個問題,記錄如下。但是,原因沒懂,如果看此文章的你了解原理,歡迎留言交流吖。

github代碼鏈接:

問題1:“Mismatch in shape: grad_output[0] has a shape of torch.Size([1]) and output[0] has a shape of torch.Size([]).”

方法1:

根據定位的錯誤位置,我的是215行,將criticD_real.bachward(mone)改為criticD_real.bachward(mone.mean())上一行注釋。
保存后運行,發現又報類似的錯:Mismatch in shape: grad_output[0] has a shape of torch.Size([]) and output[0] has a shape of torch.Size([64,1]).

方法2:將“方法1”中的改動復原,找到聲明mone的位置,

111行: one=torch.FloatTensor([1])
112行: mone = one * -1

將111行的one 改為,one= torch.tensor(1, dtype=torch.float),保存后運行,發現報問題2的錯。

問題2:UserWarning: invalid index of a 0-dim tensor.Use tensor.item() to convert a 0-dim tensor to a Python number .

      方法1:搜索data[0],將其全部更換成item()。

至此,代碼的錯誤解決。
簡單完美,雖然我也沒能理解why?
參考鏈接:
https://blog.csdn.net/xiangpijiao/article/details/103830086


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM