Shortcuts

pytorch_lightning.metrics.converters module

This file provides functions and decorators for automated input and output conversion to/from numpy.ndarray and torch.Tensor as well as utilities to sync tensors between different processes in a DDP scenario, when needed.

pytorch_lightning.metrics.converters._apply_to_inputs(func_to_apply, *dec_args, **dec_kwargs)[source]

Decorator function to apply a function to all inputs of a function.

Parameters
  • func_to_apply (Callable) – the function to apply to the inputs

  • *dec_args – positional arguments for the function to be applied

  • **dec_kwargs – keyword arguments for the function to be applied

Return type

Callable

Returns

the decorated function

pytorch_lightning.metrics.converters._apply_to_outputs(func_to_apply, *dec_args, **dec_kwargs)[source]

Decorator function to apply a function to all outputs of a function.

Parameters
  • func_to_apply (Callable) – the function to apply to the outputs

  • *dec_args – positional arguments for the function to be applied

  • **dec_kwargs – keyword arguments for the function to be applied

Return type

Callable

Returns

the decorated function

pytorch_lightning.metrics.converters._convert_to_numpy(data)[source]

Convert all tensors and numpy arrays to numpy arrays.

Parameters

data (Union[Tensor, ndarray, Number]) – the tensor or array to convert to numpy

Return type

ndarray

Returns

the resulting numpy array

pytorch_lightning.metrics.converters._convert_to_tensor(data)[source]

Maps all kind of collections and numbers to tensors.

Parameters

data (Any) – the data to convert to tensor

Return type

Any

Returns

the converted data

pytorch_lightning.metrics.converters._numpy_metric_conversion(func_to_decorate)[source]

Decorator handling the argument conversion for metrics working on numpy. All inputs of the decorated function will be converted to numpy and all outputs will be converted to tensors.

Parameters

func_to_decorate (Callable) – the function whose inputs and outputs shall be converted

Return type

Callable

Returns

the decorated function

pytorch_lightning.metrics.converters._numpy_metric_input_conversion(func_to_decorate)[source]

Decorator converting all inputs of a function to numpy

Parameters

func_to_decorate (Callable) – the function whose inputs shall be converted

Returns

the decorated function

Return type

Callable

pytorch_lightning.metrics.converters._sync_ddp_if_available(result, group=None, reduce_op=None)[source]

Function to reduce the tensors from several ddp processes to one master process

Parameters
  • result (Tensor) – the value to sync and reduce (typically tensor or number)

  • group (Optional[Any]) – the process group to gather results from. Defaults to all processes (world)

  • reduce_op (Optional[ReduceOp]) – the reduction operation. Defaults to sum.

Return type

Tensor

Returns

reduced value

pytorch_lightning.metrics.converters._tensor_collection_metric_conversion(func_to_decorate)[source]

Decorator Handling the argument conversion for metrics working on tensors. All inputs of the decorated function and all numpy arrays and numbers in it’s outputs will be converted to tensors

Parameters

func_to_decorate (Callable) – the function whose inputs and outputs shall be converted

Return type

Callable

Returns

the decorated function

pytorch_lightning.metrics.converters._tensor_collection_metric_output_conversion(func_to_decorate)[source]

Decorator converting all numpy arrays and numbers occuring in the outputs of a function to tensors

Parameters

func_to_decorate (Callable) – the function whose outputs shall be converted

Returns

the decorated function

Return type

Callable

pytorch_lightning.metrics.converters._tensor_metric_conversion(func_to_decorate)[source]

Decorator Handling the argument conversion for metrics working on tensors. All inputs and outputs of the decorated function will be converted to tensors

Parameters

func_to_decorate (Callable) – the function whose inputs and outputs shall be converted

Return type

Callable

Returns

the decorated function

pytorch_lightning.metrics.converters._tensor_metric_input_conversion(func_to_decorate)[source]

Decorator converting all inputs of a function to tensors

Parameters

func_to_decorate (Callable) – the function whose inputs shall be converted

Returns

the decorated function

Return type

Callable

pytorch_lightning.metrics.converters._tensor_metric_output_conversion(func_to_decorate)[source]

Decorator converting all outputs of a function to tensors

Parameters

func_to_decorate (Callable) – the function whose outputs shall be converted

Returns

the decorated function

Return type

Callable

pytorch_lightning.metrics.converters.numpy_metric(group=None, reduce_op=None)[source]

This decorator shall be used on all function metrics working on numpy arrays. It handles the argument conversion and DDP reduction for metrics working on numpy. All inputs of the decorated function will be converted to numpy and all outputs will be converted to tensors. In DDP Training all output tensors will be reduced according to the given rules.

Parameters
  • group (Optional[Any]) – the process group to gather results from. Defaults to all processes (world)

  • reduce_op (Optional[ReduceOp]) – the reduction operation. Defaults to sum

Return type

Callable

Returns

the decorated function

pytorch_lightning.metrics.converters.sync_ddp(group=None, reduce_op=None)[source]

This decorator syncs a functions outputs across different processes for DDP.

Parameters
  • group (Optional[Any]) – the process group to gather results from. Defaults to all processes (world)

  • reduce_op (Optional[ReduceOp]) – the reduction operation. Defaults to sum

Return type

Callable

Returns

the decorated function

pytorch_lightning.metrics.converters.tensor_collection_metric(group=None, reduce_op=None)[source]

This decorator shall be used on all function metrics working on tensors and returning collections that cannot be converted to tensors. It handles the argument conversion and DDP reduction for metrics working on tensors. All inputs and outputs of the decorated function will be converted to tensors. In DDP Training all output tensors will be reduced according to the given rules.

Parameters
  • group (Optional[Any]) – the process group to gather results from. Defaults to all processes (world)

  • reduce_op (Optional[ReduceOp]) – the reduction operation. Defaults to sum

Return type

Callable

Returns

the decorated function

pytorch_lightning.metrics.converters.tensor_metric(group=None, reduce_op=None)[source]

This decorator shall be used on all function metrics working on tensors. It handles the argument conversion and DDP reduction for metrics working on tensors. All inputs and outputs of the decorated function will be converted to tensors. In DDP Training all output tensors will be reduced according to the given rules.

Parameters
  • group (Optional[Any]) – the process group to gather results from. Defaults to all processes (world)

  • reduce_op (Optional[ReduceOp]) – the reduction operation. Defaults to sum

Return type

Callable

Returns

the decorated function