Windows 10 下 TensorFlow,Anaconda的安装

由于项目需要,最近开始学习深度学习,计划用TensorFlow来做。刚开始接触遇到很多问题,写篇博文将方法记录下来,也希望给后来的人有所帮助。下面详细讲解Windows下TensorFlow的安装:
1.官网下载 Anaconda,TensorFlow当前只支持Python3.5版本,因此直接下载anaconda3-4.2.0对应版本。标准的Windows程序,一路安装即可,安装完在cmd命令行输入“Python”确认一下是不是3.5版本的。
2.接下来安装TensorFlow,cmd输入:

# GPU版本
pip3 install --upgrade tensorflow-gpu

# CPU版本
pip3 install --upgrade tensorflow 

根据笔者安装经验,在执行上面命令前最好先更新一下pip版本,命令如下:

python -m pip install --upgrade pip

安装过程视网速而定,如果中间出现错误:Cannot remove entries from nonexistent file c:\users\li\anaconda3\lib\site-packages\easy-install.pth,更新一下setuptools,代码如下:

pip install --upgrade --ignore-installed setuptools

安装成功后,会有successfully的字样。至此,TensorFlow安装完成,下面安装CUDA。
  
3.安装CUDA时如果安装失败,一般是由于vistual stdio integration的原因,所以建议分两步安装:
  第一步,安装时选择“自定义”,把CUDA中的vistual stdio integration这一项取消安装,安装完成即可。
  第二步,显然,只选择刚才那一项,其他的取消,安装完即可。命令行输入以下代码,如果看到NVDIA版本信息,说明安装成功

nvcc -v
  

4.安装cuDNN 5.0 ,下载安装包,解压到任意目录即可,解压完是一个名为“cuda”的文件夹。笔者解压到了c盘根目录,接下来添加环境变量“C:\cuda”,按说这一步基本就算完了,在python中导入TensorFlow测试一下:

import tensorflow as tf

如果没有报错,则成功。如果出现一下错误:

Traceback (most recent call last):
File "C:\Users\test\AppData\Local\Programs\Python\Python35\lib\site-packages\t
ensorflow\python\pywrap_tensorflow.py", line 18, in swig_import_helper
return importlib.import_module(mname)
File "C:\Users\test\AppData\Local\Programs\Python\Python35\lib\importlib_ini
t_.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 986, in _gcd_import
File "", line 969, in _find_and_load
File "", line 958, in _find_and_load_unlocked
File "", line 666, in _load_unlocked
File "", line 577, in module_from_spec
File "", line 906, in create_module
File "", line 222, in _call_with_frames_removed
ImportError: DLL load failed: The specified module could not be found.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\test\AppData\Local\Programs\Python\Python35\lib\site-packages\t
ensorflow\python_init_.py", line 66, in 
from tensorflow.python import pywrap_tensorflow
File "C:\Users\test\AppData\Local\Programs\Python\Python35\lib\site-packages\t
ensorflow\python\pywrap_tensorflow.py", line 21, in 
_pywrap_tensorflow = swig_import_helper()
File "C:\Users\test\AppData\Local\Programs\Python\Python35\lib\site-packages\t
ensorflow\python\pywrap_tensorflow.py", line 20, in swig_import_helper
return importlib.import_module('pywrap_tensorflow')
File "C:\Users\test\AppData\Local\Programs\Python\Python35\lib\importlib_ini
t.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
ImportError: No module named '_pywrap_tensorflow'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\test\Desktop\firstp.py", line 1, in 
import tensorflow as tf
File "C:\Users\test\AppData\Local\Programs\Python\Python35\lib\site-packages\t
ensorflow_init_.py", line 24, in 
from tensorflow.python import *
File "C:\Users\test\AppData\Local\Programs\Python\Python35\lib\site-packages\t
ensorflow\python_init_.py", line 72, in 
raise ImportError(msg)
ImportError: Traceback (most recent call last):
File "C:\Users\test\AppData\Local\Programs\Python\Python35\lib\site-packages\t
ensorflow\python\pywrap_tensorflow.py", line 18, in swig_import_helper
return importlib.import_module(mname)
File "C:\Users\test\AppData\Local\Programs\Python\Python35\lib\importlib_ini
t_.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 986, in _gcd_import
File "", line 969, in _find_and_load
File "", line 958, in _find_and_load_unlocked
File "", line 666, in _load_unlocked
File "", line 577, in module_from_spec
File "", line 906, in create_module
File "", line 222, in _call_with_frames_removed
ImportError: DLL load failed: The specified module could not be found.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\test\AppData\Local\Programs\Python\Python35\lib\site-packages\t
ensorflow\python_init_.py", line 66, in 
from tensorflow.python import pywrap_tensorflow
File "C:\Users\test\AppData\Local\Programs\Python\Python35\lib\site-packages\t
ensorflow\python\pywrap_tensorflow.py", line 21, in 
_pywrap_tensorflow = swig_import_helper()
File "C:\Users\test\AppData\Local\Programs\Python\Python35\lib\site-packages\t
ensorflow\python\pywrap_tensorflow.py", line 20, in swig_import_helper
return importlib.import_module('pywrap_tensorflow')
File "C:\Users\test\AppData\Local\Programs\Python\Python35\lib\importlib_ini
t.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
ImportError: No module named '_pywrap_tensorflow'

Failed to load the native TensorFlow runtime.

See https://github.com/tensorflow/tensorflow/blob/master/tensorflow/g3doc/get_st
arted/os_setup.md#import_error

for some common reasons and solutions. Include the entire stack trace
above this error message when asking for help.

在环境变量的path路径中再添加“C:\cuda\bin”(根据自己的解压目录来确定),重启cmd终端,再次在Python中import TensorFlow,应该就会成功了。

%%%
补充:
1.如果用上述方法安装TensorFlow网速慢的话,可以到TensorFlow官网的GitHub页面下载离线包,再进行安装。
  TensorFlow官方github主页:https://github.com/tensorflow/tensorflow
2.如果安装的是py3.5版本的,但出现“pip3不是内部或外部命令”时,可能是由于以前装过Python2,没有卸载干净(卸载失败时,可能需要下载对应的Python安装包先修复,再卸载),卸载完成后还需要到环境变量的path中把有关以前的Python2有关的删除,重启电脑,再输入pip3,则不会报错。
%%%

以上就是我安装TensorFlow过程中出现的所有问题及解决方法,特此记录下来。第一次写博客,排版做的很差,忘见谅!如果有什么不明白的,可以留言,我会尽量帮助您!

猜你喜欢

转载自blog.csdn.net/john_kai/article/details/71439139
今日推荐