Keras: The Python Deep Learning library

    Keras is a high-level neural networks API, written in Python and capable of running on top of , CNTK, or . 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.

    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.

    Read the documentation at Keras.io.

    Keras is compatible with: Python 2.7-3.6.

    Multi-backend Keras and tf.keras:

    At this time, we recommend that Keras users who use multi-backend Keras with the TensorFlow backend switch to in TensorFlow 2.0. tf.keras is better maintained and has better integration with TensorFlow features (eager execution, distribution support and other).

    Keras 2.2.5 was the last release of Keras implementing the 2.2.* API. It was the last release to only support TensorFlow 1 (as well as Theano and CNTK).

    The current release is Keras 2.3.0, which makes significant API changes and add support for TensorFlow 2.0. The 2.3.0 release will be the last major release of multi-backend Keras. Multi-backend Keras is superseded by tf.keras.

    Bugs present in multi-backend Keras will only be fixed until April 2020 (as part of minor releases).

    For more information about the future of Keras, see .

    • User friendliness. Keras is an API designed for human beings, not machines. It puts user experience front and center. Keras follows best practices for reducing cognitive load: it offers consistent & simple APIs, it minimizes the number of user actions required for common use cases, and it provides clear and actionable feedback upon user error.

    • Modularity. A model is understood as a sequence or a graph of standalone, fully configurable modules that can be plugged together with as few restrictions as possible. In particular, neural layers, cost functions, optimizers, initialization schemes, activation functions and regularization schemes are all standalone modules that you can combine to create new models.

    Getting started: 30 seconds to 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 , which allows to build arbitrary graphs of layers.

    Here is the Sequential model:

    Stacking layers is as easy as .add():

    1. from keras.layers import Dense
    2. model.add(Dense(units=10, activation='softmax'))

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

    1. model.compile(loss='categorical_crossentropy',
    2. optimizer='sgd',
    3. 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).

    1. model.compile(loss=keras.losses.categorical_crossentropy,
    2. optimizer=keras.optimizers.SGD(lr=0.01, momentum=0.9, nesterov=True))

    You can now iterate on your training data in batches:

    Alternatively, you can feed batches to your model manually:

    Evaluate your performance in one line:

    1. loss_and_metrics = model.evaluate(x_test, y_test, batch_size=128)

    Or generate predictions on new data:

    1. classes = model.predict(x_test, batch_size=128)

    Building a question answering system, an image classification model, a Neural Turing Machine, or any other model is just as fast. The ideas behind deep learning are simple, so why should their implementation be painful?

    For a more in-depth tutorial about Keras, you can check out:

    In the of the repository, you will find more advanced models: question-answering with memory networks, text generation with stacked LSTMs, etc.

    You may also consider installing the following optional dependencies:

    • (recommended if you plan on running Keras on GPU).
    • HDF5 and h5py (required if you plan on saving Keras models to disk).
    • and pydot (used by to plot model graphs).

    Then, you can install Keras itself. There are two ways to install Keras:

    • Install Keras from PyPI (recommended):

    Note: These installation steps assume that you are on a Linux or Mac environment.If you are on Windows, you will need to remove sudo to run the commands below.

    If you are using a virtualenv, you may want to avoid using sudo:

    1. pip install keras
    • Alternatively: install Keras from the GitHub source:

    First, clone Keras using git:

    1. git clone https://github.com/keras-team/keras.git

    Then, cd to the Keras folder and run the install command:

    1. sudo python setup.py install

    Configuring your Keras backend

    By default, Keras will use TensorFlow as its tensor manipulation library. Follow these instructions to configure the Keras backend.

    You can ask questions and join the development discussion:

    • On the . Use this link to request an invitation to the channel.

    You can also post bug reports and feature requests (only) in . Make sure to read our guidelines first.

    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).

    Keras was initially developed as part of the research effort of project ONEIROS (Open-ended Neuro-Electronic Intelligent Robot Operating System).