left-icon

Keras Succinctly®
by James McCaffrey

Previous
Chapter

of
A
A
A

CHAPTER 1

Getting Started

Getting Started


Keras is an open-source, neural-network library written in the Python language. Keras requires a backend engine and can use TensorFlow, CNTK (Microsoft Cognitive Toolkit), Theano, or MXNet. The motivation for Keras is that, although it's possible to create deep neural systems using TensorFlow directly (or CNTK, Theano, MXNet), because TensorFlow works at a relatively low level of abstraction, coding TensorFlow directly is quite challenging. Keras adds a relatively easy-to-use layer of abstraction over TensorFlow.

Keras, which means "horn" in Greek, was first released in March 2015. This e-book is based on Keras version 2.1.5, which was released in March 2018. Because Keras is in active development, by the time you read this e-book, the latest version will certainly be different. However, any changes to Keras will likely be relatively minor and consist mainly of additional functionality rather than major architecture changes. In other words, the code presented here should work with any Keras 2.x version with few, if any, changes needed.  

Keras runs on Windows, Linux, and Mac systems. This e-book focuses on Keras on Windows, but because Keras programs run in a shell, Keras on Linux or Mac systems is almost exactly the same.

The screenshot in Figure 1-1 shows a simplified Keras session on Windows. Notice that the program is just an ordinary Python script, wheat_nn.py, that references Keras as a Python package, and Keras programs run in an ordinary shell.

Example Keras Session

 Figure 1-1: Example Keras Session

This e-book assumes you have intermediate or better programming skill with a C-family language, but doesn't assume you know anything about Keras. Enough chit-chat already—let's get started.

Installing Keras and Anaconda

Every programmer I know, including me, learns how to program in a new language or framework by getting an example program up and running, and then experimenting with the example by making changes. So if you want to learn Keras, the first step is to install it.

Keras installation may be a bit different from other software installation you've done before. You don't install Keras directly. Instead, you install Keras as an add-on package for Python. Briefly, you first install a Python distribution (Anaconda), which contains the base Python language interpreter plus several additional packages that are required by Keras, in particular, the NumPy package. Next, you install the TensorFlow package, and then the Keras package.

It is possible to install Python, NumPy, and the other dependencies separately. But instead, I strongly recommend that you install the Anaconda distribution of Python, which has everything you need to successfully install and run Keras. Before beginning the installation process, you must carefully determine compatible versions of Keras, TensorFlow, and Anaconda. Most of the installation failures I've seen are due to incompatible versions.

The first step is to determine which version of Keras you want to use. In general, you'll want to install the most recent stable version of Keras. However, this e-book is based on Keras version 1.7.0 rather than the most recent version, along with Anaconda3 4.1.1 (which contains Python 3.5.2) and TensorFlow 2.1.5. I'm confident the code in this e-book will work with newer versions of Keras with few, if any, modifications needed.

Before installing Anaconda/Python, you should check your machine to determine if you already have an existing Python installation. The simplest scenario is when your machine doesn't have an existing Python installed, and you can proceed. If, however, you already have one or more versions of Python installed, you should either uninstall them all (if feasible) or note their installation locations, if uninstalling them is not feasible. With multiple Python instances installed, you may run into some Python versioning issues at some point.

To install Anacoinda3 4.1.1, you can either do an internet search for "archive Anaconda install" or go directly to this location. See Figure 1-2.

On that page, you'll see many different Anaconda distributions. Be careful here; even though I've installed and uninstalled Anaconda for Keras/TensorFlow dozens of times, I've selected the incorrect version of Anaconda several times.

The Anaconda distribution contains over 500 Python packages that are compatible with each other. Some packages, such as NumPy and SciPy, are absolutely essential. Some packages are specific to a field of study, such as BioPython for molecular biology, and some are not essential, but very useful, such as MatPlotLib for creating graphs and charts. You can view the complete list of packages included with Anaconda here.

Find Correct Anaconda Archived Install Link

Figure 1-2: Find Correct Anaconda Archived Install Link

Because I'm using a 64-bit Windows machine, and I want Python 3 with Anaconda version 4.1.1, I will click the link Anaconda3-4.1.1-Windows-x86_64.exe. This will launch a self-extracting installation program. You can select the Run option.

To recap, at this point you've determined which versions of Keras (1.7.0) and TensorFlow (2.1.5) you want to use, and then determined which version of Anaconda to use (Anaconda3 4.1.1 for a 64-bit Windows machine in this example), and are now beginning the Anaconda installation process.

