|
OpenNN
Open-source neural networks library
|
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" |
Unified loss container supporting MSE, cross-entropy, Minkowski, weighted, and regularized variants.
|
strong |
Error function selector used to dispatch the loss kernel.
| Enumerator | |
|---|---|
| MeanSquaredError | |
| NormalizedSquaredError | |
| WeightedSquaredError | |
| CrossEntropy | |
| CrossEntropy3d | |
| MinkowskiError | |
|
strong |
| opennn::Loss::Loss | ( | NeuralNetwork * | = nullptr, |
| Dataset * | = nullptr ) |
|
virtualdefault |
| void opennn::Loss::back_propagate | ( | const Batch & | , |
| ForwardPropagation & | , | ||
| BackPropagation & | ) const |
Performs the full backward pass: output deltas, layer gradients, and regularization gradient.
| 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. |
| EvaluationResult opennn::Loss::calculate_error | ( | const Batch & | , |
| const ForwardPropagation & | ) const |
Computes the loss for one batch using the cached forward-pass outputs.
| batch | The minibatch containing inputs and targets. |
| forward_propagation | Cached forward-pass state for this batch. |
|
static |
Returns the finite-difference step size h tuned for the given parameter value.
| float opennn::Loss::calculate_regularization | ( | const VectorR & | ) | const |
Returns the regularization penalty (L1, L2, or ElasticNet) for the given parameter vector.
| void opennn::Loss::from_JSON | ( | const JsonDocument & | ) |
Restores loss configuration (error type, regularization, weights) from a JSON document.
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
Prints a human-readable description of the loss (no-op default).
| void opennn::Loss::regularization_from_JSON | ( | const JsonDocument & | ) |
Restores the regularization sub-configuration from JSON.
|
inlinestatic |
Returns the static string<->enum map used to (de)serialize regularization types.
| void opennn::Loss::regularization_to_JSON | ( | JsonWriter & | ) | const |
Serializes the regularization sub-configuration to JSON.
|
inlinestatic |
Returns the canonical string name for a Regularization value.
| void opennn::Loss::set | ( | NeuralNetwork * | = nullptr, |
| Dataset * | = nullptr ) |
Resets the bound neural network and dataset pointers.
|
inlinevirtual |
| void opennn::Loss::set_error | ( | const Error & | ) |
Selects the error function variant.
| void opennn::Loss::set_error | ( | const string & | ) |
Selects the error function variant from its string name.
|
inline |
| void opennn::Loss::set_normalization_coefficient | ( | ) |
Recomputes the normalization coefficient (used by NormalizedSquaredError) from the dataset.
|
inline |
|
inline |
|
inline |
|
inlinestatic |
Parses a regularization name (accepts both "NoRegularization" and "None") back to the enum.
| void opennn::Loss::to_JSON | ( | JsonWriter & | ) | const |
Serializes the loss configuration (error type, regularization, weights) to JSON.
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |