Skip to content

Commit 5b459da

Browse files
committed
adding comments for SequentialLayers
1 parent 3edd12c commit 5b459da

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

docs/mnn.seq_layers.md

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<!-- markdownlint-disable -->
2+
3+
<a href="../mnn/seq_layers.py#L0"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
4+
5+
# <kbd>module</kbd> `mnn.seq_layers`
6+
7+
8+
9+
10+
11+
12+
---
13+
14+
<a href="../mnn/seq_layers.py#L5"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
15+
16+
## <kbd>class</kbd> `SequentialLayers`
17+
## Background knowledge
18+
19+
Each layer $f$ in neural network is just a function mapping from $f: \mathbb{R}^m \rightarrow \mathbb{R}^n $.
20+
21+
Without loss of generality, suppose $z(t) = f(x(t), y(t))$, we can show:
22+
23+
$$ \begin{aligned} z'(t) &= \lim_{dt \to 0} \frac{f(x(t+dt), y(t+dt)) - f(x(t), y(t))}{dt} \\\\ &= \lim_{dt \to 0} \frac{ f(x(t+dt), y(t+dt)) - f(x(t+dt), y(t)) + f(x(t+dt), y(t)) - f(x(t), y(t)) }{dt} \\\\ &= \lim_{dt \to 0} \frac{f(x(t+dt), y(t+dt)) - f(x(t+dt), y(t))}{dt} + \lim_{dt \to 0} \frac{f(x(t+dt), y(t)) - f(x(t), y(t))}{dt} \\\\ &= \lim_{dt \to 0} \frac{f(x(t+dt), y(t+dt)) - f(x(t+dt), y(t))} {y(t+dt) - y(t)} \times \frac{y(t+dt) - y(t)}{dt} \\\\ &+ \lim_{dt \to 0} \frac{f(x(t+dt), y(t)) - f(x(t), y(t))} {x(x+dt) - x(t)} \times \frac{x(x+dt) - x(t)}{dt} \\\\ &\doteq \lim_{dt \to 0} \frac{f(x(t+dt), y(t) + \Delta y) - f(x(t+dt), y(t))} {\Delta y} \times \frac{y(t+dt) - y(t)}{dt} \\\\ &+ \lim_{dt \to 0} \frac{f(x(t) + \Delta x, y(t)) - f(x(t), y(t))} {\Delta x} \times \frac{x(x+dt) - x(t)}{dt} \\\\ &= \frac{\partial z}{\partial y} \times \frac{\partial y}{\partial t} + \frac{\partial z}{\partial x} \times \frac{\partial x}{\partial t} \end{aligned} $$
24+
25+
<a href="../mnn/seq_layers.py#L25"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
26+
27+
### <kbd>method</kbd> `__init__`
28+
29+
```python
30+
__init__(layers)
31+
```
32+
33+
34+
35+
36+
37+
38+
39+
40+
---
41+
42+
<a href="../mnn/seq_layers.py#L36"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
43+
44+
### <kbd>method</kbd> `backward`
45+
46+
```python
47+
backward(debug=False)
48+
```
49+
50+
## Gradients w.r.t. $W$
51+
52+
---
53+
54+
<a href="../mnn/seq_layers.py#L50"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
55+
56+
### <kbd>method</kbd> `step`
57+
58+
```python
59+
step()
60+
```
61+
62+
63+
64+
65+
66+

gen_docs.py

+2
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@
1616
continue
1717
modules.append(f'mnn.layer.{layer}')
1818

19+
modules.append('mnn.seq_layers')
20+
1921
generate_docs(modules, output_path=docs,
2022
watermark=False, remove_package_prefix=True)

mnn/seq_layers.py

+22
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,25 @@
33

44

55
class SequentialLayers():
6+
r'''
7+
## Background knowledge
8+
9+
Each layer $f$ in neural network is just a function mapping from
10+
$f: \mathbb{R}^m \rightarrow \mathbb{R}^n $.
11+
12+
Without loss of generality, suppose $z(t) = f(x(t), y(t))$, we can show:
13+
14+
$$
15+
\begin{aligned}
16+
z'(t) &= \lim_{dt \to 0} \frac{f(x(t+dt), y(t+dt)) - f(x(t), y(t))}{dt} \\\\
17+
&= \lim_{dt \to 0} \frac{ f(x(t+dt), y(t+dt)) - f(x(t+dt), y(t)) + f(x(t+dt), y(t)) - f(x(t), y(t)) }{dt} \\\\
18+
&= \lim_{dt \to 0} \frac{f(x(t+dt), y(t+dt)) - f(x(t+dt), y(t))}{dt} + \lim_{dt \to 0} \frac{f(x(t+dt), y(t)) - f(x(t), y(t))}{dt} \\\\
19+
&= \lim_{dt \to 0} \frac{f(x(t+dt), y(t+dt)) - f(x(t+dt), y(t))} {y(t+dt) - y(t)} \times \frac{y(t+dt) - y(t)}{dt} \\\\ &+ \lim_{dt \to 0} \frac{f(x(t+dt), y(t)) - f(x(t), y(t))} {x(x+dt) - x(t)} \times \frac{x(x+dt) - x(t)}{dt} \\\\
20+
&\doteq \lim_{dt \to 0} \frac{f(x(t+dt), y(t) + \Delta y) - f(x(t+dt), y(t))} {\Delta y} \times \frac{y(t+dt) - y(t)}{dt} \\\\ &+ \lim_{dt \to 0} \frac{f(x(t) + \Delta x, y(t)) - f(x(t), y(t))} {\Delta x} \times \frac{x(x+dt) - x(t)}{dt} \\\\
21+
&= \frac{\partial z}{\partial y} \times \frac{\partial y}{\partial t} + \frac{\partial z}{\partial x} \times \frac{\partial x}{\partial t} \end{aligned}
22+
$$
23+
24+
'''
625
def __init__(self, layers):
726
self.layers = layers
827

@@ -15,6 +34,9 @@ def __call__(self, inputs, targets=None, debug=False):
1534
return v
1635

1736
def backward(self, debug=False):
37+
r'''
38+
## Gradients w.r.t. $W$
39+
'''
1840
gradients = []
1941
for layer in reversed(self.layers):
2042
if len(gradients) == 0:

0 commit comments

Comments
 (0)