pytorch学习笔记(一) model的cuda运行,torc

2017-12-01  本文已影响0人  夜空_7ddd

官方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
上一篇下一篇

猜你喜欢

热点阅读