Keras for beginners
To get started we need to install CUDA and CUDNN if you want to use your internal GPU for running tensorflow.
Firstly we need to install nvidia driver I personally recomend nvidia 390. To install that open up terminal and type:
sudo apt-get install nvidia-390
I recommended installing cuda 9.0 or 9.1 as 9.2 is not fully supported and you might run into mulitple errors when playing around with tensorflow
- Go to Cuda tool kit and scroll down to legacy releases
- Download the local runfile. Make sure you select the correct operating system in this tutorial i will be using linix
- Navigate to the runfile using the cd commond for instance if its on the desktop type:
cd Desktop/
- To run the .run file type in:
sudo sh cuda_9.1.85_387.26_linux.run
- Make sure you press N when it asks to install the nvidia drivers as we have previosuly installed them.
- Once cuda is installed we need to open the .bashrc for that type in:
nano ~/.bashrc
and then add these lines at the end of the file (this is done so that libs such as tensorflow can locate cuda) :
export PATH=/usr/local/cuda-9.1/bin${PATH:+:${PATH}}
Press Ctrl+X to exit out make sure you press save when exiting out
- Run the following command so that the changes are applied:
source ~/.bashrc
- Reboot you pc and verify nvidia and cuda drivers are install properly this will be done by:
cat /proc/driver/nvidia/version
and for cuda
nvcc -V
- Now you need to install cuDNN to do that firstly you need register for an nvidia develper account
- Now download cuDNN 7.1 for linix
- Once downloaded navigate to that folder and unpack the file
tar -xzvf cudnn-8.0-linux-x64-v5.1.tgz
Copy cuDNN files to cuda folder
sudo cp cuda/lib64/* /usr/local/cuda/lib64/
sudo cp cuda/include/cudnn.h /usr/local/cuda/include/
- To install a virtual enviornment firstly type:
sudo apt-get install python3-venv
- Name your virtual enviornment
virtualenv --system-site-packages ~/keras
- To activate the virtual enviornment type:
source ~/keras/bin/activate
- Now install tensorflow by using this command:
pip3 install --upgrade tensorflow-gpu
- Now install Keras Library:
pip3 install keras
- Finally to test everthing create a text file with
import tensorflow as tf
import keras
import numpy as np
import scipy
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
- Save it as test.py and run it using:
python3 test.py