api.md 8.6 KB
Newer Older
Y
Yi Wang 已提交
1
# PaddlePaddle API
Y
Yi Wang 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

## Ingredients

As the first step of our design, we list important concepts in deep
learning and try to figure their relationship, as shown below:

```
Model = {topology, parameters}

Evaluator = {Model*, activations}
- forward
- test

15
GradientMachine = {Evaluator*, gradients}
Y
Yi Wang 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29
- backward

Optimizer = {Model*, Evaluator*, GradientMachine*}
- train
- update
- checkpoint
```

where the pair of curly braces `{` and `}` indicate *composition*, `*`
indicates a *reference*, and `-` marks a "class method".


### Model

Y
Yi Wang 已提交
30 31
We used to think that parameters are part of the topology (or layers).
But that is not true because multiple layers could share the same
Y
Yi Wang 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
parameter matrix.  An example is a network that compares two text
segments in a semantic space:

```
          semantic
text A -> projection ---\
          layer A        \
                          cosine
                          similarity -> output
                          layer
          semantic       /
text B -> projection ---/
          layer B
```

In this network, the two semantic projection layers (A and B) share
the same parameter matrix.

For more information about our API that specifies topology and
parameter sharing, please refer to [TODO: API].


### Evaluator

Supposed that we have a trained ranking model, we should be able to
use it in our search engine.  The search engine's Web server is a
concurrent program so to serve many HTTP requests simultaneously.  It
Y
Yi Wang 已提交
59
doesn't make sense for each of these threads to have its own copy of the model because that would duplicate topologies and parameters.
Y
Yi Wang 已提交
60 61 62 63 64 65 66 67 68 69 70 71
However, each thread should be able to record layer outputs, i.e.,
activations, computed from an input, derived from the request.  With
*Evaluator* that saves activations, we can write the over-simplified
server program as:

```python
m = paddle.model.load("trained.model")

http.handle("/",
            lambda req:
                e = paddle.evaluator.create(m)
                e.forward(req)
Y
Yi Wang 已提交
72
                e.activation(layer="output")) # returns activations of layer "output"
Y
Yi Wang 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
```

### GradientMachine

