Skip to content

doc

Define preprocessors used in documentation tutorials.

Classes:

Name Description
Scaler

Air quality, ECG, HAR and Gas Price preprocessors.

ScaleImage

MNIST preprocessor, given an image in range [0, 256], scale the pixel values to [-1 ,1].

Scaler(input_size: tuple[int, ...], mean: torch.Tensor, scale: torch.Tensor) #

Air quality, ECG, HAR and Gas Price preprocessors.

Methods:

Name Description
transform

Transform.

inverse_transform

Apply inverse transform.

Source code in src/xpdeep/dataset/preprocessor/zoo/doc.py
def __init__(self, input_size: tuple[int, ...], mean: torch.Tensor, scale: torch.Tensor):
    super().__init__(input_size=input_size)
    # Saved as buffer for torch.export: saved loaded with `state_dict` but not optimized with `optimizer.step()
    self.register_buffer("mean", mean)
    self.register_buffer("scale", scale)

transform(inputs: torch.Tensor) -> torch.Tensor #

Transform.

Source code in src/xpdeep/dataset/preprocessor/zoo/doc.py
def transform(self, inputs: torch.Tensor) -> torch.Tensor:
    """Transform."""
    return (inputs - self.mean) / self.scale  # type: ignore[operator]

inverse_transform(output: torch.Tensor) -> torch.Tensor #

Apply inverse transform.

Source code in src/xpdeep/dataset/preprocessor/zoo/doc.py
def inverse_transform(self, output: torch.Tensor) -> torch.Tensor:
    """Apply inverse transform."""
    return output * self.scale + self.mean  # type: ignore[operator]

ScaleImage(input_size: tuple[int, ...], module_transform: torch.nn.Module | None = None, module_inverse_transform: torch.nn.Module | None = None, **additional_attributes: object) #

MNIST preprocessor, given an image in range [0, 256], scale the pixel values to [-1 ,1].

Methods:

Name Description
transform

Transform.

inverse_transform

Apply inverse transform.

Source code in src/xpdeep/dataset/preprocessor/preprocessor.py
def __init__(
    self,
    input_size: tuple[int, ...],
    module_transform: torch.nn.Module | None = None,
    module_inverse_transform: torch.nn.Module | None = None,
    **additional_attributes: object,
):
    """Initialize the preprocessor.

    Parameters
    ----------
    input_size : tuple[int, ...]
        The dimensions of the data that the preprocessor expects, excluding the batch size.
        `input_size` must match the dimensions of the data in your dataset.
        - Set an empty tuple `()` if no specific dimensions are provided (e.g., for scalar values).
        - Or by example,For an array of size `(3, 2)` in your dataset, set `input_size` to `(3, 2)`.
    module_transform : torch.nn.Module | None
        A PyTorch module to preprocess data from the raw input space to the preprocessed space.
        If `transform` is not inherited, this will override the default `transform` method.
    module_inverse_transform : torch.nn.Module | None
        A PyTorch module to reverse the preprocessing, converting data from the preprocessed space
        back to the raw input space.
        If `inverse_transform` is not inherited, this will override the default `inverse_transform` method.
    **additional_attributes : object
        Any additional keyword arguments can be passed when instantiating a TorchPreprocessor or a child class.
        These arguments will be set as class attributes.
        It can be especially useful in order to better customize the implementation of `transform` and
        `ìnverse_transform` methods.
    """
    super().__init__()

    self.input_size = input_size
    self.ward = True
    self.module_transform = module_transform
    self.module_inverse_transform = module_inverse_transform

    for additional_attr_name, additional_attr_value in additional_attributes.items():
        setattr(self, additional_attr_name, additional_attr_value)

transform(inputs: torch.Tensor) -> torch.Tensor #

Transform.

Source code in src/xpdeep/dataset/preprocessor/zoo/doc.py
def transform(self, inputs: torch.Tensor) -> torch.Tensor:  # noqa: PLR6301
    """Transform."""
    return inputs / 128.0 - 1.0

inverse_transform(output: torch.Tensor) -> torch.Tensor #

Apply inverse transform.

Source code in src/xpdeep/dataset/preprocessor/zoo/doc.py
def inverse_transform(self, output: torch.Tensor) -> torch.Tensor:  # noqa: PLR6301
    """Apply inverse transform."""
    return (output + 1.0) * 128.0