Installing tensorflow GPU under linux

Posted by doug007 on Sun, 17 Nov 2019 16:37:31 +0100

Install NVIDIA drive

1. Driver download address: https://www.nvidia.cn/Download/index.aspx?lang=cn
2. Unload the original drive
Ctrl+Alt+F1

sudo apt-get remove nvidia-*
sudo apt-get autoremove
sudo nvidia-uninstall

Reset computer

shutdown -r now

3. Drive installation
Ctrl+Alt+F1

sudo service lightdm stop
sudo sh ./filename.run -no-x-check -no-nouveau-check -no-opengl-files
sudo service lightdm restart

If you encounter the following problems:

ERROR: Unable to load the kernel module 'nvidia.ko'.  This happens most
   frequently when this kernel module was built against the wrong or
   improperly configured kernel sources, with a version of gcc that differs
   from the one used to build the target kernel, or if a driver such as
   rivafb/nvidiafb is present and prevents the NVIDIA kernel module from
   obtaining ownership of the NVIDIA graphics device(s), or NVIDIA GPU
   installed in this system is not supported by this NVIDIA Linux graphics
   driver release.

Solution: update Ubuntu 16.04 source

cd /etc/apt/
sudo cp sources.list sources.list.bak
sudo gedit sources.list

Add the following code to sources.list:

deb http://mirrors.ustc.edu.cn/ubuntu/ xenial main restricted universe multiverse 
deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-security main restricted universe multiverse 
deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse 
deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse 
deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse 
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial main restricted universe multiverse 
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-security main restricted universe multiverse 
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse 
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse 
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse 

Finally, update the source and the installed package, and then reinstall:

sudo apt-get update 
sudo apt-get upgrade 

Install Anaconda

1. Download address of Anaconda: https://www.anaconda.com/distribution/#linux
2, installation

bash Anaconda3-5.1.0-Linux-x86_64.sh

3. Configure environment variables

sudo gedit /etc/profile

Add at the end:

export PATH=/root/anaconda2/bin:$PATH

Save and apply

source /etc/profile 

Update after terminal restart

conda update -n base conda

Install tensorflow GPU and Keras

Create environment and specify python version

conda create -n tf python=2.7

Activate environment

conda activate tf  or  source activate tf

install

conda install tensorflow-gpu==1.6.0 keras

Install other libraries

conda install tensorflow-gpu keras
conda install matplotlib
conda install scikit-learn
conda install graphviz pydot

Exit environment

conda deactivate  or  source deactivate

test

Function test.py As follows:

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:1'):
    c = a+b
    
sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True,log_device_placement=True))
#sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
sess.run(tf.global_variables_initializer())
print(sess.run(c))

If the operation result is as follows, the environment is normal:

Reference resources

https://blog.csdn.net/gsww404/article/details/78328897
https://blog.csdn.net/weixin_39954229/article/details/79961172

Topics: sudo Ubuntu Linux Anaconda