环境:Ubuntu 16.04  64bit

1.    安装Anaconda

  Anaconda 是 Python 的一个科学计算发行版,内置了数百个Python经常会使用的库,也包括了许多机器学习和数据挖掘依赖的库,包括 Scikit-learn、NumPy、SciPy 和 Pandas等,其中可能有一些还是 Tensorflow 的依赖库。

  Anaconda 提供了一个编译好的环境可以直接安装。同时 Anaconda 自动集成了最新版的MKL(Math Kernel Libaray)库,这是Intel推出的底层数值计算库,功能上包含了 BLAS(Basic Linear Algebra Software)等矩阵计算库的功能,可以作为 NumPy、SciPy、Scikit-learn、NumExpr 等库的底层依赖,加速这些库的矩阵运算。

  简单来说,Anaconda 是目前最好的科学计算的 Python 环境,方便了安装,也提高了性能。

  安装步骤:

  1)  在官网上下载(www.continuum.io/downloads)相应版本

  2)  在下载目录下执行命令,例如: bash Anaconda3-4.3.1-Linux-x86_64.sh

  3)  回车确认,进入 Anaconda 的 License 文档,可以按 q 跳过,然后输入 yes 确认。下一步会让输入 Anaconda 的安装路径,可以按回车键使用默认路径

4)  安装完成,程序提示是否把 anaconda3 的 binary 路径加入到 .bashrc,建议添加。这样 python 命令就可以自动使用 Anaconda 的 Python 的环境了。

2.    安装Tensorflow

  由于是在 Python 的环境下使用 Tensorflow,可以使用 Python 的默认包管理器安装。如果 pip 的版本>9,可以直接运行:

pip install tensorflow
jingyg@jingyg:~$ pip install tensorflow
Collecting tensorflow
Downloading tensorflow-1.1.-cp36-cp36m-manylinux1_x86_64.whl (.4MB)
% |?..?..?..?..?..?..?..?..?..?..?..?..?..?..?..?..| .4MB 48kB/s
Requirement already satisfied: six>=1.10. in ./anaconda3/lib/python3./site-packages (from tensorflow)
Collecting protobuf>=3.2. (from tensorflow)
Downloading protobuf-3.3.-cp36-cp36m-manylinux1_x86_64.whl (.7MB)
% |?..?..?..?..?..?..?..?..?..?..?..?..?..?..?..?..| .7MB 242kB/s
Requirement already satisfied: wheel>=0.26 in ./anaconda3/lib/python3./site-packages (from tensorflow)
Requirement already satisfied: werkzeug>=0.11. in ./anaconda3/lib/python3./site-packages (from tensorflow)
Requirement already satisfied: numpy>=1.11. in ./anaconda3/lib/python3./site-packages (from tensorflow)
Requirement already satisfied: setuptools in ./anaconda3/lib/python3./site-packages/setuptools-27.2.-py3..egg (from protobuf>=3.2.->tensorflow)
Installing collected packages: protobuf, tensorflow
Successfully installed protobuf-3.3. tensorflow-1.1.

================================分割线========================================

但是直接安装时,由于一些编译参数的原因,在使用 Tensorflow 时,会出现如下的warnings:

jingyg@jingyg:~$ python
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))

在我的机器上当执行到 sess = tf.Session() 时,会提示:

-- ::08.176641: W tensorflow/core/platform/cpu_feature_guard.cc:] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
-- ::08.176687: W tensorflow/core/platform/cpu_feature_guard.cc:] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
-- ::08.176701: W tensorflow/core/platform/cpu_feature_guard.cc:] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.

意思是说:你的机器上有这些指令集可以用,并且用了他们会加快你的 CPU 运行速度,但是你的 TensorFlow 在编译的时候并没有用到这些指令集。

如果觉得不好的话,可以尝试用编译源码安装解决。

编译源码安装

1.    安装 Bazel

  查看:https://bazel.build/versions/master/docs/install-ubuntu.html#install-on-ubuntu

  Bazel 是 Google 自家的编译工具,使用它来编译 Tensorflow :

  1)    Add Bazel distribution URI as a package source (one time setup),执行命令

echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -

  If you want to use the JDK 7, please replace jdk1.8 with jdk1.7 and if you want to install the testing version of Bazel, replace stable with testing.

  2)    Install and update Bazel,执行命令

sudo apt-get update && sudo apt-get install bazel

  3) Once installed, you can upgrade to a newer version of Bazel with:

sudo apt-get upgrade bazel

2.    卸载已经安装的 Tensorflow:

pip uninstall tensorflow 

3.    克隆 Tensorflow 仓库:

git clone --recurse-submodules https://github.com/tensorflow/tensorflow 

4.    配置Tensorflow

