安装 Tensorflow

环境: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
[email protected]:~$ pip install tensorflow
Collecting tensorflow
  Downloading tensorflow-1.1.0-cp36-cp36m-manylinux1_x86_64.whl (31.4MB)
    100% |?..?..?..?..?..?..?..?..?..?..?..?..?..?..?..?..| 31.4MB 48kB/s
Requirement already satisfied: six>=1.10.0 in ./anaconda3/lib/python3.6/site-packages (from tensorflow)
Collecting protobuf>=3.2.0 (from tensorflow)
  Downloading protobuf-3.3.0-cp36-cp36m-manylinux1_x86_64.whl (5.7MB)
    100% |?..?..?..?..?..?..?..?..?..?..?..?..?..?..?..?..| 5.7MB 242kB/s
Requirement already satisfied: wheel>=0.26 in ./anaconda3/lib/python3.6/site-packages (from tensorflow)
Requirement already satisfied: werkzeug>=0.11.10 in ./anaconda3/lib/python3.6/site-packages (from tensorflow)
Requirement already satisfied: numpy>=1.11.0 in ./anaconda3/lib/python3.6/site-packages (from tensorflow)
Requirement already satisfied: setuptools in ./anaconda3/lib/python3.6/site-packages/setuptools-27.2.0-py3.6.egg (from protobuf>=3.2.0->tensorflow)
Installing collected packages: protobuf, tensorflow
Successfully installed protobuf-3.3.0 tensorflow-1.1.0

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

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

[email protected]:~$ python
>>> import tensorflow as tf
>>> hello = tf.constant(‘Hello, TensorFlow!‘)
>>> sess = tf.Session()
>>> print(sess.run(hello))

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

2017-05-25 07:33:08.176641: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn‘t compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2017-05-25 07:33:08.176687: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn‘t compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-05-25 07:33:08.176701: W tensorflow/core/platform/cpu_feature_guard.cc:45] 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脚本

[email protected]:~/share/tensorflow$ cd tensorflow/
[email protected]:~/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.6/site-packages
Please input the desired Python library path to use.  Default is [/home/jingyg/anaconda3/lib/python3.6/site-packages]

Using python library path: /home/jingyg/anaconda3/lib/python3.6/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.1 --copt=-msse4.2 --copt=-mavx //tensorflow/tools/pip_package:build_pip_package

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

# 生成swap镜像文件
sudo dd if=/dev/zero of=/mnt/1024Mb.swap bs=1M count=1024
# 对该镜像文件格式化
sudo mkswap /mnt/1024Mb.swap
# 挂载该镜像文件
sudo swapon /mnt/1024Mb.swap

使用free -m 即可查看到swap空间已经增加成功

否则,在编译过程中,可能出现内存不足的问题:
ERROR: /home/jingyg/share/tensorflow/tensorflow/tensorflow/core/kernels/BUILD:2339:1: 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 134 argument(s) skipped): com.google.devtools.build.lib.shell.BadExitStatusException: Process exited with status 4.
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 的错误

[email protected]:~/share/tensorflow/tensorflow$ cd ..
[email protected]:~/share/tensorflow$ python
Python 3.6.0 |Anaconda 4.3.1 (64-bit)| (default, Dec 23 2016, 12:22:00)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] 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 版本低了

解决办法,安装最新的:

[email protected]:~/share/tensorflow$ conda install libgcc
时间: 2024-08-06 12:35:31

安装 Tensorflow的相关文章

在Win10 Anaconda中安装Tensorflow

有需要的朋友可以参考一下 1.安装Anaconda 下载:https://www.continuum.io/downloads,我用的是Python 3.5 下载完以后,安装. 安装完以后,打开Anaconda Prompt,输入清华的仓库镜像,更新包更快: conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ conda config --set show_channel_url

Ubuntu16.04安装tensorflow+安装opencv+安装openslide+安装搜狗输入法

