-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathtuning.py
180 lines (178 loc) · 11 KB
/
tuning.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
168
169
170
171
172
173
174
175
176
177
178
179
180
## author@ Wenhui Yu email: [email protected] 2021.02.16
## Low-pass Collaborative Filtering Network (LCFN) and baselines
from train_model import train_model
from print_save import print_params, save_params
from get_hyperparameters import get_hyperparameter
import tensorflow as tf
import numpy as np
import random as rd
import time
def tuning(path_excel_dir, para_name, para, data, lr, lamda, min_num_coarse, max_num_coarse, min_num_fine, max_num_fine):
## tuning settings
x_cen, y_cen = 1, 1
score_matrix = np.zeros((3,3))
hyper_matrix = np.array([[[lr * (10 ** i), lamda * (10 ** j)] for i in range(-1, 2)] for j in range(-1, 2)])
num_matrix = np.zeros((3,3))
## coarse tuning
for i in range(2):
for x_curr, y_curr in [[x_cen, y_cen], [x_cen - 1, y_cen], [x_cen, y_cen - 1], [x_cen + 1, y_cen], [x_cen, y_cen + 1]]:
if (num_matrix[x_curr, y_curr] < min_num_coarse or (x_curr == x_cen and y_curr == y_cen)) and (num_matrix[x_curr, y_curr] < 0.5 or score_matrix[x_curr, y_curr] >= 0.7 * score_matrix.max()):
para[3: 5] = hyper_matrix[x_curr, y_curr]
print_params(para_name, para)
path_excel = path_excel_dir + str(int(time.time())) + str(int(rd.uniform(100, 900))) + '.xlsx'
save_params(para_name, para, path_excel)
score = train_model(para, data, path_excel)
if para[2] not in ['GCMC', 'NGCF', 'SCF', 'CGMC', 'LightGCN']: tf.reset_default_graph()
score_matrix[x_curr, y_curr] = (score_matrix[x_curr, y_curr] * num_matrix[x_curr, y_curr] + score)/(num_matrix[x_curr, y_curr] + 1)
num_matrix[x_curr, y_curr] += 1
print(score_matrix)
print(num_matrix)
x_argmax, y_argmax = np.where(score_matrix == score_matrix.max())
x_argmax, y_argmax = x_argmax[0], y_argmax[0]
print('When \eta and \lambda is: ', hyper_matrix[x_argmax, y_argmax])
print('the model achieves the best performance: ', score_matrix.max())
while num_matrix[x_cen, y_cen] < max_num_coarse or score_matrix.max() != score_matrix[x_cen, y_cen]:
x_cen, y_cen = np.where(score_matrix == score_matrix.max())
x_cen, y_cen = x_cen[0], y_cen[0]
## extending the matrices
if y_cen == 0:
y_cen += 1
pad = np.zeros(score_matrix.shape[0])
score_matrix = np.c_[pad, score_matrix]
num_matrix = np.c_[pad, num_matrix]
hyper_matrix = np.concatenate((np.zeros((hyper_matrix.shape[0], 1, 2)), hyper_matrix), axis=1)
for i in range(hyper_matrix.shape[0]):
hyper_matrix[i, 0, 0] = hyper_matrix[i, 1, 0] / 10
hyper_matrix[i, 0, 1] = hyper_matrix[i, 1, 1]
if y_cen == score_matrix.shape[1] - 1:
pad = np.zeros(score_matrix.shape[0])
score_matrix = np.c_[score_matrix, pad]
num_matrix = np.c_[num_matrix, pad]
hyper_matrix = np.concatenate((hyper_matrix, np.zeros((hyper_matrix.shape[0], 1, 2))), axis=1)
for i in range(hyper_matrix.shape[0]):
hyper_matrix[i, -1, 0] = hyper_matrix[i, -1-1, 0] * 10
hyper_matrix[i, -1, 1] = hyper_matrix[i, -1-1, 1]
if x_cen == 0:
x_cen += 1
pad = np.zeros((1, score_matrix.shape[1]))
score_matrix = np.r_[pad, score_matrix]
num_matrix = np.r_[pad, num_matrix]
hyper_matrix = np.concatenate((np.zeros((1, hyper_matrix.shape[1], 2)), hyper_matrix), axis=0)
for i in range(hyper_matrix.shape[1]):
hyper_matrix[0, i, 0] = hyper_matrix[1, i, 0]
hyper_matrix[0, i, 1] = hyper_matrix[1, i, 1] / 10
if x_cen == score_matrix.shape[0] - 1:
pad = np.zeros((1, score_matrix.shape[1]))
score_matrix = np.r_[score_matrix, pad]
num_matrix = np.r_[num_matrix, pad]
hyper_matrix = np.concatenate((hyper_matrix, np.zeros((1, hyper_matrix.shape[1], 2))), axis=0)
for i in range(hyper_matrix.shape[1]):
hyper_matrix[-1, i, 0] = hyper_matrix[-1-1, i, 0]
hyper_matrix[-1, i, 1] = hyper_matrix[-1-1, i, 1] * 10
## finding the best performance
for x_curr, y_curr in [[x_cen, y_cen], [x_cen - 1, y_cen], [x_cen, y_cen - 1], [x_cen + 1, y_cen], [x_cen, y_cen + 1]]:
if (num_matrix[x_curr, y_curr] < min_num_coarse or (x_curr == x_cen and y_curr == y_cen)) and (num_matrix[x_curr, y_curr] < 0.5 or score_matrix[x_curr, y_curr] >= 0.7 * score_matrix.max()):
para[3: 5] = hyper_matrix[x_curr, y_curr]
print_params(para_name, para)
path_excel = path_excel_dir + str(int(time.time())) + str(int(rd.uniform(100, 900))) + '.xlsx'
save_params(para_name, para, path_excel)
score = train_model(para, data, path_excel)
if para[2] not in ['GCMC', 'NGCF', 'SCF', 'CGMC', 'LightGCN']: tf.reset_default_graph()
score_matrix[x_curr, y_curr] = (score_matrix[x_curr, y_curr] * num_matrix[x_curr, y_curr] + score)/(num_matrix[x_curr, y_curr] + 1)
num_matrix[x_curr, y_curr] += 1
print(score_matrix)
print(num_matrix)
x_argmax, y_argmax = np.where(score_matrix == score_matrix.max())
x_argmax, y_argmax = x_argmax[0], y_argmax[0]
print('When \eta and \lambda is: ', hyper_matrix[x_argmax, y_argmax])
print('the model achieves the best performance: ', score_matrix.max())
## fine tuning
x_cen, y_cen = np.where(score_matrix == score_matrix.max())
x_cen, y_cen = x_cen[0], y_cen[0]
score_max = score_matrix[x_cen, y_cen]
num_max = num_matrix[x_cen, y_cen]
lr, lamda = hyper_matrix[x_cen, y_cen]
x_cen, y_cen = 2, 2
score_matrix = np.zeros((5, 5))
score_matrix[x_cen, y_cen] = score_max
num_matrix = np.zeros((5, 5))
num_matrix[x_cen, y_cen] = num_max
hyper_matrix_lr = np.expand_dims(np.array([get_hyperparameter(lr) for i in range(5)]), axis=-1)
hyper_matrix_lamda = np.expand_dims(np.array([get_hyperparameter(lamda) for i in range(5)]).T, axis=-1)
hyper_matrix = np.concatenate((hyper_matrix_lr, hyper_matrix_lamda), axis=-1)
## initializing the score matrix
for i in range(3):
for x_curr, y_curr in [[1,1],[1,3],[3,1],[3,3]]:
para[3: 5] = hyper_matrix[x_curr, y_curr]
print_params(para_name, para)
path_excel = path_excel_dir + str(int(time.time())) + str(int(rd.uniform(100, 900))) + '.xlsx'
save_params(para_name, para, path_excel)
score = train_model(para, data, path_excel)
if para[2] not in ['GCMC', 'NGCF', 'SCF', 'CGMC', 'LightGCN']: tf.reset_default_graph()
score_matrix[x_curr, y_curr] = (score_matrix[x_curr, y_curr] * num_matrix[x_curr, y_curr] + score) / (num_matrix[x_curr, y_curr] + 1)
num_matrix[x_curr, y_curr] += 1
print(score_matrix)
print(num_matrix)
x_argmax, y_argmax = np.where(score_matrix == score_matrix.max())
x_argmax, y_argmax = x_argmax[0], y_argmax[0]
print('When \eta and \lambda is: ', hyper_matrix[x_argmax, y_argmax])
print('the model achieves the best performance: ', score_matrix.max())
while num_matrix[x_cen, y_cen] < max_num_fine or score_matrix.max() != score_matrix[x_cen, y_cen]:
x_cen, y_cen = np.where(score_matrix == score_matrix.max())
x_cen, y_cen = x_cen[0], y_cen[0]
## extending matrices
if y_cen == 0:
y_cen += 1
pad = np.zeros(score_matrix.shape[0])
score_matrix = np.c_[pad, score_matrix]
num_matrix = np.c_[pad, num_matrix]
pad_lr = np.ones(hyper_matrix.shape[0]) * get_hyperparameter(hyper_matrix[x_cen, y_cen, 0])[1]
pad_lamda = hyper_matrix[:, 0, 1]
hyper_matrix = np.concatenate((np.zeros((hyper_matrix.shape[0], 1, 2)), hyper_matrix), axis=1)
hyper_matrix[:, 0, 0] = pad_lr
hyper_matrix[:, 0, 1] = pad_lamda
if y_cen == score_matrix.shape[1] - 1:
pad = np.zeros(score_matrix.shape[0])
score_matrix = np.c_[score_matrix, pad]
num_matrix = np.c_[num_matrix, pad]
pad_lr = np.ones(hyper_matrix.shape[0]) * get_hyperparameter(hyper_matrix[x_cen, y_cen, 0])[3]
pad_lamda = hyper_matrix[:, 0, 1]
hyper_matrix = np.concatenate((hyper_matrix, np.zeros((hyper_matrix.shape[0], 1, 2))), axis=1)
hyper_matrix[:, -1, 0] = pad_lr
hyper_matrix[:, -1, 1] = pad_lamda
if x_cen == 0:
x_cen += 1
pad = np.zeros((1, score_matrix.shape[1]))
score_matrix = np.r_[pad, score_matrix]
num_matrix = np.r_[pad, num_matrix]
pad_lr = hyper_matrix[0, :, 0]
pad_lamda = np.ones(hyper_matrix.shape[1]) * get_hyperparameter(hyper_matrix[x_cen, y_cen, 1])[1]
hyper_matrix = np.concatenate((np.zeros((1, hyper_matrix.shape[1], 2)), hyper_matrix), axis=0)
hyper_matrix[0, :, 0] = pad_lr
hyper_matrix[0, :, 1] = pad_lamda
if x_cen == score_matrix.shape[0] - 1:
pad = np.zeros((1, score_matrix.shape[1]))
score_matrix = np.r_[score_matrix, pad]
num_matrix = np.r_[num_matrix, pad]
pad_lr = hyper_matrix[0, :, 0]
pad_lamda = np.ones(hyper_matrix.shape[1]) * get_hyperparameter(hyper_matrix[x_cen, y_cen, 1])[3]
hyper_matrix = np.concatenate((hyper_matrix, np.zeros((1, hyper_matrix.shape[1], 2))), axis=0)
hyper_matrix[-1, :, 0] = pad_lr
hyper_matrix[-1, :, 1] = pad_lamda
## finding the best performance
for x_curr, y_curr in [[x_cen, y_cen], [x_cen - 1, y_cen], [x_cen, y_cen - 1], [x_cen + 1, y_cen], [x_cen, y_cen + 1]]:
if (num_matrix[x_curr, y_curr] < min_num_fine or (x_curr == x_cen and y_curr == y_cen)) and (num_matrix[x_curr, y_curr] < 0.5 or score_matrix[x_curr, y_curr] >= 0.7 * score_matrix.max()):
para[3: 5] = hyper_matrix[x_curr, y_curr]
print_params(para_name, para)
path_excel = path_excel_dir + str(int(time.time())) + str(int(rd.uniform(100, 900))) + '.xlsx'
save_params(para_name, para, path_excel)
score = train_model(para, data, path_excel)
if para[2] not in ['GCMC', 'NGCF', 'SCF', 'CGMC', 'LightGCN']: tf.reset_default_graph()
score_matrix[x_curr, y_curr] = (score_matrix[x_curr, y_curr] * num_matrix[x_curr, y_curr] + score)/(num_matrix[x_curr, y_curr] + 1)
num_matrix[x_curr, y_curr] += 1
print(score_matrix)
print(num_matrix)
x_argmax, y_argmax = np.where(score_matrix == score_matrix.max())
x_argmax, y_argmax = x_argmax[0], y_argmax[0]
print('When \eta and \lambda is: ', hyper_matrix[x_argmax, y_argmax])
print('the model achieves the best performance: ', score_matrix.max())