-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathregularization.py
More file actions
47 lines (41 loc) · 1.26 KB
/
regularization.py
File metadata and controls
47 lines (41 loc) · 1.26 KB
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
from train import *
acc_result = []
s = 5
for j in range(30):
temp_res = np.array([[0.0,0.0,0.0]])
for i in range(1,s+1):
lamb = 1000+j*6#(10**(j/20+2))
print("===Validation Block(%d/%d)===" % (i,s))
x,y,vx,vy = load_data(False,s,i)
print("Training Set ...")
w = np.zeros(len(x[0]))
w = np.matmul(np.matmul(inv(np.matmul(x.T, x) + lamb * np.identity(len(x[0]))), x.T), y)
np.save('model_w.npy', w)
#train(0,s,i,0,0,lamb)
B_T = valid(x,y)
print('Valid Set ...')
B_W = valid(vx, vy)
temp_res+=np.array([[lamb,B_T,B_W]])
acc_result.append(temp_res/5)
for i in acc_result:
print(i)
acc_result = np.array(acc_result)
np.save('reg_acc_0-9.npy',acc_result)
print(acc_result.T[0][0])
print(acc_result.T[1][0])
print(acc_result.T[2][0])
plt.semilogx(
acc_result.T[0][0],
acc_result.T[1][0],
acc_result.T[0][0],
acc_result.T[2][0]
)
# for i,j in zip(acc_result.T[0][0],acc_result.T[1][0]):
# plt.text(i,j,str(j))
# for i,j in zip(acc_result.T[0][0],acc_result.T[2][0]):
# plt.text(i, j, str(j))
plt.legend(['Training','Validation'])
plt.ylabel('ave cost')
plt.xlabel('lambda')
plt.title('Compare between diff regularization')
plt.savefig('reg_MT500-1500.png')