-
Notifications
You must be signed in to change notification settings - Fork 0
/
step59.py
74 lines (59 loc) · 1.68 KB
/
step59.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
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
if '__file__' in globals():
import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
import numpy as np
import matplotlib.pyplot as plt
import dezero
from dezero import Model
import dezero.functions as F
import dezero.layers as L
# Hyperparameters
max_epoch = 100
hidden_size = 100
bptt_length = 30
train_set = dezero.datasets.SinCurve(train=True)
seqlen = len(train_set)
class SimpleRNN(Model):
def __init__(self, hidden_size, out_size):
super().__init__()
self.rnn = L.RNN(hidden_size)
self.fc = L.Linear(out_size)
def reset_state(self):
self.rnn.reset_state()
def __call__(self, x):
h = self.rnn(x)
y = self.fc(h)
return y
model = SimpleRNN(hidden_size, 1)
optimizer = dezero.optimizers.Adam().setup(model)
# Start training.
for epoch in range(max_epoch):
model.reset_state()
loss, count = 0, 0
for x, t in train_set:
x = x.reshape(1, 1)
y = model(x)
loss += F.mean_squared_error(y, t)
count += 1
if count % bptt_length == 0 or count == seqlen:
model.cleargrads()
loss.backward()
loss.unchain_backward()
optimizer.update()
avg_loss = float(loss.data) / count
print('| epoch %d | loss %f' % (epoch + 1, avg_loss))
# Plot
xs = np.cos(np.linspace(0, 4 * np.pi, 1000))
model.reset_state()
pred_list = []
with dezero.no_grad():
for x in xs:
x = np.array(x).reshape(1, 1)
y = model(x)
pred_list.append(float(y.data))
plt.plot(np.arange(len(xs)), xs, label='y=cos(x)')
plt.plot(np.arange(len(xs)), pred_list, label='predict')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()