-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodules.py
More file actions
42 lines (32 loc) · 1.14 KB
/
modules.py
File metadata and controls
42 lines (32 loc) · 1.14 KB
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
from tensor import Tensor
import numpy as np
class Module: # Every lego piece fits like this, must have a set of parameters, able to zer_grad and a calling mechanism
def parameters(self):
return []
def zero_grad(self):
for p in self.parameters():
p.zero_grad()
def __call__(self, *args, **kwargs):
return self.forward(*args, **kwargs)
class Sequential(Module):
def __init__(self, layers):
self.layers = layers
def forward(self, x: Tensor):
for layer in self.layers: # Iterate the input through each layer
x = layer(x)
return x
def parameters(self):
params = []
for layer in self.layers:
params.extend(layer.parameters())
return params
class Linear(Module): # Linear layer
def __init__(self, in_features, out_features):
self.W = Tensor(np.random.randn(in_features, out_features) * 0.01)
self.b = Tensor(np.zeros(out_features))
def forward(self, x: Tensor):
out = x @ self.W
out = out + self.b
return out
def parameters(self):
return [self.W, self.b]