Skip to content

encoders

CategoricalInputEncoderPerFeatureEncoderStep

Bases: SeqEncStep

Expects input of size 1.

forward

forward(
    state: dict,
    cache_trainset_representation: bool = False,
    **kwargs: Any
) -> dict

Perform the forward pass of the encoder step.

Parameters:

Name Type Description Default
state dict

The input state dictionary containing the input tensors.

required
cache_trainset_representation bool

Whether to cache the training set representation. Only supported for _fit and _transform (not _forward).

False
**kwargs Any

Additional keyword arguments passed to the encoder step.

{}

Returns:

Type Description
dict

The updated state dictionary with the output tensors assigned to the output keys.

FrequencyFeatureEncoderStep

Bases: SeqEncStep

Encoder step to add frequency-based features to the input.

forward

forward(
    state: dict,
    cache_trainset_representation: bool = False,
    **kwargs: Any
) -> dict

Perform the forward pass of the encoder step.

Parameters:

Name Type Description Default
state dict

The input state dictionary containing the input tensors.

required
cache_trainset_representation bool

Whether to cache the training set representation. Only supported for _fit and _transform (not _forward).

False
**kwargs Any

Additional keyword arguments passed to the encoder step.

{}

Returns:

Type Description
dict

The updated state dictionary with the output tensors assigned to the output keys.

InputEncoder

Bases: Module

Base class for input encoders.

All input encoders should subclass this class and implement the forward method.

forward

forward(x: Tensor, single_eval_pos: int) -> Tensor

Encode the input tensor.

Parameters:

Name Type Description Default
x Tensor

The input tensor to encode.

required
single_eval_pos int

The position to use for single evaluation.

required

Returns:

Type Description
Tensor

The encoded tensor.

InputNormalizationEncoderStep

Bases: SeqEncStep

Encoder step to normalize the input in different ways.

Can be used to normalize the input to a ranking, remove outliers, or normalize the input to have unit variance.

forward

forward(
    state: dict,
    cache_trainset_representation: bool = False,
    **kwargs: Any
) -> dict

Perform the forward pass of the encoder step.

Parameters:

Name Type Description Default
state dict

The input state dictionary containing the input tensors.

required
cache_trainset_representation bool

Whether to cache the training set representation. Only supported for _fit and _transform (not _forward).

False
**kwargs Any

Additional keyword arguments passed to the encoder step.

{}

Returns:

Type Description
dict

The updated state dictionary with the output tensors assigned to the output keys.

reset_seed

reset_seed() -> None

Reset the random seed.

LinearInputEncoder

Bases: Module

A simple linear input encoder.

forward

forward(*x: Tensor, **kwargs: Any) -> tuple[Tensor]

Apply the linear transformation to the input.

Parameters:

Name Type Description Default
*x Tensor

The input tensors to concatenate and transform.

()
**kwargs Any

Unused keyword arguments.

{}

Returns:

Type Description
tuple[Tensor]

A tuple containing the transformed tensor.

LinearInputEncoderStep

Bases: SeqEncStep

A simple linear input encoder step.

forward

forward(
    state: dict,
    cache_trainset_representation: bool = False,
    **kwargs: Any
) -> dict

Perform the forward pass of the encoder step.

Parameters:

Name Type Description Default
state dict

The input state dictionary containing the input tensors.

required
cache_trainset_representation bool

Whether to cache the training set representation. Only supported for _fit and _transform (not _forward).

False
**kwargs Any

Additional keyword arguments passed to the encoder step.

{}

Returns:

Type Description
dict

The updated state dictionary with the output tensors assigned to the output keys.

NanHandlingEncoderStep

Bases: SeqEncStep

Encoder step to handle NaN and infinite values in the input.

forward

forward(
    state: dict,
    cache_trainset_representation: bool = False,
    **kwargs: Any
) -> dict

Perform the forward pass of the encoder step.

Parameters:

Name Type Description Default
state dict

The input state dictionary containing the input tensors.

required
cache_trainset_representation bool

Whether to cache the training set representation. Only supported for _fit and _transform (not _forward).

False
**kwargs Any

Additional keyword arguments passed to the encoder step.

{}

Returns:

Type Description
dict

The updated state dictionary with the output tensors assigned to the output keys.

RemoveDuplicateFeaturesEncoderStep

Bases: SeqEncStep

Encoder step to remove duplicate features.

forward

forward(
    state: dict,
    cache_trainset_representation: bool = False,
    **kwargs: Any
) -> dict

Perform the forward pass of the encoder step.

Parameters:

Name Type Description Default
state dict

The input state dictionary containing the input tensors.

