Using Keras & Tensorflow with AMD GPU

Using Keras & Tensorflow with AMD GPU

This tutorial will explain how to set-up a neural network environment, using AMD GPUs in a single or multiple configurations. On the software side: we will be

AMD is developing a new HPC platform, called ROCm. Its ambition is to create a common, open-source environment, capable to interface both with Nvidia (using CUDA) and AMD GPUs (further information).

This tutorial will explain how to set-up a neural network environment, using AMD GPUs in a single or multiple configurations.

On the software side: we will be able to run Tensorflow v1.12.0 as a backend to Keras on top of the ROCm kernel, using Docker.

To install and deploy ROCm are required particular hardware/software configurations.

Hardware requirements

The official documentation (ROCm v2.1) suggests the following hardware solutions.

Supported CPUs

Current CPUs which support PCIe Gen3 + PCIe Atomics are:

  • AMD Ryzen CPUs;
  • The CPUs in AMD Ryzen APUs;
  • AMD Ryzen Threadripper CPUs
  • Intel Xeon E7 v3 or newer CPUs;
  • Intel Xeon E5 v3 or newer CPUs;
  • Intel Xeon E3 v3 or newer CPUs;
  • Intel Core i7 v4 (i7–4xxx), Core i5 v4 (i5–4xxx), Core i3 v4 (i3–4xxx) or newer CPUs (i.e. Haswell family or newer).
  • Some Ivy Bridge-E systems

Supported GPUs

ROCm officially supports AMD GPUs that use the following chips:

  • GFX8 GPUs
  • “Fiji” chips, such as on the AMD Radeon R9 Fury X and Radeon Instinct MI8
  • “Polaris 10” chips, such as on the AMD Radeon RX 480/580 and Radeon Instinct MI6
  • “Polaris 11” chips, such as on the AMD Radeon RX 470/570 and Radeon Pro WX 4100
  • “Polaris 12” chips, such as on the AMD Radeon RX 550 and Radeon RX 540
  • GFX9 GPUs
  • “Vega 10” chips, such as on the AMD Radeon RX Vega 64 and Radeon Instinct MI25
  • “Vega 7nm” chips (Radeon Instinct MI50, Radeon VII)
Software requirements

On the software side, the current version of ROCm (v2.1), is supported only in Linux-based systems.

The ROCm 2.1.x platform supports the following operating systems:

Ubuntu 16.04.x and 18.04.x (Version 16.04.3 and newer or kernels 4.13 and newer)

CentOS 7.4, 7.5, and 7.6 (Using devtoolset-7 runtime support)

RHEL 7.4, 7.5, and 7.6 (Using devtoolset-7 runtime support)

Testing setup

The following hardware/software configuration has been used, by the author, to test and validate the environment:

  • CPU: Intel Xeon E5–2630L
  • RAM: 2 x 8 GB
  • Motherboard: MSI X99A Krait Edition
  • GPU: 2 x RX480 8GB + 1 x RX580 4GB
  • SSD: Samsung 850 Evo (256 GB)
  • HDD: WDC 1TB
  • OS: Ubuntu 18.04 LTS
ROCm installation

In order to get everything working properly, is recommended to start the installation process, within a fresh installed operating system. The following steps are referring to Ubuntu 18.04 LTS operating system, for other OS please refer to the official documentation.

The first step is to install ROCm kernel and dependencies:

Update your system

Open a new terminal CTRL + ALT + T

sudo apt update
sudo apt dist-upgrade
sudo apt install libnuma-dev
sudo reboot
Add the ROCm apt repository

To download and install ROCm stack is required to add related repositories:

wget -qO - | sudo apt-key add -
echo 'deb [arch=amd64] xenial main' | sudo tee /etc/apt/sources.list.d/rocm.list
Install ROCm

Is now required to update apt repository list and install rocm-dkms meta-package:

sudo apt update
sudo apt install rocm-dkms
Set permissions

The official documentation suggests creating a new video group in order to have access to GPU resources, using the current user.

Firstly, check the groups in your system, issuing:


Then add yourself to the video group:

sudo usermod -a -G video $LOGNAME

You may want to ensure that any future users you add to your system are put into the “video” group by default. To do that, you can run the following commands:

echo 'ADD_EXTRA_GROUPS=1' | sudo tee -a /etc/adduser.conf
echo 'EXTRA_GROUPS=video' | sudo tee -a /etc/adduser.conf

Then reboot the system:

Testing ROCm stack

Is now suggested to test the ROCm installation issuing the following commands.

