-
Notifications
You must be signed in to change notification settings - Fork 0
/
handwriting_Linear.py
executable file
·70 lines (55 loc) · 1.43 KB
/
handwriting_Linear.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
#!/usr/local/bin/python3
#p.139
import numpy as np
import chainer.functions as F
import chainer.links as L
from chainer import Variable, optimizers
from chainer import Chain #from chainer import FunctionSet
import matplotlib.pyplot as plt
from sklearn import datasets
MNIST = datasets.load_digits()
X = MNIST.data.astype(np.float32)
t = MNIST.target
print(X.shape)
print(t)
#plt.imshow(X[0,:].reshape(8,8))
#plt.show()
[N,M] = X.shape #N: number of source, M: number of 2D space
C = np.max(t)+1 #number of character kind
target = np.zeros([N,C]).astype(np.float32)
for i in range(N):
target[i,t[i]] = 1.0
Ntrain = 1000
Ntest = N - Ntrain
index = np.random.permutation(range(N))
xtrain = X[index[0:Ntrain],:]
print(xtrain.shape)
ytrain = target[index[0:Ntrain],:]
xtest = X[index[Ntrain:N],:]
yans = target[index[Ntrain:N]]
NNset = Chain(l1=L.Linear(M,20),l2=L.Linear(20,C))
def model(x):
h = F.sigmoid(NNset.l1(x))
y = F.sigmoid(NNset.l2(h))
return y
x = Variable(xtrain)
t = Variable(ytrain)
optimizer = optimizers.Adam()
optimizer.setup(NNset)
Tall = 1000
train_loss = []
for i in range(Tall):
NNset.cleargrads()
y = model(x)
loss = F.mean_squared_error(y,t)
loss.backward()
optimizer.update()
train_loss.append(loss.data)
plt.figure(figsize=(8,6))
plt.plot(range(Tall),train_loss)
plt.title('optimizatin vol4')
plt.xlabel('step')
plt.ylabel('loss function')
plt.xlim([0,Tall])
plt.ylim([0,0.5])
plt.show()