開始是按照教程: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
嘗試四:(這個應該可以,但是要翻牆連接)
直接用conda安裝解決問題
其中注意要翻牆,否則HTTP連接超時,以及注意numpy版本為1.15.0,默認CUDA版本是9.0?