required
cache_trainset_representation bool

Whether to cache the training set representation. Only supported for _fit and _transform (not _forward).

False
**kwargs Any

Additional keyword arguments passed to the encoder step.

{}

Returns:

Type Description
dict

The updated state dictionary with the output tensors assigned to the output keys.

RemoveEmptyFeaturesEncoderStep

Bases: SeqEncStep

Encoder step to remove empty (constant) features.

forward

forward(
    state: dict,
    cache_trainset_representation: bool = False,
    **kwargs: Any
) -> dict

Perform the forward pass of the encoder step.

Parameters:

Name Type Description Default
state dict

The input state dictionary containing the input tensors.

required
cache_trainset_representation bool

Whether to cache the training set representation. Only supported for _fit and _transform (not _forward).

False
**kwargs Any

Additional keyword arguments passed to the encoder step.

{}

Returns:

Type Description
dict

The updated state dictionary with the output tensors assigned to the output keys.

SeqEncStep

Bases: Module

Abstract base class for sequential encoder steps.

SeqEncStep is a wrapper around a module that defines the expected input keys and the produced output keys. The outputs are assigned to the output keys in the order specified by out_keys.

Subclasses should either implement _forward or _fit and _transform. Subclasses that transform x should always use _fit and _transform, creating any state that depends on the train set in _fit and using it in _transform. This allows fitting on data first and doing inference later without refitting. Subclasses that work with y can alternatively use _forward instead.

forward

forward(
    state: dict,
    cache_trainset_representation: bool = False,
    **kwargs: Any
) -> dict

Perform the forward pass of the encoder step.

Parameters:

Name Type Description Default
state dict

The input state dictionary containing the input tensors.

required
cache_trainset_representation bool

Whether to cache the training set representation. Only supported for _fit and _transform (not _forward).

False
**kwargs Any

Additional keyword arguments passed to the encoder step.

{}

Returns:

Type Description
dict

The updated state dictionary with the output tensors assigned to the output keys.

SequentialEncoder

Bases: Sequential, InputEncoder

An encoder that applies a sequence of encoder steps.

SequentialEncoder allows building an encoder from a sequence of EncoderSteps. The input is passed through each step in the provided order.

forward

forward(input: dict, **kwargs: Any) -> Tensor

Apply the sequence of encoder steps to the input.

Parameters:

Name Type Description Default
input dict

The input state dictionary. If the input is not a dict and the first layer expects one input key, the input tensor is mapped to the key expected by the first layer.

required
**kwargs Any

Additional keyword arguments passed to each encoder step.

{}

Returns:

Type Description
Tensor

The output of the final encoder step.

VariableNumFeaturesEncoderStep

Bases: SeqEncStep

Encoder step to handle variable number of features.

Transforms the input to a fixed number of features by appending zeros. Also normalizes the input by the number of used features to keep the variance of the input constant, even when zeros are appended.

forward

forward(
    state: dict,
    cache_trainset_representation: bool = False,
    **kwargs: Any
) -> dict

Perform the forward pass of the encoder step.

Parameters:

Name Type Description Default
state dict

The input state dictionary containing the input tensors.

required
cache_trainset_representation bool

Whether to cache the training set representation. Only supported for _fit and _transform (not _forward).

False
**kwargs Any

Additional keyword arguments passed to the encoder step.

{}

Returns:

Type Description
dict

The updated state dictionary with the output tensors assigned to the output keys.

normalize_data

normalize_data(
    data: Tensor,
    *,
    normalize_positions: int = -1,
    return_scaling: bool = False,
    clip: bool = True,
    std_only: bool = False,
    mean: Tensor | None = None,
    std: Tensor | None = None
) -> Tensor | tuple[Tensor, tuple[Tensor, Tensor]]

Normalize data to mean 0 and std 1.

Parameters:

Name Type Description Default
data Tensor

The data to normalize. (T, B, H)

required
normalize_positions int

If > 0, only use the first normalize_positions positions for normalization.

-1
return_scaling bool

If True, return the scaling parameters as well (mean, std).

False
std_only bool

If True, only divide by std.

False
clip bool

If True, clip the data to [-100, 100].

True
mean Tensor | None

If given, use this value instead of computing it.

None
std Tensor | None

If given, use this value instead of computing it.

None

select_features

select_features(x: Tensor, sel: Tensor) -> Tensor

Select features from the input tensor based on the selection mask.

Parameters:

Name Type Description Default
x Tensor

The input tensor.

required
sel Tensor

The boolean selection mask indicating which features to keep.

required

Returns:

Type Description
Tensor

The tensor with selected features.