|
OpenNN
Open-source neural networks library
|
Trainable loss function attached to a NeuralNetwork and a Dataset. More...
#include <loss.h>
Classes | |
| struct | EvaluationResult |
| Output of calculate_error(). More... | |
Public Types | |
| enum class | Error { MeanSquaredError , NormalizedSquaredError , WeightedSquaredError , CrossEntropy , CrossEntropy3d , MinkowskiError } |
| Built-in loss functions. More... | |
| enum class | Regularization { L1 , L2 , ElasticNet , NoRegularization } |
| Parameter-norm regularization terms. More... | |
Public Member Functions | |
| Loss (NeuralNetwork *neural_network=nullptr, Dataset *dataset=nullptr) | |
| Constructs a Loss bound to a network and dataset. | |
| virtual | ~Loss ()=default |
| Virtual destructor. | |
| const NeuralNetwork * | get_neural_network () const |
| Read-only access to the bound network. | |
| NeuralNetwork * | get_neural_network () |
| Mutable access to the bound network. | |
| const Dataset * | get_dataset () const |
| Read-only access to the bound dataset. | |
| Dataset * | get_dataset () |
| Mutable access to the bound dataset. | |
| void | set (NeuralNetwork *neural_network=nullptr, Dataset *dataset=nullptr) |
| Re-initializes the Loss by binding network and dataset pointers. | |
| void | set_neural_network (NeuralNetwork *new_neural_network) |
| Sets the bound network. | |
| virtual void | set_dataset (Dataset *new_dataset) |
| Sets the bound dataset; subclasses may override to refresh cached state derived from the dataset. | |
| void | set_regularization (const string &new_regularization_method) |
| Sets the regularization method by name. | |
| void | set_regularization (Regularization new_regularization) |
| Sets the regularization method directly. | |
| void | set_regularization_weight (const float new_regularization_weight) |
| Sets the strength of the regularization term. | |
| void | set_normalization_coefficient () |
| Recomputes the dataset-derived normalization coefficient. | |
| EvaluationResult | calculate_error (const Batch &batch, const ForwardPropagation &forward_propagation) const |
| Computes the loss on a batch given its forward intermediates. | |
| void | set_error (const Error &) |
| Sets the loss term directly. | |
| void | set_error (const string &) |
| Sets the loss term by name. | |
| Error | get_error () const |
| Currently selected loss term. | |
| void | back_propagate (const Batch &batch, ForwardPropagation &forward_propagation, BackPropagation &back_propagation) const |
| Computes the gradient of the loss with respect to the parameters. | |
| float | calculate_regularization (const VectorR ¶meters) const |
| Computes the regularization term given the parameter vector. | |
| void | from_JSON (const JsonDocument &) |
| Loads loss hyperparameters from a parsed JSON document. | |
| void | to_JSON (JsonWriter &) const |
| Writes loss hyperparameters to a streaming JSON writer. | |
| void | regularization_from_JSON (const JsonDocument &) |
| Reads only the regularization fields from a parsed JSON document. | |
| void | regularization_to_JSON (JsonWriter &) const |
| Writes only the regularization fields to a streaming JSON writer. | |
| const string & | get_name () const |
| Canonical name of the loss (e.g. "Loss"). | |
| void | print () const |
| Prints a human-readable summary of the loss to stdout. | |
Static Public Member Functions | |
| static const EnumMap< Regularization > & | regularization_map () |
| Returns the singleton string<->enum mapping for Regularization values. | |
| static const string & | regularization_to_string (Regularization regularization) |
| Converts a Regularization to its canonical string name. | |
| static Regularization | string_to_regularization (const string &name) |
| Parses a Regularization from string. | |
| static float | calculate_h (const float parameter) |
| Computes the finite-difference perturbation step for a parameter. | |
Protected Attributes | |
| Error | error = Error::MeanSquaredError |
| Currently selected loss term. | |
| float | normalization_coefficient = 1.0f |
| Variance of the targets, used by NormalizedSquaredError. | |
| float | positives_weight = 1.0f |
| Weight applied to positive-class samples (WeightedSquaredError). | |
| float | negatives_weight = 1.0f |
| Weight applied to negative-class samples (WeightedSquaredError). | |
| float | minkowski_parameter = 1.5f |
| Exponent p used by MinkowskiError. | |
| Regularization | regularization_method = Regularization::L2 |
| Currently selected regularization term. | |
| float | regularization_weight = 0.001f |
| Multiplier applied to the regularization term in the total loss. | |
| NeuralNetwork * | neural_network = nullptr |
| Network whose parameters are being trained; not owned. | |
| Dataset * | dataset = nullptr |
| Dataset that provides training data; not owned. | |
| string | name = "Loss" |
| Canonical name of the loss instance. | |
Trainable loss function attached to a NeuralNetwork and a Dataset.
Holds non-owning pointers to a NeuralNetwork and a Dataset, the choice of loss term (Loss::Error) and regularization term (Loss::Regularization). Per-loss hyperparameters (e.g. minkowski_parameter, positives_weight) are stored as protected fields and configured through the corresponding setters.
|
strong |
Built-in loss functions.
|
strong |
| opennn::Loss::Loss | ( | NeuralNetwork * | neural_network = nullptr, |
| Dataset * | dataset = nullptr ) |
|
virtualdefault |
Virtual destructor.
| void opennn::Loss::back_propagate | ( | const Batch & | batch, |
| ForwardPropagation & | forward_propagation, | ||
| BackPropagation & | back_propagation ) const |
Computes the gradient of the loss with respect to the parameters.
| batch | Current training batch. |
| forward_propagation | Forward intermediates for the batch. |
| back_propagation | Output buffer in which to accumulate gradients. |
| EvaluationResult opennn::Loss::calculate_error | ( | const Batch & | batch, |
| const ForwardPropagation & | forward_propagation ) const |
Computes the loss on a batch given its forward intermediates.
| batch | Current training batch. |
| forward_propagation | Forward intermediates for the batch. |
|
static |
Computes the finite-difference perturbation step for a parameter.
| parameter | Parameter value at which the gradient is evaluated. |
| float opennn::Loss::calculate_regularization | ( | const VectorR & | parameters | ) | const |
Computes the regularization term given the parameter vector.
| parameters | Flat vector of all trainable parameters. |
| void opennn::Loss::from_JSON | ( | const JsonDocument & | ) |
Loads loss hyperparameters from a parsed JSON document.
|
inline |
Mutable access to the bound dataset.
|
inline |
Read-only access to the bound dataset.
|
inline |
Currently selected loss term.
|
inline |
Canonical name of the loss (e.g. "Loss").
|
inline |
Mutable access to the bound network.
|
inline |
Read-only access to the bound network.
|
inline |
Prints a human-readable summary of the loss to stdout.
| void opennn::Loss::regularization_from_JSON | ( | const JsonDocument & | ) |
Reads only the regularization fields from a parsed JSON document.
|
inlinestatic |
Returns the singleton string<->enum mapping for Regularization values.
| void opennn::Loss::regularization_to_JSON | ( | JsonWriter & | ) | const |
Writes only the regularization fields to a streaming JSON writer.
|
inlinestatic |
Converts a Regularization to its canonical string name.
| regularization | Regularization value. |
| void opennn::Loss::set | ( | NeuralNetwork * | neural_network = nullptr, |
| Dataset * | dataset = nullptr ) |
|
inlinevirtual |
Sets the bound dataset; subclasses may override to refresh cached state derived from the dataset.
| new_dataset | Dataset that provides training data. |
| void opennn::Loss::set_error | ( | const Error & | ) |
Sets the loss term directly.
Receives the Loss::Error enum value to install.
| void opennn::Loss::set_error | ( | const string & | ) |
Sets the loss term by name.
Receives the canonical name of the loss term.
|
inline |
Sets the bound network.
| new_neural_network | Network whose parameters are being trained. |
| void opennn::Loss::set_normalization_coefficient | ( | ) |
Recomputes the dataset-derived normalization coefficient.
Called automatically after the dataset is bound; only relevant for NormalizedSquaredError.
|
inline |
Sets the regularization method by name.
| new_regularization_method | Canonical name (see Regularization). |
|
inline |
Sets the regularization method directly.
| new_regularization | Regularization enum value. |
|
inline |
Sets the strength of the regularization term.
| new_regularization_weight | Multiplier applied to the regularization term. |
|
inlinestatic |
Parses a Regularization from string.
Accepts canonical names plus the alias "NoRegularization" (also encoded as "None" in the map).
| name | String to parse. |
| void opennn::Loss::to_JSON | ( | JsonWriter & | ) | const |
Writes loss hyperparameters to a streaming JSON writer.
|
protected |
Currently selected loss term.
|
protected |
Exponent p used by MinkowskiError.
|
protected |
Canonical name of the loss instance.
|
protected |
Weight applied to negative-class samples (WeightedSquaredError).
|
protected |
Network whose parameters are being trained; not owned.
|
protected |
Variance of the targets, used by NormalizedSquaredError.
|
protected |
Weight applied to positive-class samples (WeightedSquaredError).
|
protected |
Currently selected regularization term.
|
protected |
Multiplier applied to the regularization term in the total loss.