-
Notifications
You must be signed in to change notification settings - Fork 22
/
early_stopping.py
50 lines (41 loc) · 1.44 KB
/
early_stopping.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
from __future__ import print_function
'''
source from http://forensics.tistory.com/29
'''
class EarlyStopping():
def __init__(self, patience=0, measure='loss', verbose=0):
"""Set early stopping condition
Args:
patience: how many times to be patient before early stopping.
measure: checking measure, loss | f1 | accuracy.
verbose: if 1, enable verbose mode.
"""
self._step = 0
if measure == 'loss': # loss
self._value = float('inf')
else: # f1, accuracy
self._value = 0
self.patience = patience
self.verbose = verbose
def reset(self, value):
self._step = 0
self._value = value
def status(self):
print('Status: step / patience = %d / %d, value = %f\n' % (self._step, self.patience, self._value))
def step(self):
return self._step
def validate(self, value, measure='loss'):
going_worse = False
if measure == 'loss': # loss
if self._value < value: going_worse = True
else: # f1, accuracy
if self._value > value: going_worse = True
if going_worse:
self._step += 1
if self._step > self.patience:
if self.verbose:
print('Training process is stopped early!')
return True
else:
self.reset(value)
return False