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

improve: update InfoLM class to dynamically set higher_is_better #2674

Merged
merged 15 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
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
25 changes: 24 additions & 1 deletion src/torchmetrics/text/infolm.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@
__doctest_skip__ = ["InfoLM", "InfoLM.plot"]


_information_measure_higher_is_better = {
# following values are <0
"kl_divergence": True,
"alpha_divergence": True,
# following values are >0
"beta_divergence": False,
"ab_divergence": False,
"renyi_divergence": False,
"l1_distance": False,
"l2_distance": False,
"l_infinity_distance": False,
"fisher_rao_distance": False,
}


class InfoLM(Metric):
"""Calculate `InfoLM`_.

Expand Down Expand Up @@ -111,7 +126,6 @@ class InfoLM(Metric):
"""

is_differentiable = False
higher_is_better = True
preds_input_ids: List[Tensor]
preds_attention_mask: List[Tensor]
target_input_ids: List[Tensor]
Expand Down Expand Up @@ -156,6 +170,15 @@ def __init__(
self.add_state("target_input_ids", [], dist_reduce_fx="cat")
self.add_state("target_attention_mask", [], dist_reduce_fx="cat")

@property
def higher_is_better(self) -> bool:
"""Returns a bool indicating whether a higher value of the information measure is better.

Done this way as depends on if the information measure is positive or negative.

"""
return _information_measure_higher_is_better[self.information_measure]

def update(self, preds: Union[str, Sequence[str]], target: Union[str, Sequence[str]]) -> None:
"""Update state with predictions and targets."""
preds_input_ids, preds_attention_mask, target_input_ids, target_attention_mask = _infolm_update(
Expand Down
17 changes: 16 additions & 1 deletion tests/unittests/text/test_infolm.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import pytest
import torch
from torchmetrics.functional.text.infolm import infolm
from torchmetrics.text.infolm import InfoLM
from torchmetrics.text.infolm import InfoLM, _information_measure_higher_is_better
from torchmetrics.utilities.imports import _TRANSFORMERS_GREATER_EQUAL_4_4

from unittests._helpers import skip_on_connection_issues
Expand Down Expand Up @@ -182,3 +182,18 @@ def test_infolm_differentiability(self, preds, targets, information_measure, idf
metric_functional=infolm,
metric_args=metric_args,
)

@skip_on_connection_issues()
def test_infolm_higher_is_better_property(self, preds, targets, information_measure, idf, alpha, beta):
"""Test the `higher_is_better` property of the metric."""
metric_args = {
"model_name_or_path": MODEL_NAME,
"information_measure": information_measure,
"idf": idf,
"alpha": alpha,
"beta": beta,
"max_length": MAX_LENGTH,
}

metric = InfoLM(**metric_args)
assert metric.higher_is_better == _information_measure_higher_is_better[information_measure]
Loading