OpenNN
Open-source neural networks library
Loading...
Searching...
No Matches
opennn::Scaling Class Referencefinal

Per-feature input normalization layer. More...

#include <scaling_layer.h>

Inheritance diagram for opennn::Scaling:
[legend]

Public Member Functions

 Scaling (const Shape &input_shape={})
 Constructs a Scaling layer.
 
Shape get_input_shape () const override
 Returns the per-sample input shape.
 
Shape get_output_shape () const override
 Returns the per-sample output shape (same as input).
 
VectorR get_minimums () const
 Returns the per-feature minimum statistics.
 
VectorR get_maximums () const
 Returns the per-feature maximum statistics.
 
VectorR get_means () const
 Returns the per-feature mean statistics.
 
VectorR get_standard_deviations () const
 Returns the per-feature standard-deviation statistics.
 
const vector< ScalerMethod > & get_scalers () const
 Read-only access to the per-feature scaler method list.
 
float get_min_range () const
 Lower bound of the target range used by min-max scalers.
 
float get_max_range () const
 Upper bound of the target range used by min-max scalers.
 
vector< Operator * > get_operators () override
 Returns the single Scale operator that implements this layer.
 
void set (const Shape &input_shape={})
 Re-initializes the layer.
 
void set_input_shape (const Shape &) override
 Updates the input shape and resizes the scaler vector.
 
void set_output_shape (const Shape &) override
 Updates the output shape (kept equal to the input shape).
 
void set_descriptives (const vector< Descriptives > &)
 Sets the per-feature descriptive statistics used by the scalers.
 
void set_min_max_range (float min, float max)
 Sets the target range used by min-max scalers.
 
void set_scalers (const vector< string > &)
 Sets the scaler method per feature from a vector of names.
 
void set_scalers (const string &)
 Sets the same scaler method for every feature.
 
void forward_propagate (ForwardPropagation &, size_t, bool) noexcept override
 Forward pass: applies the configured scaler to each feature.
 
string write_no_scaling_expression (const vector< string > &inputs_names, const vector< string > &outputs_names) const
 Generates a symbolic expression that performs no scaling.
 
string write_minimum_maximum_expression (const vector< string > &inputs_names, const vector< string > &outputs_names) const
 Generates a symbolic expression for min-max scaling.
 
string write_mean_standard_deviation_expression (const vector< string > &inputs_names, const vector< string > &outputs_names) const
 Generates a symbolic expression for mean / standard-deviation scaling.
 
string write_standard_deviation_expression (const vector< string > &inputs_names, const vector< string > &outputs_names) const
 Generates a symbolic expression for standard-deviation scaling.
 
void read_JSON_body (const Json *) override
 Reads the layer-specific JSON body (input shape, descriptives, scaler methods, target range).
 
void write_JSON_body (JsonWriter &) const override
 Writes the layer-specific JSON body (input shape, descriptives, scaler methods, target range).
 
- Public Member Functions inherited from opennn::Layer
virtual ~Layer ()=default
 Virtual destructor; subclasses are owned via unique_ptr<Layer>.
 
const string & get_label () const
 Returns the user-assigned label of this layer.
 
const string & get_name () const
 Returns the canonical type name of this layer.
 
LayerType get_type () const
 Returns the LayerType enumerator for this layer.
 
void set_label (string new_label)
 Sets the human-readable label of this layer.
 
Index get_parameters_number () const
 Total number of trainable parameters in this layer.
 
virtual vector< pair< Shape, Type > > get_parameter_specs () const
 Specifications of the trainable parameter tensors owned by this layer.
 
virtual vector< pair< Shape, Type > > get_state_specs () const
 Specifications of the persistent state tensors of this layer.
 
virtual vector< pair< Shape, Type > > get_forward_specs (Index batch_size) const
 Specifications of the forward intermediate buffers for one batch.
 
virtual vector< pair< Shape, Type > > get_backward_specs (Index batch_size) const
 Specifications of the backward intermediate buffers for one batch.
 
vector< Shapeget_parameter_shapes () const
 Shape-only view of get_parameter_specs().
 
vector< Shapeget_state_shapes () const
 Shape-only view of get_state_specs().
 
vector< Shapeget_forward_shapes (Index b) const
 Shape-only view of get_forward_specs() for batch size b.
 
vector< Shapeget_backward_shapes (Index b) const
 Shape-only view of get_backward_specs() for batch size b.
 
vector< Typeget_parameter_dtypes () const
 Dtype-only view of get_parameter_specs().
 
vector< Typeget_forward_dtypes (Index b) const
 Dtype-only view of get_forward_specs() for batch size b.
 
vector< Typeget_backward_dtypes (Index b) const
 Dtype-only view of get_backward_specs() for batch size b.
 
