keras/README.md

170 lines
7.4 KiB
Markdown
Raw Normal View History

# Keras: Deep Learning library for TensorFlow and Theano
2015-03-28 00:59:42 +00:00
2016-04-11 14:49:29 +00:00
[![Build Status](https://travis-ci.org/fchollet/keras.svg?branch=master)](https://travis-ci.org/fchollet/keras)
2016-07-04 04:19:05 +00:00
[![license](https://img.shields.io/github/license/mashape/apistatus.svg?maxAge=2592000)](https://github.com/fchollet/keras/blob/master/LICENSE)
2015-12-06 19:32:32 +00:00
2015-03-28 00:59:42 +00:00
## You have just found Keras.
2016-10-08 22:53:24 +00:00
Keras is a high-level neural networks library, written in Python and capable of running on top of either [TensorFlow](https://github.com/tensorflow/tensorflow) or [Theano](https://github.com/Theano/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.*
2015-03-28 00:59:42 +00:00
Use Keras if you need a deep learning library that:
2015-12-30 21:09:16 +00:00
- Allows for easy and fast prototyping (through total modularity, minimalism, and extensibility).
- Supports both convolutional networks and recurrent networks, as well as combinations of the two.
- Supports arbitrary connectivity schemes (including multi-input and multi-output training).
- Runs seamlessly on CPU and GPU.
2015-03-28 00:59:42 +00:00
Read the documentation at [Keras.io](http://keras.io).
2015-12-12 21:34:46 +00:00
Keras is compatible with: __Python 2.7-3.5__.
2015-11-29 00:34:35 +00:00
------------------
2015-03-28 00:59:42 +00:00
## Guiding principles
2015-07-18 10:49:10 +00:00
- __Modularity.__ A model is understood as a sequence or a graph of standalone, fully-configurable modules that can be plugged together with as little restrictions as possible. In particular, neural layers, cost functions, optimizers, initialization schemes, activation functions, regularization schemes are all standalone modules that you can combine to create new models.
2015-03-28 00:59:42 +00:00
2015-11-29 00:34:35 +00:00
- __Minimalism.__ Each module should be kept short and simple. Every piece of code should be transparent upon first reading. No black magic: it hurts iteration speed and ability to innovate.
- __Easy extensibility.__ New modules are dead simple to add (as new classes and functions), and existing modules provide ample examples. To be able to easily create new modules allows for total expressiveness, making Keras suitable for advanced research.
- __Work with Python__. No separate models configuration files in a declarative format. Models are described in Python code, which is compact, easier to debug, and allows for ease of extensibility.
2015-03-28 00:59:42 +00:00
2015-11-29 00:34:35 +00:00
------------------
2015-03-28 00:59:42 +00:00
2015-11-29 00:34:35 +00:00
## Getting started: 30 seconds to Keras
2015-03-28 00:59:42 +00:00
2016-04-26 02:06:31 +00:00
The core data structure of Keras is a __model__, a way to organize layers. The main type of model is the [`Sequential`](http://keras.io/getting-started/sequential-model-guide) model, a linear stack of layers. For more complex architectures, you should use the [Keras functional API](http://keras.io/getting-started/functional-api-guide).
2015-11-29 00:34:35 +00:00
2016-04-05 04:46:27 +00:00
Here's the `Sequential` model:
2015-03-28 00:59:42 +00:00
```python
from keras.models import Sequential
model = Sequential()
```
2015-11-29 00:34:35 +00:00
Stacking layers is as easy as `.add()`:
2015-03-28 00:59:42 +00:00
```python
2016-05-24 06:59:34 +00:00
from keras.layers import Dense, Activation
2015-11-29 00:34:35 +00:00
2016-04-05 04:46:27 +00:00
model.add(Dense(output_dim=64, input_dim=100))
2015-11-29 00:34:35 +00:00
model.add(Activation("relu"))
2016-04-05 04:46:27 +00:00
model.add(Dense(output_dim=10))
2015-11-29 00:34:35 +00:00
model.add(Activation("softmax"))
2015-03-28 00:59:42 +00:00
```
2015-11-29 00:34:35 +00:00
Once your model looks good, configure its learning process with `.compile()`:
```python
2016-04-05 04:46:27 +00:00
model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])
2015-11-29 00:34:35 +00:00
```
2015-03-28 00:59:42 +00:00
2015-11-29 00:34:35 +00:00
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).
2015-03-28 00:59:42 +00:00
```python
from keras.optimizers import SGD
2015-11-29 00:34:35 +00:00
model.compile(loss='categorical_crossentropy', optimizer=SGD(lr=0.01, momentum=0.9, nesterov=True))
2015-03-28 00:59:42 +00:00
```
2015-11-29 00:34:35 +00:00
You can now iterate on your training data in batches:
2015-03-28 00:59:42 +00:00
```python
2015-11-29 00:34:35 +00:00
model.fit(X_train, Y_train, nb_epoch=5, batch_size=32)
2015-03-28 00:59:42 +00:00
```
2015-11-29 00:34:35 +00:00
Alternatively, you can feed batches to your model manually:
```python
model.train_on_batch(X_batch, Y_batch)
```
2015-03-28 00:59:42 +00:00
2015-11-29 00:34:35 +00:00
Evaluate your performance in one line:
```python
2016-04-07 00:33:39 +00:00
loss_and_metrics = model.evaluate(X_test, Y_test, batch_size=32)
2015-11-29 00:34:35 +00:00
```
2015-03-28 00:59:42 +00:00
2015-11-29 00:34:35 +00:00
Or generate predictions on new data:
2015-03-28 00:59:42 +00:00
```python
2015-11-29 00:34:35 +00:00
classes = model.predict_classes(X_test, batch_size=32)
proba = model.predict_proba(X_test, batch_size=32)
2015-03-28 00:59:42 +00:00
```
2016-04-07 00:33:39 +00:00
Building a question answering system, an image classification model, a Neural Turing Machine, a word2vec embedder or any other model is just as fast. The ideas behind deep learning are simple, so why should their implementation be painful?
2015-07-19 02:04:58 +00:00
2016-04-05 04:46:27 +00:00
For a more in-depth tutorial about Keras, you can check out:
2015-03-28 00:59:42 +00:00
2016-04-07 00:33:39 +00:00
- [Getting started with the Sequential model](http://keras.io/getting-started/sequential-model-guide)
- [Getting started with the functional API](http://keras.io/getting-started/functional-api-guide)
2016-04-05 04:46:27 +00:00
In the [examples folder](https://github.com/fchollet/keras/tree/master/examples) of the repository, you will find more advanced models: question-answering with memory networks, text generation with stacked LSTMs, etc.
2015-03-28 00:59:42 +00:00
2015-11-29 00:34:35 +00:00
------------------
2015-03-28 00:59:42 +00:00
## Installation
Keras uses the following dependencies:
- numpy, scipy
2015-07-18 10:49:10 +00:00
- pyyaml
- HDF5 and h5py (optional, required if you use model saving/loading functions)
2015-03-28 00:59:42 +00:00
- Optional but recommended if you use CNNs: cuDNN.
2015-11-29 00:34:35 +00:00
2015-12-01 18:03:37 +00:00
*When using the TensorFlow backend:*
2015-12-30 21:09:16 +00:00
2015-11-29 00:34:35 +00:00
- TensorFlow
- [See installation instructions](https://github.com/tensorflow/tensorflow#download-and-setup).
*When using the Theano backend:*
- Theano
- [See installation instructions](http://deeplearning.net/software/theano/install.html#install).
2015-12-01 18:03:37 +00:00
To install Keras, `cd` to the Keras folder and run the install command:
```sh
sudo python setup.py install
```
2015-03-28 00:59:42 +00:00
2015-07-18 10:49:10 +00:00
You can also install Keras from PyPI:
```sh
2015-07-18 10:49:10 +00:00
sudo pip install keras
```
2015-11-29 00:34:35 +00:00
------------------
## Switching from TensorFlow to Theano
2015-11-29 00:34:35 +00:00
By default, Keras will use TensorFlow as its tensor manipulation library. [Follow these instructions](http://keras.io/backend/) to configure the Keras backend.
2015-11-29 00:34:35 +00:00
------------------
## Support
2016-10-13 03:11:43 +00:00
You can ask questions and join the development discussion:
- On the [Keras Google group](https://groups.google.com/forum/#!forum/keras-users).
2016-12-16 20:40:55 +00:00
- On the [Keras Slack channel](https://kerasteam.slack.com). Use [this link](https://keras-slack-autojoin.herokuapp.com/) to request an invitation to the channel.
2015-11-29 00:34:35 +00:00
2016-12-16 20:40:55 +00:00
You can also post **bug reports and feature requests** (only) in [Github issues](https://github.com/fchollet/keras/issues). Make sure to read [our guidelines](https://github.com/fchollet/keras/blob/master/CONTRIBUTING.md) first.
2015-11-29 00:34:35 +00:00
------------------
2015-03-28 00:59:42 +00:00
## Why this name, Keras?
2015-03-30 03:59:19 +00:00
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).
2015-03-28 00:59:42 +00:00
2015-11-29 00:34:35 +00:00
Keras was initially developed as part of the research effort of project ONEIROS (Open-ended Neuro-Electronic Intelligent Robot Operating System).
2015-03-28 00:59:42 +00:00
2015-07-18 10:49:10 +00:00
>_"Oneiroi are beyond our unravelling --who can be sure what tale they tell? Not all that men look for comes to pass. Two gates there are that give passage to fleeting Oneiroi; one is made of horn, one of ivory. The Oneiroi that pass through sawn ivory are deceitful, bearing a message that will not be fulfilled; those that come out through polished horn have truth behind them, to be accomplished for men who see them."_ Homer, Odyssey 19. 562 ff (Shewring translation).
2015-11-29 00:34:35 +00:00
2015-12-06 19:32:32 +00:00
------------------