api.md 2.7 KB
Newer Older
Y
Yi Wang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
# Design Doc: PaddlePaddle API

## 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

GradientMachine = {Model*, gradients}
- 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

We used to think that parameters are part of the toplogy (or layers).
But that is not true, because multiple layers could share the same
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
doens't make sense for each of these threads to have its own copy of
model, because that would duplicate topologies and parameters.
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)
				e.activation(layer="output")) # returns activations of layer "output"
```

### 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
multiple simultaneious threads to train a model -- just let each
thread has its own GradientMachine object.