Loss Functions API
Loss functions quantify how well the model's predictions match the ground truth. They provide the gradient signal used for optimization.
Base Loss
mpneuralnetwork.losses.Loss
Base class for loss functions.
Computes the error between the model's predictions and the true values, as well as the gradient of this error for backpropagation.
Source code in src/mpneuralnetwork/losses.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |
direct(output, output_expected)
abstractmethod
Computes the scalar loss value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output
|
ArrayType
|
Model predictions. |
required |
output_expected
|
ArrayType
|
Ground truth values. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The loss value. |
Source code in src/mpneuralnetwork/losses.py
14 15 16 17 18 19 20 21 22 23 24 25 | |
prime(output, output_expected)
abstractmethod
Computes the gradient of the loss function w.r.t the input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output
|
ArrayType
|
Model predictions. |
required |
output_expected
|
ArrayType
|
Ground truth values. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ArrayType |
ArrayType
|
Gradient of the loss. |
Source code in src/mpneuralnetwork/losses.py
27 28 29 30 31 32 33 34 35 36 37 38 | |
Regression
mpneuralnetwork.losses.MSE
Bases: Loss
Mean Squared Error (MSE) Loss.
Formula
L = (1/N) * sum((y_pred - y_true)^2)
Derivative
dL/dy_pred = (2/N) * (y_pred - y_true)
Used for regression problems.
Source code in src/mpneuralnetwork/losses.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | |
Classification
mpneuralnetwork.losses.BinaryCrossEntropy
Bases: Loss
Binary Cross Entropy Loss.
Formula (conceptually):
L = - (y * log(p) + (1-y) * log(1-p))
Implementation details
Assumes the input output corresponds to LOGITS (raw scores), not probabilities.
The Sigmoid activation is applied internally using the numerically stable log-sum-exp trick.
Used for binary classification problems.
Source code in src/mpneuralnetwork/losses.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | |
mpneuralnetwork.losses.CategoricalCrossEntropy
Bases: Loss
Categorical Cross Entropy Loss.
Formula (conceptually):
L = - sum(y_i * log(p_i))
Implementation details
Assumes the input output corresponds to LOGITS.
The Softmax activation is applied internally.
Used for multi-class classification problems (one-hot encoded targets).
Source code in src/mpneuralnetwork/losses.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
__init__(temperature=1.0, epsilon=1e-08)
Initializes the Categorical Cross Entropy loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
temperature
|
float
|
Temperature parameter for the softmax. Defaults to 1.0. |
1.0
|
epsilon
|
float
|
Small float added to denominator to avoid dividing by zero. Defaults to 1e-8. |
1e-08
|
Source code in src/mpneuralnetwork/losses.py
108 109 110 111 112 113 114 115 116 117 | |