运行configure脚本

jingyg@jingyg:~/share/tensorflow$ cd tensorflow/
jingyg@jingyg:~/share/tensorflow/tensorflow$ ./configure
Please specify the location of python. [Default is /home/jingyg/anaconda3/bin/python]:
Found possible Python library paths:
/home/jingyg/anaconda3/lib/python3./site-packages
Please input the desired Python library path to use. Default is [/home/jingyg/anaconda3/lib/python3./site-packages] Using python library path: /home/jingyg/anaconda3/lib/python3./site-packages
Do you wish to build TensorFlow with MKL support? [y/N]
No MKL support will be enabled for TensorFlow
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]:
Do you wish to use jemalloc as the malloc implementation? [Y/n]
jemalloc enabled
Do you wish to build TensorFlow with Google Cloud Platform support? [y/N]
No Google Cloud Platform support will be enabled for TensorFlow
Do you wish to build TensorFlow with Hadoop File System support? [y/N]
No Hadoop File System support will be enabled for TensorFlow
Do you wish to build TensorFlow with the XLA just-in-time compiler (experimental)? [y/N]
No XLA support will be enabled for TensorFlow
Do you wish to build TensorFlow with VERBS support? [y/N]
No VERBS support will be enabled for TensorFlow
Do you wish to build TensorFlow with OpenCL support? [y/N]
No OpenCL support will be enabled for TensorFlow
Do you wish to build TensorFlow with CUDA support? [y/N]
No CUDA support will be enabled for TensorFlow
Extracting Bazel installation...
.................
INFO: Starting clean (this may take a while). Consider using --async if the clean takes more than several minutes.
Configuration finished

可以根据自己的情况进行选择。

5.    编译(比较耗时):

bazel build -c opt --copt=-msse4. --copt=-msse4. --copt=-mavx //tensorflow/tools/pip_package:build_pip_package

Tips:最好先增加下 swap 的空间:

# 生成swap镜像文件
sudo dd if=/dev/zero of=/mnt/1024Mb.swap bs=1M count=
# 对该镜像文件格式化
sudo mkswap /mnt/1024Mb.swap
# 挂载该镜像文件
sudo swapon /mnt/1024Mb.swap 使用free -m 即可查看到swap空间已经增加成功 否则,在编译过程中,可能出现内存不足的问题:
ERROR: /home/jingyg/share/tensorflow/tensorflow/tensorflow/core/kernels/BUILD::: C++ compilation of rule '//tensorflow/core/kernels:cwise_op' failed: gcc failed: error executing command /usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -B/usr/bin -B/usr/bin -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -g0 -O2 '-D_FORTIFY_SOURCE=1' -DNDEBUG ... (remaining argument(s) skipped): com.google.devtools.build.lib.shell.BadExitStatusException: Process exited with status .
virtual memory exhausted: Cannot allocate memory

6.    上述命令会生成一个叫做 build_pip_package 的脚本,按照如下命令运行这个脚本,在 /tmp/tensorflow_pkg 文件夹中创建pip的安装包:

bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg 

7.    用pip命令安装:

pip install /tmp/tensorflow_pkg/tensorflow-1.2.0rc0-cp36-cp36m-linux_x86_64.whl 

Tensorflow测试:

  退出 Tensorflow 目录,如果不退出执行下面的命令,会出现 Failed to load the native TensorFlow runtime 的错误

