pytorch報錯:
RuntimeError: Expected object of type Variable[torch.LongTensor] but found type Variable[torch.cuda.ByteTensor] for argument #1 ‘argument1'
解決方法:
pytorch框架在存儲labels時,采用LongTensor來存儲,所以在一開始dataset返回label時,就要返回與LongTensor對應的數據類型,即numpy.int64
補充:使用pytorch遇到的各種問題及解決方案
自己在使用pytorch遇到的各種問題及解決方案:
RuntimeError: Expected object of scalar type Float but got scalar type Double for argument #4 'mat1'
RuntimeError: The size of tensor a (12800) must match the size of tensor b (100) at non-singleton dimension 0
輸入維度為12800,輸出維度為100,輸入輸出的維度不一致,正確的例子如下:
inputs = [(1,2,3), (2,3,4)]
outsputs = [4, 5]
將輸入輸出的長度改為一致
取tensor的第一個元素
XXX.item() # XXX為tensor對象
tensor中的元素改變數據類型
# 常常因為數據類型出錯,要修改數據類型
XXX.int()
XXX.float()
補充:Pytorch的Dataloader報錯:TypeError: batch must contain tensors, numbers, dicts or lists
具體報錯:
TypeError: batch must contain tensors, numbers, dicts or lists; found class 'PIL.Image.Image'>
loader的代碼:
dataloader=torch.utils.data.DataLoader(dataset,batch_size=1,shuffle=True)
表面上看這個代碼沒有問題,實際上問題出在了dataloader機制的要求上,dataloader要求接收的是一個tensor,而我的dataset沒有做transform,所以dataset的getitem函數返回的是一個PIL的Image對象,所以就會報錯
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
您可能感興趣的文章:- PyTorch中Tensor的數據類型和運算的使用
- pytorch中tensor張量數據類型的轉化方式
- pytorch使用 to 進行類型轉換方式