实现
首先由两种版本可以选择,cpu和gpu版本。gpu会比cpu快很多,所以建议优先考虑使用gpu版本。
使用gpu版本是有条件的,那就是NVIDIA 版本的显卡。
前提是已经安装Python 和 pip
# CPU 版的
C:\> pip install tensorflow
# GPU 版的
C:\> pip install tensorflow-gpu
注意 Windows 运行 Tensorflow 如果遇到这个报错:
Error importing tensorflow. Unless you are using bazel,
you should not try to import tensorflow from its source directory;
please exit the tensorflow source tree, and relaunch your python interpreter
from there.
不要惊慌, 尝试下载安装 Windows 的 Microsoft Visual C++ 2015 redistributable update 3 64 bit. 就能解决这个问题.
参考链接:莫烦PYTHON
测试
运行这段代码,没问题说明成功了
import tensorflow
报错
如果gpu不是NVIDIA 的话会报一下错,解决方法就是装cpu版本的。如果你已经确定你的显卡就是NVIDIA,那么就是你的cuDNN 模块没装上,也就是你得去更新驱动
File "C:\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 18, in swig_import_helper
return importlib.import_module(mname)
File "C:\Anaconda3\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 666, in _load_unlocked
File "<frozen importlib._bootstrap>", line 577, in module_from_spec
File "<frozen importlib._bootstrap_external>", line 906, in create_module
File "<frozen importlib._bootstrap>", 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:\Anaconda3\lib\site-packages\tensorflow\python\__init__.py", line 66, in <module>
from tensorflow.python import pywrap_tensorflow
File "C:\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 21, in <module>
_pywrap_tensorflow = swig_import_helper()
File "C:\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 20, in swig_import_helper
return importlib.import_module('_pywrap_tensorflow')
File "C:\Anaconda3\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
ImportError: No module named '_pywrap_tensorflow'