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 NeuralNetwork * | get_neural_network () const |
| NeuralNetwork * | get_neural_network () |
| const Dataset * | get_dataset () const |
| Dataset * | get_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 |
| NeuralNetwork * | neural_network = nullptr |
| Dataset * | dataset = nullptr |
| string | name = "Loss" |
Detailed Description
Unified loss container supporting MSE, cross-entropy, Minkowski, weighted, and regularized variants.
Member Enumeration Documentation
◆ Error
|
strong |
Error function selector used to dispatch the loss kernel.
| Enumerator | |
|---|---|
| MeanSquaredError | |
| NormalizedSquaredError | |
| WeightedSquaredError | |
| CrossEntropy | |
| CrossEntropy3d | |
| MinkowskiError | |
◆ Regularization
|
strong |
Constructor & Destructor Documentation
◆ Loss()
| opennn::Loss::Loss | ( | NeuralNetwork * | = nullptr, |
| Dataset * | = nullptr ) |
◆ ~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
-
batch The minibatch containing inputs and targets. forward_propagation Cached forward-pass state (may be updated for autograd-style layers). back_propagation Output 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
-
batch The minibatch containing inputs and targets. forward_propagation Cached forward-pass state for this batch.
- Returns
- Loss value and, for classification losses, accuracy and active-token count.
◆ calculate_h()
|
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]
|
inline |
◆ get_dataset() [2/2]
|
inline |
◆ get_error()
|
inline |
◆ get_name()
|
inline |
◆ get_neural_network() [1/2]
|
inline |
◆ get_neural_network() [2/2]
|
inline |
◆ print()
|
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()
|
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()
|
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()
|
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()
|
inline |
◆ set_normalization_coefficient()
| void opennn::Loss::set_normalization_coefficient | ( | ) |
Recomputes the normalization coefficient (used by NormalizedSquaredError) from the dataset.
◆ set_regularization() [1/2]
|
inline |
◆ set_regularization() [2/2]
|
inline |
◆ set_regularization_weight()
|
inline |
◆ string_to_regularization()
|
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
|
protected |
◆ error
|
protected |
◆ minkowski_parameter
|
protected |
◆ name
|
protected |
◆ negatives_weight
|
protected |
◆ neural_network
|
protected |
◆ normalization_coefficient
|
protected |
◆ positives_weight
|
protected |
◆ regularization_method
|
protected |
◆ regularization_weight
|
protected |