Ubuntu 上安装 TensorFlow(python2.7 版)
我的系统环境:
Ubuntu 16.04 LTS
python 2.7
python 3.5
TensorFlow 的两个版本:
TensorFlow 的安装形式主要有以下几种:
virtualenv
pip
docker
anaconda
源代码编译
我这里使用 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 环境:
[zero@zero-virtual-machine:~]$ virtualenv --system-site-packages tensorflow
Running virtualenv with interpreter /usr/bin/python2
New python executable in /home/zero/tensorflow/bin/python2
Also creating executable in /home/zero/tensorflow/bin/python
Installing setuptools, pkg_resources, pip, wheel...done.
3. 激活 Virtualenv 环境:
[zero@zero-virtual-machine:~]$ source ~/tensorflow/bin/activate
(tensorflow) [zero@zero-virtual-machine:~]$
4. 安装 pip:
(tensorflow) [zero@zero-virtual-machine ~]$ easy_install -U pip
5. 安装 tensorflow, 我这里安装的是 cpu 版本的:
(tensorflow) [zero@zero-virtual-machine ~]$ pip install --upgrade tensorflow
提示: 如果你的 pip 安装的很慢的话, 是因为默认使用的是国外的源, 我们可以更换成国内的源:
[zero@zero-virtual-machine ~]$ vim .pip/pip.conf # 编辑为以下内容
[global]
index-url = http://pypi.douban.com/simple
trusted-host = pypi.douban.com #没有这句会包 warning
disable-pip-version-check = true #版本不检查
timeout = 120 #超时时间设置
注: 如果. pip/pip.conf 没有则创建即可.
6. 安装完之后进入 python 命令行, 导入 tensorflow 包, 如果没有任何输出则代表安装成功:
(tensorflow) [zero@zero-virtual-machine ~]$ python
Python 2.7.12 (default, Dec 4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
>>>
其他的一些操作:
deactivate 命令可退出虚拟环境:
(tensorflow) [zero@zero-virtual-machine ~]$ deactivate
[zero@zero-virtual-machine ~]$
删除虚拟环境只需要把生成的目录删除即可:
[zero@zero-virtual-machine ~]$ rm -rf tensorflow/
[zero@zero-virtual-machine ~]$
以上我们演示了通过 virtualenv 来安装 tensorflow, 接下来再演示一下通过本地 pip 来安装 tensorflow:
1. 首先需要安装 pip 及 dev:
[zero@zero-virtual-machine ~]$ sudo apt-get install python-pip python-dev
2. 然后使用 pip 进行安装即可:
[zero@zero-virtual-machine ~]$ pip install tensorflow
3. 安装完之后也是进入 python 命令行, 导入 tensorflow 包, 如果没有任何输出则代表安装成功:
[zero@zero-virtual-machine ~]$ python
Python 2.7.12 (default, Dec 4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
>>>
这种方式是安装在本地的系统上, 而之前那种安装方式则是安装在一个虚拟的环境中, 每次都需要进入虚拟环境中才可以使用 tensorflow, 安装在本地系统则不需要.
卸载 tensorflow 使用以下命令:
sudo pip uninstall tensorflow
我们可以使用 pip 来安装一些 Python 的基本类库:
pip install numpy
pip install pandas
pip install matplotlib
编写第一个 TensorFlow 程序: Hello World
以上我们已经安装好了 TensorFlow, 那么我们就来编写第一个 TensorFlow 程序: Hello World
1. 创建好相应的目录:
[zero@zero-virtual-machine ~]$ mkdir TensorFlow
[zero@zero-virtual-machine ~]$ cd !$
cd TensorFlow
[zero@zero-virtual-machine ~/TensorFlow]$ mkdir HelloWorld
[zero@zero-virtual-machine ~/TensorFlow]$ cd !$
cd HelloWorld
[zero@zero-virtual-machine ~/TensorFlow/HelloWorld]$
2. 编辑一个 python 文件:
[zero@zero-virtual-machine ~/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. 运行这个文件, 看看是否正常输出:
[zero@zero-virtual-machine ~/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 FMA
Hello Wolrd!
[zero@zero-virtual-machine ~/TensorFlow/HelloWorld]$
如上, 可以看到 Hello Wolrd! 被正常输出了, 以及打印了一些提示信息, 到此为止我们的第一个 TensorFlow 程序就编写完成了.
来源: http://www.bubuko.com/infodetail-2482776.html