Open a new terminal CTRL + ALT + T , issue the following commands:


the output should look as follows: link

Then double-check issuing:


The output should look like that: link

Official documentation finally suggests to add ROCm binaries to PATH:

echo 'export PATH=$PATH:/opt/rocm/bin:/opt/rocm/profiler/bin:/opt/rocm/opencl/bin/x86_64' | sudo tee -a /etc/profile.d/

Congratulations! ROCm is properly installed in your system and the command:


should display your hardware information and stats:

rocm-smi command output

Tensorflow Docker

The fastest and more reliable method to get ROCm + Tensorflow backend to work is to use the docker image provided by AMD developers.

Install Docker CE

First, is required to install Docker. In order to do that, please follow the instructions for Ubuntu systems:

Get Docker Engine

Tip: To avoid inserting sudo docker <command> instead of docker <command> it’s useful to provide access to non-root users: Manage Docker as a non-root user.

Pull ROCm Tensorflow image

It’s now time to pull the Tensorflow docker provided by AMD developers.

Open a new terminal CTRL + ALT + T and issue:

docker pull rocm/tensorflow

after a few minutes, the image will be installed in your system, ready to go.

Create a persistent space

Because of the ephemeral nature of Docker containers, once a docker session is closed all the modifications and files stored, will be deleted with the container.

For this reason is useful to create a persistent space in the physical drive for storing files and Jupyter notebooks. The simpler method is to create a folder to initialize with a docker container. To do that issue the command:

mkdir /home/$LOGNAME/tf_docker_share

This command will create a folder named tf_docker_share useful for storing and reviewing data created within the docker.

Starting Docker

Now, execute the image in a new container session. Simply send the following command:

docker run -i -t \
--network=host \
--device=/dev/kfd \
--device=/dev/dri \
--group-add video \
--cap-add=SYS_PTRACE \
--security-opt seccomp=unconfined \
--workdir=/tf_docker_share \
-v $HOME/tf_docker_share:/tf_docker_share rocm/tensorflow:latest /bin/bash

The docker is in execution on the directory /tf_docker_share and you should see something similar to:

it means that you are now operating inside the Tensorflow-ROCm virtual system.

Installing Jupyter

Jupyter is a very useful tool, for the development, debug and test of neural networks. Unfortunately, it’s not currently installed, as default, on the Tensorflow-ROCm, Docker image, published by ROCm team. It’s therefore required to manually install Jupyter.

In order to do that, within Tensorflow-ROCm virtual system prompt,

1. Issue the following command:

pip3 install jupyter

It will install the Jupyter package into the virtual system. Leave open this terminal.

2. Open a new terminal CTRL + ALT + T .

Find the CONTAINER ID issuing the command:

docker ps

A table, similar to the following should appear:

Container ID on the left

The first column represents the Container ID of the executed container. Copy that because it’s necessary for the next step.

3. It’s time to commit, to permanently write modifications of the image. From the same terminal, execute:

docker commit <container-id> rocm/tensorflow:<tag>

where tag value is an arbitrary name for example personal .

4. To double check that the image has been generated correctly, from the same terminal, issue the command:

docker images

that should result generate a table similar to the following:

It’s important to note that we will refer to this newly generated image for the rest of the tutorial.

The new docker run command, to use, will look like:

docker run -i -t \
--network=host \
--device=/dev/kfd \
--device=/dev/dri \
--group-add video \
--cap-add=SYS_PTRACE \
--security-opt seccomp=unconfined \
--workdir=/tf_docker_share \
-v $HOME/tf_docker_share:/tf_docker_share rocm/tensorflow:<tag> /bin/bash

where once again, tag value is arbitrary, for example personal .

Entering Jupyter notebook environment

We can finally enter the Jupyter environment. Inside it, we will create the first neural network using Tensorflow v1.12 as backend and Keras as frontend.


Firstly close all the previously executing Docker containers.

  1. Check the already open containers:
docker ps

2. Close all the Docker container/containers:

docker container stop <container-id1> <container-id2> ... <container-idn>

3. Close all the already open terminals.

Executing Jupyter

Let’s open a new terminal CTRL + ALT + T :

  1. Run a new Docker container (personal tag will be used as default):
docker run -i -t \
--network=host \
--device=/dev/kfd \
--device=/dev/dri \
--group-add video \
--cap-add=SYS_PTRACE \
--security-opt seccomp=unconfined \
--workdir=/tf_docker_share \
-v $HOME/tf_docker_share:/tf_docker_share rocm/tensorflow:personal /bin/bash

