Running Local GPUs in Keras

Machine Learning and Artificial Intelligence have made data analysis very easy. Almost all the big IT firms nowadays use AI or ML in some way or the other. Data is the fuel of the 21st century. The main problem beginners face while working with neural networks or any other algorithm is handling data.

Training your model on Huge Data sets can take a lot of time. Clearly, we need methods to train models at a faster speed. In this tutorial, we will learn how to use the Graphics Processing Unit, or GPU on our local machines with TensorFlow.

In this tutorial, we are going to see how we can run local GPU in Keras.

CPU vs GPU

Modern PCs along with the main CPU, also have a GPU, generally. Unlike CPUs, GPUs can perform thousands of operations at once. GPUs, although developed back in 1999, has gained importance just recently. Algorithms like Image recognition need very huge datasets. For example, suppose there are 1 million images in a data set. A CPU can take more than 48 hours to just train this. Whereas a GPU might be able to do it in less than 24 hours.

Undoubtedly GPUs have become very important for modern-day computing. Moreover, we can use these units on our local machines easily. To test if your machine can run GPUs:

  1. You should have Tensorflow Installed
  2. Install Anaconda and type “conda install keras-gpu”
  3. Use Google Colab if you have a good internet connection.

In addition to this you should run the following code:

import tensorflow as tf
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())

#Check if GPU is visible to keras
from keras import backend as K
K.tensorflow_backend._get_available_gpus()

Finally, you will have this output:

You can see that the machine is running on “XLA_GPU” on Tesla K80 Computer. Keep in mind that GPUs can only be used with NVIDIA, and not with AMD. You need to add the following code if you are working on a local machine:

import keras
#Assuming you have a 48 core CPU
config = tf.ConfigProto( device_count = {'GPU': 1 , 'CPU': 48} ) 
sess = tf.Session(config=config) 
keras.backend.set_session(sess)

Tensor Processing Units

In addition to GPUs, models can also be trained on TPUs. This hardware accelerator was developed by Google. Without a doubt, these are the fastest circuits for neural network machine learning. To get started with Neural networks you can check out: Convolutional Neural Networks Using Keras.

Undoubtedly, TPUs have the highest rate of Data Transfer. They have a speed ranging from 180 to 240 Tera Flops. Does this mean they would perform best in any data set? They have the highest performance with large batches and CNNs. TPUs can be used in Google Colab by Changing Runtime type.

Differing from TPUs, it has been observed that GPUs show better flexibility and better performance for irregular computations. For Recurrent Neural Networks, CPUs show the best performance. Thus every platform has advantages based on the model characteristics.

If you have any doubt, you can ask in the comments section.

Machine learning is ❤️

Leave a Reply

Your email address will not be published.