Introduction and usage of Keras

What is keras?

Keras is a high-level neural networks API, written in Python and capable of running on top of TensorFlowCNTK, or Theano.
It was developed with a focus on enabling fast experimentation. 
Being able to go from idea to result with the least possible delay is key to doing good research.

1.1 Why this name, Keras?

Keras means horn in Greek.
It is a reference to a literary image from ancient Greek and Latin literature, first found in the Odyssey, where dream spirits (Oneiroi, singular Oneiros) are divided between those who deceive men with false visions, who arrive to Earth through a gate of ivory, and those who announce a future that will come to pass, who arrive through a gate of horn.
It's a play on the words horn / fulfill, and ivory) / deceive.

When use keras?

Use Keras if you need a deep learning library that:

  • Allows for easy and fast prototyping (through user friendliness, modularity, and extensibility).
  • Supports both convolutional networks and recurrent networks, as well as combinations of the two.
  • Runs seamlessly on CPU and GPU.


pip install keras

The core data structure of Keras is a model, a way to organize layers.
The simplest type of model is the Sequential model, a linear stack of layers.
For more complex architectures, you should use the Keras functional API, which allows to build arbitrary graphs of layers.
Here is the Sequential model:

from keras.models import Sequential
model = Sequential()
Stacking layers is as easy as .add():
from keras.layers import Dense
model.add(Dense(units=64, activation='relu', input_dim=100))
model.add(Dense(units=10, activation='softmax'))

Once your model looks good, configure its learning process with .compile():


If you need to, you can further configure your optimizer.
A core principle of Keras is to make things reasonably simple, while allowing the user to be fully in control when they need to (the ultimate control being the easy extensibility of the source code).

              optimizer=keras.optimizers.SGD(lr=0.01, momentum=0.9, nesterov=True))
You can now iterate on your training data in batches:
# x_train and y_train are Numpy arrays --just like in the Scikit-Learn API., y_train, epochs=5, batch_size=32)

Alternatively, you can feed batches to your model manually:

model.train_on_batch(x_batch, y_batch)
Evaluate your performance in one line:
loss_and_metrics = model.evaluate(x_test, y_test, batch_size=128)
## Or generate predictions on new data:
classes = model.predict(x_test, batch_size=128)

Keras makes it easy to turn models into products

Your Keras models can be easily deployed across a greater range of platforms than any other deep learning framework:

Keras supports multiple backend engines and does not lock you into one ecosystem

Your Keras models can be developed with a range of different deep learning backends.
Importantly, any Keras model that only leverages built-in layers will be portable across all these backends: you can train a model with one backend, and load it with another (e.g. for deployment). Available backends include:

  • The TensorFlow backend (from Google)
  • The CNTK backend (from Microsoft)
  • The Theano backend

Amazon is also currently working on developing a MXNet backend for Keras.

As such, your Keras model can be trained on a number of different hardware platforms beyond CPUs:

Keras has strong multi-GPU support and distributed training support

Realated article



Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?