-
Notifications
You must be signed in to change notification settings - Fork 0
/
play.mojo
138 lines (104 loc) · 4.52 KB
/
play.mojo
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from engine import Value
from nn import MLP
from python import Python, PythonObject
fn py_to_f32(x: PythonObject) -> Float32:
var f64: Float64 = x.to_float64()
return f64.cast[DType.float32]()
fn mojo_pls_make_moons(n_samples: Int = 100) raises -> (Pointer[Pointer[Pointer[Value]]], Pointer[Pointer[Value]]):
var sklearn = Python.import_module("sklearn.datasets")
var out = sklearn.make_moons(n_samples, noise = 0.1)
var npy_X = out[0]
var npy_y = out[1]
# in testing for one sample it was Pointer[Pointer[Value]]
var X = Pointer[Pointer[Pointer[Value]]].alloc(n_samples)
for i in range(n_samples):
var tmp_sample_ptr: Pointer[Pointer[Value]] = Pointer[Pointer[Value]].alloc(2)
for j in range(2): # for X out points
var tmp_x: Float32 = py_to_f32(npy_X[i][j])
var tmp_val: Value = Value(tmp_x)
var tmp_ptr: Pointer[Value] = Pointer[Value].alloc(1)
tmp_ptr.store(tmp_val)
tmp_sample_ptr.store(j, tmp_ptr)
X.store(i, tmp_sample_ptr)
var y: Pointer[Pointer[Value]] = Pointer[Pointer[Value]].alloc(n_samples)
for i in range(n_samples):
var tmp_ptr: Pointer[Value] = Pointer[Value].alloc(1)
var tmp_y: Float32 = py_to_f32(npy_y[i])
var tmp_val: Value = Value(tmp_y * 2 - 1)
tmp_ptr.store(tmp_val)
y.store(i, tmp_ptr)
return X, y
fn train(inout model: MLP, inout X: Pointer[Pointer[Pointer[Value]]], inout y: Pointer[Pointer[Value]], inout n_samples: Int, inout epochs: Int):
var const: Float32 = 1
var alpha: Float32 = 1e-4
for i in range(epochs):
# Forward Pass
var score_ptr: Pointer[Pointer[Value]] = Pointer[Pointer[Value]].alloc(n_samples)
for idx in range(n_samples):
var tmp_x = X.load(idx)
# print(tmp_x.load(0).load().data.load())
# print(tmp_x.load(1).load().data.load())
# print("----")
var out_ptr: Pointer[Pointer[Value]] = model.forward(tmp_x)
# print("out score:", out_ptr.load().load().data.load())
score_ptr.store(idx, out_ptr.load())
var loss: Value = Value(0)
for idx in range(n_samples):
var tmp_score: Value = score_ptr.load(idx).load()
var tmp_y: Value = y.load(idx).load()
# print("Score: ", tmp_score.data.load())
var tmp = -tmp_y
tmp = tmp * tmp_score
tmp = tmp + const
# print("Before relu: ",tmp.data.load())
tmp = tmp.relu()
# print("After relu: ",tmp.data.load())
loss = loss + tmp
var len = Float32(n_samples)
var data_loss: Value = loss / len
var loss_p: Value = Value(0)
for item in model.parameters():
var tmp_val: Value = item[].load()
tmp_val = tmp_val * tmp_val
loss_p = loss_p + tmp_val
var reg_loss: Value = loss_p * alpha
var total_loss: Value = data_loss + reg_loss
var accuracy: Float32 = 0
for idx in range(n_samples):
var tmp_score: Value = score_ptr.load(idx).load()
var tmp_y: Value = y.load(idx).load()
# print("tmp_score:", tmp_score.data.load())
# print("tmp_y:", tmp_y.data.load())
var check1: Bool = tmp_score.data.load() > 0
var check2: Bool = tmp_y.data.load() > 0
var check3: Bool = check1 == check2
if check3:
accuracy += 1
accuracy = accuracy / n_samples
print("Epochs: ",i," | loss - ",total_loss.data.load()," | acc - ",accuracy * 100)
# Backward Pass
model.zero_grad()
total_loss.backward()
var learning_rate: Float32 = 1.0 - 0.9*i/100
# parameter update
for item in model.parameters():
var tmp_val: Value = item[].load() # this loads a copy of the value
# print('Grad: ', tmp_val.grad.load())
var update: Float32 = tmp_val.data.load() - tmp_val.grad.load() * learning_rate
item[].load().data.store(update)
score_ptr.free()
fn main():
try:
var out = mojo_pls_make_moons(100)
var X = out[0]
var y = out[1]
# print(X)
# print(y)
var nin: Int = 2
var nouts: List[Int] = List[Int](16, 16, 1)
var mlp: MLP = MLP(nin, nouts)
var n_samples: Int = 100
var epochs: Int = 100
train(mlp, X, y, n_samples, epochs)
except:
print("In except")