-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrainer.py
executable file
·49 lines (37 loc) · 1.12 KB
/
trainer.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np
import mlp
import pca
X = np.load('dataset_X.npy')
Y = np.load('dataset_Y.npy')
cnt = np.empty((X.shape[0], 1))
for idx, x in enumerate(X):
cnt[idx, 0] = sum(x > 127)/len(x) # last column
X = np.append(X, cnt, axis=1)
train_size = int(0.8 * X.shape[0])
shuffle_ids = np.arange(X.shape[0])
np.random.seed(666)
np.random.shuffle(shuffle_ids)
X = X[shuffle_ids]
Y = Y[shuffle_ids]
train_X = X[:train_size]
train_Y = Y[:train_size]
test_X = X[train_size:]
test_Y = Y[train_size:]
train_X, test_X = pca.pca(train_X, test_X, numpc=40)
model = mlp.MLP([train_X.shape[1], 20, train_Y.shape[1]])
model.train(train_X, train_Y)
print('finished training')
correct = 0
for idx, x, expected in zip(range(test_X.shape[0]), test_X, test_Y):
obtained = model.solve(x)
obtained = np.argmax(obtained)
expected = np.argmax(expected)
if obtained == expected:
correct += 1
if idx % 50 == 0:
print('%6.2f%%' % (idx/len(test_X) * 100))
model.save('%f.mlp' % (correct/test_X.shape[0]))
print()
print('Result: ', correct/len(test_X) * 100, '%', sep='')