generated from minitorch/Module-0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_manual.py
49 lines (39 loc) · 1.28 KB
/
run_manual.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
Be sure you have minitorch installed in you Virtual Env.
>>> pip install -Ue .
"""
import random
import minitorch
class Network(minitorch.Module):
def __init__(self):
super().__init__()
self.linear = Linear(2, 1)
def forward(self, x):
y = self.linear(x)
return minitorch.operators.sigmoid(y[0])
class Linear(minitorch.Module):
def __init__(self, in_size, out_size):
super().__init__()
random.seed(100)
self.weights = []
self.bias = []
for i in range(in_size):
weights = []
for j in range(out_size):
w = self.add_parameter(f"weight_{i}_{j}", 2 * (random.random() - 0.5))
weights.append(w)
self.weights.append(weights)
for j in range(out_size):
b = self.add_parameter(f"bias_{j}", 2 * (random.random() - 0.5))
self.bias.append(b)
def forward(self, inputs):
y = [b.value for b in self.bias]
for i, x in enumerate(inputs):
for j in range(len(y)):
y[j] = y[j] + x * self.weights[i][j].value
return y
class ManualTrain:
def __init__(self, hidden_layers):
self.model = Network()
def run_one(self, x):
return self.model.forward((x[0], x[1]))