Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CategoricalOutput #1158

Merged
merged 32 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
a8d27cd
Adding extra test for leaf
marcromeyn Jun 22, 2023
d8d7f9e
Some fixes
marcromeyn Jun 22, 2023
99caf67
First commit
marcromeyn Jun 22, 2023
03e90d1
Adding logits_temperature scaling
marcromeyn Jun 26, 2023
13b9e17
move schema and metrics from output base to children
edknv Jun 27, 2023
86a0f6f
add docstrings
edknv Jun 27, 2023
a844804
delay class initialization in add_route_for_each
edknv Jun 27, 2023
370d40a
add unit tests
edknv Jun 27, 2023
f0adf4a
add docstrings
edknv Jun 27, 2023
6e7ca37
add more unit tests
edknv Jun 27, 2023
d8224fd
lint
edknv Jun 27, 2023
80f94a5
100% test coverage in torch/outputs
edknv Jun 28, 2023
89dce42
In-complete commit
marcromeyn Jun 28, 2023
39d73f7
First pass over proposed-API for weight-tying
marcromeyn Jun 28, 2023
ed7dc0c
Increase test-coverage for new design w.r.t. weight-tying
marcromeyn Jun 29, 2023
8c225e0
Fixing rebase bugs
marcromeyn Jun 30, 2023
4281058
Change default metrics of CategoricalOutput to retrieval-metrics
marcromeyn Jun 30, 2023
a9f0342
Merge branch 'main' into torch/categorical-pred
edknv Jul 1, 2023
10cf3da
Merge branch 'main' into torch/categorical-pred
marcromeyn Jul 3, 2023
53ed2cd
Running linting
marcromeyn Jul 3, 2023
8dd234e
Remove default_metrics method from RegressionOutput
marcromeyn Jul 3, 2023
8f20448
Merge branch 'main' into torch/categorical-pred
marcromeyn Jul 3, 2023
9dcf6de
Merge branch 'main' into torch/categorical-pred
marcromeyn Jul 4, 2023
71eb600
Merge branch 'main' into torch/categorical-pred
marcromeyn Jul 4, 2023
0d72145
Fixing metrics of BinaryOutput
marcromeyn Jul 5, 2023
9553995
Merge branch 'main' into torch/categorical-pred
marcromeyn Jul 5, 2023
2b565a8
Fixing contents of ValueError in feature_weights
marcromeyn Jul 5, 2023
7c34037
Merge branch 'main' into torch/categorical-pred
marcromeyn Jul 5, 2023
823d9dc
Fixing failing tests
marcromeyn Jul 5, 2023
ce61834
Fixing failing tests
marcromeyn Jul 5, 2023
da1366c
Fixing failing tests
marcromeyn Jul 5, 2023
258bb71
Merge branch 'main' into torch/categorical-pred
marcromeyn Jul 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion merlin/models/torch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@
from merlin.models.torch.models.base import Model
from merlin.models.torch.models.ranking import DLRMModel
from merlin.models.torch.outputs.base import ModelOutput
from merlin.models.torch.outputs.classification import BinaryOutput
from merlin.models.torch.outputs.classification import (
BinaryOutput,
CategoricalOutput,
CategoricalTarget,
EmbeddingTablePrediction,
)
from merlin.models.torch.outputs.regression import RegressionOutput
from merlin.models.torch.outputs.tabular import TabularOutputBlock
from merlin.models.torch.router import RouterBlock
Expand All @@ -40,6 +45,7 @@
"Batch",
"BinaryOutput",
"Block",
"DLRMBlock",
"MLPBlock",
"Model",
"EmbeddingTable",
Expand All @@ -60,6 +66,9 @@
"Concat",
"Stack",
"schema",
"CategoricalOutput",
"CategoricalTarget",
"EmbeddingTablePrediction",
"input_schema",
"output_schema",
"feature_schema",
Expand Down
8 changes: 8 additions & 0 deletions merlin/models/torch/inputs/embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,14 @@ def update_feature(self, col_schema: ColumnSchema) -> "EmbeddingTable":

return self

def feature_weights(self, name: str):
if name not in self.domains:
raise ValueError(f"{name} not found in table: {self}")

domain = self.domains[name]

return self.table.weight[int(domain.min) : int(domain.max)]

def select(self, selection: Selection) -> Selectable:
selected = select(self.input_schema, selection)

Expand Down
37 changes: 17 additions & 20 deletions merlin/models/torch/outputs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import inspect
from copy import deepcopy
from typing import Optional, Sequence

Expand All @@ -21,7 +22,7 @@
from torchmetrics import Metric

from merlin.models.torch.block import Block
from merlin.schema import ColumnSchema, Schema
from merlin.models.torch.transforms.bias import LogitsTemperatureScaler


class ModelOutput(Block):
Expand All @@ -47,44 +48,34 @@ class ModelOutput(Block):

Parameters
----------
schema: Optional[ColumnSchema]
The schema defining the column properties.
loss: nn.Module
The loss function used for training.
metrics: Sequence[Metric]
The metrics used for evaluation.
logits_temperature: float, optional
Parameter used to reduce model overconfidence, so that logits / T.
by default 1.0
name: Optional[str]
The name of the model output.
"""

def __init__(
self,
*module: nn.Module,
schema: Optional[ColumnSchema] = None,
loss: Optional[nn.Module] = None,
metrics: Sequence[Metric] = (),
metrics: Optional[Sequence[Metric]] = None,
logits_temperature: float = 1.0,
name: Optional[str] = None,
):
"""Initializes a ModelOutput object."""
super().__init__(*module, name=name)

self.loss = loss
self.metrics = metrics
self.output_schema: Schema = Schema()

if schema:
self.setup_schema(schema)
self.create_target_buffer()

def setup_schema(self, schema: Optional[ColumnSchema]):
"""Set up the schema for the output.

Parameters
----------
schema: ColumnSchema or None
The schema defining the column properties.
"""
self.output_schema = Schema([schema])
if logits_temperature != 1.0:
self.append(LogitsTemperatureScaler(logits_temperature))

def create_target_buffer(self):
self.register_buffer("target", torch.zeros(1, dtype=torch.float32))
Expand All @@ -103,18 +94,24 @@ def eval(self):
return self.train(False)

def copy(self):
metrics = self.metrics
metrics = deepcopy(self.metrics)
self.metrics = []

output = deepcopy(self)

copied_metrics = []
for metric in metrics:
m = metric.__class__()
params = inspect.signature(metric.__class__.__init__).parameters
kwargs = {}
for arg_name, arg_value in params.items():
if arg_name in metric.__dict__:
kwargs[arg_name] = metric.__dict__[arg_name]
m = metric.__class__(**kwargs)
m.load_state_dict(metric.state_dict())
copied_metrics.append(m)

self.metrics = metrics
output.metrics = copied_metrics
output.loss = deepcopy(self.loss)

return output
Loading