Ubuntu上安装TensorFlow(python2.7版),,笔记内容:Ubunt


笔记内容:Ubuntu上安装TensorFlow(python2.7版)
笔记日期:2018-01-31


Ubuntu上安装TensorFlow(python2.7版)

我的系统环境:

Ubuntu 16.04 LTSpython 2.7python 3.5

TensorFlow的两个版本:
技术分享图片

TensorFlow的安装形式主要有以下几种:

virtualenvpipdockeranaconda源代码编译

我这里使用pip进行安装,pip是Python软件包管理系统:Pip Install Packages 递归缩写

pip安装软件包命令的格式如下:

pip install 软件包名

pip卸载软件包命令的格式如下:

pip uninstall 软件包名

我这里使用virtualenv形式的安装,以下是TensorFlow官方的安装文档:

https://tensorflow.google.cn/install/install_linux

1.安装python-pip、python-dev以及python-virtualenv等包,我这里安装的是python2.7版本的:

$ sudo apt-get install python-pip python-dev python-virtualenv

2.创建一个Virtualenv环境:

[[email protected]:~]$ virtualenv --system-site-packages tensorflowRunning virtualenv with interpreter /usr/bin/python2New python executable in /home/zero/tensorflow/bin/python2Also creating executable in /home/zero/tensorflow/bin/pythonInstalling setuptools, pkg_resources, pip, wheel...done.

3.激活Virtualenv环境:

[[email protected]:~]$ source ~/tensorflow/bin/activate(tensorflow) [[email protected]:~]$

4.安装pip:

(tensorflow) [[email protected] ~]$ easy_install -U pip

5.安装tensorflow,我这里安装的是cpu版本的:

(tensorflow) [[email protected] ~]$ pip install --upgrade tensorflow

提示:如果你的pip安装的很慢的话,是因为默认使用的是国外的源,我们可以更换成国内的源:

[[email protected] ~]$ vim .pip/pip.conf  # 编辑为以下内容[global]index-url = http://pypi.douban.com/simpletrusted-host = pypi.douban.com   #没有这句会包warningdisable-pip-version-check = true   #版本不检查timeout = 120   #超时时间设置 

注:如果.pip/pip.conf没有则创建即可。

6.安装完之后进入python命令行,导入tensorflow包,如果没有任何输出则代表安装成功:

(tensorflow) [[email protected] ~]$ pythonPython 2.7.12 (default, Dec  4 2017, 14:50:18) [GCC 5.4.0 20160609] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import tensorflow>>> 

其他的一些操作:
deactivate命令可退出虚拟环境:

(tensorflow) [[email protected] ~]$ deactivate[[email protected] ~]$ 

删除虚拟环境只需要把生成的目录删除即可:

[[email protected] ~]$ rm -rf tensorflow/[[email protected] ~]$

以上我们演示了通过virtualenv来安装tensorflow,接下来再演示一下通过本地pip来安装tensorflow:

1.首先需要安装pip及dev:

[[email protected] ~]$ sudo apt-get install python-pip python-dev

2.然后使用pip进行安装即可:

[[email protected] ~]$ pip install tensorflow

3.安装完之后也是进入python命令行,导入tensorflow包,如果没有任何输出则代表安装成功:

[[email protected] ~]$ pythonPython 2.7.12 (default, Dec  4 2017, 14:50:18) [GCC 5.4.0 20160609] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import tensorflow>>> 

这种方式是安装在本地的系统上,而之前那种安装方式则是安装在一个虚拟的环境中,每次都需要进入虚拟环境中才可以使用tensorflow,安装在本地系统则不需要。

卸载tensorflow使用以下命令:

sudo pip uninstall tensorflow

我们可以使用pip来安装一些Python的基本类库:

pip install numpypip install pandaspip install matplotlib

编写第一个TensorFlow程序:Hello World

以上我们已经安装好了TensorFlow,那么我们就来编写第一个TensorFlow程序:Hello World

1.创建好相应的目录:

[[email protected] ~]$ mkdir TensorFlow[[email protected] ~]$ cd !$cd TensorFlow[[email protected] ~/TensorFlow]$ mkdir HelloWorld[[email protected] ~/TensorFlow]$ cd !$cd HelloWorld[[email protected] ~/TensorFlow/HelloWorld]$ 

2.编辑一个python文件:

[[email protected] ~/TensorFlow/HelloWorld]$ vi helloworld.py  # 内容如下# -*- coding: UTF-8 -*-# 引入 Tensorflow 库import tensorflow as tf# 创建一个常量 Operation (操作)hw = tf.constant("Hello Wolrd!")# 启动一个Tensorflow 的 Session(会话)sess = tf.Session()# 运行 Graph (计算图)print sess.run(hw)# 关闭 Session(会话)sess.close()

3.运行这个文件,看看是否正常输出:

[[email protected] ~/TensorFlow/HelloWorld]$ python helloworld.py 2018-02-01 00:22:43.680173: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMAHello Wolrd![[email protected] ~/TensorFlow/HelloWorld]$ 

如上,可以看到 Hello Wolrd! 被正常输出了,以及打印了一些提示信息,到此为止我们的第一个TensorFlow程序就编写完成了。

Ubuntu上安装TensorFlow(python2.7版)

评论关闭