1
2
3
4
5
6
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
93
94
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/// `LossFunction` represents a loss function used to evaluate how well the model's predictions
/// match the actual targets during training. It contains two function pointers:
/// one for the loss function itself and another for computing its derivative (gradient),
/// which is used during backpropagation.
///
/// # Fields
/// * `function` - A function that computes the value of the loss function given the model's predictions and the true target values.
/// * `derivative` - A function that computes the gradient of the loss with respect to the predictions, which is used for backpropagation.
///
/// # Example
/// ```
/// let loss = LossFunction {
/// function: mean_squared_error,
/// derivative: mse_derivative,
/// };
/// ```
/// `MSE` is a constant that holds the `LossFunction` for Mean Squared Error.
/// It contains both the `mean_squared_error` function and its derivative `mse_derivative`.
/// Mean Squared Error is a commonly used loss function for regression tasks.
///
/// # Example
/// ```
/// let predictions = vec![1.0, 2.0, 3.0];
/// let targets = vec![1.1, 2.1, 3.1];
/// let loss_value = MSE.function(&predictions, &targets);
/// let gradients = MSE.derivative(&predictions, &targets);
/// ```
pub const MSE: LossFunction = LossFunction ;
/// `mean_squared_error` computes the Mean Squared Error (MSE) between the predictions and the targets.
/// The MSE is the average of the squared differences between predicted and actual values.
///
/// # Arguments
/// * `predictions` - A slice of `f32` values representing the predicted values from the model.
/// * `targets` - A slice of `f32` values representing the true target values.
///
/// # Returns
/// The Mean Squared Error value, which is a non-negative `f32` representing the average squared difference
/// between the predictions and targets.
///
/// # Example
/// ```
/// let predictions = vec![1.0, 2.0, 3.0];
/// let targets = vec![1.1, 2.1, 3.1];
/// let loss = mean_squared_error(&predictions, &targets);
/// assert_eq!(loss, 0.01);
/// ```
/// `mse_derivative` computes the derivative of the Mean Squared Error (MSE) loss with respect to the predictions.
/// This derivative is used during backpropagation to update the model's parameters.
///
/// # Arguments
/// * `predictions` - A slice of `f32` values representing the predicted values from the model.
/// * `targets` - A slice of `f32` values representing the true target values.
///
/// # Returns
/// A `Vec<f32>` where each element is the gradient of the loss with respect to the corresponding prediction.
///
/// # Example
/// ```
/// let predictions = vec![1.0, 2.0, 3.0];
/// let targets = vec![1.1, 2.1, 3.1];
/// let gradients = mse_derivative(&predictions, &targets);
/// assert_eq!(gradients, vec![-0.2, -0.2, -0.2]);
/// ```
/// `MAE` is a constant that holds the `LossFunction` for Mean Absolute Error.
/// It contains both the `mean_absolute_error` function and its derivative `mae_derivative`.
/// Mean Absolute Error is commonly used in regression tasks where we want to penalize the absolute difference
/// between the predictions and targets.
///
/// # Example
/// ```
/// let predictions = vec![1.0, 2.0, 3.0];
/// let targets = vec![1.1, 2.1, 3.1];
/// let loss_value = MAE.function(&predictions, &targets);
/// let gradients = MAE.derivative(&predictions, &targets);
/// ```
pub const MAE: LossFunction = LossFunction ;
/// `mean_absolute_error` computes the Mean Absolute Error (MAE) between the predictions and the targets.
/// The MAE is the average of the absolute differences between predicted and actual values.
///
/// # Arguments
/// * `predictions` - A slice of `f32` values representing the predicted values from the model.
/// * `targets` - A slice of `f32` values representing the true target values.
///
/// # Returns
/// The Mean Absolute Error value, which is a non-negative `f32` representing the average absolute difference
/// between the predictions and targets.
///
/// # Example
/// ```
/// let predictions = vec![1.0, 2.0, 3.0];
/// let targets = vec![1.1, 2.1, 3.1];
/// let loss = mean_absolute_error(&predictions, &targets);
/// assert_eq!(loss, 0.1);
/// ```
///
/// `mae_derivative` computes the derivative of the Mean Absolute Error (MAE) loss with respect to the predictions.
/// The derivative of MAE is 1 or -1 depending on whether the prediction is greater or smaller than the target.
/// This is used during backpropagation to update the model's parameters.
///
/// # Arguments
/// * `predictions` - A slice of `f32` values representing the predicted values from the model.
/// * `targets` - A slice of `f32` values representing the true target values.
///
/// # Returns
/// A `Vec<f32>` where each element is the gradient of the loss with respect to the corresponding prediction.
///
/// # Example
/// ```
/// let predictions = vec![1.0, 2.0, 3.0];
/// let targets = vec![1.1, 2.1, 3.1];
/// let gradients = mae_derivative(&predictions, &targets);
/// assert_eq!(gradients, vec![-1.0, -1.0, -1.0]);
/// ```
///
/// `CrossEntropy` is a constant that holds the `LossFunction` for Cross-Entropy loss.
/// It contains both the `cross_entropy` function and its derivative `cross_entropy_derivative`.
/// Cross-Entropy Loss is used primarily in classification tasks.
///
/// # Example
/// ```
/// let predictions = vec![0.1, 0.9];
/// let targets = vec![0.0, 1.0];
/// let loss_value = CrossEntropy.function(&predictions, &targets);
/// let gradients = CrossEntropy.derivative(&predictions, &targets);
/// ```
pub const CROSS_ENTROPY: LossFunction = LossFunction ;
/// `cross_entropy` computes the Cross-Entropy loss between the predictions and the targets.
/// Cross-Entropy is commonly used in classification tasks, particularly in binary and multi-class problems.
///
/// # Arguments
/// * `predictions` - A slice of `f32` values representing the predicted probabilities from the model (between 0 and 1).
/// * `targets` - A slice of `f32` values representing the true class labels (usually 0 or 1 for binary classification).
///
/// # Returns
/// The Cross-Entropy loss value, which is a non-negative `f32` representing the difference between the true distribution
/// and the predicted distribution.
///
/// # Example
/// ```
/// let predictions = vec![0.1, 0.9];
/// let targets = vec![0.0, 1.0];
/// let loss = cross_entropy(&predictions, &targets);
/// assert_eq!(loss, 0.105360516);
/// ```
///
/// `cross_entropy_derivative` computes the derivative of the Cross-Entropy loss with respect to the predictions.
/// This derivative is used during backpropagation to update the model's parameters in classification tasks.
///
/// # Arguments
/// * `predictions` - A slice of `f32` values representing the predicted probabilities from the model.
/// * `targets` - A slice of `f32` values representing the true target values (0 or 1).
///
/// # Returns
/// A `Vec<f32>` where each element is the gradient of the loss with respect to the corresponding prediction.
///
/// # Example
/// ```
/// let predictions = vec![0.1, 0.9];
/// let targets = vec![0.0, 1.0];
/// let gradients = cross_entropy_derivative(&predictions, &targets);
/// assert_eq!(gradients, vec![ -1.1111112, 1.1111112 ]);
/// ```
///