OpenNN
Open-source neural networks library
Loading...
Searching...
No Matches
opennn::Loss Class Reference

Unified loss container supporting MSE, cross-entropy, Minkowski, weighted, and regularized variants. More...

#include <loss.h>

Classes

struct  EvaluationResult
 Result of calculate_error; accuracy and active_tokens_count are populated only by classification losses. More...
 

Public Types

enum class  Error {
  MeanSquaredError , NormalizedSquaredError , WeightedSquaredError , CrossEntropy ,
  CrossEntropy3d , MinkowskiError
}
 Error function selector used to dispatch the loss kernel. More...
 
enum class  Regularization { L1 , L2 , ElasticNet , NoRegularization }
 Parameter regularization method applied on top of the base loss. More...
 

Public Member Functions

 Loss (NeuralNetwork *=nullptr, Dataset *=nullptr)
 Constructs a Loss bound to an optional neural network and dataset.
 
virtual ~Loss ()=default
 
const NeuralNetworkget_neural_network () const
 
NeuralNetworkget_neural_network ()
 
const Datasetget_dataset () const
 
Datasetget_dataset ()
 
void set (NeuralNetwork *=nullptr, Dataset *=nullptr)
 Resets the bound neural network and dataset pointers.
 
void set_neural_network (NeuralNetwork *new_neural_network)
 
virtual void set_dataset (Dataset *new_dataset)
 
void set_regularization (const string &new_regularization_method)
 
void set_regularization (Regularization new_regularization)
 
void set_regularization_weight (const float new_regularization_weight)
 
void set_normalization_coefficient ()
 Recomputes the normalization coefficient (used by NormalizedSquaredError) from the dataset.
 
EvaluationResult calculate_error (const Batch &, const ForwardPropagation &) const
 Computes the loss for one batch using the cached forward-pass outputs.
 
void set_error (const Error &)
 Selects the error function variant.
 
void set_error (const string &)
 Selects the error function variant from its string name.
 
Error get_error () const
 
void back_propagate (const Batch &, ForwardPropagation &, BackPropagation &) const
 Performs the full backward pass: output deltas, layer gradients, and regularization gradient.
 
float calculate_regularization (const VectorR &) const
 Returns the regularization penalty (L1, L2, or ElasticNet) for the given parameter vector.
 
void from_JSON (const JsonDocument &)
 Restores loss configuration (error type, regularization, weights) from a JSON document.
 
void to_JSON (JsonWriter &) const
 Serializes the loss configuration (error type, regularization, weights) to JSON.
 
void regularization_from_JSON (const JsonDocument &)
 Restores the regularization sub-configuration from JSON.
 
void regularization_to_JSON (JsonWriter &) const
 Serializes the regularization sub-configuration to JSON.
 
const string & get_name () const
 
void print () const
 Prints a human-readable description of the loss (no-op default).
 

Static Public Member Functions

static const EnumMap< Regularization > & regularization_map ()
 Returns the static string<->enum map used to (de)serialize regularization types.
 
static const string & regularization_to_string (Regularization regularization)
 Returns the canonical string name for a Regularization value.
 
static Regularization string_to_regularization (const string &name)
 Parses a regularization name (accepts both "NoRegularization" and "None") back to the enum.
 
static float calculate_h (const float)
 Returns the finite-difference step size h tuned for the given parameter value.
 

Protected Attributes

Error error = Error::MeanSquaredError
 
float normalization_coefficient = 1.0f
 
float positives_weight = 1.0f
 
float negatives_weight = 1.0f
 
float minkowski_parameter = 1.5f
 
Regularization regularization_method = Regularization::L2
 
float regularization_weight = 0.001f
 
NeuralNetworkneural_network = nullptr
 
Datasetdataset = nullptr
 
string name = "Loss"
 

Detailed Description

Unified loss container supporting MSE, cross-entropy, Minkowski, weighted, and regularized variants.

Member Enumeration Documentation

◆ Error

enum class opennn::Loss::Error
strong

Error function selector used to dispatch the loss kernel.

Enumerator
MeanSquaredError 
NormalizedSquaredError 
WeightedSquaredError 
CrossEntropy 
CrossEntropy3d 
MinkowskiError 

◆ Regularization

enum class opennn::Loss::Regularization
strong

Parameter regularization method applied on top of the base loss.

Enumerator
L1 
L2 
ElasticNet 
NoRegularization 

Constructor & Destructor Documentation

◆ Loss()

opennn::Loss::Loss ( NeuralNetwork * = nullptr,
Dataset * = nullptr )

Constructs a Loss bound to an optional neural network and dataset.

Parameters
neural_networkNetwork whose outputs are scored (may be null and set later).
datasetDataset providing batches for error computation (may be null and set later).

◆ ~Loss()

virtual opennn::Loss::~Loss ( )
virtualdefault

Member Function Documentation

◆ back_propagate()

void opennn::Loss::back_propagate ( const Batch & ,
ForwardPropagation & ,
BackPropagation &  ) const