jingyg@jingyg:~/share/tensorflow/tensorflow$ cd ..
jingyg@jingyg:~/share/tensorflow$ python
Python 3.6. |Anaconda 4.3. (-bit)| (default, Dec , ::)
[GCC 4.4. (Red Hat 4.4.-)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf

Tips:  ImportError: /home/jingyg/anaconda3/bin/../lib/libstdc++.so.6: version `GLIBCXX_3.4.21' not found

原因是 Anaconda 里的 libgcc 版本低了

解决办法,安装最新的:

jingyg@jingyg:~/share/tensorflow$ conda install libgcc

安装 Tensorflow的更多相关文章

  1. Ubuntu 14.04 64bit 安装tensorflow(GPU版本)

    本博客主要用于在Ubuntu14.04 64bit 操作系统上搭建google开源的深度学习框架tensorflow. 0.安装CUDA和cuDNN 如果要安装GPU版本的tensorflow,就必须 ...

  2. 学习tensorflow之mac上安装tensorflow

    背景 听说谷歌的第二代机器学习的框架tensorflow开源了,我也心血来潮去探探大牛的产品.怎奈安装就折腾了一天,现在整理出来备忘. tensorflow官方网站给出的安装步骤很简单: # Only ...

  3. mac安装tensorflow报错

    问题:mac安装tensorflow过程中,爆出oserror:permission denied 解决方案:关闭mac的sip,然后sudo安装 关闭sip的方法:重启mac,按住command+R ...

  4. 采用ubuntu系统来安装tensorflow

    最近在学习google新开源的深度学习框架tensorflow.发现安装它的时候,需要依赖python2.7.X;我之前一直使用的linux是centos.而centos不更新了,里面的自带的pyth ...

  5. 在 windows 上面安装 tensorflow

    这个是 tensorflow 官网地址, https://www.tensorflow.org/get_started/os_setup#pip_installation_on_windows 上面有 ...

  6. 安装TensorFlow的步骤

    安装步骤: 1.安装虚拟机: 2.安装liunx系统: 3.安装TensorFlow. 1.安装虚拟机:虚拟机的版本是不能太低的.我使用的是:VMware-workstation-full-12.0. ...

  7. ubuntu14.04 安装 tensorflow

    如果内容侵权的话,联系我,我会立马删了的-因为参考的太多了,如果一一联系再等回复,战线太长了--蟹蟹给我贡献技术源泉的作者们- 最近准备从理论和实验两个方面学习深度学习,所以,前面装好了Theano环 ...

  8. linux下安装TensorFlow(centos)

    一.python安装 centos自带python2.7.5,这一步可以省略掉. 二.python-pip pip--python index package,累世linux的yum,安装管理pyth ...

  9. windows 10 安装tensorflow

    人工智能一浪接一浪,随着谷歌公布tensorflow源码,尤其是支持windows 10平台的python3.5以上版本,更是让更多人都想用windows操作tensorflow. 第一次安装,也不知 ...

  10. Windows10下通过anaconda安装tensorflow

    博主经历了很多的坎坷磨难才找到一个比较好的在win10下安装TensorFlow的方法: 首先需要说明的是如果你想通过Anaconda来安装tensorflow的话,首先要确认你的python的版本是 ...

随机推荐

  1. Django自带的用户认证auth模块

    一.介绍 基本上在任何网站上,都无可避免的需要设计实现网站的用户系统.此时我们需要实现包括用户注册.用户登录.用户认证.注销.修改密码等功能. 使用Django,我们可以不需要自己写这些功能,因为Dj ...

  2. Linux 版本svn安装

    CentOS6.5离线安装subversion 下载 linux rpm安装包 (我只下载了subversion-1.6.12-1.rhel5.x86_64.rpm),直接安装时会提示缺少依赖,在 r ...

  3. FFT什么的

    目录 多项式 多项式加法 多项式乘法 多项式的表示 系数表达 点值表达 系数形式表示的多项式的快速乘法 DFT&FFT&IDFT 单位复数根 DFT FFT IDFT 多项式乘法 蝶形 ...

  4. flv.js怎么用?全面解读flv.js代码

    flv.js项目的代码有一定规模,如果要研究的话,我建议从demux入手,理解了demux就掌握了媒体数据处理的关键步骤,前面的媒体数据下载和后面的媒体数据播放就变得容易理解了. 先普及点背景知识,为 ...

  5. Vim和Neovim安装YouCompleteMe

    确定支持Python,Vim一般支持,Neovim默认不支持 Vim的就不介绍方法了,很多系统都不一样.Neovim添加Python支持 pip install setuptools pip inst ...

  6. 时间函数(1):time,ctime,gmtime,localtime

    asctime(将时间和日期以字符串格式表示) #include<time.h> 定义函数 char * asctime(const struct tm * timeptr); 函数说明 ...

  7. 一条sql执行过长的时间,你如何优化,从哪些方面?

    1.查看sql是否涉及多表的联表或者子查询,如果有,看是否能进行业务拆分,相关字段冗余或者合并成临时表(业务和算法的优化) 2.涉及链表的查询,是否能进行分表查询,单表查询之后的结果进行字段整合 3. ...

  8. Numpy系列(三)- 基本运算操作

    Numpy 中数组上的算术运算符使用元素级别.最后的结果使用新的一个数组来返回. import numpy as np a = np.array( [20,30,40,50] ) b = np.ara ...

  9. 第三节:SignalR之PersistentConnection模型详解(步骤、用法、分组、跨域、第三方调用)

    一. 承上声明 在上一个章节里,啰里啰嗦写了一堆关于介绍SignalR的“废话”,从这一篇开始往后正式撸代码,这期间不少人(包括 张善友大哥)建议我直接用.Net Core下的SignalR,关于此简 ...

  10. python中的GIL详解

    GIL是什么 首先需要明确的一点是GIL并不是Python的特性,它是在实现Python解析器(CPython)时所引入的一个概念.就好比C++是一套语言(语法)标准,但是可以用不同的编译器来编译成可 ...