如何在Ubuntu 16.04上安装Tensorflow GPU

时间:2018-07-05 07:50:15

标签: ubuntu tensorflow nvidia

我将AWS P2 gpu实例与Tesla K80 GPU和Ubuntu 16.04 LTS一起使用。为CPU安装Tensorflow很容易:

pip install tensorflow

但是无法为GPU安装Tensorflow:

pip install tensorflow-gpu

2 个答案:

答案 0 :(得分:3)

安装Nvidia驱动程序:

sudo apt-get remove nvidia* && sudo apt autoremove  
sudo reboot  # you will have to wait till reboot completes
sudo apt-get update
sudo apt-get install openjdk-8-jdk
curl -O http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_9.0.176-1_amd64.deb
sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub
sudo dpkg -i ./cuda-repo-ubuntu1604_9.0.176-1_amd64.deb
sudo apt-get update
sudo apt-get install cuda-9-0  

以下代码应向您显示有关您的GPU的详细信息

nvidia-smi

如果您看到有关GPU的详细信息,请继续执行以下命令

wget https://s3.amazonaws.com/open-source-william-falcon/cudnn-9.0-linux-x64-v7.1.tgz  
sudo tar -xzvf cudnn-9.0-linux-x64-v7.1.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*
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64"
export CUDA_HOME=/usr/local/cuda
source ~/.bashrc

确保卸载以前的Tensorflow。然后激活您的虚拟环境并在其中安装tensorflow_gpu。如果您没有虚拟环境(应该这样做),则以下代码将为您提供帮助:

sudo pip install virtualenv
mkdir ~/virtualenv #creating a folder to store all your virtual env
cd ~/virtualenv/
virtualenv -p python3 venv1 #creates venv1 with new python installed on your ubuntu
source venv1/bin/activate #activates your venv
pip uninstall tensorflow
pip uninstall tensorflow-gpu
pip install jupyter # coz i like jupyter
pip install tensorflow-gpu

现在让我们测试Tensorflow-gpu是否有效

python
import tensorflow as tf   
hello = tf.constant('Hello, TensorFlow!') #if no error received, its working
from tensorflow.python.client import device_lib
device_lib.list_local_devices() #this will show all CPU's and GPU's on your system

是的!

答案 1 :(得分:0)

自动安装所有推荐的ubuntu驱动程序(包括nvidia驱动程序):

sudo apt-get purge nvidia*     
sudo ubuntu-drivers autoinstall

将要求您设置密码以验证安装是否正确。设置该密码后,重新启动计算机。然后不要以正常模式启动,进入MOK模式并输入该密码作为密钥。 重新启动(重新启动)后,请通过以下方法检查驱动程序是否安装成功:

nvidia-smi

然后安装Anaconda link,它将处理所有事情。