-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
64 lines (54 loc) · 1.59 KB
/
test.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
import cupy as cp
import numpy as np
from sklearn.datasets import load_digits
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import OneHotEncoder
from sklearn.model_selection import train_test_split
from patrick.nn import NN as nn
from patrick.losses import mse_loss
from patrick.activations import leaky_relu
from patrick.layers import FCLayer as linear
"""
train test split
"""
data_bunch = load_digits()
x_train, y_train = cp.array(data_bunch.data), cp.array(data_bunch.target)
x_train, x_test, y_train, y_test = train_test_split(x_train, y_train, test_size = 0.1,shuffle = True)
"""
one hot encoding
"""
encoder = OneHotEncoder()
y_train = cp.array(encoder.fit_transform(y_train.get().reshape(-1,1)).toarray())
# print(x_train.shape, y_train.shape)
"""
split into batches
"""
batch_size = 7
x_train = x_train.reshape(x_train.shape[0]//batch_size, batch_size, x_train.shape[1]) /16.0
y_train = y_train.reshape(y_train.shape[0]//batch_size, batch_size, 10)
"""
model
"""
class model(nn):
def __init__(self):
self.layers = [
linear(64,150),
leaky_relu(),
linear(150, 100),
leaky_relu(),
linear(100, 52),
leaky_relu(),
linear(52,10)
]
net = model()
"""
train
"""
net.fit(x_train, y_train, epochs=60, learning_rate=0.005, loss = mse_loss)
"""
test
"""
out = net(x_test/16.0).get()
out = np.array([np.argmax(i) for i in out]).astype(np.uint8)
labels = y_test.get().flatten()
print("Accuracy:", accuracy_score( labels, out))