Keras Tutorial
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.
- Automatic backup & easy recovery
- Intuitive scheduling and management
- AI-based threat protection
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.
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
To display the version of the Python interpreter
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:
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
Load the .bash_profile file
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
Show version of the Python interpreter
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
Install Anaconda updates
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:
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
Install Keras
As before, it’s best to check the installed Keras version. Use the following line of code to do this:
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:
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.
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
To create an empty Python script
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:
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.
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.