1. 程式人生 > >pytorch實踐中module 'torch' has no attribute 'form_numpy'問題的解決

pytorch實踐中module 'torch' has no attribute 'form_numpy'問題的解決

最近開始仔細玩了一下pytorch,發現裡面有個BUG之前都沒有發現。

在測試torch最基本的示例的情況下,居然碰到了個pytorch無法轉化numpy為Tensor的問題,呈現的問題如下:

[email protected]:~/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 torch has the member function from_numpy. It's because torch.from_numpy is actually torch._C.from_numpy as 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
然後再測試一下,發現就正常了。
[email protected]:~/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沒有更新到這個包。

相關推薦

pytorch實踐module 'torch' has no attribute 'form_numpy'問題的解決

最近開始仔細玩了一下pytorch,發現裡面有個BUG之前都沒有發現。 在測試torch最基本的示例的情況下,居然碰到了個pytorch無法轉化numpy為Tensor的問題,呈現的問題如下: [em

module 'h5py' has no attribute 'File' 解決辦法

之前執行一直沒有出現過的問題,現在換了環境總是出現 AttributeError: module 'h5py' has no attribute 'File' 解決辦法: AttributeError: module 'h5py' has no attribute 'F

module 'pandas' has no attribute 'expression' 解決辦法

匯入 import tensorflow as tf import skflow 遇到了錯誤 module 'pandas' has no attribute 'expression' 原因是tensorflow pandas dask

Python指令碼報錯AttributeError: ‘module’ object has no attribute’xxx’解決方法

最近在編寫Python指令碼過程中遇到一個問題比較奇怪:Python指令碼完全正常沒問題,但執行總報錯"AttributeError: 'module' object has no attribute 'xxx'"。這其實是.pyc檔案存在問題。 問題定位:

Python基礎學習-'module' object has no attribute 'urlopen'解決方法

參考“http://blog.sina.com.cn/s/blog_5cf74e410102uxsg.html” 用的是python 3.4 非常簡單的一小段程式碼 #!/usr/bin/python # -*- coding: UTF-8 -*- import ur

【python學習筆記(2)】指令碼報錯"AttributeError: 'module' object has no attribute 'xxx'"解決方法

最近在編寫Python指令碼過程中遇到一個問題比較奇怪:Python指令碼完全正常沒問題,但執行總報錯"AttributeError: 'module' object has no attribute 'xxx'"。這其實是.pyc檔案存在問題。 問題定位: 檢視imp

AttributeError: module 'pandas' has no attribute 'Series'解決辦法

pandas是我們進行資料處理和分析時最常用的包之一,但是有時候出現AttributeError: module 'pandas' has no attribute 'Series'這樣的錯誤,在網上看了好多各種各樣的解決辦法, 但是其實真正的錯誤主要是兩個方面: (

20180925:問題:pycharm匯入flask-wtf失敗,報錯:module 'pip' has no attribute 'main'

前後臺頁面搭建完成,開始後臺邏輯操作。 在開始之前,根據視訊要匯入flask-wtf模組。在pycharm中匯入flask-wtf時,卻顯示報錯,錯誤最後一句提示:module 'pip' has no attribute 'main' 試圖在Termianl面

pytorch低版本載入高版本pytorch訓練得到的模型,出現‘module’ object has no attribute ‘_rebuild_tensor_v2’錯誤

情景 使用pytorch0.3來載入Mobilenetv1的模型(用更高版本的pytorch訓練得到的),出現“AttributeError: ‘module’ object has no attrib

Python 3.x使用urllib出現AttributeError: module 'urllib' has no attribute 'request'錯誤

剛剛開始學習爬蟲,開始寫部落格打算把錯誤記錄下來,已杜自己忘記,並給同樣的小白幫助python 3.x中urllib庫和urilib2庫合併成了urllib庫,python3.X中應該使用urllib.request,即替換掉(python中的)urllib2成urllib.

error: ‘module’ object has no attribute ‘_rebuild_tensor_v2’

bject itl func hooks except attribute def ttr imp import torch._utils try: torch._utils._rebuild_tensor_v2 except AttributeError:

針對AttributeError: ‘module’ object has no attribute’xxx’的錯誤歸類

找不到 with 類型 error: 開頭 -a 發現 使用 def 目前遇見的有三種類型: 拼寫錯誤,模塊一定要拼寫錯誤,這個也是最容易犯的,發現找不到模塊的時候,最好先檢查一遍自己引入的模塊拼寫尤其是那些名字非常長的比如HTTPPasswordMgrWithDefau

module ‘markdown‘ has no attribute ‘version‘

anaconda har evel pycha 定位 oca arm 2.6 ttr 最近在寫一個CMDB的項目,遇到drf與django版本問題... 錯誤如下: AttributeError at / module ‘markdown‘ has no attribute

解決pycharm問題:module ‘pip‘ has no attribute ‘main‘

article 卸載 根據 sta 建議 pack blog htm tin 一:可能性解決方法一《解決pycharm問題:module ‘pip‘ has no attribute ‘main‘》http://www.cnblogs.com/Fordestiny/p/89

在Pycharm出現“pip has no attribute 'main'”問題的解決

在使用Pycharm中的setting來安裝包的時候,出現如標題的報錯。我就有點納悶了,上網找解決方案,能夠這樣解決 1.首先,這個是Pycharm的問題,在Pycharm的安裝目錄下面的helpers資料夾下找到packaging_tool.py,並開啟 然後,找到下面的程式碼段:

module' object has no attribute 'urlretrieve'

python27的程式在python33中除錯時出現:‘module’ object has no attribute ‘urlretrieve’,的錯誤。 -- encoding:UTF-8 -- import urllib.request ‘module’

IDA執行加密演算法識別外掛findcrypt-yara報錯‘module’ object has no attribute’set_name’

10月12日 findcrypt下載的是github專案, 負責人似乎沒維護好,指令碼原始碼findcrypt3.py裡的195行出錯 idc.set_name(value[0], name

解決pycharm問題:module 'pip' has no attribute 'main'

解決pycharm問題:module ‘pip’ has no attribute ‘main’ 修改 do_install和 do_uninstall def do_install(pkgs):

Python 在import時明明寫的正確,卻提示錯誤AttributeError: module 'test003' has no attribute 'desc'

在自己寫一個小專案的時候,發現明明自己使用import引入自己專案中的另一個Python檔案是正確的寫法,在執行的時候卻發現總是提示錯誤。因為筆者本人也是學習Python不久,經過一個小時的琢磨,感覺應該是兩個檔案迴圈引用導致錯誤。比如一個a.py在使用的時候需要引用b.py

tensorflow 出錯module 'tensorflow' has no attribute 'layers'

出錯:module 'tensorflow' has no attribute 'layers' 解決方法:由於已經安裝的tensorflow是0.x的版本,0.x版本沒有layers模組所以程式出錯,需要重新安裝tensorflow 1.0以上的版本,即更新tensorf