Anaconda Installation Welcome

Figure 1-3: Anaconda Installation Welcome

A few seconds after you click Run, the Anaconda installation Welcome window will appear, as shown in Figure 1-3. Click Next.


You will see the Anaconda License Agreement window, as shown in Figure 1-4. Click I Agree.

Anaconda License Agreement

Figure 1-4: Anaconda License Agreement

You will see the Select Installation Type window, as shown in Figure 1-5. I strongly suggest you keep the default Just Me (recommended) option. This will reduce the likelihood of Python versioning collisions if there are multiple user accounts on your machine. Click Next.

Anaconda Installation Type

Figure 1-5: Anaconda Installation Type


Next, you'll see the Choose Install Location information. You should accept the default location (C:\Users\<user>\AppData\Local\Continuum\Anaconda3 on Windows) if possible, because some Python packages may assume this location. Click Next.

Default Anaconda Python Installation Location

Figure 1-6: Default Anaconda Python Installation Location

Next, you will see the Advanced Installation Options window. You should accept both default options. The first adds Anaconda to your System PATH variable. The second option makes Anaconda your default Python version. If you have an existing Python installation, this will usually override the existing instance, and you may want to install a Python version selector program. Click Install.

Installation PATH and Default Python Information

Figure 1-7: Installation PATH and Default Python Information


Installing Anaconda takes about 10 to 15 minutes. There will be no options for you to consider, so you don't need to attend to the installation. However, you may want to observe the progress bar and see which packages are installed, such as NumPy, shown in Figure 1-8.

Anaconda Installation Progress

Figure 1-8: Anaconda Installation Progress

When the installation completes successfully, you'll see an Installation Complete window. Click Next, as shown in Figure 1-9.

Successful Anaconda Installation Window

Figure 1-9: Successful Anaconda Installation Window


You will see a final window, with an option to view marketing information from Continuum, the company that maintains the Anaconda distribution. In Figure 1-10, I unchecked that option, and clicked Finish. To summarize, the Anaconda install is a self-extracting executable with a wizard-like process. You can accept all the default options.

Final Anaconda Installation Window

Figure 1-10: Final Anaconda Installation Window

After the Anaconda installation is complete, you may want to take a look at the installation file and directory structure, as shown in Figure 1-11. Notice there are directories named Lib, Library, and libs. Just below the files shown in Figure 1-11 is the python.exe main execution engine.

The Anaconda Installation Location

Figure 1-11: The Anaconda Installation Location


Before installing the TensorFlow and Keras add-on packages, you should verify that your Anaconda Python distribution is working. Open a command shell and enter python --version (with two hyphens). Python should respond as shown in Figure 1-12.

Verifying the Anaconda Python Installation

Figure 1-12: Verifying the Anaconda Python Installation

You can test the Python interpreter by issuing the command python. This will launch the interpreter, and you'll see the >>> prompt. Enter a print('hello') statement. You can exit the interpreter by typing the exit() command.

Installing TensorFlow and Keras

The next step is to install TensorFlow, and there are several ways to do it. I recommend using the PyPi (Python Package Index) repository.

Downloading the TensorFlow .whl File

Figure 1-13: Downloading the TensorFlow .whl File

Do an Internet search for "install TensorFlow 1.7.0" or go directly to this page  and click the Download files link. You'll go to a page that lists .whl files for different types of systems. In my case, because I'm using Python 3.5 and a Windows machine, I clicked on the link tensorflow-1.7.0-cp35-cp35m-win_amd64.whl (the cp35 indicates Python 3.5). See Figure 1-13.

You'll be asked if you want to open or save the .whl file. Click the Save as option. You can save the wheel file in any convenient location. I saved at C:\KerasSuccinctly\Wheels. Now, open a command shell and navigate to the directory where you saved the TensorFlow .whl file, and enter the following command:

C:\KerasSuccinctly\Wheels> pip install tensorflow-1.7.0-cp35-cp35m-win_amd64.whl

The pip utility installs Python packages using .whl files. Installation takes less than a minute, and then you'll see this message: "Successfully installed tensorflow-1.7.0".

The process for installing Keras is very much the same. Do an Internet search for "install Keras 2.1.5", or go directly to this webpage and click the link labeled Download files. See Figure 1-14.

Downloading the Keras .whl File

Figure 1-14: Downloading the Keras .whl File  

