pytorch实践中module 'torch' has no attribute 'form_numpy'问题的解决
2017-09-25 10:57
826 查看
最近开始仔细玩了一下pytorch,发现里面有个BUG之前都没有发现。
在测试torch最基本的示例的情况下,居然碰到了个pytorch无法转化numpy为Tensor的问题,呈现的问题如下:
ndscbigdata@ndscbigdata:~/work/change/AI$ python
Python 3.6.1 (default, Jul 14 2017, 17:08:44)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> import numpy as np
>>> a = np.ones(5)
>>> a
array([ 1., 1., 1., 1., 1.])
>>> b=torch.form_numpy(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'torch' has no attribute 'form_numpy'
>>> print(torch.__version__)
0.2.0_3
关于这个问题,很难查找网上对应的解决办法。因此将此问题的解决办法写下来。
在torch的主页上有这样一句话,经过仔细分析才明白其中的意思:
Pylint isn't picking up that
the member function
It's because
actually
far as Pylint is concerned.
本身而言,pytorch并不直接包含from_numpy这个方法,而需要通过_C这样的命名空间来调用。
因此利用._C的办法进行测试,果然顺利通过。
>>> b=torch.form_numpy(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'torch' has no attribute 'form_numpy'
>>> print(torch.__version__)
0.2.0_3
>>> c = torch.Tensor(3,3)
>>> c
1.00000e-32 *
-4.4495 0.0000 0.0000
0.0000 0.0000 0.0000
0.0000 0.0000 0.0000
[torch.FloatTensor of size 3x3]
>>> b = torch._C.from_numpy(a)
>>> b
1
1
1
1
1
[torch.DoubleTensor of size 5]
那么在代码中大部分都是直接torch.from_numpy的方法,直接每个代码添加._C的方式并不可靠。
For reference, you can have Pylint ignore these by wrapping "problematic" calls with the following comments.
# pylint: disable=E1101
tensor = torch.from_numpy(np_array)
# pylint: enable=E1101
同时又看到这样的一段话,才发现有个pylint的工具。
于是重新再次安装一下这个工具。
然后再测试一下,发现就正常了。
ndscbigdata@ndscbigdata:~/work/change/AI$ python
Python 3.6.1 (default, Jul 14 2017, 17:08:44)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> import numpy as np
>>> a = np.ones(5)
>>> b=torch.from_numpy(a)
>>> b
1
1
1
1
1
[torch.DoubleTensor of size 5]
>>>
分析一下原因,可能是由于pytorch安装的时间比pylint较晚,因此之前的pylint没有更新到这个包。
在测试torch最基本的示例的情况下,居然碰到了个pytorch无法转化numpy为Tensor的问题,呈现的问题如下:
ndscbigdata@ndscbigdata:~/work/change/AI$ python
Python 3.6.1 (default, Jul 14 2017, 17:08:44)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> import numpy as np
>>> a = np.ones(5)
>>> a
array([ 1., 1., 1., 1., 1.])
>>> b=torch.form_numpy(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'torch' has no attribute 'form_numpy'
>>> print(torch.__version__)
0.2.0_3
关于这个问题,很难查找网上对应的解决办法。因此将此问题的解决办法写下来。
在torch的主页上有这样一句话,经过仔细分析才明白其中的意思:
Pylint isn't picking up that
torchhas
the member function
from_numpy.
It's because
torch.from_numpyis
actually
torch._C.from_numpyas
far as Pylint is concerned.
本身而言,pytorch并不直接包含from_numpy这个方法,而需要通过_C这样的命名空间来调用。
因此利用._C的办法进行测试,果然顺利通过。
>>> b=torch.form_numpy(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'torch' has no attribute 'form_numpy'
>>> print(torch.__version__)
0.2.0_3
>>> c = torch.Tensor(3,3)
>>> c
1.00000e-32 *
-4.4495 0.0000 0.0000
0.0000 0.0000 0.0000
0.0000 0.0000 0.0000
[torch.FloatTensor of size 3x3]
>>> b = torch._C.from_numpy(a)
>>> b
1
1
1
1
1
[torch.DoubleTensor of size 5]
那么在代码中大部分都是直接torch.from_numpy的方法,直接每个代码添加._C的方式并不可靠。
For reference, you can have Pylint ignore these by wrapping "problematic" calls with the following comments.
# pylint: disable=E1101
tensor = torch.from_numpy(np_array)
# pylint: enable=E1101
同时又看到这样的一段话,才发现有个pylint的工具。
于是重新再次安装一下这个工具。
pip install pylint
然后再测试一下,发现就正常了。
ndscbigdata@ndscbigdata:~/work/change/AI$ python
Python 3.6.1 (default, Jul 14 2017, 17:08:44)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> import numpy as np
>>> a = np.ones(5)
>>> b=torch.from_numpy(a)
>>> b
1
1
1
1
1
[torch.DoubleTensor of size 5]
>>>
分析一下原因,可能是由于pytorch安装的时间比pylint较晚,因此之前的pylint没有更新到这个包。
相关文章推荐
- 关于python里运行出现'module' object has no attribute 'xxxx'等问题解决方法
- AttributeError: 'module' object has no attribute 'face' 问题解决 opencv+python
- AttributeError: ‘module’ object has no attribute’xxx 问题解决
- 'module' object has no attribute 'OP_NO_TLSv1_1'问题解决
- 'module' object has no attribute 'MLPRegressor'问题解决(Python2.7)
- 关于Python的:'module' object has no attribute 'inet_pton'问题解决
- AttributeError: module 'numpy' has no attribute 'dtype' 问题
- python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法
- web.py 直接使用示例代码,web.application报错, 'module' object has no attribute 'application',问题原因分析
- 解决Pycharm中matplotlib画图出错问题(AttributeError: module 'matplotlib' has no attribute 'verbose')
- 解决问题 AttributeError: 'module' object has no attribute 'face_recognition_model_v1'
- Tensorflow protobuf版本报错问题解决(AttributeError: 'module' object has no attribute 'Default')
- TF版本问题:解决AttributeError: module 'tensorflow' has no attribute 'mul'—Jason niu
- python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法
- 解决pycharm问题:module 'pip' has no attribute 'main'
- pyhton 安装pip 以及 numpy (解决python:ModuleNotFoundError:No module named "numpy" 等类似缺包问题
- ubuntu环境下Pytorch安装及No module named 'torch._C'问题解决
- Tensorflow出现AttributeError: module 'tensorflow' has no attribute 'constant'问题怎么办?
- module 'pandas' has no attribute 'expression' 解决办法
- AttributeError: 'module' object has no attribute 'Frame' 解决方法