pytorch,AttributeError:模块“torch"没有属性“Tensor" [英] pytorch, AttributeError: module 'torch' has no attribute 'Tensor'

查看:59
本文介绍了pytorch,AttributeError:模块“torch"没有属性“Tensor"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在装有 CentOS Linux 7.3.1611(核心)操作系统的计算机上使用 Python 3.5.1.

我正在尝试使用 PyTorch 并且我正在开始使用 本教程.

不幸的是,示例的 #4 行造成了麻烦:

<预><代码>>>>火炬.Tensor(5, 3)回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中AttributeError: 模块torch"没有属性Tensor"

我无法理解这个错误...当然在 Torch 中,'torch' 确实有一个属性 'Tensor'.相同的命令适用于 Torch.

我该如何解决这个问题?

解决方案

您正在运行的 Python 二进制文件没有安装 torch.它确实在模块搜索路径上有一个名为 torch 的目录,它被视为 命名空间包:

$密码/一些/路径$ python3 -c '导入火炬;打印(手电筒);打印(火炬.__path__)'回溯(最近一次调用最后一次):文件<string>",第 1 行,在 <module> 中ModuleNotFoundError: 没有名为torch"的模块$ mkdir 手电筒$ python3 -c '导入火炬;打印(手电筒);打印(火炬.__path__)'<模块'torch'(命名空间)>_NamespacePath(['/some/path/torch'])

任何不存在 __init__.py 文件的目录(位于您的模块搜索路径上)都将被视为命名空间,前提是 没有其他具有该名称的 Python 模块或包在搜索路径的其他任何地方找到.

这意味着如果为您的 Python 二进制文件安装了 torch,那么是否存在本地 torch 目录并不重要:

$ ls -ld torch/drwxr-xr-x 2 mjpieters 用户 68 Nov 23 13:57 torch/$ mkdir -p additional_path/torch/$ touch additional_path/torch/__init__.py$ PYTHONPATH="./additional_path" python3 -c 'import os.path as p, sys;print(*(t for t in (p.join(e, "torch") for e in sys.path) if p.exists(t)), sep="
")'火炬/some/path/additional_path/torch$ PYTHONPATH="./additional_path" python3 -c 'import torch;打印(手电筒);打印(火炬.__path__)'<来自'/some/path/additional_path/torch/__init__.py'的模块'torch'>['/some/path/additional_path/torch']

上面显示sys.path首先列出了torch目录,然后是additional_path/torch,但后者作为torch 模块,当您尝试导入它时.这是因为 Python 在加载命名空间包之前优先考虑顶级模块和包.

您需要为当前的 Python 二进制文件正确安装 Torch,请参阅项目主页;使用 pip 时,您可能希望使用带有 -m 开关的 Python 二进制文件:

python3.5 -m pip install http://download.pytorch.org/whl/cu80/torch-0.2.0.post3-cp35-cp35m-manylinux1_x86_64.whlpython3.5 -m pip 安装torchvision

因此将主页指令使用的pip3替换为python3.5 -m pippython3.5 也可以是 Python 二进制文件的完整路径.

请使用正确的 download.pytorch.org URL 获取最新版本.

您不必将目录移到一边,但如果您确实想要但不知道它的位置,请使用 print(torch.__path__) 如我上面所示.

再次注意,如果你确实在本地 torch 目录中有一个 __init__.py 文件,它就会变成一个普通的包,并且将把 pip 安装的包屏蔽到正常的 site-packages 位置.如果您有这样的包或本地 torch.py​​ 单文件模块,则需要重命名它们.在这种情况下,诊断信息看起来有所不同:

$密码/一些/路径$ python3 -c '导入火炬;打印(手电筒);打印(火炬.__path__)'回溯(最近一次调用最后一次):文件<string>",第 1 行,在 <module> 中ModuleNotFoundError: 没有名为torch"的模块$ mkdir 手电筒$ touch torch/__init__.py # 使其成为一个包$ python3 -c '导入火炬;打印(手电筒);打印(火炬.__path__)'<来自'/some/path/torch/__init__.py'的模块'torch'>['/一些/路径/火炬']$ rm -rf 手电筒/$ touch torch.py​​ # 让它成为一个模块$ python3 -c '导入火炬;打印(手电筒);打印(火炬.__文件__)'<来自'/some/path/torch.py​​'的模块'torch'>/some/path/torch.py

