-
-
Notifications
You must be signed in to change notification settings - Fork 292
/
step44.py
executable file
·39 lines (29 loc) · 712 Bytes
/
step44.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
if '__file__' in globals():
import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
import numpy as np
import dezero.functions as F
import dezero.layers as L
np.random.seed(0)
x = np.random.rand(100, 1)
y = np.sin(2 * np.pi * x) + np.random.rand(100, 1)
l1 = L.Linear(10)
l2 = L.Linear(1)
def predict(x):
y = l1(x)
y = F.sigmoid(y)
y = l2(y)
return y
lr = 0.2
iters = 10000
for i in range(iters):
y_pred = predict(x)
loss = F.mean_squared_error(y, y_pred)
l1.cleargrads()
l2.cleargrads()
loss.backward()
for l in [l1, l2]:
for p in l.params():
p.data -= lr * p.grad.data
if i % 1000 == 0:
print(loss)