Keras tutorial: deep learning example with Keras and Python

Keras is an open source library for creating deep learning applications. Keras is written in Python and offers a uniform interface for various deep learning backends, such as ‘TensorFlow’ and ‘Theano’. Deep Learning is a sub-genre of machine learning and is based on artificial neural networks.

The aim of Keras is to make deep learning less daunting. In the course of this Keras tutorial, we’ll explore the functionality of Keras by using a simple example.

Cloud backup from IONOS

Make costly downtime a thing of the past and back up your business the easy way!

Simple
Secure
Integrated

Preparing your system for the use of Keras

The best way to prepare your system to use Keras is by installing the ‘Anaconda’ software package. Anaconda is a free platform that’s used in data science. It includes useful libraries and tools, and comes with a Python3 installation as standard.

Installing Anaconda and Python

The following explanations and the below code are intended for macOS users. In principle, the code should also run on other operating systems. However, you may have to make some adjustments, especially if you use Windows.

If you are familiar with the command line and have the free software package management system Homebrew installed on your Mac, you can use it to install Anaconda. To do this, open a command line (‘Terminal.App’ on Mac), copy the following line of code into the terminal, and execute it.

brew cask install anaconda

If you are not familiar with Homebrew or if you want to install Anaconda on a Windows or Linux system, you can download the appropriate installation package for your system from the following page: Anaconda – Individual Edition.

Installation testing of Anaconda and Python

To make sure that Anaconda and Python have been correctly installed, execute the following commands in the command line:

To display the version of the Conda package manager

conda –version

To display the version of the Python interpreter

python --version

If you receive the error message ‘Command not found’ while typing, you may need to set the path to the Anaconda binaries. To do this, continue reading. If the tests worked, you can skip the section below.

Setting the path to Anaconda binaries

The path environment variable ‘PATH’ contains information about where in the file system certain utility programs are located. Individual paths within the environment variable are separated by a colon. You can append more paths, shown here for Anaconda in version 3:

export PATH=/usr/local/anaconda3/bin:"$PATH"

To make sure that the path is actually active, you must store this line of code in your system. Depending on your system and the shell that you use (bash, zsh etc.), the file in which the change is made differs. Below we illustrate this using an example for bash in macOS.

Execute the following lines of code in the command line to adjust the path variable in the ‘.bash_profile’ file:

Path variable to extend the Anaconda installation

echo -n 'export PATH=/usr/local/anaconda3/bin:"$PATH"' >> "$HOME/.bash_profile"

Load the .bash_profile file

source "$HOME/.bash_profile"
Tip

Use the command ‘cat "$HOME/.bash_profile"‘ in the command line to show the existing ‘.bash_profile’ file.

Now, repeat the tests:

Show version of the Conda package manager

conda –version

Show version of the Python interpreter

python --version

You should now be able to see the version numbers. If this is the case, you can continue with the following steps.

Updating Anaconda and Python installations to the latest version

Before you begin a new project, it’s best to update the underlying libraries. The Anaconda platform comes with the package manager ‘conda.’ Use the following conda commands to make use of available updates:

Install updates for the Conda package manager

conda update conda

Install Anaconda updates

conda update anaconda

Checking the version numbers of installed Keras deep learning packages

Keras is written in Python and builds on a host of different Python modules. Execute the following block of code in the command line to show the version numbers of the most-used Keras deep learning packages:

python << EOF
print()
# scipy
import scipy
print('scipy: %s' % scipy.__version__)
# numpy
import numpy
print('numpy: %s' % numpy.__version__)
# matplotlib
import matplotlib
print('matplotlib: %s' % matplotlib.__version__)
# pandas
import pandas
print('pandas: %s' % pandas.__version__)
# statsmodels
import statsmodels
print('statsmodels: %s' % statsmodels.__version__)
# scikit-learn
import sklearn
print('sklearn: %s' % sklearn.__version__)
print()
EOF

Installing Keras on your system

Now that we have prepared the system, we can proceed with installing Keras. To do this, execute the following lines of code in the command line:

Installing TensorFlow

pip install tensorflow

Install Keras

pip install keras

As before, it’s best to check the installed Keras version. Use the following line of code to do this:

python -c "import keras; print(keras.__version__)"

If Keras was installed a while back, it’s a good idea to search for any available updates and install them. The following line of code will do this for you. Copy it as usual and execute the code in the command line:

pip install --upgrade keras

Deep learning with Keras made easy: an example

