红联Linux门户
Linux帮助

ubuntu14.04Lts安装Tensorflow

发布时间:2016-05-30 09:44:57来源:linux网站作者:zhouchao_fight

1、关于ubuntu系统的安装在此就不再详述,可见ubuntu安装。


2、由于实验过程中使用到GPU,所以在安装完ubuntu14.04之后,安装相对应于GPU的显卡驱动,安装方法可见nvidia驱动安装。


3、首先我们得从github上克隆Tensorflow库:

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


4、接下来安装Bazel


5、安装一些依赖

$sudo apt-get install python-numpy swig python-dev python-wheel 


6、安装cuda

检测显卡的计算能力,Tensorflow要求显卡的计算能力大于3.5,查询显卡计算能力。

下载和安装cuda toolkit7.5,cuda下载。

安装依赖库:

$sudo apt-get install freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libgl1-mesa-glx libglu1-mesa libglu1-mesa-dev  

安装cuda
$ sudo dpkg -i cuda-repo-ubuntu1504-7-5-local_7.5-18_amd64 

$ sudo apt-get update 

$ sudo apt-get install -y cuda 

装好之后将cuda的路径bin和lib64写进环境变量。


7、安装cudnn v5

下载cudnn v5。cudnn下载

tar xvzf cudnn-7.5-linux-x64-v5.tgz 
sudo cp cudnn-7.5-linux-x64-v5/cudnn.h /usr/local/cuda/include 
sudo cp cudnn-7.5-linux-x64-v5/libcudnn* /usr/local/cuda/lib64 
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn* 


8、配置Tensorflow

进入Tensorflow目录

$ ./configure 
Please specify the location of python. [Default is /usr/bin/python]: 
Do you wish to build TensorFlow with GPU support? [y/N] y 
GPU support will be enabled for TensorFlow 

Please specify which gcc nvcc should use as the host compiler. [Default is 
/usr/bin/gcc]: /usr/bin/gcc-4.9 

Please specify the Cuda SDK version you want to use, e.g. 7.0. [Leave 
empty to use system default]: 7.5 

Please specify the location where CUDA 7.5 toolkit is installed. Refer to 
README.md for more details. [default is: /usr/local/cuda]: /usr/local/cuda 

Please specify the Cudnn version you want to use. [Leave empty to use system 
default]: 4.0.4 

Please specify the location where the cuDNN 4.0.4 library is installed. Refer to 
README.md for more details. [default is: /usr/local/cuda]: /usr/local/cudnn-r4-rc/ 

Please specify a list of comma-separated Cuda compute capabilities you want to 
build with. You can find the compute capability of your device at: 
https://developer.nvidia.com/cuda-gpus. 
Please note that each additional compute capability significantly increases your 
build time and binary size. [Default is: \"3.5,5.2\"]: 3.5 

Setting up Cuda include 
Setting up Cuda lib64 
Setting up Cuda bin 
Setting up Cuda nvvm 
Setting up CUPTI include 
Setting up CUPTI lib64 
Configuration finished 


9、构建GPU支持

$ bazel build -c opt --config=cuda //tensorflow/cc:tutorials_example_trainer 

$ bazel-bin/tensorflow/cc/tutorials_example_trainer --use_gpu 
# Lots of output. This tutorial iteratively calculates the major eigenvalue of 
# a 2x2 matrix, on GPU. The last few lines look like this. 
000009/000005 lambda = 2.000000 x = [0.894427 -0.447214] y = [1.788854 -0.894427] 
000006/000001 lambda = 2.000000 x = [0.894427 -0.447214] y = [1.788854 -0.894427] 
000009/000009 lambda = 2.000000 x = [0.894427 -0.447214] y = [1.788854 -0.894427] 


10、问题
在配置Tensorflow时,会安装boringssl,由于网络的限制,无法从google上下载软件,需要从github中下载。

解决方案:打开/tensorflow/tensorflow/wokspca.bzl,修改commit和remote

commit = "053931e",remote = "https://github.com/google/boringssl",


以上是安装Tensorflow过程,本过程是由Tensorflow官网所提供,其他解决方案都是由github上的大神所提供。


已经将Tensorflow装好了,但是还是没有办法将Tensorflow导入Python使用,这篇博客就是解决这个问题。


1、接着上面的工作继续安装,建立一个pip package

$ bazel build -c opt //tensorflow/tools/pip_package:build_pip_package 

# To build with GPU support: 
$ bazel build -c opt --config=cuda //tensorflow/tools/pip_package:build_pip_package 

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

# The name of the .whl file will depend on your platform. 
$ sudo pip install /tmp/tensorflow_pkg/tensorflow-0.8.0-py2-none-any.whl 

上述代码最后一行的.whl的文件名会不一样,可以在路径下查看


2、Tensorflow的扩展

bazel build -c opt //tensorflow/tools/pip_package:build_pip_package 

# To build with GPU support: 
bazel build -c opt --config=cuda //tensorflow/tools/pip_package:build_pip_package 

mkdir _python_build 
cd _python_build 
ln -s ../bazel-bin/tensorflow/tools/pip_package/build_pip_package.runfiles/org_tensorflow* . 
ln -s ../tensorflow/tools/pip_package/* . 
python setup.py develop 


3、训练第一个Tensorflow网络模型

$ cd tensorflow/models/image/mnist 
$ python convolutional.py 

在进行上述操作时,出现GPU连接问题

这主要是lib没有写进环境变量里,没有找到这样的连接

sudo gedit ~/.bashrc 

将安装cuda的路径写进去,然后重新构建就成。


以上安装Tensorflow的步骤都是根据Tensorflow官网的教程。


本文永久更新地址:http://www.linuxdiyf.com/linux/21073.html