Skip to content

Commit 381ce0e

Browse files
authored
Merge pull request #7437 from reyoung/feature/add_design_param_field_to_layers_output
Extend return value for layer functions
2 parents a795a0d + 24cde57 commit 381ce0e

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

doc/design/python_api.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,26 @@ class LayerHelper(object):
279279
return tmp
280280
```
281281

282+
### Return value of layer functions
283+
284+
The layer will return a Variable, which is also the output of an operator. However, outputs of a layer function have more attributes than an operator. There are parameter variables, and their gradient variables need to return. To return them is useful. For example,
285+
286+
1. Users can debug the network by printing parameter gradients.
287+
2. Users can append attributes to a parameter, such as, `param.stop_gradient=True` will make a parameter stop generate the gradient. We can fix the parameter value during training by using this attribute.
288+
289+
However, it is good to return a Variable for layers, since all layers and operators use Variables as their parameters. We can just append a `param` field and a `grad` field for layer function since the Python is dynamic typing.
290+
291+
The sample usage is
292+
293+
```python
294+
data = fluid.layers.data(...)
295+
hidden = fluid.layers.fc(data, ...)
296+
...
297+
298+
executor.run(fetch_list=[hidden.param, hidden.param.grad], ...)
299+
```
300+
301+
282302
## Optimizer
283303

284304
[Optimizer Design Doc](./optimizer.md)

0 commit comments

Comments
 (0)