padl.util_transforms

Extra useful Transforms.

class padl.util_transforms.IfEval(if_: padl.transforms.Transform, else_: Optional[padl.transforms.Transform] = None)

Perform if_ if called in “eval” mode, else perform else_.

Parameters
  • if – Transform for the “eval” mode.

  • else – Transform otherwise (defaults to the identity transform).

class padl.util_transforms.IfInMode(if_: padl.transforms.Transform, target_mode: Literal['infer', 'eval', 'train'], else_: Optional[padl.transforms.Transform] = None)

Perform if_ if called in mode target_mode, else perform else_.

Example:

>>> from padl import transform
>>> a = transform(lambda x: x + 10)
>>> b = transform(lambda x: x * 99)
>>> iim = IfInMode(a, 'infer', b)
>>> iim.infer_apply(1)
11
>>> list(iim.eval_apply([1]))
[99]
Parameters
  • if – Transform to apply when the mode matches.

  • target_mode – Mode (one of ‘train’, ‘eval’, ‘infer’).

  • else – Transform to apply when the mode doesn’t match (defaults to identity transform).

class padl.util_transforms.IfInfer(if_: padl.transforms.Transform, else_: Optional[padl.transforms.Transform] = None)

Perform if_ if called in “infer” mode, else perform else_.

Parameters
  • if – Transform for the “infer” mode.

  • else – Transform otherwise (defaults to the identity transform).

class padl.util_transforms.IfTrain(if_: padl.transforms.Transform, else_: Optional[padl.transforms.Transform] = None)

Perform if_ if called in “train” mode, else perform else_.

Parameters
  • if – Transform for the “train” mode.

  • else – Transform otherwise (defaults to the identity transform).

class padl.util_transforms.Try(transform: padl.transforms.Transform, catch_transform: padl.transforms.Transform, exceptions: Union[List, Tuple, Exception], else_transform: padl.transforms.Transform = Identity(), finally_transform: padl.transforms.Transform = Identity(), pd_name: Optional[str] = None)

Perform transform. If this fails with any exception from exceptions, perform catch_transform. If transform is completed successfully, else_transform is performed with the output of transform. Regardless of any error occurring on the other Transform (or not), finally_transform is carried out. No change of mode can happen inside any of these Transform.

Parameters
  • transform – Transform to try.

  • catch_transform – Transform to fall back on.

  • exceptions – Catch conditions.

  • else_transform – Transform to carry on the else clause of the try statement.

  • finally_transform – Transform to carry on the finally clause of the try statement.

  • pd_name – The Transform’s name.