【Pytorch】問題解決:AttributeError: 'int' object has no attribute 'float'

前言

這篇是我在使用 Pytorch 的時候碰到的問題,出現了以下的訊息:

AttributeError: 'int' object has no attribute 'float'

解決方法

我碰到這個問題的時候,最後解決問題的方法是發現根本沒有 load 資料進來!

發生錯誤的程式碼部份:

print(type(mae))
mae = mae.float() / num_examples

主要是第二行,mae 無法進行 float() 的動作,
實際原因是因為資料量為 0,導致 type 不為 Tensor

導致 print(type(mae)) 得到 <class ‘int’>
而正確的應該是 <class ’torch.Tensor’>

本次結論

請檢查資料有沒有成功的 load 進來!!!

Licensed under CC BY-NC-SA 4.0