pytorch神經網絡的小問題

  1. UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).

     feature = torch.tensor(torch.from_numpy(feature), dtype=torch.float32)
    

改爲:

feature = torch.as_tensor(torch.from_numpy(feature), dtype=torch.float32)
  1. RuntimeError: Expected object of scalar type Float but got scalar type Double for argument #2 ‘mat1’ in call to _th_addmm

解決方法:

feature = torch.tensor(torch.from_numpy(feature))加上, dtype=torch.float32
feature = torch.as_tensor(torch.from_numpy(feature), dtype=torch.float32)

或者

#在代碼前添加一行,將輸入的數據轉成是dtype=torch.float32的。
    #X= torch.tensor(X, dtype=torch.float32)
    output = net(X)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章