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