Performs the full backward pass: output deltas, layer gradients, and regularization gradient.

Parameters
batchThe minibatch containing inputs and targets.
forward_propagationCached forward-pass state (may be updated for autograd-style layers).
back_propagationOutput structure receiving error and parameter gradients.

◆ calculate_error()

EvaluationResult opennn::Loss::calculate_error ( const Batch & ,
const ForwardPropagation &  ) const

Computes the loss for one batch using the cached forward-pass outputs.

Parameters
batchThe minibatch containing inputs and targets.
forward_propagationCached forward-pass state for this batch.
Returns
Loss value and, for classification losses, accuracy and active-token count.

◆ calculate_h()

static float opennn::Loss::calculate_h ( const float )
static

Returns the finite-difference step size h tuned for the given parameter value.

◆ calculate_regularization()

float opennn::Loss::calculate_regularization ( const VectorR & ) const

Returns the regularization penalty (L1, L2, or ElasticNet) for the given parameter vector.

◆ from_JSON()

void opennn::Loss::from_JSON ( const JsonDocument & )

Restores loss configuration (error type, regularization, weights) from a JSON document.

◆ get_dataset() [1/2]

Dataset * opennn::Loss::get_dataset ( )
inline

◆ get_dataset() [2/2]

const Dataset * opennn::Loss::get_dataset ( ) const
inline

◆ get_error()

Error opennn::Loss::get_error ( ) const
inline

◆ get_name()

const string & opennn::Loss::get_name ( ) const
inline

◆ get_neural_network() [1/2]

NeuralNetwork * opennn::Loss::get_neural_network ( )
inline

◆ get_neural_network() [2/2]

const NeuralNetwork * opennn::Loss::get_neural_network ( ) const
inline

◆ print()

void opennn::Loss::print ( ) const
inline

Prints a human-readable description of the loss (no-op default).

◆ regularization_from_JSON()

void opennn::Loss::regularization_from_JSON ( const JsonDocument & )

Restores the regularization sub-configuration from JSON.

◆ regularization_map()

static const EnumMap< Regularization > & opennn::Loss::regularization_map ( )
inlinestatic

Returns the static string<->enum map used to (de)serialize regularization types.

◆ regularization_to_JSON()

void opennn::Loss::regularization_to_JSON ( JsonWriter & ) const

Serializes the regularization sub-configuration to JSON.

◆ regularization_to_string()

static const string & opennn::Loss::regularization_to_string ( Regularization regularization)
inlinestatic

Returns the canonical string name for a Regularization value.

◆ set()

void opennn::Loss::set ( NeuralNetwork * = nullptr,
Dataset * = nullptr )

Resets the bound neural network and dataset pointers.

◆ set_dataset()

virtual void opennn::Loss::set_dataset ( Dataset * new_dataset)
inlinevirtual

◆ set_error() [1/2]

void opennn::Loss::set_error ( const Error & )

Selects the error function variant.

◆ set_error() [2/2]

void opennn::Loss::set_error ( const string & )

Selects the error function variant from its string name.

◆ set_neural_network()

void opennn::Loss::set_neural_network ( NeuralNetwork * new_neural_network)
inline

◆ set_normalization_coefficient()

void opennn::Loss::set_normalization_coefficient ( )

Recomputes the normalization coefficient (used by NormalizedSquaredError) from the dataset.

◆ set_regularization() [1/2]

void opennn::Loss::set_regularization ( const string & new_regularization_method)
inline

◆ set_regularization() [2/2]

void opennn::Loss::set_regularization ( Regularization new_regularization)
inline

◆ set_regularization_weight()

void opennn::Loss::set_regularization_weight ( const float new_regularization_weight)
inline

◆ string_to_regularization()

static Regularization opennn::Loss::string_to_regularization ( const string & name)
inlinestatic

Parses a regularization name (accepts both "NoRegularization" and "None") back to the enum.

◆ to_JSON()

void opennn::Loss::to_JSON ( JsonWriter & ) const

Serializes the loss configuration (error type, regularization, weights) to JSON.

Member Data Documentation

◆ dataset

Dataset* opennn::Loss::dataset = nullptr
protected

◆ error

Error opennn::Loss::error = Error::MeanSquaredError
protected

◆ minkowski_parameter

float opennn::Loss::minkowski_parameter = 1.5f
protected

◆ name

string opennn::Loss::name = "Loss"
protected

◆ negatives_weight

float opennn::Loss::negatives_weight = 1.0f
protected

◆ neural_network

NeuralNetwork* opennn::Loss::neural_network = nullptr
protected

◆ normalization_coefficient

float opennn::Loss::normalization_coefficient = 1.0f
protected

◆ positives_weight

float opennn::Loss::positives_weight = 1.0f
protected

◆ regularization_method

Regularization opennn::Loss::regularization_method = Regularization::L2
protected

◆ regularization_weight

float opennn::Loss::regularization_weight = 0.001f
protected