在对python有了一定的了解之后,就开始来学习鼎鼎有名的Tensorflow吧,这里将主要介绍如何在Windows系统下使用anaconda安装Tensorflow,如何在spyder和jupyter中使用Tensorflow。
电脑配置:普通CPU版本+Win7系统+64位+anaconda3+python3
1. 使用anaconda安装Tensorflow
Tensorflow的安装方法貌似有很多种,由于电脑中前期就一直安装的是anaconda,这里就直接在anaconda中来安装它了,感觉这样操作也是比较省事的。首先打开cmd命令行,进入conda环境,接着输入命令行
conda create -n tensorflow python=3.6
系统会显示需要下载安装的包,确认安装后,它就开始自动安装了。接着激活这个conda环境,输入命令行:
activate tensorflow
并安装cpu版本的tensorflow
pip install --ignore-installed --upgrade
测试是否安装成功,在tensorflow环境中进入python,输入以下代码:
>>>import tensorflow as tf
>>>hello = tf.constant("hello, tensorflow!")
>>>sess = tf.Session()
>>>print(sess.run(hello))
如果安装成功了,会显示如下结果:
b'hello,tensorflow'
使用tensorflow后,要记得关闭环境:
deactivate tensorflow
后记:在后来的使用中,按照上述方式却一直无法成功import tensorflow,ImportError: DLL load failed。具体报错信息为:
(tensorflow) C:\Users\Administrator>python
Python 3.6.3 |Anaconda, Inc.| (default, Nov 8 2017, 15:10:56) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
Traceback (most recent call last):
File "D:\ProgramFiles\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
from tensorflow.python.pywrap_tensorflow_internal import *
File "D:\ProgramFiles\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
_pywrap_tensorflow_internal = swig_import_helper()
File "D:\ProgramFiles\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 24, in swig_import_helper
_mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
File "D:\ProgramFiles\Anaconda\envs\tensorflow\lib\imp.py", line 243, in load_module
return load_dynamic(name, filename, file)
File "D:\ProgramFiles\Anaconda\envs\tensorflow\lib\imp.py", line 343, in load_dynamic
return _load(spec)
ImportError: DLL load failed: 找不到指定的模块。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\ProgramFiles\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\__init__.py", line 22, in <module>
from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-import
File "D:\ProgramFiles\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\__init__.py", line 49, in <module>
from tensorflow.python import pywrap_tensorflow
File "D:\ProgramFiles\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 74, in <module>
raise ImportError(msg)
ImportError: Traceback (most recent call last):
File "D:\ProgramFiles\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
from tensorflow.python.pywrap_tensorflow_internal import *
File "D:\ProgramFiles\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
_pywrap_tensorflow_internal = swig_import_helper()
File "D:\ProgramFiles\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 24, in swig_import_helper
_mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
File "D:\ProgramFiles\Anaconda\envs\tensorflow\lib\imp.py", line 243, in load_module
return load_dynamic(name, filename, file)
File "D:\ProgramFiles\Anaconda\envs\tensorflow\lib\imp.py", line 343, in load_dynamic
return _load(spec)
ImportError: DLL load failed: 找不到指定的模块。
Failed to load the native TensorFlow runtime.
See https://www.tensorflow.org/install/install_sources#common_installation_problems
for some common reasons and solutions. Include the entire stack trace
above this error message when asking for help.
针对该问题的解决方法,可以参考这里的方法。
2. 在spyder中启动Tensorflow
我们在安装好tensorflow后,打开anaconda nagavitor可以发现,除了原本的root环境外,已经有了一个新的tensorflow环境。
在新的tensorflow环境下,我们再次安装spyder和jupyter notebook。原来的anaconda目录下又多了几个带有tensorflow的图标。
此时打开带有tensorflow的spyder图标,就可以在spyder中使用tensorflow了~
3. 在jupyter notebook 中启动Tensorflow
jupyter notebook中启动tensorflow要比上面的spyder麻烦一些。虽然我们已经安装了带有tensorflow的jupyter,但是当我们打开jupyter,输入
import tensorflow as tf
发现系统还是会报错
其实还是要接着在cmd中的tensorflow下输入以下:
ipython kernelspec install-self --user
如果运行正常的话,会给出这样的结果:
Installed kernelspec python3
此时再去jupyter中新建文件,输入以下测试代码:
import tensorflow as tf
hello = tf.constant("hello, tensorflow!")
sess = tf.Session()
print(sess.run(hello))
会得到正确结果;
b'hello, tensorflow!'
这样就成功在jupyter中使用tensorflow了~