-
Notifications
You must be signed in to change notification settings - Fork 148
/
base.py
839 lines (712 loc) · 28.8 KB
/
base.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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
#
# Copyright (c) 2021, NVIDIA CORPORATION.
#
# 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.
#
import copy
import inspect
import os
import pathlib
from collections import defaultdict
from types import SimpleNamespace
from typing import Callable, Dict, Iterable, List, Optional, Type, Union, cast
import numpy as np
import torch
import torchmetrics as tm
from merlin.schema import ColumnSchema
from merlin.schema import Schema as Core_Schema
from tqdm import tqdm
from transformers.modeling_utils import SequenceSummary
from merlin_standard_lib import Schema, Tag
from merlin_standard_lib.registry import camelcase_to_snakecase
from ..block.base import BlockBase, BlockOrModule, BlockType
from ..features.base import InputBlock
from ..features.sequence import TabularFeaturesType
from ..typing import TabularData, TensorOrTabularData
from ..utils.torch_utils import LossMixin, MetricsMixin
def name_fn(name, inp):
return "/".join([name, inp]) if name else None
class PredictionTask(torch.nn.Module, LossMixin, MetricsMixin):
"""Individual prediction-task of a model.
Parameters
----------
loss: torch.nn.Module
The loss to use during training of this task.
metrics: torch.nn.Module
The metrics to calculate during training & evaluation.
target_name: str, optional
Name of the target, this is needed when there are multiple targets.
task_name: str, optional
Name of the prediction task, if not provided a name will be automatically constructed based
on the target-name & class-name.
forward_to_prediction_fn: Callable[[torch.Tensor], torch.Tensor]
Function to apply before the prediction
task_block: BlockType
Module to transform input tensor before computing predictions.
pre: BlockType
Module to compute the predictions probabilities.
summary_type: str
This is used to summarize a sequence into a single tensor. Accepted values are:
- `"last"` -- Take the last token hidden state (like XLNet)
- `"first"` -- Take the first token hidden state (like Bert)
- `"mean"` -- Take the mean of all tokens hidden states
- `"cls_index"` -- Supply a Tensor of classification token position (GPT/GPT-2)
- `"attn"` -- Not implemented now, use multi-head attention
"""
def __init__(
self,
loss: torch.nn.Module,
metrics: Iterable[tm.Metric] = None,
target_name: Optional[str] = None,
task_name: Optional[str] = None,
forward_to_prediction_fn: Callable[[torch.Tensor], torch.Tensor] = lambda x: x,
task_block: Optional[BlockType] = None,
pre: Optional[BlockType] = None,
summary_type: str = "last",
):
super().__init__()
self.summary_type = summary_type
self.sequence_summary = SequenceSummary(
SimpleNamespace(summary_type=self.summary_type) # type: ignore
) # noqa
self.target_name = target_name
self.forward_to_prediction_fn = forward_to_prediction_fn
self.set_metrics(metrics)
self.loss = loss
self.pre = pre
self.task_block = task_block
self._task_name = task_name
def build(
self,
body: BlockType,
input_size,
inputs: Optional[InputBlock] = None,
device=None,
task_block: Optional[BlockType] = None,
pre=None,
):
"""
The method will be called when block is converted to a model,
i.e when linked to prediction head.
Parameters
----------
block:
the model block to link with head
device:
set the device for the metrics and layers of the task
"""
if task_block:
# TODO: What to do when `self.task_block is not None`?
self.task_block = task_block
if pre:
# TODO: What to do when `self.pre is not None`?
self.pre = pre
# Build task block
pre_input_size = input_size
if self.task_block:
if isinstance(self.task_block, torch.nn.Module):
self.task_block = copy.deepcopy(self.task_block)
else:
self.task_block = self.task_block.build(input_size)
pre_input_size = self.task_block.output_size() # type: ignore
if self.pre:
if isinstance(self.pre, torch.nn.Module):
self.pre = copy.deepcopy(self.pre)
else:
self.pre = self.pre.build(pre_input_size)
if device:
self.to(device)
for metric in self.metrics:
metric.to(device)
self.built = True
def forward(
self,
inputs: torch.Tensor,
targets: torch.Tensor = None,
training: bool = False,
testing: bool = False,
):
x = inputs
if len(x.size()) == 3 and self.summary_type:
x = self.sequence_summary(x)
if self.task_block:
x = self.task_block(x) # type: ignore
if self.pre:
x = self.pre(x) # type: ignore
if training or testing:
# add support of computing the loss inside the forward
# and return a dictionary as standard output
loss = self.loss(x, target=targets)
return {"loss": loss, "labels": targets, "predictions": x}
return x
@property
def task_name(self):
if self._task_name:
return self._task_name
base_name = camelcase_to_snakecase(self.__class__.__name__)
return name_fn(self.target_name, base_name) if self.target_name else base_name
def child_name(self, name):
return name_fn(self.task_name, name)
def set_metrics(self, metrics):
self.metrics = torch.nn.ModuleList(metrics)
def calculate_metrics( # type: ignore
self,
predictions: torch.Tensor,
targets: torch.Tensor,
) -> Dict[str, torch.Tensor]:
outputs = {}
predictions = self.forward_to_prediction_fn(cast(torch.Tensor, predictions))
from .prediction_task import BinaryClassificationTask
for metric in self.metrics:
if isinstance(metric, tuple(type(x) for x in BinaryClassificationTask.DEFAULT_METRICS)):
targets = cast(torch.Tensor, targets).int()
outputs[self.metric_name(metric)] = metric(predictions, targets)
return outputs
def compute_metrics(self, **kwargs):
return {self.metric_name(metric): metric.compute() for metric in self.metrics}
def metric_name(self, metric: tm.Metric) -> str:
return self.child_name(camelcase_to_snakecase(metric.__class__.__name__))
def reset_metrics(self):
for metric in self.metrics:
metric.reset()
def to_head(self, body, inputs=None, **kwargs) -> "Head":
return Head(body, self, inputs=inputs, **kwargs)
def to_model(self, body, inputs=None, **kwargs) -> "Model":
return Model(Head(body, self, inputs=inputs, **kwargs), **kwargs)
class Head(torch.nn.Module, LossMixin, MetricsMixin):
"""Head of a Model, a head has a single body but could have multiple prediction-tasks.
Parameters
----------
body: Block
TODO
prediction_tasks: Union[List[PredictionTask], PredictionTask], optional
TODO
task_blocks
TODO
task_weights: List[float], optional
TODO
loss_reduction: str, default="mean"
TODO
inputs: TabularFeaturesType, optional
TODO
"""
def __init__(
self,
body: BlockBase,
prediction_tasks: Union[List[PredictionTask], PredictionTask],
task_blocks: Optional[Union[BlockType, Dict[str, BlockType]]] = None,
task_weights: Optional[List[float]] = None,
loss_reduction: str = "mean",
inputs: Optional[TabularFeaturesType] = None,
):
super().__init__()
self.body = body
self.loss_reduction = loss_reduction
self.prediction_task_dict = torch.nn.ModuleDict()
if prediction_tasks:
if not isinstance(prediction_tasks, list):
prediction_tasks = [prediction_tasks]
for i, task in enumerate(prediction_tasks):
self.prediction_task_dict[task.task_name] = task
self._task_weights = defaultdict(lambda: 1.0)
if task_weights:
for task, val in zip(cast(List[PredictionTask], prediction_tasks), task_weights):
self._task_weights[task.task_name] = val
self.build(inputs=inputs, task_blocks=task_blocks)
def build(self, inputs=None, device=None, task_blocks=None):
"""Build each prediction task that's part of the head.
Parameters
----------
body
inputs
device
task_blocks
"""
if not getattr(self.body, "output_size", lambda: None)():
raise ValueError(
"Can't infer output-size of the body, please provide "
"a `Block` with a output-size. You can wrap any torch.Module in a Block."
)
input_size = self.body.output_size()
if device:
self.to(device)
for name, task in self.prediction_task_dict.items():
task_block = task_blocks
if task_blocks and isinstance(task_blocks, dict) and name in task_blocks:
task_block = task_blocks[name]
task.build(self.body, input_size, inputs=inputs, device=device, task_block=task_block)
self.input_size = input_size
@classmethod
def from_schema(
cls,
schema: Schema,
body: BlockBase,
task_blocks: Optional[Union[BlockType, Dict[str, BlockType]]] = None,
task_weight_dict: Optional[Dict[str, float]] = None,
loss_reduction: str = "mean",
inputs: Optional[TabularFeaturesType] = None,
) -> "Head":
"""Instantiate a Head from a Schema through tagged targets.
Parameters
----------
schema: DatasetSchema
Schema to use for inferring all targets based on the tags.
body
task_blocks
task_weight_dict
loss_reduction
inputs
Returns
-------
Head
"""
task_weight_dict = task_weight_dict or {}
tasks: List[PredictionTask] = []
task_weights = []
from .prediction_task import BinaryClassificationTask, RegressionTask
for binary_target in schema.select_by_tag(Tag.BINARY_CLASSIFICATION).column_names:
tasks.append(BinaryClassificationTask(binary_target))
task_weights.append(task_weight_dict.get(binary_target, 1.0))
for regression_target in schema.select_by_tag(Tag.REGRESSION).column_names:
tasks.append(RegressionTask(regression_target))
task_weights.append(task_weight_dict.get(regression_target, 1.0))
# TODO: Add multi-class classification here. Figure out how to get number of classes
return cls(
body,
tasks,
task_blocks=task_blocks,
task_weights=task_weights,
loss_reduction=loss_reduction,
inputs=inputs,
)
def pop_labels(self, inputs: TabularData) -> TabularData:
"""Pop the labels from the different prediction_tasks from the inputs.
Parameters
----------
inputs: TabularData
Input dictionary containing all targets.
Returns
-------
TabularData
"""
outputs = {}
for name in self.prediction_task_dict.keys():
outputs[name] = inputs.pop(name)
return outputs
def forward(
self,
body_outputs: Union[torch.Tensor, TabularData],
training: bool = False,
testing: bool = False,
targets: Union[torch.Tensor, TabularData] = None,
call_body: bool = False,
**kwargs,
) -> Union[torch.Tensor, TabularData]:
outputs = {}
if call_body:
body_outputs = self.body(body_outputs, training=training, testing=testing, **kwargs)
if training or testing:
losses = []
labels = {}
predictions = {}
for name, task in self.prediction_task_dict.items():
if isinstance(targets, dict):
label = targets.get(task.target_name, None)
else:
label = targets
if label is not None:
# Remove dimension `1` as merlin dataloader returns tensors of shape (-1, 1)
label = torch.squeeze(label.float(), -1)
task_output = task(
body_outputs, targets=label, training=training, testing=testing, **kwargs
)
labels[name] = task_output["labels"]
predictions[name] = task_output["predictions"]
losses.append(task_output["loss"] * self._task_weights[name])
loss_tensor = torch.stack(losses)
loss = getattr(loss_tensor, self.loss_reduction)()
outputs = {"loss": loss, "labels": labels, "predictions": predictions}
else:
for name, task in self.prediction_task_dict.items():
outputs[name] = task(
body_outputs, targets=targets, training=training, testing=testing, **kwargs
)
return outputs
def calculate_metrics( # type: ignore
self,
predictions: Union[torch.Tensor, TabularData],
targets: Union[torch.Tensor, TabularData],
) -> Dict[str, Union[Dict[str, torch.Tensor], torch.Tensor]]:
"""Calculate metrics of the task(s) set in the Head instance.
Parameters
----------
predictions: Union[torch.Tensor, TabularData]
The predictions tensors to use for calculate metrics.
They can be either a torch.Tensor if a single task is used or
a dictionary of torch.Tensor if multiple tasks are used. In the
second case, the dictionary is indexed by the tasks names.
targets:
The tensor or dictionary of targets to use for computing the metrics of
one or multiple tasks.
"""
metrics = {}
for name, task in self.prediction_task_dict.items():
label = targets
output = predictions
if isinstance(targets, dict):
# The labels are retrieved from the task's output
# and indexed by the task name.
label = targets[name]
if isinstance(predictions, dict):
output = predictions[name]
metrics.update(
task.calculate_metrics(
predictions=output,
targets=label,
)
)
return _output_metrics(metrics)
def compute_metrics(self, mode: str = None) -> Dict[str, Union[float, torch.Tensor]]:
def name_fn(x):
return "_".join([mode, x]) if mode else x
metrics = {
name_fn(name): task.compute_metrics()
for name, task in self.prediction_task_dict.items()
}
return _output_metrics(metrics)
def reset_metrics(self):
""""""
for task in self.prediction_task_dict.values():
task.reset_metrics()
@property
def task_blocks(self) -> Dict[str, Optional[BlockOrModule]]:
return {name: task.task_block for name, task in self.prediction_task_dict.items()}
def to_model(self, **kwargs) -> "Model":
"""Convert the head to a Model.
Returns
-------
Model
"""
return Model(self, **kwargs)
class Model(torch.nn.Module, LossMixin, MetricsMixin):
def __init__(
self,
*head: Head,
head_weights: Optional[List[float]] = None,
head_reduction: str = "mean",
optimizer: Type[torch.optim.Optimizer] = torch.optim.Adam,
name: str = None,
):
"""Model class that can aggregate one or multiple heads.
Parameters
----------
head: Head
One or more heads of the model.
head_weights: List[float], optional
Weight-value to use for each head.
head_reduction: str, optional
How to reduce the losses into a single tensor when multiple heads are used.
optimizer: Type[torch.optim.Optimizer]
Optimizer-class to use during fitting
name: str, optional
Name of the model.
"""
if head_weights:
if not isinstance(head_weights, list):
raise ValueError("`head_weights` must be a list")
if not len(head_weights) == len(head):
raise ValueError(
"`head_weights` needs to have the same length " "as the number of heads"
)
super().__init__()
self.name = name
self.heads = torch.nn.ModuleList(head)
self.head_weights = head_weights or [1.0] * len(head)
self.head_reduction = head_reduction
self.optimizer = optimizer
def forward(
self, inputs: TensorOrTabularData, targets=None, training=False, testing=False, **kwargs
):
# Convert inputs to float32 which is the default type, expected by PyTorch
for name, val in inputs.items():
if torch.is_floating_point(val):
inputs[name] = val.to(torch.float32)
# Squeeze second dimension `1` of non-list inputs
for name, val in inputs.items():
inputs[name] = torch.squeeze(val, -1)
if isinstance(targets, dict) and len(targets) == 0:
# `pyarrow`` dataloader is returning {} instead of None
# TODO remove this code when `PyarraowDataLoader` is dropped
targets = None
# TODO: Optimize this
if training or testing:
losses = []
labels = {}
predictions = {}
for i, head in enumerate(self.heads):
head_output = head(
inputs,
call_body=True,
targets=targets,
training=training,
testing=testing,
**kwargs,
)
labels.update(head_output["labels"])
predictions.update(head_output["predictions"])
losses.append(head_output["loss"] * self.head_weights[i])
loss_tensor = torch.stack(losses)
loss = getattr(loss_tensor, self.head_reduction)()
if len(labels) == 1:
labels = list(labels.values())[0]
predictions = list(predictions.values())[0]
return {"loss": loss, "labels": labels, "predictions": predictions}
else:
outputs = {}
for head in self.heads:
outputs.update(
head(
inputs,
call_body=True,
targets=targets,
training=training,
testing=testing,
**kwargs,
)
)
if len(outputs) == 1:
return list(outputs.values())[0]
return outputs
def calculate_metrics( # type: ignore
self,
predictions: Union[torch.Tensor, TabularData],
targets: Union[torch.Tensor, TabularData],
) -> Dict[str, Union[Dict[str, torch.Tensor], torch.Tensor]]:
"""Calculate metrics of the task(s) set in the Head instance.
Parameters
----------
predictions: Union[torch.Tensor, TabularData]
The predictions tensors returned by the model.
They can be either a torch.Tensor if a single task is used or
a dictionary of torch.Tensor if multiple heads/tasks are used. In the
second case, the dictionary is indexed by the tasks names.
targets:
The tensor or dictionary of targets returned by the model.
They are used for computing the metrics of one or multiple tasks.
"""
outputs = {}
for head in self.heads:
outputs.update(
head.calculate_metrics(
predictions,
targets,
)
)
return outputs
def compute_metrics(self, mode=None) -> Dict[str, Union[float, torch.Tensor]]:
metrics = {}
for head in self.heads:
metrics.update(head.compute_metrics(mode=mode))
return metrics
def reset_metrics(self):
for head in self.heads:
head.reset_metrics()
def to_lightning(self):
import pytorch_lightning as pl
parent_self = self
class BlockWithHeadLightning(pl.LightningModule):
def __init__(self):
super(BlockWithHeadLightning, self).__init__()
self.parent = parent_self
def forward(self, inputs, targets=None, training=False, testing=False, *args, **kwargs):
return self.parent(
inputs, targets=targets, training=training, testing=testing, *args, **kwargs
)
def training_step(self, batch, batch_idx, targets=None, training=True, testing=False):
loss = self.parent(*batch, targets=targets, training=training, testing=testing)[
"loss"
]
self.log("train_loss", loss)
return loss
def configure_optimizers(self):
optimizer = self.parent.optimizer(self.parent.parameters(), lr=1e-3)
return optimizer
return BlockWithHeadLightning()
def fit(
self,
dataloader,
optimizer=torch.optim.Adam,
eval_dataloader=None,
num_epochs=1,
amp=False,
train=True,
verbose=True,
compute_metric=True,
):
if isinstance(dataloader, torch.utils.data.DataLoader):
dataset = dataloader.dataset
else:
dataset = dataloader
if inspect.isclass(optimizer):
optimizer = optimizer(self.parameters())
self.train(mode=train)
epoch_losses = []
with torch.set_grad_enabled(mode=train):
for epoch in range(num_epochs):
losses = []
batch_iterator = enumerate(iter(dataset))
if verbose:
batch_iterator = tqdm(batch_iterator)
for batch_idx, (x, y) in batch_iterator:
if amp:
with torch.cuda.amp.autocast():
output = self(x, targets=y, training=True)
else:
output = self(x, targets=y, training=True)
losses.append(float(output["loss"]))
if compute_metric:
self.calculate_metrics(
output["predictions"],
targets=output["labels"],
)
if train:
optimizer.zero_grad()
output["loss"].backward()
optimizer.step()
if verbose:
print(self.compute_metrics(mode="train"))
if eval_dataloader:
print(self.evaluate(eval_dataloader, verbose=False))
epoch_losses.append(np.mean(losses))
return np.array(epoch_losses)
def evaluate(
self, dataloader, targets=None, training=False, testing=True, verbose=True, mode="eval"
):
if isinstance(dataloader, torch.utils.data.DataLoader):
dataset = dataloader.dataset
else:
dataset = dataloader
batch_iterator = enumerate(iter(dataset))
if verbose:
batch_iterator = tqdm(batch_iterator)
self.reset_metrics()
for batch_idx, (x, y) in batch_iterator:
output = self(x, targets=y, training=training, testing=testing)
self.calculate_metrics(
output["predictions"],
targets=output["labels"],
)
return self.compute_metrics(mode=mode)
def _get_name(self):
if self.name:
return self.name
return super(Model, self)._get_name()
@property
def input_schema(self):
# return the input schema given by the model
# loop over the heads to get input schemas
schemas = []
for head in self.heads:
schemas.append(head.body.inputs.schema)
model_schema = sum(schemas, Schema())
# TODO: rework T4R to use Merlin Schemas.
# In the meantime, we convert model_schema to merlin core schema
core_schema = Core_Schema()
for column in model_schema:
name = column.name
dtype = {0: np.float32, 2: np.int64, 3: np.float32}[column.type]
tags = column.tags
is_list = column.value_count.max > 0
int_domain = {"min": column.int_domain.min, "max": column.int_domain.max}
properties = {
"int_domain": int_domain,
}
col_schema = ColumnSchema(
name,
dtype=dtype,
tags=tags,
properties=properties,
is_list=is_list,
is_ragged=False,
)
core_schema[name] = col_schema
return core_schema
@property
def output_schema(self):
from .prediction_task import BinaryClassificationTask, RegressionTask
# if the model has one head with one task, the output is a tensor
# if multiple heads and/or multiple prediction task, the output is a dictionary
output_cols = []
for head in self.heads:
for name, task in head.prediction_task_dict.items():
target_dim = task.target_dim
int_domain = {"min": target_dim, "max": target_dim}
if (
isinstance(task, (BinaryClassificationTask, RegressionTask))
and not task.summary_type
):
is_list = True
else:
is_list = False
properties = {
"int_domain": int_domain,
}
col_schema = ColumnSchema(
name, dtype=np.float32, properties=properties, is_list=is_list, is_ragged=False
)
output_cols.append(col_schema)
return Core_Schema(output_cols)
def save(self, path: Union[str, os.PathLike], model_name="t4rec_model_class"):
"""Saves the model to f"{export_path}/{model_name}.pkl" using `cloudpickle`
Parameters
----------
path : Union[str, os.PathLike]
Path to the directory where the T4Rec model should be saved.
model_name : str, optional
the name given to the pickle file storing the T4Rec model,
by default 't4rec_model_class'
"""
try:
import cloudpickle
except ImportError:
raise ValueError("cloudpickle is required to save model class")
export_path = pathlib.Path(path)
export_path.mkdir(exist_ok=True)
model_name = model_name + ".pkl"
export_path = export_path / model_name
with open(export_path, "wb") as out:
cloudpickle.dump(self, out)
@classmethod
def load(cls, path: Union[str, os.PathLike], model_name="t4rec_model_class") -> "Model":
"""Loads a T4Rec model that was saved with `model.save()`.
Parameters
----------
path : Union[str, os.PathLike]
Path to the directory where the T4Rec model is saved.
model_name : str, optional
the name given to the pickle file storing the T4Rec model,
by default 't4rec_model_class'.
"""
try:
import cloudpickle
except ImportError:
raise ValueError("cloudpickle is required to load T4Rec model")
export_path = pathlib.Path(path)
model_name = model_name + ".pkl"
export_path = export_path / model_name
return cloudpickle.load(open(export_path, "rb"))
def _output_metrics(metrics):
# If there is only a single head with metrics, returns just those metrics
if len(metrics) == 1 and isinstance(metrics[list(metrics.keys())[0]], dict):
return metrics[list(metrics.keys())[0]]
return metrics