pyopmnearwell.ml.scaler_layers module

Provide MinMax scaler layers for tensorflow.keras.

Warning: Tensorflow 2.17 and Keras 3.0 introduce many pylint errors, hence we disable linting completely. It is possible that the module is not functional at the moment.

class pyopmnearwell.ml.scaler_layers.MinMaxScalerLayer(*args, **kwargs)

Bases: ScalerLayer, Layer

Scales the input according to MinMaxScaling.

See https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.MinMaxScaler.html for an explanation of the transform.

Attributes:
compute_dtype

The dtype of the computations performed by the layer.

data_max
data_min
dtype

Alias of layer.variable_dtype.

dtype_policy
feature_range
input

Retrieves the input tensor(s) of a symbolic operation.

input_dtype

The dtype layer inputs should be converted to.

input_spec
is_adapted
losses

List of scalar losses from add_loss, regularizers and sublayers.

metrics

List of all metrics.

metrics_variables

List of all metric variables.

non_trainable_variables

List of all non-trainable layer state.

non_trainable_weights

List of all non-trainable weight variables of the layer.

output

Retrieves the output tensor(s) of a layer.

path

The path of the layer.

quantization_mode

The quantization mode of this layer, None if not quantized.

supports_masking

Whether this layer supports computing a mask using compute_mask.

trainable

Settable boolean, whether this layer should be trainable or not.

trainable_variables

List of all trainable layer state.

trainable_weights

List of all trainable weight variables of the layer.

variable_dtype

The dtype of the state (weights) of the layer.

variables

List of all layer state, including random seeds.

weights

List of all weight variables of the layer.

Methods

__call__(*args, **kwargs)

Call self as a function.

adapt(data)

Fit the layer to the min and max of the data.

add_loss(loss)

Can be called inside of the call() method to add a scalar loss.

add_variable(shape, initializer[, dtype, ...])

Add a weight variable to the layer.

add_weight([shape, initializer, dtype, ...])

Add a weight variable to the layer.

build(input_shape)

Initialize data_min and data_max with the default values if they have not been initialized yet.

build_from_config(config)

Builds the layer's states with the supplied config dict.

call(inputs)

compute_output_shape(input_shape)

Calculate the output shape.

count_params()

Count the total number of scalars composing the weights.

from_config(config)

Reconstruct the layer from its config.

get_build_config()

Returns a dictionary with the layer's input shape.

get_config()

Return the config for serialization.

get_weights()

Return parameters of the scaling.

load_own_variables(store)

Loads the state of the layer.

save_own_variables(store)

Saves the state of the layer.

set_weights(weights)

Set parameters of the scaling.

stateless_call(trainable_variables, ...[, ...])

Call the layer without any side effects.

add_metric

compute_mask

compute_output_spec

quantize

quantized_build

quantized_call

symbolic_call

call(inputs: Tensor) Tensor
compute_output_shape(input_shape)

Calculate the output shape.

classmethod from_config(config)

Reconstruct the layer from its config.

get_config()

Return the config for serialization.

class pyopmnearwell.ml.scaler_layers.MinMaxUnScalerLayer(*args, **kwargs)

Bases: ScalerLayer, Layer

Unscales the input by applying the inverse transform of MinMaxScalerLayer.

See https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.MinMaxScaler.html for an explanation of the transformation.

Attributes:
compute_dtype

The dtype of the computations performed by the layer.

data_max
data_min
dtype

Alias of layer.variable_dtype.

dtype_policy
feature_range
input

Retrieves the input tensor(s) of a symbolic operation.

input_dtype

The dtype layer inputs should be converted to.

input_spec
is_adapted
losses

List of scalar losses from add_loss, regularizers and sublayers.

metrics

List of all metrics.

metrics_variables

List of all metric variables.

non_trainable_variables

List of all non-trainable layer state.

non_trainable_weights

List of all non-trainable weight variables of the layer.

output

Retrieves the output tensor(s) of a layer.

path

The path of the layer.

quantization_mode

The quantization mode of this layer, None if not quantized.

supports_masking

Whether this layer supports computing a mask using compute_mask.

trainable

Settable boolean, whether this layer should be trainable or not.

trainable_variables

List of all trainable layer state.

trainable_weights

List of all trainable weight variables of the layer.

variable_dtype

The dtype of the state (weights) of the layer.

variables

List of all layer state, including random seeds.

weights

List of all weight variables of the layer.

Methods

__call__(*args, **kwargs)

Call self as a function.

adapt(data)

Fit the layer to the min and max of the data.

add_loss(loss)

