-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
classifier_domain.py
506 lines (428 loc) · 17.7 KB
/
classifier_domain.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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# Copyright 2016 The Oppia Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Domain objects for classifier models."""
from __future__ import annotations
import copy
import datetime
from core import feconf
from core import utils
from core.domain import state_domain
from typing import Dict, List
from typing_extensions import TypedDict
class ClassifierTrainingJobDict(TypedDict):
"""Dictionary that represents ClassifierTrainingJob."""
job_id: str
algorithm_id: str
interaction_id: str
exp_id: str
exp_version: int
next_scheduled_check_time: datetime.datetime
state_name: str
status: str
training_data: List[state_domain.TrainingDataDict]
algorithm_version: int
class ClassifierTrainingJob:
"""Domain object for a classifier training job.
A classifier training job is an abstraction of a request made by Oppia
for training a classifier model using certain dataset and a particular ML
algorithm denoted by the algorithm id. The classifier training jobs are
then picked up by the Virtual Machine (VM) through APIs exposed by Oppia.
The training_data is populated lazily when the job is fetched from the
database upon the request from the VM.
Attributes:
job_id: str. The unique id of the classifier training job.
algorithm_id: str. The id of the algorithm that will be used for
generating the classifier.
interaction_id: str. The id of the interaction to which the algorithm
belongs.
exp_id: str. The id of the exploration that contains the state
for which the classifier will be generated.
exp_version: int. The version of the exploration when
the training job was generated.
next_scheduled_check_time: datetime.datetime. The next scheduled time to
check the job.
state_name: str. The name of the state for which the classifier will be
generated.
status: str. The status of the training job request. This can be either
NEW (default value), FAILED, PENDING or COMPLETE.
training_data: list(dict). The training data that is used for training
the classifier. This field is populated lazily when the job request
is picked up by the VM. The list contains dicts where each dict
represents a single training data group, for example:
training_data = [
{
'answer_group_index': 1,
'answers': ['a1', 'a2']
},
{
'answer_group_index': 2,
'answers': ['a2', 'a3']
}
]
algorithm_version: int. The version of the classifier algorithm to be
trained. The algorithm version determines the training algorithm,
format in which trained parameters are stored along with the
prediction algorithm to be used. We expect this to change only when
the classifier algorithm is updated. This depends on the
algorithm ID.
"""
def __init__(
self,
job_id: str,
algorithm_id: str,
interaction_id: str,
exp_id: str,
exp_version: int,
next_scheduled_check_time: datetime.datetime,
state_name: str,
status: str,
training_data: List[state_domain.TrainingDataDict],
algorithm_version: int
) -> None:
"""Constructs a ClassifierTrainingJob domain object.
Args:
job_id: str. The unique id of the classifier training job.
algorithm_id: str. The id of the algorithm that will be used for
generating the classifier.
interaction_id: str. The id of the interaction to which the
algorithm belongs.
exp_id: str. The id of the exploration id that contains the state
for which classifier will be generated.
exp_version: int. The version of the exploration when
the training job was generated.
next_scheduled_check_time: datetime.datetime. The next scheduled
time to check the job.
state_name: str. The name of the state for which the classifier
will be generated.
status: str. The status of the training job request. This can be
either NEW (default), PENDING (when a job has been picked up)
or COMPLETE.
training_data: list(dict). The training data that is used for
training the classifier. This is populated lazily when the job
request is picked up by the VM. The list contains dicts where
each dict represents a single training data group, for example:
training_data = [
{
'answer_group_index': 1,
'answers': ['a1', 'a2']
},
{
'answer_group_index': 2,
'answers': ['a2', 'a3']
}
]
algorithm_version: int. Schema version of the classifier model to
be trained. This depends on the algorithm ID.
"""
self._job_id = job_id
self._algorithm_id = algorithm_id
self._interaction_id = interaction_id
self._exp_id = exp_id
self._exp_version = exp_version
self._next_scheduled_check_time = next_scheduled_check_time
self._state_name = state_name
self._status = status
self._training_data = copy.deepcopy(training_data)
self._algorithm_version = algorithm_version
@property
def job_id(self) -> str:
"""Returns the job_id of the classifier training job.
Returns:
str. The unique id of the classifier training job.
"""
return self._job_id
@property
def algorithm_id(self) -> str:
"""Returns the algorithm_id of the algorithm used for generating
the classifier.
Returns:
str. The id of the algorithm used for generating the classifier.
"""
return self._algorithm_id
@property
def interaction_id(self) -> str:
"""Returns the interaction_id to which the algorithm belongs.
Returns:
str. The id of the interaction to which the algorithm belongs.
"""
return self._interaction_id
@property
def exp_id(self) -> str:
"""Returns the exploration id for which the classifier will be
generated.
Returns:
str. The id of the exploration that contains the state
for which classifier will be generated.
"""
return self._exp_id
@property
def exp_version(self) -> int:
"""Returns the exploration version.
Returns:
int. The version of the exploration when the training job was
generated.
"""
return self._exp_version
@property
def next_scheduled_check_time(self) -> datetime.datetime:
"""Returns the next scheduled time to check the job.
Returns:
datetime.datetime. The next scheduled time to check the job.
"""
return self._next_scheduled_check_time
@property
def state_name(self) -> str:
"""Returns the state_name for which the classifier will be generated.
Returns:
str. The name of the state for which the classifier will be
generated.
"""
return self._state_name
@property
def status(self) -> str:
"""Returns the status of the training job request.
Returns:
str. The status of the training job request. This can be either
NEW (default), PENDING (when a job has been picked up) or
COMPLETE.
"""
return self._status
@property
def training_data(self) -> List[state_domain.TrainingDataDict]:
"""Returns the training data used for training the classifier.
Returns:
list(dict). The training data that is used for training the
classifier. This is populated lazily when the job request is
picked up by the VM. The list contains dicts where each dict
represents a single training data group, for example:
training_data = [
{
'answer_group_index': 1,
'answers': ['a1', 'a2']
},
{
'answer_group_index': 2,
'answers': ['a2', 'a3']
}
]
"""
return self._training_data
@property
def classifier_data_filename(self) -> str:
"""Returns file name of the GCS file which stores classifier data
for this training job.
Returns:
str. The GCS file name of the classifier data.
"""
return '%s-classifier-data.pb.xz' % (self.job_id)
@property
def algorithm_version(self) -> int:
"""Returns the algorithm version of the classifier.
Returns:
int. Version of the classifier algorithm. This depends on the
algorithm ID.
"""
return self._algorithm_version
def update_status(self, status: str) -> None:
"""Updates the status attribute of the ClassifierTrainingJob domain
object.
Args:
status: str. The status of the classifier training job.
Raises:
Exception. The status is not valid.
"""
initial_status = self._status
if status not in (
feconf.ALLOWED_TRAINING_JOB_STATUS_CHANGES[initial_status]):
raise Exception(
'The status change %s to %s is not valid.' % (
initial_status, status))
self._status = status
def to_dict(self) -> ClassifierTrainingJobDict:
"""Constructs a dict representation of training job domain object.
Returns:
dict. A dict representation of training job domain object.
"""
return {
'job_id': self._job_id,
'algorithm_id': self._algorithm_id,
'interaction_id': self._interaction_id,
'exp_id': self._exp_id,
'exp_version': self._exp_version,
'next_scheduled_check_time': self._next_scheduled_check_time,
'state_name': self._state_name,
'status': self._status,
'training_data': self._training_data,
'algorithm_version': self._algorithm_version
}
def validate(self) -> None:
"""Validates the training job before it is saved to storage."""
algorithm_ids = []
utils.require_valid_name(self.state_name, 'the state name')
if self.status not in feconf.ALLOWED_TRAINING_JOB_STATUSES:
raise utils.ValidationError(
'Expected status to be in %s, received %s'
% (feconf.ALLOWED_TRAINING_JOB_STATUSES, self.status))
if self.interaction_id not in feconf.INTERACTION_CLASSIFIER_MAPPING:
raise utils.ValidationError(
'Invalid interaction id: %s' % self.interaction_id)
algorithm_ids = [
classifier_details['algorithm_id'] for classifier_details in
feconf.INTERACTION_CLASSIFIER_MAPPING.values()]
if self.algorithm_id not in algorithm_ids:
raise utils.ValidationError(
'Invalid algorithm id: %s' % self.algorithm_id)
if not isinstance(self.training_data, list):
raise utils.ValidationError(
'Expected training_data to be a list, received %s' % (
self.training_data))
for grouped_answers in self.training_data:
if 'answer_group_index' not in grouped_answers:
raise utils.ValidationError(
'Expected answer_group_index to be a key in training_data'
'list item')
if 'answers' not in grouped_answers:
raise utils.ValidationError(
'Expected answers to be a key in training_data list item')
class StateTrainingJobsMappingDict(TypedDict):
"""Dictionary that represents StateTrainingJobsMapping."""
exp_id: str
exp_version: int
state_name: str
algorithm_ids_to_job_ids: Dict[str, str]
class StateTrainingJobsMapping:
"""Domain object for a state-to-training job mapping model.
This object represents a one-to-many relation between a particular state
of the particular version of the particular exploration and a set of
classifier training jobs. Each <exp_id, exp_version, state_name> is mapped
to an algorithm_id_to_job_id dict which maps all the valid algorithm_ids for
the given state to their training jobs. A state may have multiple
algorithm_ids valid for it: for example, one algorithm would serve Oppia
web users while another might support Oppia mobile or Android user. The
number of algorithm_ids that are valid for a given state depends upon the
interaction_id of that state.
Attributes:
exp_id: str. ID of the exploration.
exp_version: int. The exploration version at the time the corresponding
classifier's training job was created.
state_name: str. The name of the state to which the classifier
belongs.
algorithm_ids_to_job_ids: dict(str, str). Mapping of algorithm IDs to
corresponding unique training job IDs.
"""
def __init__(
self,
exp_id: str,
exp_version: int,
state_name: str,
algorithm_ids_to_job_ids: Dict[str, str]
) -> None:
"""Constructs a StateTrainingJobsMapping domain object.
Args:
exp_id: str. ID of the exploration.
exp_version: int. The exploration version at the time the
corresponding classifier's training job was created.
state_name: str. The name of the state to which the classifier
belongs.
algorithm_ids_to_job_ids: dict(str, str). The mapping from
algorithm IDs to the IDs of their corresponding classifier
training jobs.
"""
self._exp_id = exp_id
self._exp_version = exp_version
self._state_name = state_name
self._algorithm_ids_to_job_ids = algorithm_ids_to_job_ids
@property
def exp_id(self) -> str:
"""Returns the exploration id.
Returns:
str. The id of the exploration.
"""
return self._exp_id
@property
def exp_version(self) -> int:
"""Returns the exploration version.
Returns:
int. The exploration version at the time the
corresponding classifier's training job was created.
"""
return self._exp_version
@property
def state_name(self) -> str:
"""Returns the state_name to which the classifier belongs.
Returns:
str. The name of the state to which the classifier belongs.
"""
return self._state_name
@property
def algorithm_ids_to_job_ids(self) -> Dict[str, str]:
"""Returns the algorithm_ids_to_job_ids of the training jobs.
Returns:
dict(str, str). Mapping of algorithm IDs to corresponding unique
training job IDs.
"""
return self._algorithm_ids_to_job_ids
def to_dict(self) -> StateTrainingJobsMappingDict:
"""Constructs a dict representation of StateTrainingJobsMapping
domain object.
Returns:
dict. A dict representation of StateTrainingJobsMapping domain
object.
"""
return {
'exp_id': self._exp_id,
'exp_version': self._exp_version,
'state_name': self.state_name,
'algorithm_ids_to_job_ids': self._algorithm_ids_to_job_ids
}
def validate(self) -> None:
"""Validates the mapping before it is saved to storage."""
if not self.exp_version > 0:
raise utils.ValidationError(
'Expected version to be greater than 0')
class OppiaMLAuthInfo:
"""Domain object containing information necessary for authentication
of Oppia ML.
Attributes:
message: bytes. The message being communicated.
vm_id: str. The ID of the Oppia ML VM to be authenticated.
signature: str. The authentication signature signed by Oppia ML.
"""
def __init__(
self,
message: bytes,
vm_id: str,
signature: str
) -> None:
"""Creates new OppiaMLAuthInfo object.
Args:
message: bytes. The message being communicated.
vm_id: str. The ID of the Oppia ML VM to be authenticated.
signature: str. The authentication signature signed by Oppia ML.
"""
self._message = message
self._vm_id = vm_id
self._signature = signature
@property
def message(self) -> bytes:
"""Returns the message sent by OppiaML."""
return self._message
@property
def vm_id(self) -> str:
"""Returns the vm_id of OppiaML VM."""
return self._vm_id
@property
def signature(self) -> str:
"""Returns the signature sent by OppiaML."""
return self._signature