pytorch学习笔记(一) model的cuda运行,torch.unsqueeze

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

最近在看廖星宇写的<<深度学习入门之pytorch>>,发现了一些问题(可能是版本问题),在这边总结一下:
书中代码地址https://github.com/SherlockLiao/code-of-learn-deep-learning-with-pytorch/tree/master/
本文代码地址https://github.com/qianhaoq/pytorch_test

一.第三章线性回归的代码实现中,3.2.4章代码实现的问题

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

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

本来很好理解,就是cuda可用时就把模型放在cuda上运行,整个程序写下来,在torch.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)

可以看到是预测模型这块的问题,在底层的数据类型不匹配造成的,我尝试过通过 tensor.cpu()等方法强制使用cpu()来处理,但这样也是去了利用gpu计算的优势,在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个函数的作用以及使用方法,查阅了官方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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 本次笔记使用的材料是2018版的斯坦福计算机课程Lecture8的ppt,相较于2017版改动了一些内容,总体是比...
    HRain阅读 980评论 0 1
  • 本文代码基于PyTorch 1.0版本,需要用到以下包 1. 基础配置 检查PyTorch版本 更新PyTorch...
    婉妃阅读 2,877评论 0 13
  • 只想来见你一面,已是父亲这辈子最深情的表达。 2017年8月26日 星期六 晴转多云 记忆中,父亲在身边的日子...
    涂涂tyf阅读 799评论 9 8
  • 这些天一直有讲自己的简历刷新,遇到认为可以的就去面试,遇到合适的开始工作吧,孩子还小,我才需要更加的努力呀,我...
    黑胡子船长22阅读 172评论 0 0
  • 你喜欢精明的人吗?那个人很聪明,和他在一起,会万事周全,但是,那个人,对身边的人,都太精明,对你也是,这样的人,你...
    九辞_64f1阅读 374评论 0 0