前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Ubuntu16.04使用Anaconda5搭建TensorFlow使用环境 图文详细教程

Ubuntu16.04使用Anaconda5搭建TensorFlow使用环境 图文详细教程

原创
作者头像
Steve Wang
修改2018-01-10 17:56:20
1.5K0
修改2018-01-10 17:56:20
举报
文章被收录于专栏:从流域到海域从流域到海域

本文转载于:http://blog.csdn.net/solo95/article/details/78960389,即专栏作者本人的博客,保留所有版权,禁止转载,腾讯云+专栏对markdown的支持不是很好,可以到原博客查看,请见谅。

说明

<font face='黑体'>

  • Ubuntu版本16.04 LTS
  • Anaconda版本 5.0.1 (对应Python 3.6.3)
  • Tensorflow 1.3.0(由Anaconda提供,你也可以选择其他版本,包括GPU的)

<font face='宋体'>

Anaconda指的是一个开源的Python发行版本,其包含了conda、Python等180多个科学包及其依赖项,也提供了tensorflow的安装。

GPU版本和CPU版本各有优劣,CPU版本IO处理高效,GPU版本矩阵运算高效,处理在线数据建议使用CPU版,处理下载好的数据建议使用GPU版。

安装GPU版需要先安装CUDA和cuDNN,其他相同。

<font face='楷体'>

本博客撰写于2018年1月2日,前面已经更新了一系列Python博文,之后还会继续更新,与此同时开启TensorFlow新篇章,祝大家新年快乐。

你在其他博客上看到的安装教程可能要比本篇繁琐的多,由于使用了Anaconda,并在虚拟环境中安装,要简便很多。

(欢迎点击浏览器的星星按钮收藏本博客,也欢迎关注博主微博@从流域到海域,私信必回。)

安装Anaconda5.0.1

Anaconda 5.1官网下载页面
Anaconda 5.1官网下载页面

下载地址:

https://www.anaconda.com/download/#linux

依照系统位数选择你需要的版本,本博客选择的是:64-Bit (x86) Installer (525 MB),然后安装。

代码语言:txt
复制
sudo bash Anaconda3-5.0.1-Linux-x86_64.sh
这里写图片描述
这里写图片描述

ctrl + c跳到license agreement最底,输入yes回车。

安装过程遇到问题需要重新安装,先运行下面的命令删掉之前的文件夹。

代码语言:txt
复制
sudo rm -rf anaconda3

安装过程需要注意一点:

代码语言:txt
复制
[/home/steve/anaconda3] >>> 
PREFIX=/home/steve/anaconda3
installing: python-3.6.3-hc9025b9_1 ...
......(省略一系列安装)
Do you wish the installer to prepend the Anaconda3 install location
to PATH in your /home/steve/.bashrc ? [yes|no]
[no] >>> yes   
#一定要在此处选择yes 选择yes之后直接进入下个步骤。
#如果它自动选no跳过了 按照提示在.bashrc里添加提示给出的export的语句 其他教程如下:
vi ~/.bashrc
然后按o,把终端给出的Export语句粘贴进去
然后按i,输入:wq 保存退出

完成安装后,重启terminal,输入:

代码语言:txt
复制
source ~/.bashrc   #不重启电脑的情况下激活设置

再输入python。看到原来的python2.7被替换成python3.6.3 | Anaconda,证明安装成功。

代码语言:txt
复制
steve@steve-Lenovo-V2000:~$ python
Python 3.6.3 |Anaconda, Inc.| (default, Oct 13 2017, 12:02:49) 
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

安装TensorFlow

创建一个tensorflow 虚拟环境:

代码语言:txt
复制
conda create -n tensorflow python=3.6

激活tensorflow虚拟环境(之后的使用每次也都要先激活虚拟环境才可用):

代码语言:txt
复制
source activate tensorflow
代码语言:txt
复制
anaconda search -t conda tensorflow #查找当前可用的tensorflow包 下面是结果

