README.md 8.2 KB
Newer Older
F
fchollet 已提交
1
# Keras: Deep Learning for Python
F
Francois 已提交
2

F
fchollet 已提交
3
![Keras logo](https://s3.amazonaws.com/keras.io/img/keras-logo-2018-transparent-large.png)
F
Francois Chollet 已提交
4

F
Francois Chollet 已提交
5
[![Build Status](https://travis-ci.org/fchollet/keras.svg?branch=master)](https://travis-ci.org/fchollet/keras)
F
fchollet 已提交
6
[![license](https://img.shields.io/github/license/mashape/apistatus.svg?maxAge=2592000)](https://github.com/fchollet/keras/blob/master/LICENSE)
7

F
Francois 已提交
8 9
## You have just found Keras.

A
Alan Yee 已提交
10
Keras is a high-level neural networks API, written in Python and capable of running on top of [TensorFlow](https://github.com/tensorflow/tensorflow), [CNTK](https://github.com/Microsoft/cntk), 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.*
F
Francois 已提交
11 12

Use Keras if you need a deep learning library that:
F
Francois Chollet 已提交
13

F
Francois Chollet 已提交
14
- Allows for easy and fast prototyping (through user friendliness, modularity, and extensibility).
15 16
- Supports both convolutional networks and recurrent networks, as well as combinations of the two.
- Runs seamlessly on CPU and GPU.
F
Francois 已提交
17

A
Alan Yee 已提交
18
Read the documentation at [Keras.io](https://keras.io).
19

F
Francois Chollet 已提交
20
Keras is compatible with: __Python 2.7-3.6__.
F
Francois Chollet 已提交
21 22 23 24


------------------

25

F
Francois 已提交
26 27
## Guiding principles

F
Francois Chollet 已提交
28
- __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.
F
Francois 已提交
29

F
Francois Chollet 已提交
30
- __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.
F
Francois Chollet 已提交
31

F
Francois Chollet 已提交
32
- __Easy extensibility.__ New modules are 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.
F
Francois Chollet 已提交
33 34 35

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

F
Francois 已提交
36

F
Francois Chollet 已提交
37
------------------
F
Francois 已提交
38 39


F
Francois Chollet 已提交
40
## Getting started: 30 seconds to Keras
F
Francois 已提交
41

A
Alan Yee 已提交
42
The core data structure of Keras is a __model__, a way to organize layers. The simplest type of model is the [`Sequential`](https://keras.io/getting-started/sequential-model-guide) model, a linear stack of layers. For more complex architectures, you should use the [Keras functional API](https://keras.io/getting-started/functional-api-guide), which allows to build arbitrary graphs of layers.
F
Francois Chollet 已提交
43

F
Francois Chollet 已提交
44
Here is the `Sequential` model:
F
Francois 已提交
45 46 47 48 49 50 51

```python
from keras.models import Sequential

model = Sequential()
```

F
Francois Chollet 已提交
52
Stacking layers is as easy as `.add()`:
F
Francois 已提交
53 54

```python
F
fchollet 已提交
55
from keras.layers import Dense, Activation
F
Francois Chollet 已提交
56

F
Francois Chollet 已提交
57
model.add(Dense(units=64, input_dim=100))
F
Francois Chollet 已提交
58
model.add(Activation('relu'))
F
Francois Chollet 已提交
59
model.add(Dense(units=10))
F
Francois Chollet 已提交
60
model.add(Activation('softmax'))
F
Francois 已提交
61 62
```

F
Francois Chollet 已提交
63
Once your model looks good, configure its learning process with `.compile()`:
F
Francois Chollet 已提交
64

F
Francois Chollet 已提交
65
```python
F
Francois Chollet 已提交
66 67 68
model.compile(loss='categorical_crossentropy',
              optimizer='sgd',
              metrics=['accuracy'])
F
Francois Chollet 已提交
69
```
F
Francois 已提交
70

F
Francois Chollet 已提交
71
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).
F
Francois 已提交
72
```python
F
Francois Chollet 已提交
73 74
model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.SGD(lr=0.01, momentum=0.9, nesterov=True))
F
Francois 已提交
75 76
```

F
Francois Chollet 已提交
77
You can now iterate on your training data in batches:
F
Francois Chollet 已提交
78

F
Francois 已提交
79
```python
F
Francois Chollet 已提交
80 81
# 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)
F
Francois 已提交
82 83
```

F
Francois Chollet 已提交
84
Alternatively, you can feed batches to your model manually:
F
Francois Chollet 已提交
85

F
Francois Chollet 已提交
86
```python
F
Francois Chollet 已提交
87
model.train_on_batch(x_batch, y_batch)
F
Francois Chollet 已提交
88
```
F
Francois 已提交
89

F
Francois Chollet 已提交
90
Evaluate your performance in one line:
F
Francois Chollet 已提交
91

F
Francois Chollet 已提交
92
```python
F
Francois Chollet 已提交
93
loss_and_metrics = model.evaluate(x_test, y_test, batch_size=128)
F
Francois Chollet 已提交
94
```
F
Francois 已提交
95

F
Francois Chollet 已提交
96
Or generate predictions on new data:
F
Francois Chollet 已提交
97

F
Francois 已提交
98
```python
F
Francois Chollet 已提交
99
classes = model.predict(x_test, batch_size=128)
F
Francois 已提交
100 101
```

F
Francois Chollet 已提交
102
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?
F
fchollet 已提交
103

104
For a more in-depth tutorial about Keras, you can check out:
F
Francois 已提交
105

A
Alan Yee 已提交
106 107
- [Getting started with the Sequential model](https://keras.io/getting-started/sequential-model-guide)
- [Getting started with the functional API](https://keras.io/getting-started/functional-api-guide)
108 109

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.
F
Francois 已提交
110 111


F
Francois Chollet 已提交
112
------------------
F
Francois 已提交
113 114 115 116


## Installation

117
Before installing Keras, please install one of its backend engines: TensorFlow, Theano, or CNTK. We recommend the TensorFlow backend.
F
Francois 已提交
118

119 120 121
- [TensorFlow installation instructions](https://www.tensorflow.org/install/).
- [Theano installation instructions](http://deeplearning.net/software/theano/install.html#install).
- [CNTK installation instructions](https://docs.microsoft.com/en-us/cognitive-toolkit/setup-cntk-on-your-machine).
F
Francois 已提交
122

123
You may also consider installing the following **optional dependencies**:
F
Francois Chollet 已提交
124

125 126 127
- cuDNN (recommended if you plan on running Keras on GPU).
- HDF5 and h5py (required if you plan on saving Keras models to disk).
- graphviz and pydot (used by [visualization utilities](https://keras.io/visualization/) to plot model graphs).
F
Francois Chollet 已提交
128

129
Then, you can install Keras itself. There are two ways to install Keras:
F
Francois Chollet 已提交
130

131
- **Install Keras from PyPI (recommended):**
C
Cheng Tang 已提交
132

133 134 135 136 137 138 139 140 141
```sh
sudo pip install keras
```

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

```sh
pip install keras
```
C
Cheng Tang 已提交
142

143
- **Alternatively: install Keras from the Github source:**
144

145
First, clone Keras using `git`:
146

147
```sh
148
git clone https://github.com/fchollet/keras.git
149
```
F
Francois 已提交
150

151
 Then, `cd` to the Keras folder and run the install command:
152
```sh
153 154
cd keras
sudo python setup.py install
F
fchollet 已提交
155 156
```

F
Francois Chollet 已提交
157 158 159
------------------


C
Cheng Tang 已提交
160
## Switching from TensorFlow to CNTK or Theano
F
Francois Chollet 已提交
161

A
Alan Yee 已提交
162
By default, Keras will use TensorFlow as its tensor manipulation library. [Follow these instructions](https://keras.io/backend/) to configure the Keras backend.
F
Francois Chollet 已提交
163 164 165 166 167 168

------------------


## Support

F
fchollet 已提交
169 170 171
You can ask questions and join the development discussion:

- On the [Keras Google group](https://groups.google.com/forum/#!forum/keras-users).
F
Francois Chollet 已提交
172
- 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.
F
Francois Chollet 已提交
173

F
Francois Chollet 已提交
174
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.
F
Francois Chollet 已提交
175 176 177 178 179


------------------


F
Francois 已提交
180 181
## Why this name, Keras?

F
fchollet 已提交
182
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).
F
Francois 已提交
183

F
Francois Chollet 已提交
184
Keras was initially developed as part of the research effort of project ONEIROS (Open-ended Neuro-Electronic Intelligent Robot Operating System).
F
Francois 已提交
185

F
fchollet 已提交
186
>_"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).
F
Francois Chollet 已提交
187

F
Fariz Rahman 已提交
188
------------------