-
Notifications
You must be signed in to change notification settings - Fork 3
/
_ink_classifier.py
323 lines (289 loc) · 12.5 KB
/
_ink_classifier.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
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
import math
from typing import Any, Optional, Dict, Tuple, Union
from typing_extensions import Self, Final
import trueskill
from truelearn.models import EventModel, LearnerModel, LearnerMetaWeights
from truelearn.base import BaseClassifier
from ._base import team_sum_quality
from .._constraint import TypeConstraint
from ._novelty_classifier import NoveltyClassifier
from ._interest_classifier import InterestClassifier
class INKClassifier(BaseClassifier):
"""A meta-classifier that combines KnowledgeClassifier and InterestClassifier.
During the training process, the meta-classifier individually trains
the KnowledgeClassifier and the InterestClassifier. After that, the
meta-classifier trains a set of weights by again using the ideas of team matching.
One team consists of the weights of the knowledge, interest and bias, and
the other team consists of the threshold. Then, the meta-classifier
uses the given label to adjust the weights accordingly.
During the prediction process, the meta-classifier individually uses the predict
function of the KnowledgeClassifier and InterestClassifier.
Then, it combines them by using the weights.
Examples:
>>> from truelearn.learning import INKClassifier
>>> from truelearn.models import EventModel, Knowledge
>>> from truelearn.models import KnowledgeComponent, LearnerMetaWeights
>>> ink_classifier = INKClassifier()
>>> ink_classifier
INKClassifier()
>>>
>>> # use custom weights
>>> weights = LearnerMetaWeights.Weights(mean=0.0, variance=0.5)
>>> meta_weights = LearnerMetaWeights(novelty_weights=weights)
>>> ink_classifier = INKClassifier(learner_meta_weights=meta_weights)
>>>
>>> # prepare an event model
>>> knowledges = [
... Knowledge({1: KnowledgeComponent(mean=0.15, variance=1e-9)}),
... Knowledge({
... 2: KnowledgeComponent(mean=0.87, variance=1e-9),
... 3: KnowledgeComponent(mean=0.18, variance=1e-9),
... }),
... Knowledge({
... 1: KnowledgeComponent(mean=0.34, variance=1e-9),
... 3: KnowledgeComponent(mean=0.15, variance=1e-9),
... }),
... ]
>>> times = [0, 10514, 53621]
>>> events = [
... EventModel(knowledge, time)
... for knowledge, time in zip(knowledges, times)
... ]
>>> engage_stats = [True, False, True]
>>> for event, engage_stats in zip(events, engage_stats):
... ink_classifier = ink_classifier.fit(event, engage_stats)
... print(
... ink_classifier.predict(event),
... ink_classifier.predict_proba(event)
... )
...
True 0.64387...
False 0.42658...
True 0.65406...
>>> ink_classifier.get_params(deep=False) # doctest:+ELLIPSIS
{...'learner_meta_weights': LearnerMetaWeights(novelty_weights=Weights(\
mean=0.20787..., variance=0.45787...), interest_weights=Weights(\
mean=0.66924..., variance=0.42672...), bias_weights=Weights(\
mean=0.13029..., variance=0.39582...))...}
"""
__DEFAULT_GLOBAL_SIGMA: Final[float] = 1e-9
__DEFAULT_DRAW_PROBA: Final[float] = 1e-9
_parameter_constraints: Dict[str, Any] = {
**BaseClassifier._parameter_constraints,
"learner_meta_weights": TypeConstraint(LearnerMetaWeights),
"novelty_classifier": TypeConstraint(NoveltyClassifier),
"interest_classifier": TypeConstraint(InterestClassifier),
"threshold": TypeConstraint(float),
"tau": TypeConstraint(float),
"greedy": TypeConstraint(bool),
}
def __init__(
self,
*,
learner_meta_weights: Optional[LearnerMetaWeights] = None,
novelty_classifier: Optional[Union[NoveltyClassifier, Dict]] = None,
interest_classifier: Optional[Union[InterestClassifier, Dict]] = None,
threshold: float = 0.5,
tau: float = 0.0,
greedy: bool = False,
) -> None:
"""Init INKClassifier object.
Args:
*:
Use to reject positional arguments.
learner_meta_weights:
The novelty/interest/bias weights.
novelty_classifier:
The NoveltyClassifier. It can be a NoveltyClassifier object or
a dictionary of parameters that can be used to instantiate a
NoveltyClassifier object.
interest_classifier:
The InterestClassifier. It can be an InterestClassifier object or
a dictionary of parameters that can be used to instantiate an
InterestClassifier object.
threshold:
A float that determines the classification threshold.
tau:
The dynamic factor of learner's learning process.
It's used to avoid the halting of the learning process.
greedy:
A bool indicating whether meta-learning should
take the greedy approach. In the greedy approach,
only incorrect predictions lead to the update of the weights.
Raises:
TrueLearnTypeError:
Types of parameters do not satisfy their constraints.
TrueLearnValueError:
Values of parameters do not satisfy their constraints.
"""
if novelty_classifier is None:
self._novelty_classifier = NoveltyClassifier()
elif isinstance(novelty_classifier, dict):
self._novelty_classifier = NoveltyClassifier(**novelty_classifier)
else:
self._novelty_classifier = novelty_classifier
if interest_classifier is None:
self._interest_classifier = InterestClassifier()
elif isinstance(interest_classifier, dict):
self._interest_classifier = InterestClassifier(**interest_classifier)
else:
self._interest_classifier = interest_classifier
self._learner_meta_weights = learner_meta_weights or LearnerMetaWeights()
self._threshold = threshold
self._tau = tau
self._greedy = greedy
self._validate_params()
def __eval_matching_quality(
self,
*,
novelty_weights: LearnerMetaWeights.Weights,
pred_novelty: float,
interest_weights: LearnerMetaWeights.Weights,
pred_interest: float,
bias_weights: LearnerMetaWeights.Weights,
pred_bias: float,
) -> float:
"""Evaluate the matching quality of learner and content given the weights.
Args:
novelty_weights:
A dict containing the mean and variance of the novelty_weights.
pred_novelty:
The predicted probability of the learner's engagement by using
NoveltyClassifier.
interest_weights:
A dict containing the mean and variance of the interest_weights.
pred_interest:
The predicted probability of the learner's engagement by using
InterestClassifier.
bias_weights:
A dict containing the mean and variance of the bias_weights.
pred_bias:
The predicted probability of the learner's engagement by using
bias. This value is always 1.0.
Returns:
A float between [0, 1], indicating the matching quality
of the learner and the content. The higher the value,
the better the match.
"""
team_learner_mean = [
novelty_weights.mean * pred_novelty,
interest_weights.mean * pred_interest,
bias_weights.mean * pred_bias,
]
team_learner_variance = [
novelty_weights.variance * (pred_novelty**2),
interest_weights.variance * (pred_interest**2),
bias_weights.variance * (pred_bias**2),
]
team_content_mean = [self._threshold]
team_content_variance = []
return team_sum_quality(
learner_mean=team_learner_mean,
learner_variance=team_learner_variance,
content_mean=team_content_mean,
content_variance=team_content_variance,
beta=0,
)
def __create_env(self):
"""Create the trueskill environment used in the training/prediction process."""
return trueskill.TrueSkill(
mu=0.0,
sigma=INKClassifier.__DEFAULT_GLOBAL_SIGMA,
beta=1,
tau=self._tau,
draw_probability=INKClassifier.__DEFAULT_DRAW_PROBA,
backend="mpmath",
)
def __update_weights(
self,
x: EventModel,
pred_actual: float,
) -> None:
"""Update the weights of novelty, interest and bias.
Args:
x:
A representation of the learning event.
pred_actual:
Whether the learner actually engages in the given event. This value is
either 0 or 1.
"""
cur_pred = self.predict(x)
# if the prediction is correct and greedy, don't train
if self._greedy and cur_pred == pred_actual:
return
pred_novelty = self._novelty_classifier.predict_proba(x)
pred_interest = self._interest_classifier.predict_proba(x)
# train
env = self.__create_env()
team_experts = (
env.create_rating(
mu=self._learner_meta_weights.novelty_weights.mean,
sigma=math.sqrt(self._learner_meta_weights.novelty_weights.variance),
),
env.create_rating(
mu=self._learner_meta_weights.interest_weights.mean,
sigma=math.sqrt(self._learner_meta_weights.interest_weights.variance),
),
env.create_rating(
mu=self._learner_meta_weights.bias_weights.mean,
sigma=math.sqrt(self._learner_meta_weights.bias_weights.variance),
),
)
team_threshold = (
env.create_rating(
mu=self._threshold, sigma=INKClassifier.__DEFAULT_GLOBAL_SIGMA
),
)
if pred_actual: # weights need to be larger than a threshold
new_team_experts, _ = env.rate(
[team_experts, team_threshold],
weights=[(pred_novelty, pred_interest, 1), (1,)],
ranks=[0, 1],
)
else:
new_team_experts, _ = env.rate(
[team_experts, team_threshold],
weights=[(pred_novelty, pred_interest, 1), (1,)],
ranks=[1, 0],
)
# update skills
self._learner_meta_weights.novelty_weights = LearnerMetaWeights.Weights(
new_team_experts[0].mu, new_team_experts[0].sigma ** 2
)
self._learner_meta_weights.interest_weights = LearnerMetaWeights.Weights(
new_team_experts[1].mu, new_team_experts[1].sigma ** 2
)
self._learner_meta_weights.bias_weights = LearnerMetaWeights.Weights(
new_team_experts[2].mu, new_team_experts[2].sigma ** 2
)
def fit(self, x: EventModel, y: bool) -> Self:
self._novelty_classifier.fit(x, y)
self._interest_classifier.fit(x, y)
self.__update_weights(x, y)
return self
def predict(self, x: EventModel) -> bool:
return self.predict_proba(x) > self._threshold
def predict_proba(self, x: EventModel) -> float:
return self.__eval_matching_quality(
novelty_weights=self._learner_meta_weights.novelty_weights,
pred_novelty=self._novelty_classifier.predict_proba(x),
interest_weights=self._learner_meta_weights.interest_weights,
pred_interest=self._interest_classifier.predict_proba(x),
bias_weights=self._learner_meta_weights.bias_weights,
pred_bias=1.0,
)
def get_learner_model(
self,
) -> Tuple[LearnerModel, LearnerModel, LearnerMetaWeights]:
"""Get the learner model associated with this classifier.
Returns:
A tuple (novelty_learner, interest_learner, meta_weights) where
novelty_learner is the learner model associated with the NoveltyClassifier,
interest_learner is the learner model associated with the
interestClassifier, meta_weights is the weights used in this classifier.
"""
return (
self._novelty_classifier.get_learner_model(),
self._interest_classifier.get_learner_model(),
self._learner_meta_weights,
)