virtual Activation::Function get_output_activation () const
 Activation function fused at the end of this layer, if any.
 
Index get_inputs_number () const
 Total number of scalar inputs per sample (product of input dims).
 
Index get_outputs_number () const
 Total number of scalar outputs per sample (product of output dims).
 
virtual void back_propagate (ForwardPropagation &, BackPropagation &, size_t) const noexcept
 Backward pass: propagates gradients through this layer.
 
virtual void from_JSON (const JsonDocument &document)
 Loads the layer configuration (hyperparameters) from JSON.
 
virtual void load_state_from_JSON (const JsonDocument &document)
 Loads parameter and state tensors from a JSON document.
 
virtual void to_JSON (JsonWriter &writer) const
 Writes the layer configuration to JSON.
 
virtual void print () const
 Prints a human-readable summary of the layer to stdout.
 
bool get_is_trainable () const
 Whether this layer has trainable parameters.
 
Type get_compute_dtype () const
 Numerical type used for forward/backward computation.
 
void set_compute_dtype (Type new_compute_dtype)
 Sets the compute dtype and triggers on_compute_dtype_changed().
 
virtual void on_compute_dtype_changed ()
 Hook invoked after set_compute_dtype() mutates the dtype.
 
virtual float * link_parameters (float *pointer)
 Wires this layer's parameter TensorViews onto an external buffer.
 
virtual float * link_states (float *pointer)
 Wires this layer's state TensorViews onto an external buffer.
 
vector< TensorView > & get_parameter_views ()
 Mutable access to this layer's parameter TensorViews.
 
const vector< TensorView > & get_parameter_views () const
 Read-only access to this layer's parameter TensorViews.
 
vector< TensorView > & get_state_views ()
 Mutable access to this layer's state TensorViews.
 
const vector< TensorView > & get_state_views () const
 Read-only access to this layer's state TensorViews.
 
void redistribute_parameters_to_operators ()
 Forwards the current parameter views down to each composing Operator.
 
void redistribute_parameter_gradients_to_operators (vector< TensorView > &gradient_views)
 Forwards externally provided gradient views down to each Operator.
 
void redistribute_states_to_operators ()
 Forwards the current state views down to each composing Operator.
 

Additional Inherited Members

- Protected Member Functions inherited from opennn::Layer
 Layer ()=default
 Default constructor; only invoked by subclasses.
 
float * link_views (float *pointer, const vector< Shape > &shapes, vector< TensorView > &views, const char *tag) const
 Builds views over a contiguous float buffer using shapes.
 
void distribute_to_operators (vector< TensorView > &views, void(Operator::*link)(const vector< TensorView > &), vector< pair< Shape, Type > >(Operator::*specs)() const)
 Generic helper used by the redistribute_*_to_operators() routines.
 
- Protected Attributes inherited from opennn::Layer
string label = "my_layer"
 User-visible label for this layer instance (default "my_layer").
 
string name = "layer"
 Canonical type name set by the subclass (e.g. "dense").
 
LayerType layer_type = LayerType::Dense
 Layer type tag set by the subclass.
 
bool is_trainable = true
 True if the layer has parameters that participate in training.
 
bool is_first_layer = false
 True if this layer is the network's input layer.
 
Type compute_dtype = Type::FP32
 Numerical type used for forward and backward computation.
 
vector< TensorViewparameters
 Parameter TensorViews bound to the network's parameter arena.
 
vector< TensorViewstates
 State TensorViews bound to the network's state arena.
 
vector< unique_ptr< Layer > > layers
 Sub-layers, when this layer is itself a composite.
 

Detailed Description

Per-feature input normalization layer.

Holds a vector of ScalerMethod values (one per feature) and the corresponding descriptive statistics (minimum, maximum, mean, standard deviation). The forward pass applies the configured scaler to each feature so that downstream layers see normalized inputs.

The layer has no trainable parameters; the scaler statistics are stored as state and are typically derived from the training dataset.

Constructor & Destructor Documentation

◆ Scaling()

opennn::Scaling::Scaling ( const Shape & input_shape = {})

Constructs a Scaling layer.

Parameters
input_shapePer-sample input shape.

Member Function Documentation

◆ forward_propagate()

void opennn::Scaling::forward_propagate ( ForwardPropagation & ,
size_t ,
bool  )
overridevirtualnoexcept

Forward pass: applies the configured scaler to each feature.

Receives the ForwardPropagation buffer slice, this layer's index and the training flag.

Reimplemented from opennn::Layer.

◆ get_input_shape()

Shape opennn::Scaling::get_input_shape ( ) const
inlineoverridevirtual

Returns the per-sample input shape.

Implements opennn::Layer.

◆ get_max_range()