On the next page, click the Keras-2.1.5-py2.py3-none-any.whl link, and you'll be prompted to open or save. Click Save as and save the .whl file in a convenient location, for example, C:\KerasSuccinctly\Wheels. Launch a command shell, navigate to the directory where you saved the Keras .whl file, and enter the following command:

C:\KerasSuccinctly\Wheels> pip install Keras-2.1.5-py2.py3-none-any.whl

Installation is very quick, and you'll see a "Successfully installed Keras-2.1.5" message. You're now ready to write Keras programs.

Editing and running Keras programs

Because a Keras program is just a specialized Python program, you can use any Python editing environment. If you are relatively new to Python, selecting a Python editor or IDE (integrated development environment) can be a confusing task because there are dozens of editors and Python IDEs to choose from.

I often use plain old Notepad, or sometimes the slightly more powerful Notepad++. Neither of these give you built-in debugging functionality, so debugging means you must insert print() statements to inspect the values of variables and objects. And there's no integrated run command, so you run programs from a shell.

Code Listing 1-1: Checking the Keras and TensorFlow Versions

# test_keras.py

import sys

import keras as K

import tensorflow as tf

py_ver = sys.version

k_ver = K.__version__

tf_ver = tf.__version__

print("Using Python version " + str(py_ver))

print("Using Keras version " + str(k_ver))

print("Using TensorFlow version " + str(tf_ver))

Launch the Notepad text editor (or any other editor you're familiar with), and copy-paste the code in Code Listing 1-1. Save the file as test_keras.py in any convenient directory. Open a command shell, navigate to the directory that holds your Python file, and execute by entering python test_keras.py, as shown in Figure 1-15.

Using Notepad and a Command Shell

Figure 1-15: Using Notepad and a Command Shell

Many of my colleagues use Visual Studio Code (VS Code), a free, open-source, cross-platform, multi-language IDE. Installing VS Code is quick and easy, and adding Python support is just a matter of a couple of clicks. See this webpage

Using Visual Studio Code

Figure 1-16: Using Visual Studio Code

Figure 1-16 shows an example using VS Code. There are many advantages of using VS Code, including IntelliSense auto-complete, pretty formatting, and integrated debugging. However, unlike Notepad, VS Code does have a non-trivial learning curve you'll have to deal with. See this tutorial for help.

Another option for editing and running Keras programs is the heavyweight Visual Studio (VS) IDE. The default configuration of VS does not support editing Python programs, but you can install the Python Tools for Visual Studio add-in. With the add-in installed, you get full Python language support, as shown in Figures 1-17 and 1-18.

Creating a Python Project using the Visual Studio IDE

Figure 1-17: Creating a Python Project using the Visual Studio IDE

One advantage of using Visual Studio is that you get support for all kinds of additional functionality, such as data connectors to SQL Server databases and Azure data sources. The main disadvantage of Visual Studio is that it has a steep learning curve.

Running a Python Program from the Visual Studio IDE

Figure 1-18: Running a Python Program from the Visual Studio IDE

If you are familiar with any Python editor or development environment, my recommendation is to continue using that system. If you are relatively new to programming, my recommendation is to start with simple Notepad, because it has essentially no learning curve. If you are an experienced developer but new to Python, my recommendation is to try VS Code.

Using the Notepad++ Editor

Figure 1-19: Using the Notepad++ Editor

Uninstalling Keras

Anaconda, Keras, and TensorFlow all have quick, easy, and reliable uninstall procedures. To uninstall Keras, launch a command shell and issue the command pip uninstall keras. The Keras package will be removed from your Python system, as shown in (the slightly edited-for-size) Figure 1-20. To uninstall TensorFlow, issue pip uninstall tensorflow.

Uninstalling Keras

Figure 1-20: Uninstalling Keras

To uninstall Python on Windows, use the Programs and Features section of the Control Panel:

Uninstalling Anaconda Python

Figure 1-21: Uninstalling Anaconda Python

If you become a regular user of Keras, eventually you'll want to upgrade your version. Although the pip utility supports an upgrade command, I recommend just deleting your current version, then installing the new version.

Scroll To Top
Disclaimer
DISCLAIMER: Web reader is currently in beta. Please report any issues through our support system. PDF and Kindle format files are also available for download.

Previous

Next



You are one step away from downloading ebooks from the Succinctly® series premier collection!
A confirmation has been sent to your email address. Please check and confirm your email subscription to complete the download.