-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
167 lines (146 loc) · 5.57 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import numpy as np
import sys
sys.path.append('NN/')
import time
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import cyNeural as cyNN
def test_xor():
epoch = 1000
structure = np.array([2, 2, 1], dtype = np.int32)
inputs = [[0,0],
[1,0],
[0,1],
[1,1]]
labels = [0, 1, 1, 0]
activations = np.array(["sigmoid"]*len(structure), dtype=str)
network = cyNN.network(structure, activations, eta = 2., w_init = 1.)
network.print_network()
start = time.time()
network.train(inputs, labels, epoch)
print(f'Elapsed time for training: {time.time() - start} seconds')
network.get_train_error(epoch)
errors = network.get_train_error(epoch)
fig, ax = plt.subplots(1,2, figsize = (10,4))
ax[0].plot(errors)
ax[0].set_title('error per epoch')
x_arr = np.linspace(0,1,100)
y_arr = np.linspace(0,1,100)
X, Y = np.meshgrid(x_arr, y_arr)
Z = np.empty(X.shape)
for i, x in enumerate(x_arr):
for j, y in enumerate(y_arr):
Z[i][j] = network.predict(np.array([[x,y]]))[0][0]
c = ax[1].pcolormesh(X, Y, Z, shading='auto')
ax[1].set_title('decision boundary')
divider = make_axes_locatable(ax[1])
cax = divider.append_axes('right', size='5%', pad=0.05)
fig.colorbar(c, cax = cax)
plt.suptitle('show results')
plt.show()
def MONK_test():
train = np.loadtxt('data/monks-1.train', usecols = np.arange(7)).astype(float)
train_labels = train[:, 0].reshape((len(train), 1))
test = np.loadtxt('data/monks-1.test', usecols = np.arange(7)).astype(float)
test_labels = test[:, 0].reshape((len(test), 1))
epoch = 1000
structure = np.array([train.shape[1], 3, 1], dtype = np.int32)
activations = np.array(['sigmoid', 'sigmoid', 'sigmoid'])
network = cyNN.network(structure, activations, eta = .01, w_init = .1)
start = time.time()
network.train(train, train_labels, epoch)
print(f'Elapsed time for training: {time.time() - start} seconds')
errors = network.get_train_error(epoch)
pred = network.predict(test)
mean_errors_predict = np.sqrt(np.sum((pred-test_labels)**2))/len(test)
print(mean_errors_predict)
plt.plot(errors)
plt.show()
return
def test_ML_cup():
data=np.loadtxt("data/dataset.csv", delimiter=",")
N_val = int(len(data)/10)
input_data=data[:-N_val,1:-2]
labels=data[:-N_val,-2:]
val_data = data[-N_val:,1:-2]
val_labels=data[-N_val:,-2:]
# Building
epoch = 1000
structure = np.array([input_data.shape[1], 10, 5, 2], dtype = np.int32)
activations = np.array(['sigmoid', 'sigmoid', 'sigmoid', 'linear'])
start = time.time()
network = cyNN.network(structure, activations, eta = .0001, w_init = 0.01, l = 0.0002)
print(f'Time for initialize the net: {time.time()-start} seconds')
start = time.time()
network.train(input_data, labels, epoch)
print(f'Elapsed time for training: {time.time() - start} seconds')
errors = network.get_train_error(epoch)
plt.plot(errors)
plt.yscale('log')
plt.show()
start = time.time()
pred = network.predict(val_data)
print(f'Elapsed time for predict: {time.time() - start} seconds')
e = np.sum((pred-val_labels)**2, axis = 1)
errors_predict = np.sum(np.sqrt(e))/len(pred)
print('error on predicted data:', errors_predict)
def ML_data_understanding():
data=np.loadtxt("data/dataset.csv", delimiter=",")
input_data=data[:,1:-2]
labels=data[:,-2:]
# fig, axs = plt.subplots(input_data.shape[1], 1)
# for feat, ax in zip(input_data.T, axs.flatten()):
# ax.boxplot(feat, vert = False)
# plt.show()
plt.scatter(input_data[:,0], input_data[:, 2], c = labels[:,0])
plt.colorbar()
plt.show()
def ML_validation_check():
data=np.loadtxt("data/dataset.csv", delimiter=",")
N_val = int(len(data)/10)
input_data=data[:-N_val,1:-2]
labels=data[:-N_val,-2:]
val_data = data[-N_val:,1:-2]
val_labels=data[-N_val:,-2:]
epoch = 1000
structure = np.array([input_data.shape[1], 4, 2], dtype = np.int32)
activations = np.array(['sigmoid', 'sigmoid', 'linear'])
val_err = []
err = []
network = cyNN.network(structure, activations, eta = .0001, w_init = .1)
for i in range(epoch):
network.train(input_data, labels, epoch = 1)
err.append(network.get_train_error(1))
pred = network.predict(val_data)
val_err.append(np.sqrt(np.sum((pred-val_labels)**2)))
plt.plot(np.array(err))
plt.plot(np.array(val_err))
plt.show()
return
def test_tick_reg():
epoch = 10000
def f(x, w=1, a = 1, noise = True):
return a*np.sin(w*x) + noise * np.random.rand(len(x)) * a/3
x = np.linspace(0, 2*np.pi, 200)
f_eval = f(x)
inputs = x[::2].reshape((len(x[::2]), 1))
labels = f_eval[::2].reshape((len(x[::2]), 1))
val = x[1::2]
val_label = f(val, noise = False)
structure = np.array([1, 20, 2, 1], dtype = np.int32)
activations = np.array(["sigmoid", 'sigmoid', 'sigmoid', 'linear'], dtype=str)
network = cyNN.network(structure, activations, eta = .004, w_init = 1., l = 0.0004)
network.print_network()
start = time.time()
network.train(inputs, labels, epoch, val, val_label, 1)
errors, val_errors = network.get_train_error(epoch, val = 1)
plt.plot(errors, c = 'blue')
plt.plot(val_errors, c = 'red')
plt.show()
val = val.reshape((len(val), 1))
pred = network.predict(val)
plt.plot(val, pred, c = 'red')
plt.scatter(inputs, labels, c = 'blue')
plt.show()
if __name__ == '__main__':
test_ML_cup()