Tensorflow GPU and keras on Ubuntu with Nvidia 960M

Posted by German Rezzonico on Sun 23 April 2017

Tensorflow GPU and Keras on Ubuntu 16.04.2 LTS with Nvidia 960M

Requirements

We will set up a machine learning development environment on Ubuntu 16.04.2 LTS and TensorFlow with GPU support. The installation was done at a laptop with a Geforce GTX 960M graphics card, the laptop also has an integrated GPU.

Instructions:

We will follow some instructions found here

MiniConda installation

Download the bash installer for miniconda here

Exceute the miniconda bash installer:

bash Miniconda3-latest-Linux-x86_64.sh

Then we will create an environment in which we will install the necessary libraries:

conda create --name environment_name python=3.5

Enter the environment:

source activate environment_name

Inside the enviroment:

conda install numpy jupyter scipy matplotlib pandas seaborn

To exit the environment:

source deactivate

Driver installation

Open Software & Updates, Additional Drivers and install the corresponding drivers to your hardware.

Cuda installation

Download the Cuda Toolkit, in this case we will install CUDA Toolkit 8.0

Follow this instructions

Make sure to follow ALL the steps!

As a recap, this is what you will be doing:

sudo dpkg -i cuda-repo-ubuntu1604-8-0-local-ga2_8.0.61-1_amd64.deb
sudo apt-get update
sudo apt-get install cuda

On the Post installation actions of the guide, be sure to check the path where cuda is installed and export that path.

cuDNN installation

Cudnn can be downloaded here You need a login to get the cudnn drivers.

We will install cuDNN v5.1 (Jan 20, 2017) because this version is supported by tensorflow (cuDNN v6.0 is not at this point), download: CUDA 8.0 cuDNN v5.1 Library for Linux.

Once you have downloaded cuDNN execute the following commands as stated here:

tar -xzvf cudnn-8.0-linux-x64-v5.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*

Environment variables

More instructions here

We need to set the LD_LIBRARY_PATH (if you follow the instructions from nvidia cuda toolkit installation guide this should be already set) and CUDA_HOME environment variables. So I add the following commands to the end of ~/.bashrc

# Path to Cuda toolkit
export PATH=/usr/local/cuda-8.0/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
# Path to cudnn
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64"
export CUDA_HOME=/usr/local/cuda

Tensorflow installation

source activate environment_name
conda install pip
pip install --ignore-installed --upgrade tensorflow-gpu

Congrats!, now you should test the installation

source activate environment_name
python3
import tensorsflow

You should get these results:

I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcublas.so.8.0 locally
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcudnn.so.5 locally
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcufft.so.8.0 locally
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcuda.so.1 locally
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcurand.so.8.0 locally

Install Keras

conda install keras-gpu

It is not recommended to upgrade the linux kernels because it will break cuda toolkit, so you may want to freeze the kernel:

avoid kernel upgrades

Check installed kernels

dpkg -l | grep linux-image

You can freeze any package including kernel packages by

sudo apt-mark hold <package_name>

In case you upgrade your linux kernels you will need to uninstall cuda and then reinstall it

Remove cuda

sudo dpkg --remove cuda

Reinstall cuda

sudo apt-get update
sudo apt-get install cuda