Ubuntu16.04在cuda以及cudnn安装好之后,安装tensorflow,tensorflow以及opencv可以到网上下载对应的安装包并且直接在安装包所在的路径下直接通过pip与conda进行安装,如下图所示: 前提是要下载好安装包.安装好tensorflow之后还需要进行在~/.bashrc文件中添加系统路径,如下图所示 Openslide是医学图像一个重要的库,这里给出三条命令进行安装 sudo apt-get install openslide-tools sudo apt-g

TensorFlow入门:mac 安装 TensorFlow

开发环境: mac os 10.12.5 Python 2.7.10 GCC 4.2.1 mac默认是不带pip的,安装pip. sudo easy_install pip 1.安装virtualenv sudo pip install virtualenv --upgrade 创建一个工作目录: sudo virtualenv --system-site-packages ~/tensorflow 进行该目录,激活沙箱 cd ~/tensorflow source bin/activate 2

ubuntu 安装TensorFlow

1.安装pip $ sudo apt-get install python-pip python-dev 2.安装 TensorFlow for Python 2.7 # Ubuntu/Linux 64-bit, CPU only, Python 2.7: $ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0-cp27-none-linux_x86_64.

使用anaconda安装tensorflow

一开始想使用源码安装,卡死在了bazel上. 后来下载了Anaconda(我使用的Anaconda2-4.3.0-Linux-x86_64.sh) 下面是安装过程 0. 操作系统环境 OS:  Centos6.5 64位 CPU: 4核 MEM: 16G DISK: 100G 1. 直接运行sh脚本,进入控制台../Anaconda2-4.3.0-Linux-x86-64.sh, 2. 在控制台使用如下命令创建anaconda环境    conda create -n tensor python

ubuntu14.04 安装 tensorflow

如果内容侵权的话,联系我,我会立马删了的-因为参考的太多了,如果一一联系再等回复,战线太长了--蟹蟹给我贡献技术源泉的作者们- 最近准备从理论和实验两个方面学习深度学习,所以,前面装好了Theano环境,后来知乎上看到这个回答,就调研了一下各个深度学习框架,我没有看源码,调研也不是很深入,仅仅是为了选择深度学习框架做的一个大概了解- 1. 如何选择深度学习框架? 参考资料如下: 1. https://github.com/zer0n/deepframeworks/blob/master/READ

Centos7安装TensorFlow

TensorFlow也火了一段时间,想想既然要研究NLP,为什么不好好应用一下Google开源的Deep Learning平台呢,一切还是先从搭建环境开始. 很多大神们也做了这方面的工作,汲取他人经验,Centos7+Python3+TensorFlow [Note:官方文档(中文版)说目前TensorFlow的API需要Python2.7,对于Python3还在开发中,所以有Python2.7的可以不用再安装Python,我这里是因为其他需要安装的Python3] 1.安装Centos7 之前

安装tensorflow

安装tensorflow 背景 听说谷歌的第二代机器学习的框架tensorflow开源了,我也心血来潮去探探大牛的产品.怎奈安装就折腾了一天,现在整理出来备忘. tensorflow官方网站给出的安装步骤很简单: # Only CPU-version is available at the moment. $ pip install https://storage.googleapis.com/tensorflow/mac/tensorflow-0.5.0-py2-none-any.whl 安装

Jetson TX2安装TensorFlow注意事项

Jetson TX2安装TensorFlow注意事项 在nvidia jetson TX 2上安装TensorFlow时,在使用下面教程进行安装时,可能会出现多次卡死在编译阶段:./buildTensorFlow.sh: nvidia jetson tx2 安装TensorFlow:http://www.ncnynl.com/archives/201706/1754.html 解决方法:引发该错误的原因是内存不足,可以通过通过Ubuntu中的swap分区分配2G虚拟内存,来解决内存不足的问题:

01 Windows安装Tensorflow

1.安装Python. 点击此处下载Python3.5.2.安装Python时一定要选择安装pip. 2.配置Python环境变量. 将%安装路径%\Scripts添加到Path下面. 3.修改Pip国内源. 在C:\Users\l{你的用户名}\AppData\Local下面创建pip文件夹,在pip文件夹中新建pip.conf文件,写入以下内容. [global] index-url = http://mirrors.aliyun.com/pypi/simple/ [install] tru