官方docs: http://pytorch.org/docs/master/
中文版docs: http://pytorch-cn.readthedocs.io/zh/latest/
最近在看廖星宇寫的<<深度學(xué)習(xí)入門之pytorch>>芳绩,發(fā)現(xiàn)了一些問題(可能是版本問題)徘溢,在這邊總結(jié)一下:
書中代碼地址https://github.com/SherlockLiao/code-of-learn-deep-learning-with-pytorch/tree/master/
本文代碼地址https://github.com/qianhaoq/pytorch_test
一.第三章線性回歸的代碼實(shí)現(xiàn)中,3.2.4章代碼實(shí)現(xiàn)的問題
在該章中彻磁, 原文把模型放在cuda上采用了如下代碼:
if torch.cuda.is_available():
model = LinearRegression().cuda()
else:
model = LinearRegression()
本來很好理解,就是cuda可用時(shí)就把模型放在cuda上運(yùn)行佃扼,整個(gè)程序?qū)懴聛碣顺玻趖orch.cuda.is_available()為false的情況下是可運(yùn)行的蔼夜,但在cuda為avaliable的時(shí)候,會(huì)報(bào)如下錯(cuò)誤:
Epoch[920/1000], loss:0.169177
Epoch[940/1000], loss:0.169152
Epoch[960/1000], loss:0.169129
Epoch[980/1000], loss:0.169108
Epoch[1000/1000], loss:0.169089
Traceback (most recent call last):
File "linear.py", line 93, in <module>
predict = model(Variable(x_train))
File "/home/qh/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 224, in __call__
result = self.forward(*input, **kwargs)
File "linear.py", line 37, in forward
out = self.linear(x)
File "/home/qh/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 224, in __call__
result = self.forward(*input, **kwargs)
File "/home/qh/anaconda3/lib/python3.6/site-packages/torch/nn/modules/linear.py", line 53, in forward
return F.linear(input, self.weight, self.bias)
File "/home/qh/anaconda3/lib/python3.6/site-packages/torch/nn/functional.py", line 553, in linear
return torch.addmm(bias, input, weight.t())
File "/home/qh/anaconda3/lib/python3.6/site-packages/torch/autograd/variable.py", line 924, in addmm
return cls._blas(Addmm, args, False)
File "/home/qh/anaconda3/lib/python3.6/site-packages/torch/autograd/variable.py", line 920, in _blas
return cls.apply(*(tensors + (alpha, beta, inplace)))
File "/home/qh/anaconda3/lib/python3.6/site-packages/torch/autograd/_functions/blas.py", line 26, in forward
matrix1, matrix2, out=output)
TypeError: torch.addmm received an invalid combination of arguments - got (int, torch.cuda.FloatTensor, int, torch.FloatTensor, torch.cuda.FloatTensor, out=torch.cuda.FloatTensor), but expected one of:
* (torch.cuda.FloatTensor source, torch.cuda.FloatTensor mat1, torch.cuda.FloatTensor mat2, *, torch.cuda.FloatTensor out)
* (torch.cuda.FloatTensor source, torch.cuda.sparse.FloatTensor mat1, torch.cuda.FloatTensor mat2, *, torch.cuda.FloatTensor out)
* (float beta, torch.cuda.FloatTensor source, torch.cuda.FloatTensor mat1, torch.cuda.FloatTensor mat2, *, torch.cuda.FloatTensor out)
* (torch.cuda.FloatTensor source, float alpha, torch.cuda.FloatTensor mat1, torch.cuda.FloatTensor mat2, *, torch.cuda.FloatTensor out)
* (float beta, torch.cuda.FloatTensor source, torch.cuda.sparse.FloatTensor mat1, torch.cuda.FloatTensor mat2, *, torch.cuda.FloatTensor out)
* (torch.cuda.FloatTensor source, float alpha, torch.cuda.sparse.FloatTensor mat1, torch.cuda.FloatTensor mat2, *, torch.cuda.FloatTensor out)
* (float beta, torch.cuda.FloatTensor source, float alpha, torch.cuda.FloatTensor mat1, torch.cuda.FloatTensor mat2, *, torch.cuda.FloatTensor out)
didn't match because some of the arguments have invalid types: (int, torch.cuda.FloatTensor, int, torch.FloatTensor, torch.cuda.FloatTensor, out=torch.cuda.FloatTensor)
* (float beta, torch.cuda.FloatTensor source, float alpha, torch.cuda.sparse.FloatTensor mat1, torch.cuda.FloatTensor mat2, *, torch.cuda.FloatTensor out)
didn't match because some of the arguments have invalid types: (int, torch.cuda.FloatTensor, int, torch.FloatTensor, torch.cuda.FloatTensor, out=torch.cuda.FloatTensor)
可以看到是預(yù)測(cè)模型這塊的問題压昼,在底層的數(shù)據(jù)類型不匹配造成的求冷,我嘗試過通過 tensor.cpu()等方法強(qiáng)制使用cpu()來處理瘤运,但這樣也是去了利用gpu計(jì)算的優(yōu)勢(shì),在pytorch的官方github的issus上有人提過類似的問題匠题,地址如下:
https://github.com/pytorch/pytorch/issues/1472
里面有人提出了一個(gè)解決方案拯坟,即使用
model = LinearRegression()
model = torch.nn.DataParallel(model).cuda()
代替
model = LinearRegression().cuda()
問題解決
二. 關(guān)于pytorch中 torch.squeeze和torch.unsqueeze的使用說明
剛接觸這一塊的時(shí)候不太了解這2個(gè)函數(shù)的作用以及使用方法,查閱了官方docs后大致了解掌握韭山,在此記錄下:
torch.squeeze(input, dim=None, out=None)
Returns a Tensor with all the dimensions of input of size 1 removed.
默認(rèn)是去掉所有維度中維度大小為1的維度并返回
若指定了dim(從0開始)郁季,則判斷該維度大小是否為1,若為1則去掉
examples
torch.unsqueeze(input, dim, out=None)
Returns a new tensor with a dimension of size one inserted at the specified position.
在指定位置插入一個(gè)1維tensor
examples