You should be logged into Tensorflow-ROCm docker container prompt.

Logged into docker container

2. Execute the Jupyter notebook:

jupyter notebook --allow-root --port=8889

a new browser window should appear, similar to the following:

Jupyter root directory

If the new tab does not appear automatically, on the browser, go back to the terminal where jupyter notebook command has been executed. On the bottom, there is a link to follow (press: CTRL + left mouse button on it) then, a new tab in your browser redirects you to Jupyter root directory.

Typical Jupyter notebook output. The example link is on the bottom

Train a neural network with Keras

In the last section, of this tutorial, we will train a simple neural network on the MNIST dataset. We will firstly build a fully connected neural network.

Fully connected neural network

Let’s create a new notebook, by selecting Python3 from the upper-right menu in Jupyter root directory.

The upper-right menu in Jupyter explorer

A new Jupiter notebook should pop-up in a new browser tab. Rename it tofc_network by clicking Untitled on the upper left corner of the window.

Notebook renaming

Let’s check Tensorflow backend. On the first cell insert:

import tensorflow as tf; print(tf.__version__)

then press SHIFT + ENTER to execute. The output should look like:

Tensorflow V1.12.0

We are using Tensorflow v1.12.0.

Let’s import some useful functions, to use next:

from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.utils import to_categorical

Let’s set batch size, epochs and number of classes.

batch_size = 128
num_classes = 10
epochs = 10

We will now download and preprocess inputs, loading them into system memory.

# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(60000, 784)
x_test = x_test.reshape(10000, 784)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
# convert class vectors to binary class matrices
y_train = to_categorical(y_train, num_classes)
y_test = to_categorical(y_test, num_classes)

It’s time to define the neural network architecture:

model = Sequential()
model.add(Dense(512, activation='relu', input_shape=(784,)))
model.add(Dense(512, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))

We will use a very simple, two-layer fully-connected network, with 512 neurons per layer. It’s also included a 20% drop probability on the neuron connections, in order to prevent overfitting.

Let’s print some insight into the network architecture:


Network architecture

Despite the simplicity of the problem, we have a considerable number of parameters to train (almost ~700.000), it means also considerable computational power consumption. Convolutional Neural Networks will solve the issue reducing computational complexity.

Now, compile the model:


and start training:

history =, y_train,
                    validation_data=(x_test, y_test))

Training process

The neural network has been trained on a single RX 480 with a respectable 47us/step. For comparison, an Nvidia Tesla K80 is reaching 43us/step but is 10x more expensive.

Multi-GPU Training

As an additional step, if your system has multiple GPUs, is possible to leverage Keras capabilities, in order to reduce training time, splitting the batch among different GPUs.

To do that, first it’s required to specify the number of GPUs to use for training by, declaring an environmental variable (put the following command on a single cell and execute):

!export HIP_VISIBLE_DEVICES=0,1,...

Numbers from 0 to … are defining which GPU to use for training. In case you want to disable GPU acceleration simply:


It’s also necessary to add multi_gpu_model function.

As an example, if you have 3 GPUs, the previous code will modify accordingly.


That concludes this tutorial. The next step will be to test a Convolutional Neural Network on the MNIST dataset. Comparing performances in both single and multi-GPU.

What’s relevant here, is that AMD GPUs perform quite well under computational load at a fraction of the price. GPU market is changing rapidly and ROCm gave to researchers, engineers, and startups, very powerful, open-source tools to adopt, lowering upfront costs in hardware equipment.

Getting Your Hands Dirty with TensorFlow 2.0 and Keras API

Getting Your Hands Dirty with TensorFlow 2.0 and Keras API

Diving into technical details of the regression model creation with TensorFlow 2.0 and Keras API. In TensorFlow 2.0, Keras comes out of the box with TensorFlow library. API is simplified and more convenient to use.

Diving into technical details of the regression model creation with TensorFlow 2.0 and Keras API. In TensorFlow 2.0, Keras comes out of the box with TensorFlow library. API is simplified and more convenient to use.

TensorFlow 2.0 comes with Keras packaged inside, there is no need to import Keras as a separate module (although you can do this if you need). TensorFlow 2.0 API is simplified and improved. This is good news for us — Machine Learning developers.

This is how you import Keras now, from TensorFlow:

from tensorflow import feature_column
from tensorflow import keras
from tensorflow.keras import layersprint(tf.__version__)

