-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
39 lines (27 loc) · 893 Bytes
/
Copy pathtrain.py
File metadata and controls
39 lines (27 loc) · 893 Bytes
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
from engine import Value
from neuron import Neuron
from layer import Layer
from loss_functions import MSELoss
from optimizer import SGD
from MLP import MLP
from LRScheduler import StepLRScheduler
import random
mlp = MLP(4, [128, 64, 1])
loss_fn = MSELoss()
scheduler = StepLRScheduler(lr=0.1)
optimizer = SGD(mlp.parameters(), scheduler = scheduler)
train_X = [[Value(random.uniform(-1,1)) for _ in range(4)] for _ in range(10)]
train_Y = [[Value(random.uniform(-1,1))] for _ in range(10)]
epochs = 30
for epoch in range(epochs):
total_loss = 0
for x, y_true in zip(train_X, train_Y):
y_pred = mlp(x)
loss = loss_fn(y_pred, y_true)
total_loss += loss.data
optimizer.zero_grad()
loss.backward()
optimizer.step()
avg_loss = total_loss / len(train_X)
print(f"Epoch {epoch + 1}/{epochs}, Loss: {avg_loss:.4f}")
print(mlp)