The team behind Keras publishes a list with Keras examples under a free license on GitHub. We’ll take a closer look at the specific example ‘mnist_cnn.py’. Here, the code creates a ‘convolutional neural network’ (CNN or ConvNet) and trains it using a training data set.

For training and test data, the Keras example script uses the MNIST data set. This is a large collection of small images, each made up of 28 x 28 pixels. Each image also contains a number written by hand. The MNIST dataset is the standard for pattern recognition and is delivered with Keras.

If you’re curious about it, you can have a look at the training and test data. To do so, copy the following block of code and execute it in the command line. Each time you execute it, you will see one of the training images.

python << EOF
# load Keras 
import keras
# load MNIST training and test data sets
from keras.datasets import mnist
# Load library for graphic representation
import matplotlib.pyplot as plt
# Load function for random image selection
from random import randint
# Load datasets
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
# Show image
plt.figure()
plt.imshow(train_images[randint(1, len(train_images) - 1)])
plt.grid(False)
plt.show()
EOF

In the following, we’ll train the neural network to correctly assign the handwritten numbers. The training of the network is computationally demanding. So don't be surprised if your computer stops working properly – this is normal. If you are using a mobile device, make sure the battery is sufficiently charged or connect the device to a power supply.

We will first create a test folder on the desktop, change to that folder, and create an empty Python script there. Use the following lines of code; copy them and run them in the command line:

To create a folder ‘keras-test’ on the desktop

mkdir "$HOME/Desktop/keras-test/" && cd "$HOME/Desktop/keras-test/"

To create an empty Python script

touch keras-test.py

Next, you have to copy the script into the file ‘keras-test.py’ and save it.

Once the test folder is created, the next step is to create the Keras example script. To do so, copy the code at the end of this article and paste it into an empty text document. Save the document in the folder ‘keras-test’ that you just created on the desktop inside the ‘keras-test.py’ file. Finally, to execute the Keras example script using the Python interpreter, enter the following lines of code in the command line:

cd "$HOME/Desktop/keras-test/" && python keras-test.py

You should now see some status information. Next, the script will start training ConvNet, showing you the progress as you go through each epoch. After running the script, the ConvNet is trained and available for the classification of handwritten numbers.

Note

Only use a code editor/’plain text’ editor to save the code. Under no circumstances should you use word processing software such as Word, OpenOffice, or LibreOffice to do this.

# Save code in the file ‘keras-test.py’ in the folder ‘keras-test’
from __future__ import print_function
# load Keras 
import keras
# MNIST load training and test data sets 
from keras.datasets import mnist
# Loading a sequential model
from keras.models import Sequential
# Load layers of the neural network
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
# Number of different data characteristics: digits 0–9
num_classes = 10
# Number of passes for training the neural network
epochs = 12
# Number of files used during one pass
batch_size = 128
# Dimensions of the input screens (28 x 28 pixels per image)
img_rows, img_cols = 28, 28
# Load training and testing data
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
if K.image_data_format() == 'channels_first':
    train_images = train_images.reshape(train_images.shape[0], 1, img_rows, img_cols)
    test_images = test_images.reshape(test_images.shape[0], 1, img_rows, img_cols)
    input_shape = (1, img_rows, img_cols)
else:
    train_images = train_images.reshape(train_images.shape[0], img_rows, img_cols, 1)
    test_images = test_images.reshape(test_images.shape[0], img_rows, img_cols, 1)
    input_shape = (img_rows, img_cols, 1)
# Set floating point data type
train_images = train_images.astype('float32')
test_images = test_images.astype('float32')
# Normalise image data
train_images /= 255
test_images /= 255
print('train_images shape:', train_images.shape)
print(train_images.shape[0], 'train samples')
print(test_images.shape[0], 'test samples')
# Convert class vectors into binary class matrices
train_labels = keras.utils.to_categorical(train_labels, num_classes)
test_labels = keras.utils.to_categorical(test_labels, num_classes)
# Create model
model = Sequential()
# Add layers to model
model.add(Conv2D(32, kernel_size=(3, 3),
                 activation='relu',
                 input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
# Compile layers
model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])
# Train model
model.fit(train_images, train_labels,
          batch_size=batch_size,
          epochs=epochs,
          verbose=1,
          validation_data=(test_images, test_labels))
# Evaluate model
score = model.evaluate(test_images, test_labels, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

Wait! We’ve got something for you!
Get your .co.uk domain for just £1/1st year.

Enter the web address of your choice in the search bar to check its availability.
12 months for £1
then £10/year