I’m using input pipeline to encode categorical columns, Keras API works well with One of the main advantages of is the fact that it acts as a bridge between data and model. There is no need to transform data by yourself, enough to define transformation rule — transformed data automatically will be applied during training.

Data is fetched from CSV file into Pandas dataframe:

column_names = ['report_id','report_params','day_part','exec_time']
raw_dataframe = pd.read_csv('report_exec_times.csv')
dataframe = raw_dataframe.copy()dataframe.head()

Column values for report_params vary, we need to normalize this column (make values to be on a similar scale):

eps=0.001 # 0 => 0.1¢
dataframe['report_params'] = np.log(dataframe.pop('report_params')+eps)

I’m using a utility method (this method is taken from TensorFlow tutorial) to create dataset from Pandas dataframe:

def df_to_dataset(dataframe, shuffle=True, batch_size=32):
  dataframe = dataframe.copy()
  labels = dataframe.pop('exec_time')
  ds =, labels))
  if shuffle:
    ds = ds.shuffle(buffer_size=len(dataframe))
  ds = ds.batch(batch_size)
  return ds

Next, we need to define data mapping for categorical columns encoding. I’m using TensorFlow vocabulary list function, including mapping for all unique values (if there are many values, it would be better to use embedding API). Two columns are encoded — report_id and day_part:

feature_columns = []feature_columns.append(feature_column.numeric_column('report_params'))report_id = feature_column.categorical_column_with_vocabulary_list('report_id', ['1', '2', '3', '4', '5'])
report_id_one_hot = feature_column.indicator_column(report_id)
feature_columns.append(report_id_one_hot)day_part = feature_column.categorical_column_with_vocabulary_list('day_part', ['1', '2', '3'])
day_part_one_hot = feature_column.indicator_column(day_part)

Create Keras dense features layer out of array with TensorFlow encodings. We will use this layer during Keras model construction to define model training features:

feature_layer = tf.keras.layers.DenseFeatures(feature_columns)

We are done with features. Next, convert Pandas dataframe to with the help of utility function:

batch_size = 32
train_ds = df_to_dataset(train, batch_size=batch_size)
val_ds = df_to_dataset(val, shuffle=False, batch_size=batch_size)
test_ds = df_to_dataset(test, shuffle=False, batch_size=batch_size)

Dense features layer is used when Keras sequential model is defined (there is no need to pass array of features later into a fit function):

def build_model(feature_layer):
  model = keras.Sequential([
    layers.Dense(16, activation='relu'),
    layers.Dense(16, activation='relu'),
  ])  optimizer = keras.optimizers.RMSprop(0.001)  model.compile(loss='mse',
                metrics=['mae', 'mse'])
  return model

Training is executed through function. We are using input pipeline to pass training and validation sets:

history =,

The awesome thing about it — data encoding happens behind the scenes, based on the rules defined for the feature layer.

How to run model.predict function with and feature layer? It’s simple.

Construct Pandas dataframe with input data:

headers = ['report_id', 'report_params', 'day_part']
dataframe_input = pd.DataFrame([[1, 15, 3]],

Convert report_params value to be on the same scale as it was for the training:

eps=0.001 # 0 => 0.1¢
dataframe_input['report_params'] = np.log(dataframe_input.pop('report_params')+eps)

Create input pipeline out of Pandas dataframe:

input_ds =
input_ds = input_ds.batch(1)

Run model.predict function:

res = model.predict(input_ds)


  • Source code with sample data is available on my GitHub repo

Enjoy !

How to Generating Faces with a Generative Adversarial Networks (GAN) in Keras/Tensorflow 2.0

How to Generating Faces with a Generative Adversarial Networks (GAN) in Keras/Tensorflow 2.0

Updated for Tensorflow 2.0. Implement a Generative Adversarial Networks (GAN) from scratch in Python using TensorFlow and Keras. Using two Kaggle datasets that contain human face images, a GAN is trained that is able to generate human faces.

Updated for Tensorflow 2.0. Implement a Generative Adversarial Networks (GAN) from scratch in Python using TensorFlow and Keras. Using two Kaggle datasets that contain human face images, a GAN is trained that is able to generate human faces.

Shooting Hoops with Keras and TensorFlow - Zack Akil

Shooting Hoops with Keras and TensorFlow - Zack Akil

PyData London Meetup #58 Tuesday, September 3, 2019 I'll show you how I turned being bad at shootin' b-ball outside of the school into a multi neural network...

I'll show you how I turned being bad at shootin' b-ball outside of the school into a multi neural network based mobile application that does real time analysis of my limited basketball skills.