Shortcuts

pytorch_lightning.metrics.metric module

class pytorch_lightning.metrics.metric.Metric(name)[source]

Bases: pytorch_lightning.utilities.device_dtype_mixin.DeviceDtypeModuleMixin, torch.nn.Module, abc.ABC

Abstract base class for metric implementation.

Should be used to implement metrics that 1. Return multiple Outputs 2. Handle their own DDP sync

Parameters

name (str) – the metric’s name

abstract forward(*args, **kwargs)[source]

Implements the actual metric computation.

Return type

Tensor

Returns

metric value

class pytorch_lightning.metrics.metric.NumpyMetric(name, reduce_group=None, reduce_op=None)[source]

Bases: pytorch_lightning.metrics.metric.Metric

Base class for metric implementation operating on numpy arrays. All inputs will be casted to numpy if necessary and all outputs will be casted to tensors if necessary. Already handles DDP sync and input/output conversions.

Parameters
  • name (str) – the metric’s name

  • reduce_group (Optional[Any]) – the process group for DDP reduces (only needed for DDP training). Defaults to all processes (world)

  • reduce_op (Optional[Any]) – the operation to perform during reduction within DDP (only needed for DDP training). Defaults to sum.

__call__(*args, **kwargs)[source]

Call self as a function.

Return type

Tensor

class pytorch_lightning.metrics.metric.TensorCollectionMetric(name, reduce_group=None, reduce_op=None)[source]

Bases: pytorch_lightning.metrics.metric.Metric

Base class for metric implementation operating directly on tensors. All inputs will be casted to tensors if necessary. Outputs won’t be casted. Already handles DDP sync and input conversions.

This class differs from TensorMetric, as it assumes all outputs to be collections of tensors and does not explicitly convert them. This is necessary, since some collections (like for ROC, Precision-Recall Curve etc.) cannot be converted to tensors at the highest level. All numpy arrays and numbers occuring in these outputs will still be converted.

Use this class as a baseclass, whenever you want to ensure inputs are tensors and outputs cannot be converted to tensors automatically

Parameters
  • name (str) – the metric’s name

  • reduce_group (Optional[Any]) – the process group for DDP reduces (only needed for DDP training). Defaults to all processes (world)

  • reduce_op (Optional[Any]) – the operation to perform during reduction within DDP (only needed for DDP training). Defaults to sum.

__call__(*args, **kwargs)[source]

Call self as a function.

Return type

Tensor

class pytorch_lightning.metrics.metric.TensorMetric(name, reduce_group=None, reduce_op=None)[source]

Bases: pytorch_lightning.metrics.metric.Metric

Base class for metric implementation operating directly on tensors. All inputs and outputs will be casted to tensors if necessary. Already handles DDP sync and input/output conversions.

Parameters
  • name (str) – the metric’s name

  • reduce_group (Optional[Any]) – the process group for DDP reduces (only needed for DDP training). Defaults to all processes (world)

  • reduce_op (Optional[Any]) – the operation to perform during reduction within DDP (only needed for DDP training). Defaults to sum.

__call__(*args, **kwargs)[source]

Call self as a function.

Return type

Tensor