注意区别;上面的命名空间包使用 ,而常规包使用 ),而普通模块使用代码>`.

首先找到此类包和模块(不是命名空间包)并停止搜索.如果找到的包或模块不是您想要的,您需要将它们移到一边或重命名.

I'm working with Python 3.5.1 on a computer having CentOS Linux 7.3.1611 (Core) operating system.

I'm trying to use PyTorch and I'm getting started with this tutorial.

Unfortunately, the #4 line of the example creates troubles:

>>> torch.Tensor(5, 3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'torch' has no attribute 'Tensor'

I cannot understand this error... of course in Torch the 'torch' does have an attribute 'Tensor'. The same command works in Torch.

How can I solve this problem?

解决方案

The Python binary that you are running does not have torch installed. It does have a directory named torch on the module search path, and it is treated as a namespace package:

$ pwd
/some/path
$ python3 -c 'import torch; print(torch); print(torch.__path__)'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'torch'
$ mkdir torch
$ python3 -c 'import torch; print(torch); print(torch.__path__)'
<module 'torch' (namespace)>
_NamespacePath(['/some/path/torch'])

Any directory without a __init__.py file present in it, located on your module search path, will be treated as a namespace, provided no other Python modules or packages by that name are found anywhere else along the search path.

This means that if torch was installed for your Python binary, it doesn't matter if there is a local torch directory:

$ ls -ld torch/
drwxr-xr-x  2 mjpieters users  68 Nov 23 13:57 torch/
$ mkdir -p additional_path/torch/
$ touch additional_path/torch/__init__.py
$ PYTHONPATH="./additional_path" python3 -c 'import os.path as p, sys; print(*(t for t in (p.join(e, "torch") for e in sys.path) if p.exists(t)), sep="
")'
torch
/some/path/additional_path/torch
$ PYTHONPATH="./additional_path" python3 -c 'import torch; print(torch); print(torch.__path__)'
<module 'torch' from '/some/path/additional_path/torch/__init__.py'>
['/some/path/additional_path/torch']

The above shows that sys.path lists the torch directory first, followed by additional_path/torch, but the latter is loaded as the torch module when you try to import it. That's because Python gives priority to top-level modules and packages before loading a namespace package.

You need to install torch correctly for your current Python binary, see the project homepage; when using pip you may want to use the Python binary with the -m switch instead:

python3.5 -m pip install http://download.pytorch.org/whl/cu80/torch-0.2.0.post3-cp35-cp35m-manylinux1_x86_64.whl 
python3.5 -m pip install torchvision

So replace the pip3 the homepage instructions use with python3.5 -m pip; python3.5 can also be the full path to your Python binary.

Do use the correct download.pytorch.org URL for the latest version.

You don't have to move the directory aside, but if you do want to and don't know where it is located, use print(torch.__path__) as I've shown above.

Again, note that if you do have an __init__.py file in a local torch directory, it becomes a regular package and it'll mask packages installed by pip into the normal site-packages location. If you have such a package, or a local torch.py single-file module, you need to rename those. The diagnostic information looks different in that case:

$ pwd
/some/path
$ python3 -c 'import torch; print(torch); print(torch.__path__)'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'torch'
$ mkdir torch
$ touch torch/__init__.py  # make it a package
$ python3 -c 'import torch; print(torch); print(torch.__path__)'
<module 'torch' from '/some/path/torch/__init__.py'>
['/some/path/torch']
$ rm -rf torch/
$ touch torch.py           # make it a module
$ python3 -c 'import torch; print(torch); print(torch.__file__)'
<module 'torch' from '/some/path/torch.py'>
/some/path/torch.py

Note the differences; a namespace package, above, uses <module 'name' (namespace)>, while a regular package uses ), while a plain module uses`.

Such packages and modules (not namespace packages) are found first and stop the search. If the found package or module is not the one you wanted, you need to move them aside or rename them.

这篇关于pytorch,AttributeError:模块“torch"没有属性“Tensor"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