pytorch學習筆記(一) model的cuda運行,torch.unsqueeze

官方docs: http://pytorch.org/docs/master/
中文版docs: http://pytorch-cn.readthedocs.io/zh/latest/

最近在看廖星宇寫的<<深度學習入門之pytorch>>,發(fā)現(xiàn)了一些問題(可能是版本問題),在這邊總結(jié)一下:
書中代碼地址https://github.com/SherlockLiao/code-of-learn-deep-learning-with-pytorch/tree/master/
本文代碼地址https://github.com/qianhaoq/pytorch_test

一.第三章線性回歸的代碼實現(xiàn)中,3.2.4章代碼實現(xiàn)的問題

在該章中, 原文把模型放在cuda上采用了如下代碼:

if torch.cuda.is_available():
    model = LinearRegression().cuda()
else:
    model = LinearRegression()

本來很好理解,就是cuda可用時就把模型放在cuda上運行,整個程序?qū)懴聛恚趖orch.cuda.is_available()為false的情況下是可運行的,但在cuda為avaliable的時候,會報如下錯誤:

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)

可以看到是預測模型這塊的問題,在底層的數(shù)據(jù)類型不匹配造成的,我嘗試過通過 tensor.cpu()等方法強制使用cpu()來處理,但這樣也是去了利用gpu計算的優(yōu)勢,在pytorch的官方github的issus上有人提過類似的問題,地址如下:
https://github.com/pytorch/pytorch/issues/1472
里面有人提出了一個解決方案,即使用

    model = LinearRegression()
    model = torch.nn.DataParallel(model).cuda()

代替

    model = LinearRegression().cuda()

問題解決


成功運行截圖

二. 關于pytorch中 torch.squeeze和torch.unsqueeze的使用說明
剛接觸這一塊的時候不太了解這2個函數(shù)的作用以及使用方法,查閱了官方docs后大致了解掌握,在此記錄下:

     torch.squeeze(input, dim=None, out=None)
     Returns a Tensor with all the dimensions of input of size 1 removed.
     默認是去掉所有維度中維度大小為1的維度并返回
     若指定了dim(從0開始),則判斷該維度大小是否為1,若為1則去掉

examples


去掉所有1維向量
     torch.unsqueeze(input, dim, out=None)
     Returns a new tensor with a dimension of size one inserted at the specified position.
     在指定位置插入一個1維tensor

examples


2017-12-01 18-08-57屏幕截圖.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 本次筆記使用的材料是2018版的斯坦福計算機課程Lecture8的ppt,相較于2017版改動了一些內(nèi)容,總體是比...
    HRain閱讀 1,025評論 0 1
  • 本文代碼基于PyTorch 1.0版本,需要用到以下包 1. 基礎配置 檢查PyTorch版本 更新PyTorch...
    婉妃閱讀 2,936評論 0 13
  • 只想來見你一面,已是父親這輩子最深情的表達。 2017年8月26日 星期六 晴轉(zhuǎn)多云 記憶中,父親在身邊的日子...
    涂涂tyf閱讀 876評論 9 8
  • 這些天一直有講自己的簡歷刷新,遇到認為可以的就去面試,遇到合適的開始工作吧,孩子還小,我才需要更加的努力呀,我...
    黑胡子船長22閱讀 230評論 0 0
  • 你喜歡精明的人嗎?那個人很聰明,和他在一起,會萬事周全,但是,那個人,對身邊的人,都太精明,對你也是,這樣的人,你...
    九辭_64f1閱讀 435評論 0 0

友情鏈接更多精彩內(nèi)容