Similar to the evaluation, the training needs to compute gradients so
to update model parameters.  Because an [optimizer](#optimizer) might
run multiple simultaneous threads to update the same model, gradients
should be separated from the model.  Because gradients are only used
in training, but not serving, they should be separate from Evaluator.
Hence the `GradientMachine`.

### Optimizer

None of Model, Evaluator, nor GradientMachine implements the training
loop, hence Optimizer.  We can define a concurrent optimizer that runs
Y
Yi Wang 已提交
88
multiple simultaneous threads to train a model -- just let each
Y
Yi Wang 已提交
89
thread has its own GradientMachine object.
Y
Yi Wang 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

Most models should be able to be trained using the
`paddle.optimizer.SGD` by calling its `train` method.  Many
customizations to the SGD algorithm happens with the update equation,
e.g., momentum and the Adam SGD algorithm.  We make `train` calls
`update` to do an update, so that we can derive a `paddle.optimizer.Adam`
from `paddle.optimizer.SGD` by overrides only the `update` method.


## Programming

A fictive example of PaddlePaddle program looks like the following:

```python
import paddle

def read(args):
    f = open_file(args["filename"])
    mb = read_a_minibatch(f)
    end_pass = eof(f)
    if end_pass:
       f = open_file(args["filename"]) # rewind for reading again
    yield mb, end_pass

input = paddle.layer.data(...)
intermediate = paddle.layers.fc(input)
output = paddle.layer.softmax(intermediate)

model = paddle.model.create(output)

paddle.train(model, data_provider=read)
```

This shows some important part of a program:

1. Define how to read (and augment) data by defining a function, in
   this example, `read`, that `yields` a minibatch and a boolean flag
   `eof_of_pass`.

1. Define the topology, `input`, `intermediate`, and `output` in this
   example.

1. Create parameters from the topology thus forms the model by calling
   `paddel.model.create`.

1. Train the model by calling `paddle.train`.


### Reader

Not all programming frameworks allow users to define I/O functions.
An example is Google MapReduce, which can only read from text,
SSTable, and RecordIO files.  Hadoop MapReduce allows users to define
readers and writers by deriving from base classes `Reader` and
`Writer`.  The former is less flexible but also less error-prone.  We
decide to provide the flexibility to users to define their readers.


#### A Synthetic Data Reader

Sometimes we want to test a topology and/or a training algorithm using
synthetic data.  We can do this by defining the reader a synthesizer:

```python
def read(args):
    x = sample_from_uniform(0.0, 1.0)
    y = sample_from_gauss(2 * x, sigma)
    yield {x, y}, False # no end-of-file so no end-of-pass
```

#### A Reader for Online Learning

Readers can also read an infinite data stream, e.g., a log stream from
a search engine and collected by Kafka:

```python
def read(args):
    log_stream = kafka.open_channel(args["kafka channel name"])
    yeild log_stream.read(), False # no end-of-pass in online learning
```

### Topology

By default, layers don't have names.  But if we want to refer to a
layer later some time, for example, when we do serving using the model
and wants activations/outputs of a layer, we should give it a name.

```python
input = paddle.layer.data(...)
intermediate = paddle.layer.fc(input, name="inter", ...)
output = paddle.layer.softmax(intermediate, name="output", ...)

m = paddle.model.create(output)
e = paddle.evaluator.create(model)
e.forward(read_an_input()) # compute activations of all layers.
print e.activations(layer="inter")  # retrieve the activations of layer "inter"
print e.activations(layer="output") # retrieve the activations of layer "output"
```

#### Sharing Parameters

In [above section](#model) we shows a network whose two layers share
the same parameter matrix.  To specify such cases, we give "parameter
names" to layers.  If some layers have the same paraemter names,
`paddle.model.create` creates a single parameter matrix for these
layers:

```python
text1 = paddle.layer.data(...)
sematic1 = paddle.layer.fc(text1, ..., parameter_name="sematic_projection")
text2 = paddle.layer.data(...)
sematic2 = paddle.layer.fc(text2, ..., parameter_name="sematic_projection")
out = paddle.layer.cosine(semantic1, semantic2)
```

We can also share parameter matrices between layers in different
models.  To do this, we need an additional parameter that refers to a
model:

```python
model1_input = paddle.layer.data(...)
model1_output = paddle.layer.softmax(model1_input, ...,
                                     parameter_name="a_parameter_matrix")
model1 = paddle.model.create(model1_output)

# Another model
model2_semantic = paddle.layer.fc(text2, ...,
                                  parameter_name="a_parameter_matrix",
                                  parameter_model=model1)
```

### Training

The recommended way to training a model is to call `paddle.train`,
which simply calls `paddle.optimizer.Default`, a global variable of
type `paddle.optimizer.SGD`.  Equivalently, we can do

```python
opt = paddle.optimizer.SGD(...)
opt.train(model, reader=read, ...)
```

#### Distributed Training

If users want to do distributed training on a cluster, s/he should
call `paddle.dist_train` and provides access tokens to the cluster as
a parameter.

For example, if the user has a TLS certificate that allows him to
access a Kubernetes cluster, s/he should be able to call

```python
paddle.dist_train(model,
                  reader=read,
                  optimizer=paddle.optimizer.SGDOptimizer(...),
                  k8s_user="yi",
                  k8s_token="kube_cluster_tls.pem",
                  k8s_job="hello",
                  num_parameter_servers=15)
```

The pseudo code if `paddle.dist_train` is as follows:

```python
def dist_train():
    if os.getenv("KUBERNETES_SERVICE_HOST") == None:
        image_name = k8s_user + '/' + k8s_job
        docker_build(image_name)
        docker_push()
        kube_ctrl_start_job(image_name, k8s_user, k8s_token)
    else:
        rank = kube_list_containers_in_job_and_return_current_containers_rank()
        if rank == 0:
            master()
        elif rank < 15:
            parameter_server()
        else:
            optimizer.train(model, reader=read)
```

Please be aware that if a process is running on the Kubernetes
cluster, it will have some environment variables pre-defined.

If `dist_train` doesn't see these environment variables, it knowns
that it's running on users' personal computer, and it should work as a
*launcher*.  Otherwise, it knows that it's running on the cluster and
need to figure out its role as either the master, or a trainer, or a
parameter server.