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

Initialize default metrics/loss inside ModelOutput instead #1164

Merged
merged 5 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
16 changes: 7 additions & 9 deletions merlin/models/torch/outputs/classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,22 @@ class BinaryOutput(ModelOutput):
The metrics used for evaluation. Default includes Accuracy, AUROC, Precision, and Recall.
"""

DEFAULT_LOSS_CLS = nn.BCEWithLogitsLoss
DEFAULT_METRICS_CLS = (Accuracy, AUROC, Precision, Recall)

def __init__(
self,
schema: Optional[ColumnSchema] = None,
loss: nn.Module = nn.BCEWithLogitsLoss(),
metrics: Sequence[Metric] = (
Accuracy(task="binary"),
AUROC(task="binary"),
Precision(task="binary"),
Recall(task="binary"),
),
loss: Optional[nn.Module] = None,
metrics: Sequence[Metric] = (),
):
"""Initializes a BinaryOutput object."""
super().__init__(
nn.LazyLinear(1),
nn.Sigmoid(),
schema=schema,
loss=loss,
metrics=metrics,
loss=loss or self.DEFAULT_LOSS_CLS(),
metrics=metrics or [m(task="binary") for m in self.DEFAULT_METRICS_CLS],
)

def setup_schema(self, target: Optional[Union[ColumnSchema, Schema]]):
Expand Down
11 changes: 7 additions & 4 deletions merlin/models/torch/outputs/regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,21 @@ class RegressionOutput(ModelOutput):
The metrics used for evaluation. Default is MeanSquaredError.
"""

DEFAULT_LOSS_CLS = nn.MSELoss
DEFAULT_METRICS_CLS = (MeanSquaredError,)

def __init__(
self,
schema: Optional[ColumnSchema] = None,
loss: nn.Module = nn.MSELoss(),
metrics: Sequence[Metric] = (MeanSquaredError(),),
loss: Optional[nn.Module] = None,
metrics: Sequence[Metric] = (),
):
"""Initializes a RegressionOutput object."""
super().__init__(
nn.LazyLinear(1),
schema=schema,
loss=loss,
metrics=metrics,
loss=loss or self.DEFAULT_LOSS_CLS(),
metrics=metrics or [m() for m in self.DEFAULT_METRICS_CLS],
)

def setup_schema(self, target: Optional[Union[ColumnSchema, Schema]]):
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/torch/outputs/test_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ def test_init(self):

assert isinstance(binary_output, mm.BinaryOutput)
assert isinstance(binary_output.loss, nn.BCEWithLogitsLoss)
assert binary_output.metrics == (
assert binary_output.metrics == [
Accuracy(task="binary"),
AUROC(task="binary"),
Precision(task="binary"),
Recall(task="binary"),
)
]
assert binary_output.output_schema == Schema()

def test_identity(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/torch/outputs/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_init(self):

assert isinstance(reg_output, mm.RegressionOutput)
assert isinstance(reg_output.loss, nn.MSELoss)
assert reg_output.metrics == (MeanSquaredError,)
assert reg_output.metrics == [MeanSquaredError()]
assert reg_output.output_schema == Schema()

def test_identity(self):
Expand Down