如何在windows10环境下安装Pytorch-0.4.1版本

开始是按照教程:https://blog.csdn.net/xiangxianghehe/article/details/80103095
安装了Pytorch0.4.0,但是安装后发现在import torch 出问题了!!!

原因已更新:我舍弃了上面教程。真正原因是要用pip3 install...,我直接用Pytorch官方安装教程成功了


E:\cudnn-8.0-windows10-x64-v7>pip install http://download.pytorch.org/whl/cu80/torch-0.4.0-cp36-cp36m-win_amd64.whl
Requirement already satisfied: torch==0.4.0 from http://download.pytorch.org/whl/cu80/torch-0.4.0-cp36-cp36m-win_amd64.whl in e:\python36\lib\site-packages (0.4.0)

E:\cudnn-8.0-windows10-x64-v7>import torch
'import' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

E:\cudnn-8.0-windows10-x64-v7>python
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "E:\Python36\lib\site-packages\torch\__init__.py", line 78, in <module>
    from torch._C import *
ImportError: DLL load failed: 找不到指定的模块。
>>> ^Z


E:\cudnn-8.0-windows10-x64-v7>pip install torchvision
Collecting torchvision
  Downloading https://files.pythonhosted.org/packages/ca/0d/f00b2885711e08bd71242ebe7b96561e6f6d01fdb4b9dcf4d37e2e13c5e1/torchvision-0.2.1-py2.py3-none-any.whl (54kB)
    100% |████████████████████████████████| 61kB 86kB/s
Requirement already satisfied: torch in e:\python36\lib\site-packages (from torchvision) (0.4.0)
Requirement already satisfied: six in e:\python36\lib\site-packages (from torchvision) (1.11.0)
Collecting pillow>=4.1.1 (from torchvision)
  Downloading https://files.pythonhosted.org/packages/1b/50/869910cd7110157fbefd0fed3db3656c1951f1bceecdd00e3716aa269609/Pillow-5.2.0-cp36-cp36m-win_amd64.whl (1.6MB)
    100% |████████████████████████████████| 1.6MB 301kB/s
Requirement already satisfied: numpy in e:\python36\lib\site-packages (from torchvision) (1.14.5)
Installing collected packages: pillow, torchvision
Successfully installed pillow-5.2.0 torchvision-0.2.1

E:\cudnn-8.0-windows10-x64-v7>

用anaconda发现也一样:

(E:\Miniconda3) C:\Users\Administrator>python
Python 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "E:\Miniconda3\lib\site-packages\torch\__init__.py", line 78, in <module>
    from torch._C import *
ImportError: DLL load failed: 找不到指定的模块。

=============================== 更新 =======================================

别尝试一,二,三,四了,我发现就不应该用pip install,而是应该用pip3 install,这才是我问题根源。我后来是直接按照Pytorch官网安装:

按照官网第二步第一步:输入命令pip3 install http://download.pytorch.org/whl/cu80/torch-0.4.1-cp36-cp36m-win_amd64.whl

然后会发现先卸载之前安装过的不成功的Torch0.4.0版本,取而代之的是安装 torch-0.4.1,安装 torch-0.4.1成功

接下来按照官网第二步就是:pip3 install torchvision

测试Pytorch安装是否成功

C:\Users\Administrator>python
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy  #先看看numpy的版本信息
>>> print(numpy.__version__)
1.14.5
>>> import torch  
>>> print(torch.__version__)
0.4.1         #输出0.4.1,说明Windows下的PyTorch0.4.1安装成功!

输出0.4.1,说明Windows下的PyTorch0.4.1安装成功!

测试下例子:https://www.jianshu.com/p/5ae644748f21
我们来看看自己电脑是否支持cuda

>>> torch.cuda.is_available()   ##看看是否支持cuda
True   

假如返回的是True那么,下面的代码x = x.cuda()y = y.cuda() 将带你飞。

>>> x = torch.Tensor(2,3)
>>> x
tensor([[0.0000, 0.0000, 0.0000],
        [0.0000, 0.0000, 0.0000]])
>>> y=torch.Tensor(4,2,3)
>>> y
tensor([[[-0.0000,  0.0000,  0.0000],
         [ 0.0000,  0.0000,  0.0000]],

        [[ 0.0000,  0.0000, -0.0000],
         [ 0.0000,  0.0000,  0.0000]],

        [[ 0.0000,  0.0000,  0.0000],
         [ 0.0000, -0.0000,  0.0000]],

        [[ 0.0000,  0.0000,  0.0000],
         [ 0.0000,  0.0000,  0.0000]]])