float opennn::Scaling::get_max_range ( ) const
inline

Upper bound of the target range used by min-max scalers.

◆ get_maximums()

VectorR opennn::Scaling::get_maximums ( ) const

Returns the per-feature maximum statistics.

◆ get_means()

VectorR opennn::Scaling::get_means ( ) const

Returns the per-feature mean statistics.

◆ get_min_range()

float opennn::Scaling::get_min_range ( ) const
inline

Lower bound of the target range used by min-max scalers.

◆ get_minimums()

VectorR opennn::Scaling::get_minimums ( ) const

Returns the per-feature minimum statistics.

◆ get_operators()

vector< Operator * > opennn::Scaling::get_operators ( )
inlineoverridevirtual

Returns the single Scale operator that implements this layer.

Reimplemented from opennn::Layer.

◆ get_output_shape()

Shape opennn::Scaling::get_output_shape ( ) const
inlineoverridevirtual

Returns the per-sample output shape (same as input).

Implements opennn::Layer.

◆ get_scalers()

const vector< ScalerMethod > & opennn::Scaling::get_scalers ( ) const
inline

Read-only access to the per-feature scaler method list.

◆ get_standard_deviations()

VectorR opennn::Scaling::get_standard_deviations ( ) const

Returns the per-feature standard-deviation statistics.

◆ read_JSON_body()

void opennn::Scaling::read_JSON_body ( const Json * )
overridevirtual

Reads the layer-specific JSON body (input shape, descriptives, scaler methods, target range).

Reimplemented from opennn::Layer.

◆ set()

void opennn::Scaling::set ( const Shape & input_shape = {})

Re-initializes the layer.

Parameters
input_shapePer-sample input shape.

◆ set_descriptives()

void opennn::Scaling::set_descriptives ( const vector< Descriptives > & )

Sets the per-feature descriptive statistics used by the scalers.

Receives a vector of Descriptives (minimum, maximum, mean, standard deviation) with one entry per input feature.

◆ set_input_shape()

void opennn::Scaling::set_input_shape ( const Shape & )
overridevirtual

Updates the input shape and resizes the scaler vector.

Reimplemented from opennn::Layer.

◆ set_min_max_range()

void opennn::Scaling::set_min_max_range ( float min,
float max )
inline

Sets the target range used by min-max scalers.

Parameters
minLower bound of the target range.
maxUpper bound of the target range.

◆ set_output_shape()

void opennn::Scaling::set_output_shape ( const Shape & )
overridevirtual

Updates the output shape (kept equal to the input shape).

Reimplemented from opennn::Layer.

◆ set_scalers() [1/2]

void opennn::Scaling::set_scalers ( const string & )

Sets the same scaler method for every feature.

Receives a single method name applied to all features.

◆ set_scalers() [2/2]

void opennn::Scaling::set_scalers ( const vector< string > & )

Sets the scaler method per feature from a vector of names.

Receives one method name per feature; see ScalerMethod for the supported set.

◆ write_JSON_body()

void opennn::Scaling::write_JSON_body ( JsonWriter & ) const
overridevirtual

Writes the layer-specific JSON body (input shape, descriptives, scaler methods, target range).

Reimplemented from opennn::Layer.

◆ write_mean_standard_deviation_expression()

string opennn::Scaling::write_mean_standard_deviation_expression ( const vector< string > & inputs_names,
const vector< string > & outputs_names ) const

Generates a symbolic expression for mean / standard-deviation scaling.

Parameters
inputs_namesVariable names used in the expression for each input.
outputs_namesVariable names used for each output.
Returns
Multi-line expression string suitable for code export.

◆ write_minimum_maximum_expression()

string opennn::Scaling::write_minimum_maximum_expression ( const vector< string > & inputs_names,
const vector< string > & outputs_names ) const

Generates a symbolic expression for min-max scaling.

Parameters
inputs_namesVariable names used in the expression for each input.
outputs_namesVariable names used for each output.
Returns
Multi-line expression string suitable for code export.

◆ write_no_scaling_expression()

string opennn::Scaling::write_no_scaling_expression ( const vector< string > & inputs_names,
const vector< string > & outputs_names ) const

Generates a symbolic expression that performs no scaling.

Parameters
inputs_namesVariable names used in the expression for each input.
outputs_namesVariable names used for each output.
Returns
Multi-line expression string suitable for code export.

◆ write_standard_deviation_expression()

string opennn::Scaling::write_standard_deviation_expression ( const vector< string > & inputs_names,
const vector< string > & outputs_names ) const

Generates a symbolic expression for standard-deviation scaling.

Parameters
inputs_namesVariable names used in the expression for each input.
outputs_namesVariable names used for each output.
Returns
Multi-line expression string suitable for code export.