Skip to content

Classwise metrics wrapper¤

Classwise metrics wrapper.

ClasswiseWrapperWithMean ¤

Bases: ClasswiseWrapper

Classwise wrapper with mean.

This metric works together with classification metrics that returns multiple values (one value per class) such that label information can be automatically included in the output. It extends the standard torchmetrics wrapper that does this, adding in an additional mean value (across all classes).

Source code in sparse_autoencoder/metrics/wrappers/classwise.py
 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
class ClasswiseWrapperWithMean(ClasswiseWrapper):
    """Classwise wrapper with mean.

    This metric works together with classification metrics that returns multiple values (one value
    per class) such that label information can be automatically included in the output. It extends
    the standard torchmetrics wrapper that does this, adding in an additional mean value (across all
    classes).
    """

    _prefix: str

    labels: list[str]

    def __init__(
        self,
        metric: Metric,
        component_names: list[str] | None = None,
        prefix: str | None = None,
    ) -> None:
        """Initialise the classwise wrapper.

        Args:
            metric: Metric to wrap.
            component_names: Component names.
            prefix: Prefix for the name (will replace the default of the class name).
        """
        super().__init__(metric, component_names, prefix)

        # Default prefix
        if not self._prefix:
            self._prefix = f"{self.metric.__class__.__name__.lower()}"

    def _convert(self, x: Tensor) -> dict[str, Tensor]:
        """Convert the input tensor to a dictionary of metrics.

        Args:
            x: The input tensor.

        Returns:
            A dictionary of metric results.
        """
        # Add a component axis if not present (as Metric squeezes it out)
        if x.ndim == 0:
            x = x.unsqueeze(dim=0)

        # Same splitting as the original classwise wrapper
        res = {f"{self._prefix}/{lab}": val for lab, val in zip(self.labels, x)}

        # Add in the mean
        res[f"{self._prefix}/mean"] = x.mean(0, dtype=torch.float)

        return res

__init__(metric, component_names=None, prefix=None) ¤

Initialise the classwise wrapper.

Parameters:

Name Type Description Default
metric Metric

Metric to wrap.

required
component_names list[str] | None

Component names.

None
prefix str | None

Prefix for the name (will replace the default of the class name).

None
Source code in sparse_autoencoder/metrics/wrappers/classwise.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def __init__(
    self,
    metric: Metric,
    component_names: list[str] | None = None,
    prefix: str | None = None,
) -> None:
    """Initialise the classwise wrapper.

    Args:
        metric: Metric to wrap.
        component_names: Component names.
        prefix: Prefix for the name (will replace the default of the class name).
    """
    super().__init__(metric, component_names, prefix)

    # Default prefix
    if not self._prefix:
        self._prefix = f"{self.metric.__class__.__name__.lower()}"