Two of the top numerical platforms in Python that provide the basis for Deep Learning research and development are Theano and TensorFlow.

Both are very powerful libraries, but both can be difficult to use directly for creating deep learning models.

In this post, you will discover the Keras Python library that provides a clean and convenient way to create a range of deep learning models on top of Theano or TensorFlow.

Let’s get started.

**Update Oct/2016**: Updated examples for Keras 1.1.0, Theano 0.8.2 and TensorFlow 0.10.0.

## What is Keras?

Keras is a minimalist Python library for deep learning that can run on top of Theano or TensorFlow.

It was developed to make implementing deep learning models as fast and easy as possible for research and development.

It runs on Python 2.7 or 3.5 and can seamlessly execute on GPUs and CPUs given the underlying frameworks. It is released under the permissive MIT license.

Keras was developed and maintained by François Chollet, a Google engineer using four guiding principles:

**Modularity**: A model can be understood as a sequence or a graph alone. All the concerns of a deep learning model are discrete components that can be combined in arbitrary ways.**Minimalism**: The library provides just enough to achieve an outcome, no frills and maximizing readability.**Extensibility**: New components are intentionally easy to add and use within the framework, intended for researchers to trial and explore new ideas.**Python**: No separate model files with custom file formats. Everything is native Python.

## How to Install Keras

Keras is relatively straightforward to install if you already have a working Python and SciPy environment.

You must also have an installation of Theano or TensorFlow on your system already.

You can see installation instructions for both platforms here:

Keras can be installed easily using PyPI, as follows:

1 |
sudo pip install keras |

At the time of writing, the most recent version of Keras is version 1.1.0. You can check your version of Keras on the command line using the following snippet:

You can check your version of Keras on the command line using the following snippet:

1 |
python -c "import keras; print keras.__version__" |

Running the above script you will see:

1 |
1.1.0 |

You can upgrade your installation of Keras using the same method:

1 |
sudo pip install --upgrade keras |

## Theano and TensorFlow Backends for Keras

Assuming you have both Theano and TensorFlow installed, you can configure the backend used by Keras.

The easiest way is by adding or editing the Keras configuration file in your home directory:

1 |
~/.keras/keras.json |

Which has the format:

1 2 3 4 5 6 |
{ "image_dim_ordering": "tf", "epsilon": 1e-07, "floatx": "float32", "backend": "tensorflow" } |

In this configuration file you can change the “*backend*” property from “*tensorflow*” (the default) to “*theano*“. Keras will then use the configuration the next time it is run.

You can confirm the backend used by Keras using the following snippet on the command line:

1 |
python -c "from keras import backend; print backend._BACKEND" |

Running this with default configuration you will see:

1 2 |
Using TensorFlow backend. tensorflow |

You can also specify the backend to use by Keras on the command line by specifying the KERAS_BACKEND environment variable, as follows:

1 |
KERAS_BACKEND=theano python -c "from keras import backend; print(backend._BACKEND)" |

Running this example prints:

1 2 |
Using Theano backend. theano |

## Build Deep Learning Models with Keras

The focus of Keras is the idea of a model.

The main type of model is called a Sequence which is a linear stack of layers.

You create a sequence and add layers to it in the order that you wish for the computation to be performed.

Once defined, you compile the model which makes use of the underlying framework to optimize the computation to be performed by your model. In this you can specify the loss function and the optimizer to be used.

Once compiled, the model must be fit to data. This can be done one batch of data at a time or by firing off the entire model training regime. This is where all the compute happens.

Once trained, you can use your model to make predictions on new data.

We can summarize the construction of deep learning models in Keras as follows:

**Define your model**. Create a sequence and add layers.**Compile your model**. Specify loss functions and optimizers.**Fit your model**. Execute the model using data.**Make predictions**. Use the model to generate predictions on new data.

## Beat the Math/Theory Doldrums and Start using Deep Learning in your own projects Today, without getting lost in “documentation hell”

Get my free Deep Learning With Python mini course and develop your own deep nets by the time you’ve finished the first PDF with just a few lines of Python.

#### Daily lessons in your inbox for 14 days, and a DL-With-Python “Cheat Sheet” you can download right now.

## Keras Resources

The list below provides some additional resources that you can use to learn more about Keras.