steve@steve-Lenovo-V2000:~$ source activate tensorflow
(tensorflow) steve@steve-Lenovo-V2000:~$ anaconda search -t conda tensorflow
Using Anaconda API: https://api.anaconda.org
Packages:
     Name                      |  Version | Package Types   | Platforms       | Builds    
     ------------------------- |   ------ | --------------- | --------------- | ----------
     GlaxoSmithKline/tensorflow |   0.12.0 | conda           | linux-64        | py27hb0d0e74_0
                                          : TensorFlow is a machine learning library
     HCC/tensorflow            |    1.4.0 | conda           | linux-64        | py27_1, py34_1, py34_0, py36_0, py27_0, py35_0, py35_1
                                          : Computation using data flow graphs for scalable machine learning.
     HCC/tensorflow-cpucompat  |    1.4.0 | conda           | linux-64        | py36_0, py27_0, py35_0, py34_0
                                          : Computation using data flow graphs for scalable machine learning.
     HCC/tensorflow-fma        |    1.4.0 | conda           | linux-64        | py27_1, py34_1, py27_0, py36_0, py34_0, py35_0, py35_1
                                          : Computation using data flow graphs for scalable machine learning.
     SentientPrime/tensorflow  |    0.6.0 | conda           | osx-64          | py27_0    
                                          : TensorFlow helps the tensors flow
     SmartAg/tensorflow_gpu    |    1.0.1 | conda           | linux-aarch64   | 0         
     aaronzs/tensorflow        |    1.4.0 | conda           | linux-64, win-64, osx-64 | py36h39705f4_0, py36h8a03e48_0, py35hc784f49_0, py36h6db853c_0, py35h2d7a08b_0, py35h1150644_0, py35h5a8cc8b_0, py35hc0f5839_0, py36hebc11a6_0, py35ha700c16_0, py36hf8f6b73_0, py36heb185b1_0, py35hf9a0815_0, py36h2003710_0, py36h4df9c7b_0, py35h6467dd0_0, py36hd42d972_0, py36he4e0f4f_0, py35h89e3332_0
                                          : TensorFlow helps the tensors flow
     aaronzs/tensorflow-gpu    |    1.4.0 | conda           | linux-64, win-64 | py35h95763ad_0, py36h03e8729_0, py35h8ac8084_0, py35hb2e3085_0, py35hc6fb95a_0, py36ha20c466_0, py35h3b8745f_0, py36hbec5d8f_0, py36h74c31d8_0, py36h6bf4e57_0, py36h7b11560_0, py35h14e71af_0, py36h559dc3e_0
                                          : TensorFlow helps the tensors flow
     aaronzs/tensorflow-tensorboard | 0.4.0rc3 | conda           | linux-64, osx-64, win-64 | py35h30a7cae_0, py36h1eb756b_0, py35h8792995_0, py35h98b1d99_0, py36hbb25e9c_0, py35h0e1fd4a_0, py36h7c6d2df_0, py35h6181586_0, py36h1ee23b2_0, py36hffc986b_0, py35h85b20a5_0, py35h93bdf65_0, py36h4568b58_0, py36h5698cb7_0, py35h985ceb1_0, py35h83d8c28_0, py36hf2576c0_0, py36h52f5384_0, py36h9271151_0, py36ha443a3c_0, py35hbab8bba_0, py35h14ff132_0, py36h9a29024_0, py35h9958e77_0, py36h662c838_0, py36hd60226d_0
                                          : TensorBoard lets you watch Tensors Flow
     acellera/tensorflow-cuda  |   0.12.1 | conda           | linux-64        | 1         
     anaconda/tensorflow       |    1.3.0 | conda           | linux-ppc64le, linux-64, osx-64, win-64 | np111py27_0, 0, np111py34_0, py36_0, np112py36_0, py27_0, np112py35_0, np111py36_0, py35_0, np112py27_0, np111py35_0
                                          : TensorFlow is a machine learning library.
     anaconda/tensorflow-base  |    1.3.0 | conda           | linux-64        | py27_0, py36h5293eaa_1, py36_0, py35h79a3156_1, py35_0, py27h0dbb4d0_1
                                          : TensorFlow is a machine learning library, base package contains only tensorflow.
     anaconda/tensorflow-gpu   |    1.3.0 | conda           | linux-ppc64le, linux-64, win-64 | py36_4, np111py27_0, py35cuda8.0cudnn6.0_0, py27_4, py35cuda7.5cudnn6.0_0, 0, py35cuda8.0cudnn5.1_0, py36cuda7.5cudnn5.1_0, py27cuda7.5cudnn5.1_0, py27cuda7.5cudnn6.0_0, np112py35_0, np112py27_0, py27cuda8.0cudnn5.1_0, np111py35_0, py27cuda8.0cudnn6.0_0, py36cuda7.5cudnn6.0_0, np112py36_0, np111py36_0, py36cuda8.0cudnn5.1_0, py36cuda8.0cudnn6.0_0, py35_4, py35cuda7.5cudnn5.1_0
                                          : TensorFlow is a machine learning library.
     anaconda/tensorflow-gpu-base |    1.3.0 | conda           | linux-64        | py27cuda8.0cudnn6.0_1, py27cuda8.0cudnn6.0_0, py35cuda8.0cudnn6.0_0, py36cuda8.0cudnn6.0_0, py36cuda8.0cudnn6.0_1, py35cuda8.0cudnn6.0_1
                                          : TensorFlow is a machine learning library, base GPU package, tensorflow only.
     anaconda/tensorflow-tensorboard |    0.1.5 | conda           | linux-64        | py36_0, py35_0, py27_0
                                          : TensorBoard lets you watch Tensors Flow
     aroth85/tensorflow        |    1.3.0 | conda           | linux-64        | py27_0    
                                          : TensorFlow helps the tensors flow
     conda-forge/r-tensorflow  |      0.7 | conda           | linux-64, osx-64, win-64 | r3.3.2_0, r3.4.1_0
     conda-forge/tensorflow    |    1.4.0 | conda           | linux-64, win-64, osx-64 | py36_2, py27_1, py34_1, py34_0, py36_0, py27_0, py27_2, py35_2, py35_0, py35_1
                                          : TensorFlow helps the tensors flow
     creditx/tensorflow        |    0.9.0 | conda           | linux-64        | py35_0, py27_0
                                          : TensorFlow helps the tensors flow
     derickl/tensorflow        |    1.0.1 | conda           | osx-64          | py27h5185c07_0
                                          : TensorFlow helps the tensors flow
     dhirschfeld/tensorflow    |    1.2.0 | conda           | win-64          | py36_0, py35_0
                                          : Computation using data flow graphs for scalable machine learning 
     dseuss/tensorflow         |          | conda           | osx-64          | py35_0    
     guyanhua/tensorflow       |    1.0.0 | conda           | linux-64        | py27_0    
     ijstokes/tensorflow       | 2017.03.03.1349 | conda, ipynb    | linux-64        | py35_0    
     intel/tensorflow          |    1.4.0 | conda, pypi     | linux-64        | np113py36_1, np113py27_1
     jjh_cio_testing/tensorflow |    1.3.0 | conda           | linux-64        | np111py27_0, np111py35_0, 0, py27_0, py36_0, np112py36_0, np113py35_0, np112py35_0, np111py36_0, np113py27_0, np112py27_0, np113py36_0, py35_0
                                          : TensorFlow is a machine learning library
     jjh_cio_testing/tensorflow-base |    1.3.0 | conda           | linux-64        | py27_0, py36h5293eaa_1, py36_0, py35h79a3156_1, py35_0, py27h0dbb4d0_1
                                          : TensorFlow is a machine learning library, base package contains only tensorflow.
     jjh_cio_testing/tensorflow-gpu |    1.3.0 | conda           | linux-64        | py35cuda7.5cudnn5.1_0, py36_0, py36_3, py36_2, py36_4, py36cuda7.5cudnn5.1_0, py35cuda8.0cudnn6.0_0, py27cuda8.0cudnn5.1_0, py27_4, py27_3, py27_2, py27_1, py27_0, py35cuda7.5cudnn6.0_0, np113py35_0, 0, py35cuda8.0cudnn5.1_0, py36cuda8.0cudnn5.1_0, np111py27_0, py27cuda7.5cudnn5.1_0, py27cuda7.5cudnn6.0_0, np112py35_0, np112py27_0, np113py36_0, np111py35_0, py27cuda8.0cudnn6.0_0, py36cuda7.5cudnn6.0_0, np112py36_0, np111py36_0, np113py27_0, py36cuda8.0cudnn6.0_0, py35_4, py35_2, py35_3, py35_0
                                          : TensorFlow is a machine learning library.
     jjh_cio_testing/tensorflow-gpu-base |    1.3.0 | conda           | linux-64        | py27cuda8.0cudnn6.0_1, py27cuda8.0cudnn6.0_0, py35cuda8.0cudnn6.0_0, py36cuda8.0cudnn6.0_0, py36cuda8.0cudnn6.0_1, py35cuda8.0cudnn6.0_1
                                          : TensorFlow is a machine learning library, base GPU package, tensorflow only.
     jjh_cio_testing/tensorflow-tensorboard |    0.1.5 | conda           | linux-64        | py36_0, py35_0, py27_0
                                          : TensorBoard lets you watch Tensors Flow
     jjh_ppc64le/tensorflow    |    1.2.1 | conda           | linux-ppc64le   | py27_0, py36_0, np112py36_0, np112py35_0, np112py27_0, py35_0
                                          : TensorFlow is a machine learning library
     jjh_ppc64le/tensorflow-gpu |    1.2.1 | conda           | linux-ppc64le   | py27cuda8.0cudnn6.0_0, np112py36_0, np112py35_0, py35cuda8.0cudnn6.0_0, py36cuda8.0cudnn6.0_0, np112py27_0
                                          : TensorFlow is a machine learning library
     jjhelmus/tensorflow       | 0.12.0rc0 | conda, pypi     | linux-64, osx-64 | py27_1, py34_1, py27_0, py34_0, py27_2, py35_0, py35_1
                                          : TensorFlow helps the tensors flow
     jjhelmus/tensorflow-gpu   |    1.0.1 | conda           | linux-64        | np112py35_5, np112py36_5, py27_2, np112py27_5
                                          : TensorFlow is a machine learning library.
     jjhelmus/tensorflow-gpu-base |    1.3.0 | conda           | linux-64        | py27cuda8.0cudnn6.0_1, py35cuda8.0cudnn6.0_1
                                          : TensorFlow is a machine learning library, base GPU package, tensorflow only.
     kevin-keraudren/tensorflow |    0.9.0 | conda           | linux-64        | py35_12   
     loopbio/tensorflow        |    1.3.0 | conda           | linux-64        | cuda8_cudnn6_mkl_xla_1
                                          : TensorFlow is a machine learning library
     marta-sd/tensorflow       |    1.2.0 | conda           | linux-64        | py27_2, py35hbaace4d_3, py27he497762_3, py36_2, py36hb9c984a_3, py35_2
     marta-sd/tensorflow-gpu   |    1.2.0 | conda           | linux-64        | py27_1, py36h1323ef4_2, py36_1, py27_0, py35hddb9974_2, py27h4f63904_2, py35_0, py35_1
     memex/tensorflow          |    0.5.0 | conda           | linux-64, osx-64 | py27_2    
                                          : TensorFlow helps the tensors flow
     mhworth/tensorflow        |    0.7.1 | conda           | osx-64          | py27_1    
                                          : TensorFlow helps the tensors flow
     miovision/tensorflow      | 0.10.0.gpu | conda           | linux-64, osx-64 | py35_1    
     msarahan/tensorflow       | 1.0.0rc2 | conda           | linux-64        | np111py36_0, np111py27_0, np111py35_0, np111py34_0
     mutirri/tensorflow        | 0.10.0rc0 | conda           | linux-64        | np111py27_0, np111py35_0, np111py34_0
     mwojcikowski/tensorflow   |    1.0.1 | conda           | linux-64        | py36_0, py35_0, py35_1
     nehaljwani/tensorflow     |    1.2.1 | conda           | osx-64, win-64  | np112py27_0, py36_0, np112py36_0, np112py35_0, py35_0
                                          : TensorFlow is a machine learning library
     nehaljwani/tensorflow-gpu |    1.1.0 | conda           | win-64          | np112py36_0, np112py35_0
                                          : TensorFlow is a machine learning library
     r/r-tensorflow            |      1.4 | conda           | linux-64, win-32, win-64, linux-32, osx-64 | py36r3.4.1_0, r342h38ebd79_0, r342h0e1eca8_0, r342hd3d5cfb_0, r342h0bf44f9_0, r3.4.1_0, r342h935e3b1_0
                                          : Interface to 'TensorFlow' <https://www.tensorflow.org/>,  an open source software library for numerical computation using data flow graphs. Nodes in the graph represent mathematical operations,  while the graph edges represent the multidimensional data arrays  (tensors) communicated between them. The flexible architecture allows you to deploy computation to one or more 'CPUs' or 'GPUs' in a desktop,  server, or mobile device with a single 'API'. 'TensorFlow' was originally developed by researchers and engineers working on the Google Brain Team  within Google's Machine Intelligence research organization for the  purposes of conducting machine learning and deep neural networks research, but the system is general enough to be applicable in a wide variety of other domains as well.
     rdonnelly/tensorflow      |    0.9.0 | conda           | linux-64        | py27_0, py35_0, py34_0
     sdvillal/tensorflow       |    1.3.0 | conda           | linux-64        | cuda8_cudnn6_mkl_xla_1, cuda8_cudnn6_mkl_xla_0, py27_1, py27_0
                                          : TensorFlow is a machine learning library
     test_org_002/tensorflow   | 0.10.0rc0 | conda           |                 | np111py27_0, np111py35_0, np111py34_0
     thomasantony/tensorflow_gpu |    1.0.1 | conda           | linux-aarch64   | 0         