>>> x = x.cuda()     #发现我的电脑GPU太老了, GPU0 表示没有GPU。Pytorch已经不再支持GT 750M了
E:\Python36\lib\site-packages\torch\cuda\__init__.py:116: UserWarning:
    Found GPU0 GeForce GT 750M which is of cuda capability 3.0.
    PyTorch no longer supports this GPU because it is too old.

  warnings.warn(old_gpu_warn % (d, name, major, capability[1]))


#我们继续运算可以看下
>>> x = x.cuda()
>>> x
tensor([[0.0000, 0.0000, 0.0000],
        [0.0000, 0.0000, 0.0000]], device='cuda:0')

>>> y = y.cuda()
>>> y
tensor([[[-0.0000,  0.0000,  0.0000],
         [ 0.0000,  0.0000,  0.0000]],

        [[ 0.0000,  0.0000, -0.0000],
         [ 0.0000,  0.0000,  0.0000]],

        [[ 0.0000,  0.0000,  0.0000],
         [ 0.0000, -0.0000,  0.0000]],

        [[ 0.0000,  0.0000,  0.0000],
         [ 0.0000,  0.0000,  0.0000]]], device='cuda:0')

注意:上面的教程都是在win+R的cmd窗口实现的。要想在Miniconda(或Anaconda)的虚拟环境中安装Pytorch,同样也要进行上述两个步骤:
Step1: 输入命令pip3 install http://download.pytorch.org/whl/cu80/torch-0.4.1-cp36-cp36m-win_amd64.whl
Step2: 输入命令pip3 install torchvision

但我发现Miniconda3出现了如下错误:

Could not install packages due to an EnvironmentError: [WinError 5] 拒绝访问。: 'e:\\miniconda3\\Lib\\site-packages\\numpy\\.libs\\libopenblas.CSRRD7HKRKC3T3YXA7VY7TAZGLSWDKW6.gfortran-win_amd64.dll'
Consider using the `--user` option or check the permissions.

具体如图所示:

根据错误原因修改为:pip3 install --user torchvision

刚开始我是用pip install --user torchvision

我觉得最好还是用这个命令好一点(虽然我已经装好了torchvision)pip3 install --user torchvision


下面是之前尝试的思路,后面没试了。。。

尝试一:(发现并没有什么卵用)

首先按照:https://github.com/pytorch/pytorch/issues/574

发现并没有卵用

尝试二:(我猜的可能是没下载CUDA8.0?该方法未尝试)

更新: 我应该是下载了啊。

之前的想法: 找到E:\Miniconda3\lib\site-packages\torch__init__.py

找到E:\Miniconda3\lib\site-packages\torch_init_.py,打开源码看看

if platform.system() == 'Windows':
    # first get nvToolsExt PATH
    def get_nvToolsExt_path():
        NVTOOLEXT_HOME = _dl_flags.getenv('NVTOOLSEXT_PATH', 'C:\\Program Files\\NVIDIA Corporation\\NvToolsExt')

        if _dl_flags.path.exists(NVTOOLEXT_HOME):
            return NVTOOLEXT_HOME + '\\bin\\x64\\'
        else:
            return ''

查看我的路径发现并没有NvToolsExt

猜想应该是没有安装CUDA8.0?

尝试三: (要外网,不然贼慢)

发现有人说是跟numpy版本有关,要卸载掉当前numpy版本,安装其他版本?
[1] https://blog.csdn.net/sparkexpert/article/details/77675581
[2] https://stackoverflow.com/questions/49395819/import-torch-giving-error-from-torch-c-import-dll-load-failed-the-specif/51142648
去这里下载numpy对应版本如numpy‑1.14.5+mkl‑cp36‑cp36m‑win_amd64.whl(应该下1.15.0版本吗?)https://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy

尝试四:(这个应该可以,但是要FQ连接)

直接用conda安装解决问题

其中注意要FQ,否则HTTP连接超时,以及注意numpy版本为1.15.0,默认CUDA版本是9.0?

posted on 2018-08-07 17:58  星辰之衍  阅读(20326)  评论(1编辑  收藏  举报

导航