http://blog.csdn.net/jiang_z_q/article/details/73264561
一、ubuntu 16.04 安装Tensorflow(CPU)
1、安装pip
打开终端输入命令:sudo apt-get install python-pip python-dev
2、安装tensorflow
sudopip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0rc0-cp27-none-linux_x86_64.whl
安装成功:
3、测试
import tensorflow as tf
a=tf.constant([1.0,2.0,3.0],shape=[3],name='a')
b=tf.constant([1.0,2.0,3.0],shape=[3],name='b')
c=a+b
sess=tf.Session(config=tf.ConfigProto(log_device_placement=True))
print sess.run(c)
二、ubuntu 16.04 安装Tensorflow(GPU)
1、安装显卡GPU驱动
打开终端:sudo apt-get update
选择系统设置→软件更新→附加驱动→选择nvidia最新驱动→应用更改.
验证安装成功:nvidia-settings
2、安装Tensorflow依赖的编译工具bazel
bazel安装方法网址:https://bazel.build/versions/master/docs/install-ubuntu.html
(1) 安装bazel前,需先安装JDK8
sudo apt-get installsoftware-properties-common
sudoadd-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get installoracle-java8-installer
验证java版本:java -version
(2) 安装bazel
echo "deb [arch=amd64]http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee/etc/apt/sources.list.d/bazel.list
sudo apt install curl
curlhttps://bazel.build/bazel-release.pub.gpg | sudo apt-key add -
sudo apt-get update
sudo apt-get upgrade bazel
3、安装cuda 8.0
下载地址:https://developer.nvidia.com/cuda-downloads
sudo dpkg -icuda-repo-ubuntu1604-8-0-local-ga2_8.0.61-1_amd64.deb
sudo apt-get update
sudo apt-get install cuda
查看gcc版本信息:gcc –v
由于cuda8.0不支持gcc 5.0以上的编译器,因此需要降级,把编译器版本降到4.9:
sudoapt-get install g++-4.9
sudoupdate-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 20
sudoupdate-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 10
sudoupdate-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.9 20
sudoupdate-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-5 10
sudoupdate-alternatives --install /usr/bin/cc cc /usr/bin/gcc 30
sudoupdate-alternatives --set cc /usr/bin/gcc
sudoupdate-alternatives --install /usr/bin/c++ c++ /usr/bin/g++ 30
sudoupdate-alternatives --set c++ /usr/bin/g++
4、安装cuDNN 6.0
下载地址: https://developer.nvidia.com/cudnn
cp cudnn-8.0-linux-x64-v6.0.solitairetheme8 cudnn-8.0-linux-x64-v6.0.tgz
tar -xvf cudnn-8.0-linux-x64-v6.0.tgz
sudo cp cuda/include/cudnn.h/usr/local/cuda/include
sudo cp cuda/lib64/libcudnn*/usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*
5、配置环境变量
sudo gedit ~/.bashrc
export LD_LIBRARY_PATH=”$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64”
export CUDA_HOME=/usr/local/cuda
exportPATH="$CUDA_HOME/bin:$PATH"
source ~/.bashrc
6、安装Tensflow
(1) 安装Tensorflow依赖的其它工具包
sudo apt-get install python-numpy swigpython-dev python-wheel
(2) 下载最新的Tensorflow源码
sudo apt-get install git
git clone https://github.com/tensorflow/tensorflow
(3) 运行configure脚本配置环境信息
(4) 通过bazel来编译pip的安装包,然后通过pip安装
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
sudo pip install/tmp/tensorflow_pkg/tensorflow-1.2.0rc2-cp27-cp27mu-linux_x86_64.whl
第一个命令中 --config=cuda参数为对GPU的支持,如何不需要支持GPU,就不需要这个参数。
安装成功:
(5) 代码测试
在配置好GPU环境的Tensorflow中,如果操作没有明确地指定运行设备,Tenserflow会优先选择GPU。
import tensorflow as tf
a=tf.constant([1.0,2.0,3.0],shape=[3],name='a')
b=tf.constant([1.0,2.0,3.0],shape=[3],name='b')
c=a+b
sess=tf.Session(config=tf.ConfigProto(log_device_placement=True))
print sess.run(c)
下面给一个通过tf.device手工指定运行设备的例子:
import tensorflow as tf
with tf.device('/cpu:0'):
a=tf.constant([1.0,2.0,3.0],shape=[3],name='a')
b=tf.constant([1.0,2.0,3.0],shape=[3],name='b')
with tf.device('/gpu:0'):
c=a+b
sess=tf.Session(config=tf.ConfigProto(log_device_placement=True))
print sess.run(c)