Found 52 packages

Run 'anaconda show <USER/PACKAGE>' to get installation details
#你可以看到1.4.0的也有 博主求稳选了Anaconda的官方包 你也可安装1.4.0版本的
#conda-forge/tensorflow 这个是1.4.0版本的

查看一个包的详情信息

代码语言:txt
复制
(tensorflow) steve@steve-Lenovo-V2000:~$ anaconda show anaconda/tensorflow
Using Anaconda API: https://api.anaconda.org
Name:    tensorflow
Summary: TensorFlow is a machine learning library.
Access:  public
Package Types:  conda
Versions:
   + 0.10.0rc0
   + 1.0.1
   + 1.1.0
   + 1.2.1
   + 1.3.0

To install this package with conda run:
     conda install --channel https://conda.anaconda.org/anaconda tensorflow

安装tensorflow(直接copy结果给出的命令)

代码语言:bash
复制
 conda install --channel https://conda.anaconda.org/anaconda tensorflow
 #过程如下 有点慢
(tensorflow) steve@steve-Lenovo-V2000:~$ conda install --channel https://conda.anaconda.org/anaconda tensorflow
Fetching package metadata .............
Solving package specifications: .

Package plan for installation in environment /home/steve/.conda/envs/tensorflow:

The following NEW packages will be INSTALLED:

    backports:              1.0-py36hfa02d7e_1    anaconda
    backports.weakref:      1.0rc1-py36_0         anaconda
    bleach:                 1.5.0-py36_0          anaconda
    html5lib:               0.9999999-py36_0      anaconda
    intel-openmp:           2018.0.0-hc7b2577_8   anaconda
    libprotobuf:            3.4.1-h5b8497f_0      anaconda
    markdown:               2.6.9-py36_0          anaconda
    mkl:                    2018.0.1-h19d6760_4   anaconda
    numpy:                  1.13.3-py36ha12f23b_0 anaconda
    protobuf:               3.4.1-py36h306e679_0  anaconda
    six:                    1.11.0-py36h372c433_1 anaconda
    tensorflow:             1.3.0-0               anaconda
    tensorflow-base:        1.3.0-py36h5293eaa_1  anaconda
    tensorflow-tensorboard: 0.1.5-py36_0          anaconda
    werkzeug:               0.12.2-py36hc703753_0 anaconda

