环境: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 tensorflowDownloading 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:

jingyg@jingyg:~$ 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脚本

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.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 的错误

jingyg@jingyg:~/share/tensorflow/tensorflow$ cd ..
jingyg@jingyg:~/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 版本低了

解决办法,安装最新的:

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

Fr:https://www.cnblogs.com/jingyg/p/6931902.html

安装 Tensorflow相关推荐

  1. 查看已安装tensorflow版本

    查看已安装tensorflow版本 2017年08月07日 20:40:23 imperfect00 阅读数 187297更多 个人分类: tensorflow学习笔记 版权声明:本文为博主原创文章, ...

  2. Mac下pycharm如何安装tensorflow

    通过虚拟环境virtualenv安装tensorflow 首先提及,下面的命令,我都是直接在macos上的pycharm中的terminal执行的 1.安装virtualenv虚拟环境: pip in ...

  3. 树莓派安装python3.5+tensorflow_树莓派4B安装Tensorflow的方法步骤

    1.写作背景 Tensorflow官方在2018年宣布,正式发布支持树莓派版本的Tensorflow,编者开始直接用: pip install tensorflow 进行安装,在安装成功后使用impo ...

  4. 吐血推荐:win下如何安装tensorflow?只需两步!!

    你可能已经搜过了,如何安装TensorFlow吧? 我的经历,按照网上的N多教程安装,全部失败. 下面这个方法特别简单. 先下个pycharm community, 下好之后再去官网安装个python ...

  5. centos安装tensorFlow的java环境

    参考问题汇总 centos安装tensorFlow版本的时候会遇到的一些问题,参考这个链接:https://blog.csdn.net/luoyexuge/article/details/783212 ...

  6. Windows 10安装Tensorflow

    Windows 10安装Tensorflow手记 解决Python3.7不能安装tensorflow<1.13的问题 验证安装: import tensorflow as tf hello = ...

  7. pycharm安装tensorflow

    file-setting -project interpreter-右上角点击+ 搜索tensorflow 右下角指定版本1.14.0 安装成功后会有提示 总结 今天在pycharm上安装tensor ...

  8. anconda安装后命令行中安装tensorflow报错

    现象  anconda安装后命令行中安装tensorflow报错 pip install --upgrade --ignore-installed tensorflow-gpu Building wh ...

  9. JetsonTX2上安装tensorflow的心酸史

    JetsonTX2上安装tensorflow的心酸史 还是那句话,做事情得有耐心,有耐心-耐心-.心--感觉像是给自己的一个心理暗示- -.-||| tensorflow安装 常见问题总结 验证 te ...

  10. 在Win10 Anaconda中安装Tensorflow

    离完成上一篇文章有近1年了.2016年发生了太多的事情,从而没能坚持哪怕是每月一篇这样的频率.终于在2017年的1月份抽出几天搞出了一些东西.一路坑洼,赶紧记录下来. 2016年初就开始看深度学习的东 ...

最新文章

  1. QT+VS打包发布流程该怎么做?
  2. 如何用计算机猜数字,杭电2010计算机复试笔试题 2道acm简单题(2010):1.猜数字游戏;2.字符串提取数字并求和;...
  3. ItChat与图灵机器人的结合
  4. 参数处理:#{}与${}取值的区别
  5. AngularJS高级程序设计学习笔记(一)
  6. node.js require 自动执行脚本 并生成html,利用node.js实现自动生成前端项目组件的方法详解...
  7. react 组件连动效果_React组件开发中常见的陷阱
  8. 2440启动文件分析
  9. P4390 [BOI2007]Mokia 摩基亚 (CDQ解决三维偏序问题)
  10. BZOJ2741 【FOTILE模拟赛】L 【可持久化trie + 分块】
  11. Flutter 萌新高频问题(加班猿妈妈叫你回家吃饭了)
  12. js基础之函数递传参数与作用域(5下)
  13. runtime无法执行grep_Caffe和py-faster-rcnn日常使用备忘录
  14. 使用ef查询有缓存的问题
  15. 《Python编程快速上手——让繁琐工作自动化》——2.8 导入模块
  16. 科研生活:避免碌碌无为的感觉
  17. php layim 图片正则替换,用正则表达式批量替换图片路径方法
  18. 笔记本连接显示器后没有声音_笔记本外接显示器后没声音怎么回事
  19. cf卡,mmc卡,sd卡,sm卡,xd卡,记忆棒的区别是什么?
  20. npm install --save 、--save-dev 、-D、-S的区别详细解说

热门文章

  1. 前17名最喜欢的操作系统黑客(2017年名单)
  2. vue为基座微前端嵌入html,使用Vue CSR的微前端实现方案实践
  3. Android编译tcpdump,android 使用tcpdump
  4. 为什么要设定PHP数据类型,php数据类型详解
  5. 小汤学编程之JAVA经典例题——嵌套集合
  6. 一键获取数据库整体信息脚本
  7. PHP--去除字符串首尾空格
  8. Harbor的搭建(vmware企业级docker镜像私服)
  9. 安卓入门程序《发短信》
  10. bootstrap模态框垂直居中显示