dmlc--dgl
a00636a02b
* several nn example * appnp * fix lint * lint * add dgi * fix * fix * fix * fff * docs * 111 * fix * change init * change result * tiaocan+1 * fix * fix lint * fix * fix
29 行
893 B
Python
29 行
893 B
Python
import numpy as np
|
|
|
|
class EarlyStopping:
|
|
def __init__(self, patience=10):
|
|
self.patience = patience
|
|
self.counter = 0
|
|
self.best_score = None
|
|
self.early_stop = False
|
|
|
|
def step(self, acc, model):
|
|
score = acc
|
|
if self.best_score is None:
|
|
self.best_score = score
|
|
self.save_checkpoint(model)
|
|
elif score < self.best_score:
|
|
self.counter += 1
|
|
print(f'EarlyStopping counter: {self.counter} out of {self.patience}')
|
|
if self.counter >= self.patience:
|
|
self.early_stop = True
|
|
else:
|
|
self.best_score = score
|
|
self.save_checkpoint(model)
|
|
self.counter = 0
|
|
return self.early_stop
|
|
|
|
def save_checkpoint(self, model):
|
|
'''Saves model when validation loss decrease.'''
|
|
model.save_weights('es_checkpoint.pb')
|