keras/README.md

238 lines
9.8 KiB
Markdown
Raw Normal View History

2015-03-28 01:37:38 +00:00
# Keras: Theano-based Deep Learning library
2015-03-28 00:59:42 +00:00
## You have just found Keras.
2015-03-28 11:48:15 +00:00
Keras is a minimalist, highly modular neural network library in the spirit of Torch, written in Python / Theano so as not to have to deal with the dearth of ecosystem in Lua. 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:
- allows for easy and fast prototyping (through total modularity, minimalism, and extensibility).
2015-07-18 10:49:10 +00:00
- 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).
Keras is compatible with __Python 2.7-3.4__.
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-07-18 10:49:10 +00:00
- __Minimalism.__ Each module should be kept short and simple (<100 lines of code). Every piece of code should be transparent upon first reading. No black magic: it hurts iteration speed and ability to innovate.
2015-03-28 00:59:42 +00:00
2015-07-23 23:51:29 +00:00
- __Easy extensibility.__ New modules are dead simple to add (as new classes/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.
2015-03-28 00:59:42 +00:00
2015-07-18 10:49:10 +00:00
- __Work with Python__. No separate models configuration files in a declarative format (like in Caffe or PyLearn2). 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
## Examples
### Multilayer Perceptron (MLP):
```python
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.optimizers import SGD
model = Sequential()
2015-10-05 23:28:17 +00:00
# Dense(64) is a fully-connected layer with 64 hidden units.
# in the first layer, you must specify the expected input data shape:
# here, 20-dimensional vectors.
model.add(Dense(64, input_dim=20, init='uniform'))
2015-03-28 00:59:42 +00:00
model.add(Activation('tanh'))
model.add(Dropout(0.5))
2015-10-05 23:28:17 +00:00
model.add(Dense(64, init='uniform'))
2015-03-28 00:59:42 +00:00
model.add(Activation('tanh'))
model.add(Dropout(0.5))
2015-10-05 23:28:17 +00:00
model.add(Dense(2, init='uniform'))
2015-03-28 00:59:42 +00:00
model.add(Activation('softmax'))
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='mean_squared_error', optimizer=sgd)
model.fit(X_train, y_train, nb_epoch=20, batch_size=16)
score = model.evaluate(X_test, y_test, batch_size=16)
```
### Alternative implementation of MLP:
```python
model = Sequential()
2015-10-05 23:28:17 +00:00
model.add(Dense(64, input_dim=20, init='uniform', activation='tanh'))
2015-03-28 00:59:42 +00:00
model.add(Dropout(0.5))
2015-10-05 23:28:17 +00:00
model.add(Dense(64, init='uniform', activation='tanh'))
2015-03-28 00:59:42 +00:00
model.add(Dropout(0.5))
2015-10-05 23:28:17 +00:00
model.add(Dense(2, init='uniform', activation='softmax'))
2015-03-28 00:59:42 +00:00
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='mean_squared_error', optimizer=sgd)
```
2015-03-28 01:37:38 +00:00
### VGG-like convnet:
2015-03-28 00:59:42 +00:00
```python
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.optimizers import SGD
model = Sequential()
2015-10-05 23:28:17 +00:00
# input: 100x100 images with 3 channels -> (3, 100, 100) tensors.
# this applies 32 convolution filters of size 3x3 each.
2015-10-09 15:31:20 +00:00
model.add(Convolution2D(32, 3, 3, border_mode='full', input_shape=(3, 100, 100)))
2015-03-28 00:59:42 +00:00
model.add(Activation('relu'))
2015-10-05 23:28:17 +00:00
model.add(Convolution2D(32, 3, 3))
2015-03-28 00:59:42 +00:00
model.add(Activation('relu'))
2015-10-09 15:31:20 +00:00
model.add(MaxPooling2D(pool_size=(2, 2)))
2015-03-28 00:59:42 +00:00
model.add(Dropout(0.25))
2015-10-09 15:31:20 +00:00
model.add(Convolution2D(64, 3, 3, border_mode='valid'))
2015-03-28 00:59:42 +00:00
model.add(Activation('relu'))
2015-10-09 15:31:20 +00:00
model.add(Convolution2D(64, 3, 3))
2015-03-28 00:59:42 +00:00
model.add(Activation('relu'))
2015-10-09 15:31:20 +00:00
model.add(MaxPooling2D(pool_size=(2, 2)))
2015-03-28 00:59:42 +00:00
model.add(Dropout(0.25))
2015-04-10 00:41:48 +00:00
model.add(Flatten())
2015-10-05 23:28:17 +00:00
# Note: Keras does automatic shape inference.
model.add(Dense(256))
2015-03-28 00:59:42 +00:00
model.add(Activation('relu'))
model.add(Dropout(0.5))
2015-10-05 23:28:17 +00:00
model.add(Dense(10))
2015-03-28 00:59:42 +00:00
model.add(Activation('softmax'))
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd)
model.fit(X_train, Y_train, batch_size=32, nb_epoch=1)
```
### Sequence classification with LSTM:
```python
from keras.models import Sequential
2015-05-18 09:59:18 +00:00
from keras.layers.core import Dense, Dropout, Activation
from keras.layers.embeddings import Embedding
2015-03-28 00:59:42 +00:00
from keras.layers.recurrent import LSTM
model = Sequential()
model.add(Embedding(max_features, 256, input_length=maxlen))
2015-10-05 23:28:17 +00:00
model.add(LSTM(output_dim=128, activation='sigmoid', inner_activation='hard_sigmoid'))
2015-03-28 00:59:42 +00:00
model.add(Dropout(0.5))
2015-10-05 23:28:17 +00:00
model.add(Dense(1))
2015-03-28 00:59:42 +00:00
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='rmsprop')
model.fit(X_train, Y_train, batch_size=16, nb_epoch=10)
score = model.evaluate(X_test, Y_test, batch_size=16)
```
### Architecture for learning image captions with a convnet and a Gated Recurrent Unit:
(word-level embedding, caption of maximum length 16 words).
2015-10-05 23:28:17 +00:00
Note that getting this to work well will require using a bigger convnet, initialized with pre-trained weights.
2015-03-28 00:59:42 +00:00
```python
max_caption_len = 16
2015-10-05 23:28:17 +00:00
vocab_size = 10000
# first, let's define an image model that
# will encode pictures into 128-dimensional vectors.
# it should be initialized with pre-trained weights.
image_model = Sequential()
2015-10-09 15:31:20 +00:00
image_model.add(Convolution2D(32, 3, 3, border_mode='full', input_shape=(3, 100, 100)))
2015-10-05 23:28:17 +00:00
image_model.add(Activation('relu'))
image_model.add(Convolution2D(32, 3, 3))
image_model.add(Activation('relu'))
2015-10-09 15:31:20 +00:00
image_model.add(MaxPooling2D(pool_size=(2, 2)))
2015-10-05 23:28:17 +00:00
2015-10-09 15:31:20 +00:00
image_model.add(Convolution2D(64, 3, 3, border_mode='full'))
2015-10-05 23:28:17 +00:00
image_model.add(Activation('relu'))
2015-10-09 15:31:20 +00:00
image_model.add(Convolution2D(64, 3, 3))
2015-10-05 23:28:17 +00:00
image_model.add(Activation('relu'))
2015-10-09 15:31:20 +00:00
image_model.add(MaxPooling2D(pool_size=(2, 2)))
2015-10-05 23:28:17 +00:00
image_model.add(Flatten())
image_model.add(Dense(128))
# let's load the weights from a save file.
image_model.load_weights('weight_file.h5')
# next, let's define a RNN model that encodes sequences of words
# into sequences of 128-dimensional word vectors.
language_model = Sequential()
language_model.add(Embedding(vocab_size, 256, input_length=max_caption_len))
language_model.add(GRU(output_dim=128, return_sequences=True))
language_model.add(Dense(128))
# let's repeat the image vector to turn it into a sequence.
2015-10-09 15:31:20 +00:00
image_model.add(RepeatVector(max_caption_len))
2015-10-05 23:28:17 +00:00
# the output of both models will be tensors of shape (samples, max_caption_len, 128).
# let's concatenate these 2 vector sequences.
model = Merge([image_model, language_model], mode='concat', concat_axis=-1)
# let's encode this vector sequence into a single vector
model.add(GRU(256, 256, return_sequences=False))
2015-10-09 15:31:20 +00:00
# which will be used to compute a probability
2015-10-05 23:28:17 +00:00
# distribution over what the next word in the caption should be!
model.add(Dense(vocab_size))
model.add(Activation('softmax'))
2015-03-28 00:59:42 +00:00
2015-10-05 23:28:17 +00:00
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
2015-03-28 00:59:42 +00:00
2015-10-05 23:28:17 +00:00
# "images" is a numpy float array of shape (nb_samples, nb_channels=3, width, height).
# "captions" is a numpy integer array of shape (nb_samples, max_caption_len)
# containing word index sequences representing partial captions.
# "next_words" is a numpy float array of shape (nb_samples, vocab_size)
# containing a categorical encoding (0s and 1s) of the next word in the corresponding
# partial caption.
model.fit([images, partial_captions], next_words, batch_size=16, nb_epoch=100)
2015-03-28 00:59:42 +00:00
```
In the examples folder, you will find example models for real datasets:
2015-10-05 23:28:17 +00:00
- CIFAR10 small images classification: Convolutional Neural Network (CNN) with realtime data augmentation
2015-03-30 03:59:19 +00:00
- IMDB movie review sentiment classification: LSTM over sequences of words
2015-07-18 10:49:10 +00:00
- Reuters newswires topic classification: Multilayer Perceptron (MLP)
- MNIST handwritten digits classification: MLP & CNN
- Character-level text generation with LSTM
2015-07-19 02:04:58 +00:00
2015-07-18 10:49:10 +00:00
...and more.
2015-03-28 00:59:42 +00:00
## Current capabilities
For complete coverage of the API, check out [the Keras documentation](http://keras.io).
2015-03-28 00:59:42 +00:00
2015-10-05 23:28:17 +00:00
A few highlights: convnets, LSTM, GRU, word2vec-style embeddings, PReLU, BatchNormalization...
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
2015-03-28 00:59:42 +00:00
- Theano
- See installation instructions: http://deeplearning.net/software/theano/install.html#install
- 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-10-05 23:28:17 +00:00
To install, `cd` to the Keras folder and run the install command:
```
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:
```
sudo pip install keras
```
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
Keras was developed as part of the research effort of project ONEIROS (Open-ended Neuro-Electronic Intelligent Robot Operating System).
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).