一. 使用docker安装
启动tensorflow容器:
docker run -it tensorflow/tensorflow:1.4.0
GPU支持docker镜像,需要安装最新版的NVIDIA驱动和NVIDIA-docker,用以下命令安装支持GPU的tensorflow镜像:
nvidia-docker run -it tensorflow/tensorflow:1.4.0-gpu
具体使用方法本人还需要多次探索,后期会持续跟新。
二. 使用pip安装
有可能是本人的cuda的系统路径问题,此方法并未成功。
1 安装pip
sudo apt install python-pip python-dev
2 找到合适的安装包URL
目前只有在安装了cuda和cudnn的Ubuntu下可以通过pip安装支持GPU的Tensorflow。(具体Tensorflow版本和Python版本可以对应更改,需要对应系统的cuda版本)
#python 2.7 环境
export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.4.0-cp27-none-linux_x86_64.whl
#python 3.5 环境
export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.4.0-cp35-cp35m-linux_x86_64.whl
#python 3.6 环境
export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.12.0-cp36-cp36m-linux_x86_64.whl
3 通过pip安装Tensorflow
# Python 2 环境
sudo pip install --upgrade $TF_BINARY_URL
# Python 3 环境
sudo pip3 install --upgrade $TF_BINARY_URL
三 从源代码编译安装
将Tensorflow源代码编译成pip安装包,然后再使用pip install的方法就可以完成安装。
四 使用anaconda虚拟环境安装
1. 安装Anaconda
2. 通过anaconda创建虚拟环境
conda create -n py3tf pip python=3.6
创建了一个叫做py3tf的虚拟环境(名字可以随便起),并在这个虚拟环境中安装了python3.6
3. 进入虚拟环境
source activate py3tf
4. 在虚拟环境中安装cuda和cudnn
conda install cudnn=7.1.2 -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/linux-64/
后面的地址是清华的源,其中cuda9以上的版本在 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/linux-64/
cuda8以下的版本在 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/linux-64/
5. 安装Tensorflow
pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.12.0-cp36-cp36m-linux_x86_64.whl
tensorflow的官方安装指导 https://tensorflow.google.cn/install/source
Tensorflow测试实例
首先在命令行界面进入Python解释器
python
输入如下指令
import tensorflow as tf
a = tf.constant([1.0,2.0], name="a")
b = tf.constant([2.0,3.0], name="b")
result = a + b
sess = tf.Session()
再次输入:
sess.run(result)
得出结果:
总结
安装GPU版的Tensorflow需要cuda的支持,没有cuda是安装不成功的,而且Tensorflow只支持Nvidia计算能力大于3.0的GPU。
tensorflow的版本对应关系