generated from asreview/template-extension-new-model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_switcher.py
71 lines (52 loc) · 2.7 KB
/
model_switcher.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
from asreview.models.classifiers.base import BaseTrainClassifier
import asreviewcontrib.models.config as cfg
class base_switcher(BaseTrainClassifier):
name = "base-switcher"
def __init__(self,
switchpoint=cfg.settings["switchpoint"],
model_1=None,
model_2=None,
save_switch_point=cfg.settings["save_switch_point"]
):
print(
"""
█▀▄▀█ ████▄ ██▄ ▄███▄ █
█ █ █ █ █ █ █ █▀ ▀ █
█ ▄ █ █ █ █ █ ██▄▄ █
█ █ ▀████ █ █ █▄ ▄▀ ███▄
█ ███▀ ▀███▀ ▀
▀
▄▄▄▄▄ ▄ ▄ ▀ ▄▄▄▄▀ ▄█▄ ▄ ▀▄ ▄███▄ █▄▄▄▄
█ ▀▄ █ █ ▄█ ▀▀▀ █ █▀ ▀▄ █ █ █▀ ▀ █ ▄▀
▄ ▀▀▀▀▄ █ ▄ █ ██ █ █ ▀ ██▀▀█ █▄▄▄ █▀▀▌
▀▄▄▄▄▀ █ █ █ ▐█ █ █▄ ▄▀ █ █ █▄ ▄▀ █ █
█ █ █ ▐ ▀ ▀███▀ █ ▀███▀ █
▀ ▀ ▀ ▀
"""
)
self._iteration = 0
self._save_switch_point = save_switch_point
self._switchpoint = switchpoint
self._model_1 = model_1
self._model_2 = model_2
def fit(self, X, y):
if(self._save_switch_point and self._iteration==self._switchpoint):
from numpy import savez
savez('../output/data-' + str(self._iteration), X=X, y=y)
self._iteration += 1
if self._iteration == self._switchpoint : print("Switching Model")
if (self._iteration < self._switchpoint):
self.log_message(type(self._model_1))
return self._model_1.fit(X, y)
else:
self.log_message(type(self._model_2))
self._model_2.fit(X, y)
def predict_proba(self, X):
if (self._iteration < self._switchpoint):
return self._model_1.predict_proba(X)
else:
return self._model_2.predict_proba(X)
def log_message(self,
modeltype = None
):
print("Iteration {}, fitting {}".format(self._iteration, modeltype))