分享

Ubuntu 16.04 安装 Tensorflow(GPU支持)

 雪柳花明 2017-03-14

原文链接:

http://blog./archives/tag/tensorflow

本文记录Ubuntu 16.04安装Tensorflow步骤,也包括怎么从源码编译安装Tensorflow。

要想安装Tensorflow GPU版本,你需要有一个新一点的Nvidia显卡。

Tensorflow CPU版本的安装

[python] view plain copy
在CODE上查看代码片派生到我的代码片
  1. $ sudo apt-get install python-pip python-dev python-virtualenv    # python 2.7  
  2. $ sudo apt-get install python3-pip python3-dev python3-virtualenv # python 3.4+  


使用虚拟环境(可选):Python虚拟环境(pyvenv、virtualenv)

[python] view plain copy
在CODE上查看代码片派生到我的代码片
  1. $ virtualenv --system-site-packages ~/tensorflow  
  2. $ source ~/tensorflow/bin/activate  
[python] view plain copy
在CODE上查看代码片派生到我的代码片
  1. # Ubuntu/Linux 64-bit, CPU only, Python 2.7  
  2. $ export TF_BINARY_URL=https://storage./tensorflow/linux/cpu/tensorflow-0.11.0rc1-cp27-none-linux_x86_64.whl  
  3.    
  4. # Ubuntu/Linux 64-bit, CPU only, Python 3.4  
  5. $ export TF_BINARY_URL=https://storage./tensorflow/linux/cpu/tensorflow-0.11.0rc1-cp34-cp34m-linux_x86_64.whl  
  6.    
  7. # Ubuntu/Linux 64-bit, CPU only, Python 3.5  
  8. $ export TF_BINARY_URL=https://storage./tensorflow/linux/cpu/tensorflow-0.11.0rc1-cp35-cp35m-linux_x86_64.whl  

安装Tensorflow:

[python] view plain copy
在CODE上查看代码片派生到我的代码片
  1. # Python 2  
  2. $ sudo pip install --upgrade $TF_BINARY_URL  
  3.    
  4. # Python 3  
  5. $ sudo pip3 install --upgrade $TF_BINARY_URL  


如果要升级Tensorflow,替换新版本的TF_BINARY_URL。https://www.

Ubuntu 16.04 安装Tensorflow(GPU支持)

编译安装Tensorflow(GPU支持)

安装NVidia显卡驱动,你可以在Ubuntu内置的附加驱动中安装。

[python] view plain copy
在CODE上查看代码片派生到我的代码片
  1. $ sudo add-apt-repository ppa:graphics-drivers/ppa  
  2. $ sudo apt update  

安装CUDA:

[python] view plain copy
在CODE上查看代码片派生到我的代码片
  1. #下载安装:  
  2. #https://developer./cuda-toolkit  
  3. $ sudo sh cuda_8.0.44_linux.run --override   # 安装位置: /usr/local/cuda  
  4.    
  5. # 默认仓库中的版本较旧  
  6. #$ sudo apt install nvidia-cuda-toolkit nvidia-cuda-dev   # 安装位置: /usr  

安装CudNN V5:https://developer./cudnn

[python] view plain copy
在CODE上查看代码片派生到我的代码片
  1. <a target="_blank" href="https://developer./cudnn" style="border-width:0px 0px 1px; border-bottom-style:solid; border-color:rgb(46,34,86); font-family:inherit; font-style:inherit; font-weight:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(46,34,86); text-decoration:none"># 下载CudNN 5.1 for Cuda 8.0  
  2. $ sudo tar -xzvf cudnn-8.0-linux-x64-v5.1.tgz  
  3. $ sudo cp cuda/include/cudnn.h /usr/local/cuda/include  
  4. $ sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64  
  5. $ sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*</a>  

在~/.bashrc文件中添加环境变量:

[python] view plain copy
在CODE上查看代码片派生到我的代码片
  1. export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64"  
  2. export CUDA_HOME=/usr/local/cuda  

使环境变量生效:

[python] view plain copy
在CODE上查看代码片派生到我的代码片
  1. $ source ~/.bashrc  

下载tensorflow源码:

[python] view plain copy
在CODE上查看代码片派生到我的代码片
  1. $ cd ~  
  2. $ git clone https://github.com/tensorflow/tensorflow  


安装一些编译和依赖工具:

[python] view plain copy
在CODE上查看代码片派生到我的代码片
  1. $ sudo apt-get install default-jdk python-dev python3-dev python-numpy python3-numpy build-essential python-pip python3-pip python-virtualenv swig python-wheel libcurl3-dev  

安装Bazel:

[python] view plain copy
在CODE上查看代码片派生到我的代码片
  1. $ echo "deb [arch=amd64] http://storage./bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list  
  2. $ curl https://storage./bazel-apt/doc/apt-key.pub.gpg | sudo apt-key add -  
  3. $ sudo apt-get update  
  4. $ sudo apt-get install bazel  
  5. $ sudo apt-get upgrade bazel  

配置编译选项:

[python] view plain copy
在CODE上查看代码片派生到我的代码片
  1. $ cd ~/tensorflow  
  2. $ ./configure  

需要输入Python路径,默认是 /usr/bin/python。如果你使用Python3,输入:/usr/bin/python3.5。

输入Python模块路径,默认是/usr/local/lib/python2.7/dist-packages。如果你使用Python3,输入:/usr/local/lib/python3.5/dist-packages。

输入Cuda SDK版本和Cudnn版本:8.0、5.1.5。

配置完成,输入如下信息:

[python] view plain copy
在CODE上查看代码片派生到我的代码片
  1. INFO: All external dependencies fetched successfully.  
  2. Configuration finished  

编译tensorflow:

[python] view plain copy
在CODE上查看代码片派生到我的代码片
  1. $ bazel build -c opt --config=cuda  # GPU支持  
  2. # CPU支持  
  3. #$ bazel build -c opt  

构建pip包:

[python] view plain copy
在CODE上查看代码片派生到我的代码片
  1. $ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg  

安装pip包:

[python] view plain copy
在CODE上查看代码片派生到我的代码片
  1. $ sudo pip install /tmp/tensorflow_pkg/tensorflow   # python2  
  2. $ sudo pip3 install /tmp/tensorflow_pkg/tensorflow  # python3  

参考:

  • https://www./versions/r0.11/get_started/os_setup.html#download-and-setup
  • tensorflow源代码:https://github.com/tensorflow/tensorflow

Share the post "Ubuntu 16.04 安装 Tensorflow(GPU支持)"

相关文章

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多