What is keras?
Keras is a high-level neural networks API, written in Python and capable of running on top of TensorFlow, CNTK, 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():
model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])
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).
model.compile(loss=keras.losses.categorical_crossentropy, 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. model.fit(x_train, 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:
- On iOS, via Apple’s CoreML (Keras support officially provided by Apple). Here's a tutorial.
- On Android, via the TensorFlow Android runtime. Example: Not Hotdog app.
- On Google Cloud, via TensorFlow-Serving.
- In a Python webapp backend (such as a Flask app).
- On the JVM, via DL4J model import provided by SkyMind.
- On Raspberry Pi.
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:
- NVIDIA GPUs
- Google TPUs, via the TensorFlow backend and Google Cloud
- OpenCL-enabled GPUs, such as those from AMD, via the PlaidML Keras backend
Keras has strong multi-GPU support and distributed training support
- Keras has built-in support for multi-GPU data parallelism
- Horovod, from Uber, has first-class support for Keras models
- Keras models can be turned into TensorFlow Estimators and trained on clusters of GPUs on Google Cloud
- Keras can be run on Spark via Dist-Keras (from CERN) and Elephas