Proceed ([y]/n)? y
   
intel-openmp-2 100% |################################| Time: 0:00:07  81.54 kB/s
mkl-2018.0.1-h 100% |################################| Time: 0:34:04  94.72 kB/s
libprotobuf-3. 100% |################################| Time: 0:01:56  36.22 kB/s
libprotobuf-3. 100% |################################| Time: 0:03:10  22.22 kB/s
backports-1.0- 100% |################################| Time: 0:00:00   7.37 MB/s
markdown-2.6.9 100% |################################| Time: 0:00:04  23.35 kB/s
numpy-1.13.3-p 100% |################################| Time: 0:01:37  41.65 kB/s
six-1.11.0-py3 100% |################################| Time: 0:00:00  26.59 kB/s
werkzeug-0.12. 100% |################################| Time: 0:00:06  61.63 kB/s
backports.weak 100% |################################| Time: 0:00:00   6.29 MB/s
html5lib-0.999 100% |################################| Time: 0:00:03  47.86 kB/s
protobuf-3.4.1 100% |################################| Time: 0:00:12  47.52 kB/s
bleach-1.5.0-p 100% |################################| Time: 0:00:00  54.13 kB/s
tensorflow-bas 100% |################################| Time: 0:01:39 376.32 kB/s
tensorflow-ten 100% |################################| Time: 0:00:02 667.15 kB/s
tensorflow-1.3 100% |################################| Time: 0:00:00   6.24 MB/s

测试一下:

代码语言:txt
复制
(tensorflow) steve@steve-Lenovo-V2000:~$ python
Python 3.6.4 |Anaconda, Inc.| (default, Dec 21 2017, 21:42:08) 
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> session = tf.Session()
2018-01-03 13:39:26.170690: 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.
2018-01-03 13:39:26.170746: 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.
2018-01-03 13:39:26.170772: 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.
2018-01-03 13:39:26.170792: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2018-01-03 13:39:26.170813: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
>>> print(session.run(hello))
b'Hello, TensorFlow!'
>>> exit()
(tensorflow) steve@steve-Lenovo-V2000:~$ source deactivate #退出虚拟环境
steve@steve-Lenovo-V2000:~$ 

可以通过在终端中输入export TF_CPP_MIN_LOG_LEVEL=2解决 warnning,博主觉得这样只是改了记录方法而已,问题依然存在,警告提示的只是tensorflow没有编译成XX指令,但在你的机器上这些加速CPU运行的指令依然是可用的。因此可用忽略。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 说明
  • 安装Anaconda5.0.1
  • 安装TensorFlow
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档