In recent years, there has been significant progress in the field of machine learning. Much of this progress can be attributed to the increasing usage of graphics processing units (GPUs) to accelerate the training of machine learning models. In particular, the extra computational power has lead to the popularization of Deep Learning – the use of complex, multi-level neural networks to create models, capable of feature detection from large amounts of unlabeled training data.

GPUs are so well-suited to deep learning because the type of calculations they were designed to process happens to be the same as those encountered in deep learning. Images, videos, and other graphics are represented as matrices, so that when you perform any operation, such as a zoom in effect or a camera rotation, all you are doing is applying some mathematical transformation to a matrix. In practice, this means that GPUs, compared to central processing units (CPUs), are more specialized at performing matrix operations and several other types of advanced mathematical transformations. This makes deep learning algorithms run several times faster on a GPU compared to a CPU. Learning times can often be reduced from days to mere hours.

So, how would one approach using GPUs for machine learning tasks? In this post we will explore the setup of a GPU-enabled AWS instance to train a neural network in Tensorflow.

To start, create a new EC2 instance in the AWS control panel.
In this guide, we will be using Ubuntu Server 16.04 LTS (HVM) as the OS, but the process should be similar on any 64-bit Linux distro.
For the instance type, select g2.2xlarge – these are enabled with NVIDIA GRID GPU. There are also instances with several of these GPUs, but utilizing more than one requires additional setup which will be discussed later in this post.
Finish the setup with your preferred security settings. Once the setup and creation is done, SSH into your instance.
Python should already be present on the system so install the required libraries:

sudo apt-get update
sudo apt-get install python-pip python-dev

Next, install Tensorflow with GPU support enabled. The simplest way is

pip install tensorflow-gpu

However, this might fail for some installations. If this happens, there is an alternative:

If you get a “locale.Error: unsupported locale setting” during TF installations, enter

export LC_ALL=C

Then, repeat the installation process.
If no further errors occur, the TF installation is over. However, for GPU acceleration to properly work, we still have to install Cuda Toolkit and cuDNN.
First, lets install the Cuda Toolkit. Before you start, please note that the installation process will download around 3gb of data.

Once the CUDA Toolkit is installed, download cuDNN Library for Linux from https://developer.nvidia.com/cudnn(note that you will need to register for the Accelerated Computing Developer Program) and copy it to your EC2 instance. Then:

The system is now ready to utilize a GPU with Tensorflow.
The changes to your Tensorflow code should be minimal. If a TensorFlow operation has both CPU and GPU implementations, the GPU devices will be prioritized when the operation is assigned to a device.

If you would like a particular operation to run on a device of your choice instead of using the defaults, you can use “with tf.device” to create a device context. This forces all the operations within that context to have the same device assignment.

Next, we will take a closer look at the benefits of utilizing the GPU.
For benchmarking purposes we will use a convolutional neural network (CNN) for recognizing images that is provided as part of the Tensorflow tutorials. CIFAR-10 classification is a common benchmark problem in machine learning. The task is to classify RGB 32×32 pixel images across 10 categories.

Let’s compare the performance of training this model on several popular configurations:

IInstance type

Price

Time to complete training

Macbook Pro

–

21.6 hours

c4.xlarge(4xCPU)

$0.199 per hour

16.1 hours

c4.4xlarge(16xCPU)

$0.796 per hour

4.8 hours

g2.2xlarge(1xGPU)

$0.650 per hour

4.72 hours

g2.8xlarge(2xGPU out of 4)

$7.2 per hour

2.5 hours

g2.8xlarge(4xGPU out of 4)

$7.2 per hour

1.6 hours

As demonstrated by the results, in this specific example it takes the power of 16 CPUs to match the power of 1 GPU. At the time of writing, utilizing a GPU is also 18% cheaper for the same training time.