TorchNormalizer#
- class pytorch_forecasting.data.encoders.TorchNormalizer(method: str = 'standard', center: bool = True, transformation: str | tuple[Callable, Callable] = None, method_kwargs: dict[str, Any] | None = None)[source]#
Bases:
InitialParameterRepresenterMixIn,BaseEstimator,TransformerMixin,TransformMixInBasic target transformer that can be fit also on torch tensors.
- Parameters:
method (str, optional, default="standard") – method to rescale series. Either “identity”, “standard” (standard scaling) or “robust” (scale using quantiles 0.25-0.75). Defaults to “standard”.
method_kwargs (Dict[str, Any], optional, default=None) –
Dictionary of method specific arguments as listed below
”robust” method: “upper”, “lower”, “center” quantiles defaulting to 0.75, 0.25 and 0.5
center (bool, optional, default=True) – If to center the output to zero. Defaults to True.
transformation (Union[str, Dict[str, Callable]] optional, default=None) –
Transform values before applying normalizer. Available options are
None (default): No transformation of values
log: Estimate in log-space leading to a multiplicative model
log1p: Estimate in log-space but add 1 to values before transforming for stability
(e.g. if many small values <<1 are present). Note, that inverse transform is still only torch.exp() and not torch.expm1().
logit: Apply logit transformation on values that are between 0 and 1
count: Apply softplus to output (inverse transformation) and x + 1 to input (transformation)
softplus: Apply softplus to output (inverse transformation) and inverse softplus to input (transformation)
relu: Apply max(0, x) to output
Dict[str, Callable] of PyTorch functions that transforms and inversely transforms values.
forwardandreverseentries are required.inversetransformation is optional and should be defined ifreverseis not the inverse of the forward transformation.inverse_torchcan be defined to provide a torch distribution transform for inverse transformations.
- Inherited-members:
Methods
extra_repr()Return extra information about parameters for representation/logging.
fit(y)Fit transformer, i.e. determine center and scale of data.
fit_transform(X[, y])Fit to data, then transform it.
get_metadata_routing()Get metadata routing of this object.
get_parameters(*args, **kwargs)Returns parameters that were used for encoding.
get_params([deep])Get parameters for this estimator.
get_transform(transformation)Return transformation functions.
inverse_preprocess(y)Inverse preprocess re-scaled data (e.g. take exp).
Inverse scale.
preprocess(y)Preprocess input data (e.g. take log).
set_output(*[, transform])Set output container.
set_params(**params)Set the parameters of this estimator.
set_transform_request(*[, return_norm, ...])Configure whether metadata should be requested to be passed to the
transformmethod.transform(y[, return_norm, target_scale])Rescale data.
Attributes
TRANSFORMATIONS- fit(y: Series | ndarray | Tensor)[source]#
Fit transformer, i.e. determine center and scale of data
- Parameters:
y (Union[pd.Series, np.ndarray, torch.Tensor]) – input data
- Returns:
TorchNormalizer
- Return type:
self
- get_parameters(*args, **kwargs) Tensor[source]#
Returns parameters that were used for encoding.
- Returns:
First element is center of data and second is scale
- Return type:
torch.Tensor
- inverse_transform(y: Tensor | ndarray) Tensor[source]#
Inverse scale.
- Parameters:
y (Union[torch.Tensor, np.ndarray])) – scaled data
- Returns:
de-scaled data
- Return type:
torch.Tensor
- set_transform_request(*, return_norm: bool | None | str = '$UNCHANGED$', target_scale: bool | None | str = '$UNCHANGED$') TorchNormalizer#
Configure whether metadata should be requested to be passed to the
transformmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed totransformif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it totransform.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
return_norm (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
return_normparameter intransform.target_scale (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
target_scaleparameter intransform.
- Returns:
self – The updated object.
- Return type:
object
- transform(y: Series | ndarray | Tensor, return_norm: bool = False, target_scale: Tensor = None) tuple[ndarray | Tensor, ndarray] | ndarray | Tensor[source]#
Rescale data.
- Parameters:
y (Union[pd.Series, np.ndarray, torch.Tensor]) – input data
return_norm (bool, optional, default=False) – [description]. Defaults to False.
target_scale (torch.Tensor, optional, default=None) – target scale to use instead of fitted center and scale
- Returns:
rescaled data with type depending on input type. returns second element if
return_norm=True- Return type:
Union[Tuple[Union[np.ndarray, torch.Tensor],np.ndarray],Union[np.ndarray, torch.Tensor]]