-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspiral.py
62 lines (52 loc) · 1.7 KB
/
spiral.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
import math
import numpy as np
import matplotlib.pyplot as plt
import dezero
from dezero import optimizers
import dezero.functions as F
from dezero.models import MLP
# Hyperparameters
max_epoch = 300
batch_size = 30
hidden_size = 10
lr = 1.0
x, t = dezero.datasets.get_spiral(train=True)
model = MLP((hidden_size, 3))
optimizer = optimizers.SGD(lr).setup(model)
data_size = len(x)
max_iter = math.ceil(data_size / batch_size) # 마지막 batch가능하게 반올림
for epoch in range(max_epoch):
index = np.random.permutation(data_size) # 데이터 shuffle
sum_loss = 0
for i in range(max_iter):
batch_index = index[i * batch_size:(i + 1) * batch_size]
batch_x = x[batch_index]
batch_t = t[batch_index]
y = model(batch_x)
loss = F.softmax_cross_entropy_simple(y, batch_t)
model.cleargrads()
loss.backward()
optimizer.update()
sum_loss += float(loss.data) * len(batch_t)
# Print loss every epoch
avg_loss = sum_loss / data_size
print('epoch %d, loss %.2f' % (epoch + 1, avg_loss))
# Plot boundary area the model predict
h = 0.001
x_min, x_max = x[:, 0].min() - .1, x[:, 0].max() + .1
y_min, y_max = x[:, 1].min() - .1, x[:, 1].max() + .1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
X = np.c_[xx.ravel(), yy.ravel()]
with dezero.no_grad():
score = model(X)
predict_cls = np.argmax(score.data, axis=1)
Z = predict_cls.reshape(xx.shape)
plt.contourf(xx, yy, Z)
# Plot data points of the dataset
N, CLS_NUM = 100, 3
markers = ['o', 'x', '^']
colors = ['orange', 'blue', 'green']
for i in range(len(x)):
c = t[i]
plt.scatter(x[i][0], x[i][1], s=40, marker=markers[c], c=colors[c])
plt.show()