Can be called inside of the call() method to add a scalar loss.

add_variable(shape, initializer[, dtype, ...])

Add a weight variable to the layer.

add_weight([shape, initializer, dtype, ...])

Add a weight variable to the layer.

build(input_shape)

Initialize data_min and data_max with the default values if they have not been initialized yet.

build_from_config(config)

Builds the layer's states with the supplied config dict.

compute_output_shape(input_shape)

Calculate the output shape.

count_params()

Count the total number of scalars composing the weights.

from_config(config)

Reconstruct the layer from its config.

get_build_config()

Returns a dictionary with the layer's input shape.

get_config()

Return the config for serialization.

get_weights()

Return parameters of the scaling.

load_own_variables(store)

Loads the state of the layer.

save_own_variables(store)

Saves the state of the layer.

set_weights(weights)

Set parameters of the scaling.

stateless_call(trainable_variables, ...[, ...])

Call the layer without any side effects.

add_metric

call

compute_mask

compute_output_spec

quantize

quantized_build

quantized_call

symbolic_call

call(inputs: Tensor) Tensor
compute_output_shape(input_shape)

Calculate the output shape.

classmethod from_config(config)

Reconstruct the layer from its config.

get_config()

Return the config for serialization.

class pyopmnearwell.ml.scaler_layers.ScalerLayer(*args, **kwargs)

Bases: Layer

MixIn to provide functionality for the Scaler Layer.

Attributes:
compute_dtype

The dtype of the computations performed by the layer.

data_max
data_min
dtype

Alias of layer.variable_dtype.

dtype_policy
feature_range
input

Retrieves the input tensor(s) of a symbolic operation.

input_dtype

The dtype layer inputs should be converted to.

input_spec
is_adapted
losses

List of scalar losses from add_loss, regularizers and sublayers.

metrics

List of all metrics.

metrics_variables

List of all metric variables.

non_trainable_variables

List of all non-trainable layer state.

non_trainable_weights

List of all non-trainable weight variables of the layer.

output

Retrieves the output tensor(s) of a layer.

path

The path of the layer.

quantization_mode

The quantization mode of this layer, None if not quantized.

supports_masking

Whether this layer supports computing a mask using compute_mask.

trainable

Settable boolean, whether this layer should be trainable or not.

trainable_variables

List of all trainable layer state.

trainable_weights

List of all trainable weight variables of the layer.

variable_dtype

The dtype of the state (weights) of the layer.

variables

List of all layer state, including random seeds.

weights

List of all weight variables of the layer.

Methods

__call__(*args, **kwargs)

Call self as a function.

adapt(data)

Fit the layer to the min and max of the data.

add_loss(loss)

Can be called inside of the call() method to add a scalar loss.

add_variable(shape, initializer[, dtype, ...])

Add a weight variable to the layer.

add_weight([shape, initializer, dtype, ...])

Add a weight variable to the layer.

build(input_shape)

Initialize data_min and data_max with the default values if they have not been initialized yet.

build_from_config(config)

Builds the layer's states with the supplied config dict.

count_params()

Count the total number of scalars composing the weights.

from_config(config)

Creates an operation from its config.

get_build_config()

Returns a dictionary with the layer's input shape.

get_config()

Returns the config of the object.

get_weights()

Return parameters of the scaling.

load_own_variables(store)

Loads the state of the layer.

save_own_variables(store)

Saves the state of the layer.

set_weights(weights)

Set parameters of the scaling.

stateless_call(trainable_variables, ...[, ...])

Call the layer without any side effects.

add_metric

call

compute_mask

compute_output_shape

compute_output_spec

quantize

quantized_build

quantized_call

symbolic_call

_adapt() None
adapt(data: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]) None

Fit the layer to the min and max of the data. This is done individually for each input feature.

Note:

So far, this is only tested for 1 dimensional input and output. For higher dimensional input and output some functionality might need to be added.

Args:

data: _description_

build(input_shape: tuple[int, ...]) None

Initialize data_min and data_max with the default values if they have not been initialized yet.

Args:

input_shape (tuple[int, …]): _description_

property data_max
property data_min
property feature_range
get_weights() list[Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]]

Return parameters of the scaling.

Returns:

list[ArrayLike]: List with three elements in the following order: self.data_min_, self.data_max_, self.feature_range_

property is_adapted
min: Tensor
scalar: Tensor
set_weights(weights: list[Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]]) None

Set parameters of the scaling.

Args:

weights (list[ArrayLike]): List with three elements in the following order: data_min, data_max, feature_range

Raises:

ValueError: If feature_range[0] >= feature_range[1].