[03b52a]: / aggmap / aggmodel / cbks.py

Download this file

354 lines (268 with data), 13.3 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
 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
from sklearn.metrics import roc_auc_score, precision_recall_curve
from sklearn.metrics import auc as calculate_auc
from sklearn.metrics import mean_squared_error
from sklearn.metrics import accuracy_score
import tensorflow as tf
import os
import numpy as np
from scipy.stats.stats import pearsonr
def r2_score(x,y):
pcc, _ = pearsonr(x,y)
return pcc**2
def prc_auc_score(y_true, y_score):
precision, recall, threshold = precision_recall_curve(y_true, y_score) #PRC_AUC
auc = calculate_auc(recall, precision)
return auc
'''
for early-stopping techniques in regression and classification task
'''
######## Regression ###############################
class Reg_EarlyStoppingAndPerformance(tf.keras.callbacks.Callback):
def __init__(self, train_data, valid_data, MASK = -1, patience=5, criteria = 'val_loss', verbose = 0):
super(Reg_EarlyStoppingAndPerformance, self).__init__()
assert criteria in ['val_loss', 'val_r2'], 'not support %s ! only %s' % (criteria, ['val_loss', 'val_r2'])
self.x, self.y = train_data
self.x_val, self.y_val = valid_data
self.history = {'loss':[],
'val_loss':[],
'rmse':[],
'val_rmse':[],
'r2':[],
'val_r2':[],
'epoch':[]}
self.MASK = MASK
self.patience = patience
# best_weights to store the weights at which the minimum loss occurs.
self.best_weights = None
self.criteria = criteria
self.best_epoch = 0
self.verbose = verbose
def rmse(self, y_true, y_pred):
N_classes = y_pred.shape[1]
rmses = []
for i in range(N_classes):
y_pred_one_class = y_pred[:,i]
y_true_one_class = y_true[:, i]
mask = ~(y_true_one_class == self.MASK)
mse = mean_squared_error(y_true_one_class[mask], y_pred_one_class[mask])
rmse = np.sqrt(mse)
rmses.append(rmse)
return rmses
def r2(self, y_true, y_pred):
N_classes = y_pred.shape[1]
r2s = []
for i in range(N_classes):
y_pred_one_class = y_pred[:,i]
y_true_one_class = y_true[:, i]
mask = ~(y_true_one_class == self.MASK)
r2 = r2_score(y_true_one_class[mask], y_pred_one_class[mask])
r2s.append(r2)
return r2s
def on_train_begin(self, logs=None):
# The number of epoch it has waited when loss is no longer minimum.
self.wait = 0
# The epoch the training stops at.
self.stopped_epoch = 0
# Initialize the best as infinity.
if self.criteria == 'val_loss':
self.best = np.Inf
else:
self.best = -np.Inf
def on_epoch_end(self, epoch, logs={}):
y_pred = self.model.predict(self.x, verbose=self.verbose)
rmse_list = self.rmse(self.y, y_pred)
rmse_mean = np.nanmean(rmse_list)
r2_list = self.r2(self.y, y_pred)
r2_mean = np.nanmean(r2_list)
y_pred_val = self.model.predict(self.x_val, verbose=self.verbose)
rmse_list_val = self.rmse(self.y_val, y_pred_val)
rmse_mean_val = np.nanmean(rmse_list_val)
r2_list_val = self.r2(self.y_val, y_pred_val)
r2_mean_val = np.nanmean(r2_list_val)
self.history['loss'].append(logs.get('loss'))
self.history['val_loss'].append(logs.get('val_loss'))
self.history['rmse'].append(rmse_mean)
self.history['val_rmse'].append(rmse_mean_val)
self.history['r2'].append(r2_mean)
self.history['val_r2'].append(r2_mean_val)
self.history['epoch'].append(epoch)
# logs is a dictionary
eph = str(epoch+1).zfill(4)
loss = '{0:.4f}'.format((logs.get('loss')))
val_loss = '{0:.4f}'.format((logs.get('val_loss')))
rmse = '{0:.4f}'.format(rmse_mean)
rmse_val = '{0:.4f}'.format(rmse_mean_val)
r2_mean = '{0:.4f}'.format(r2_mean)
r2_mean_val = '{0:.4f}'.format(r2_mean_val)
if self.verbose:
print('\repoch: %s, loss: %s - val_loss: %s; rmse: %s - rmse_val: %s; r2: %s - r2_val: %s' % (eph,
loss, val_loss,
rmse,rmse_val,
r2_mean,r2_mean_val),
end=100*' '+'\n')
if self.criteria == 'val_loss':
current = logs.get(self.criteria)
if current <= self.best:
self.best = current
self.wait = 0
# Record the best weights if current results is better (less).
self.best_weights = self.model.get_weights()
self.best_epoch = epoch
else:
self.wait += 1
if self.wait >= self.patience:
self.stopped_epoch = epoch
self.model.stop_training = True
print('\nRestoring model weights from the end of the best epoch.')
self.model.set_weights(self.best_weights)
else:
current = np.nanmean(r2_list_val)
if current >= self.best:
self.best = current
self.wait = 0
# Record the best weights if current results is better (less).
self.best_weights = self.model.get_weights()
self.best_epoch = epoch
else:
self.wait += 1
if self.wait >= self.patience:
self.stopped_epoch = epoch
self.model.stop_training = True
print('\nRestoring model weights from the end of the best epoch.')
self.model.set_weights(self.best_weights)
def on_train_end(self, logs=None):
self.model.set_weights(self.best_weights)
if self.stopped_epoch > 0:
print('\nEpoch %05d: early stopping' % (self.stopped_epoch + 1))
def evaluate(self, testX, testY):
"""evalulate, return rmse and r2"""
y_pred = self.model.predict(testX, verbose=self.verbose)
rmse_list = self.rmse(testY, y_pred)
r2_list = self.r2(testY, y_pred)
return rmse_list, r2_list
######## classification ###############################
class CLA_EarlyStoppingAndPerformance(tf.keras.callbacks.Callback):
def __init__(self, train_data, valid_data, MASK = -1, patience=5, criteria = 'val_loss', metric = 'ROC', last_avf = None, verbose = 0):
super(CLA_EarlyStoppingAndPerformance, self).__init__()
sp = ['val_loss', 'val_metric']
assert criteria in sp, 'not support %s ! only %s' % (criteria, sp)
ms = ['ROC', 'PRC', 'ACC']
assert metric in ms, 'not support %s ! only %s' % (metric, ms)
ms_dict = {'ROC':'roc_auc', 'PRC':'prc_auc', 'ACC':'accuracy'}
metric = ms_dict[metric]
val_metric = 'val_%s' % metric
self.metric = metric
self.val_metric = val_metric
self.x, self.y = train_data
self.x_val, self.y_val = valid_data
self.last_avf = last_avf
self.history = {'loss':[],
'val_loss':[],
self.metric:[],
self.val_metric:[],
'epoch':[]}
self.MASK = MASK
self.patience = patience
# best_weights to store the weights at which the minimum loss occurs.
self.best_weights = None
self.criteria = criteria
self.best_epoch = 0
self.verbose = verbose
def sigmoid(self, x):
s = 1/(1+np.exp(-x))
return s
def roc_auc(self, y_true, y_pred):
if self.last_avf == None:
y_pred_logits = self.sigmoid(y_pred)
else:
y_pred_logits = y_pred
N_classes = y_pred_logits.shape[1]
aucs = []
for i in range(N_classes):
y_pred_one_class = y_pred_logits[:,i]
y_true_one_class = y_true[:, i]
mask = ~(y_true_one_class == self.MASK)
try:
if self.metric == 'roc_auc':
auc = roc_auc_score(y_true_one_class[mask], y_pred_one_class[mask], average='weighted') #ROC_AUC
elif self.metric == 'prc_auc':
auc = prc_auc_score(y_true_one_class[mask], y_pred_one_class[mask]) #PRC_AUC
elif self.metric == 'accuracy':
auc = accuracy_score(y_true_one_class[mask], np.round(y_pred_one_class[mask])) #ACC
except:
auc = np.nan
aucs.append(auc)
return aucs
def on_train_begin(self, logs=None):
# The number of epoch it has waited when loss is no longer minimum.
self.wait = 0
# The epoch the training stops at.
self.stopped_epoch = 0
# Initialize the best as infinity.
if self.criteria == 'val_loss':
self.best = np.Inf
else:
self.best = -np.Inf
def on_epoch_end(self, epoch, logs={}):
y_pred = self.model.predict(self.x, verbose = self.verbose)
roc_list = self.roc_auc(self.y, y_pred)
roc_mean = np.nanmean(roc_list)
y_pred_val = self.model.predict(self.x_val, verbose = self.verbose)
roc_val_list = self.roc_auc(self.y_val, y_pred_val)
roc_val_mean = np.nanmean(roc_val_list)
self.history['loss'].append(logs.get('loss'))
self.history['val_loss'].append(logs.get('val_loss'))
self.history[self.metric].append(roc_mean)
self.history[self.val_metric].append(roc_val_mean)
self.history['epoch'].append(epoch)
eph = str(epoch+1).zfill(4)
loss = '{0:.4f}'.format((logs.get('loss')))
val_loss = '{0:.4f}'.format((logs.get('val_loss')))
auc = '{0:.4f}'.format(roc_mean)
auc_val = '{0:.4f}'.format(roc_val_mean)
if self.verbose:
print('\repoch: %s, loss: %s - val_loss: %s; %s: %s - %s: %s' % (eph,
loss,
val_loss,
self.metric,
auc,
self.val_metric,
auc_val), end=100*' '+'\n')
if self.criteria == 'val_loss':
current = logs.get(self.criteria)
if current <= self.best:
self.best = current
self.wait = 0
# Record the best weights if current results is better (less).
self.best_weights = self.model.get_weights()
self.best_epoch = epoch
else:
self.wait += 1
if self.wait >= self.patience:
self.stopped_epoch = epoch
self.model.stop_training = True
print('\nRestoring model weights from the end of the best epoch.')
self.model.set_weights(self.best_weights)
else:
current = roc_val_mean
if current >= self.best:
self.best = current
self.wait = 0
# Record the best weights if current results is better (less).
self.best_weights = self.model.get_weights()
self.best_epoch = epoch
else:
self.wait += 1
if self.wait >= self.patience:
self.stopped_epoch = epoch
self.model.stop_training = True
print('\nRestoring model weights from the end of the best epoch.')
self.model.set_weights(self.best_weights)
def on_train_end(self, logs=None):
self.model.set_weights(self.best_weights)
if self.stopped_epoch > 0:
print('\nEpoch %05d: early stopping' % (self.stopped_epoch + 1))
def evaluate(self, testX, testY):
y_pred = self.model.predict(testX, verbose = self.verbose)
roc_list = self.roc_auc(testY, y_pred)
return roc_list