- Keras Official Homepage (documentation)
- Keras Project on GitHub
- Keras User Group

Are you looking for a good Deep Learning tutorial to get started, take a look at:

## Summary

In this post, you discovered the Keras Python library for deep learning research and development.

You discovered that Keras is designed for minimalism and modularity allowing you to very quickly define deep learning models and run them on top of a Theano or TensorFlow backend.

Do you have any questions about Keras or this post? Ask your questions in the comments and I will do my best to answer them.

## Frustrated With Your Progress In Deep Learning?

#### What If You Could Develop Your Own Deep Nets in Minutes

...with just a few lines of Python

Discover how in my new Ebook: Deep Learning With Python

It covers **self-study tutorials** and **end-to-end projects** on topics like:*Multilayer Perceptrons*, *Convolutional Nets* and *Recurrent Neural Nets*, and more...

#### Finally Bring Deep Learning To

Your Own Projects

Skip the Academics. Just Results.

Always on the cutting edge of machine learning!

Thanks

Thanks Victor.

Great post, Jason! I will start my path in deep learning so I was wondering if you could help me with the hardware requirements. What are the minimal computational resources (disk space for saving/loading CNN models, RAM, GPU time) needed to run keras/theano/tensor flow?

You can run small networks on your CPU.

Larger networks like big CNNs do need big hardware, like GPUs. I recommend using GPUs on AWS, it can be very cost effective, e.g. less than $1 an hour.

Hi Jason, how do I check that my neural networks are trained using my GPU, and not only my CPU?

Great question.

Generally, when you use Theano as the backend and it is configured to use the GPU (by flags at runtime or in the config), it will report that it is using the GPU on the command line.

Hi Jason!

Wanted to ask – maybe you have tried Keras for 3D convolution? I ran into some questions with it and cannot get help from the Keras team (https://github.com/fchollet/keras/issues/3561). Would be great if you know what is correct there!

Sorry Linara, I have not used Keras for a 3D CNN yet.

Ok, thank you for answer anyway!

Hi Jason,

It doesn’t appear that Tensorflow has a Windows install. Is there an alternative? If not, have you any insight if a windows version is in the works?

Many Thanks,

Alan

Hi Alan,

My students on windows use Theano. In fact, generally, I recommend Theano over tensorflow in my books and tutorials. At least for now, it is faster on general hardware.

I use Vagrant to set up a VM with Keras, TensorFlow and Theano installed. I run it on Windows.

https://github.com/nirmalyaghosh/deep-learning-vm if anyone is interested.

Great approach, thanks for sharing Nirmalya.

@Alan Macshane

U can get Tensorflow on Windows by getting Windows 10 insider preview build.

I am using Tensorflow on Windows

Hi Jason

Do u have any tutorial on classifying Gender from an image using Deep learning or classical Computer Vision approach?

It will be great help

I don’t Ranjeet, but that is a very cool project idea!

Hi Jason,

I would like to know if there is a way to prepare a dataset which going to be string sequence of DNA “ATGCCGATGA along with its label ? i searched everywhere couldn’t find any answer.

Thanks a lot in advance,

Hi Steven, consider encoding the letters as numbers and treat it as a sequence classification problem. Not dissimilar from sentiment analysis.

I hope that sparks some ideas.

Hi Jason,

YOU are wonderful.

Thanks Ali.

Can you guide me in installing TensorFlow for 32-bit linux?

Sorry Ash, I do not have a specific guide for TensorFlow on 32-bit Linux.

Any alternatives you’d suggest for using keras on 32-bit linux?

You may be able to run on 32-bit linux, I don’t know, sorry.

An alternative that will work is using amazon web services to run your models:

http://machinelearningmastery.com/develop-evaluate-large-deep-learning-models-keras-amazon-web-services/

I hope that helps.

Simple! Changing the backend to “theano” will allow you to run keras on 32-bit linux. Thanks anyways! Will look into AWS as well.

I’m glad to hear it Ash.

Hi Jason,

Great post

Do u have any tutorial on classification from a speech using recurrent neural network?

It will be great help

Not at this stage leila.

Good afternoon. I have lettle problem: I installed Keras, but “Sequential – no module named”. How do I this module connect?

What is the error exactly Nastya?

Are you able to confirm that the Keras library was installed successfully?