-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnn.py
29 lines (24 loc) · 877 Bytes
/
nn.py
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
"""
A NeuralNet is jus a collection of layers.
It behaves a lot like a layer itself, although
we're not going make it one.
"""
from typing import Sequence, Iterator, Tuple
from vsdl.tensor import Tensor
from vsdl.layers import Layer
class NeuralNet:
def __init__(self, layers: Sequence[Layer]) -> None:
self.layers = layers
def forward(self, inputs: Tensor) -> Tensor:
for layer in self.layers:
inputs = layer.forward(inputs)
return inputs
def backward(self, grad: Tensor) -> Tensor:
for layer in reversed(self.layers):
grad = layer.backward(grad)
return grad
def params_and_grads(self) -> Iterator[Tuple[Tensor, Tensor]]:
for layer in self.layers:
for name, param in layer.params.items():
grad = layer.grads[name]
yield param, grad