aprender-core 0.29.1

Next-generation machine learning library in pure Rust
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
pub(crate) use super::*;
#[test]
fn test_mse_loss_perfect() {
    let y_true = Vector::from_slice(&[1.0, 2.0, 3.0]);
    let y_pred = Vector::from_slice(&[1.0, 2.0, 3.0]);

    let loss = mse_loss(&y_pred, &y_true);
    assert!((loss - 0.0).abs() < 1e-6);
}

#[test]
fn test_mse_loss_basic() {
    let y_true = Vector::from_slice(&[1.0, 2.0, 3.0]);
    let y_pred = Vector::from_slice(&[2.0, 3.0, 4.0]);

    // Errors: [1, 1, 1], squared: [1, 1, 1], mean: 1.0
    let loss = mse_loss(&y_pred, &y_true);
    assert!((loss - 1.0).abs() < 1e-6);
}

#[test]
fn test_mse_loss_different_errors() {
    let y_true = Vector::from_slice(&[0.0, 0.0, 0.0]);
    let y_pred = Vector::from_slice(&[1.0, 2.0, 3.0]);

    // Errors: [1, 2, 3], squared: [1, 4, 9], mean: 14/3 ≈ 4.667
    let loss = mse_loss(&y_pred, &y_true);
    assert!((loss - 14.0 / 3.0).abs() < 1e-5);
}

#[test]
#[should_panic(expected = "same length")]
fn test_mse_loss_mismatched_lengths() {
    let y_true = Vector::from_slice(&[1.0, 2.0]);
    let y_pred = Vector::from_slice(&[1.0, 2.0, 3.0]);

    let _ = mse_loss(&y_pred, &y_true);
}

#[test]
fn test_mae_loss_perfect() {
    let y_true = Vector::from_slice(&[1.0, 2.0, 3.0]);
    let y_pred = Vector::from_slice(&[1.0, 2.0, 3.0]);

    let loss = mae_loss(&y_pred, &y_true);
    assert!((loss - 0.0).abs() < 1e-6);
}

#[test]
fn test_mae_loss_basic() {
    let y_true = Vector::from_slice(&[1.0, 2.0, 3.0]);
    let y_pred = Vector::from_slice(&[1.5, 2.5, 2.5]);

    // Errors: [0.5, 0.5, -0.5], abs: [0.5, 0.5, 0.5], mean: 0.5
    let loss = mae_loss(&y_pred, &y_true);
    assert!((loss - 0.5).abs() < 1e-6);
}

#[test]
fn test_mae_loss_outlier_robustness() {
    let y_true = Vector::from_slice(&[1.0, 2.0, 3.0]);
    let y_pred = Vector::from_slice(&[2.0, 3.0, 100.0]);

    // MAE: (1 + 1 + 97) / 3 = 33.0
    let mae = mae_loss(&y_pred, &y_true);

    // MSE: (1 + 1 + 9409) / 3 = 3137.0
    let mse = mse_loss(&y_pred, &y_true);

    // MAE should be much less affected by outlier
    assert!(mae < mse / 10.0);
}

#[test]
#[should_panic(expected = "same length")]
fn test_mae_loss_mismatched_lengths() {
    let y_true = Vector::from_slice(&[1.0, 2.0]);
    let y_pred = Vector::from_slice(&[1.0, 2.0, 3.0]);

    let _ = mae_loss(&y_pred, &y_true);
}

#[test]
fn test_huber_loss_small_errors() {
    let y_true = Vector::from_slice(&[1.0, 2.0, 3.0]);
    let y_pred = Vector::from_slice(&[1.5, 2.5, 3.5]);

    // All errors (0.5) <= delta (1.0), so use quadratic
    // 0.5 * (0.5)² * 3 / 3 = 0.125
    let loss = huber_loss(&y_pred, &y_true, 1.0);
    assert!((loss - 0.125).abs() < 1e-6);
}

#[test]
fn test_huber_loss_large_errors() {
    let y_true = Vector::from_slice(&[0.0, 0.0, 0.0]);
    let y_pred = Vector::from_slice(&[5.0, 5.0, 5.0]);

    // All errors (5.0) > delta (1.0), so use linear
    // 1.0 * (5.0 - 0.5 * 1.0) * 3 / 3 = 4.5
    let loss = huber_loss(&y_pred, &y_true, 1.0);
    assert!((loss - 4.5).abs() < 1e-6);
}

#[test]
fn test_huber_loss_mixed_errors() {
    let y_true = Vector::from_slice(&[0.0, 0.0]);
    let y_pred = Vector::from_slice(&[0.5, 5.0]);

    // First: 0.5 <= 1.0, use 0.5 * 0.5² = 0.125
    // Second: 5.0 > 1.0, use 1.0 * (5.0 - 0.5) = 4.5
    // Mean: (0.125 + 4.5) / 2 = 2.3125
    let loss = huber_loss(&y_pred, &y_true, 1.0);
    assert!((loss - 2.3125).abs() < 1e-5);
}

#[test]
#[should_panic(expected = "Delta must be positive")]
fn test_huber_loss_zero_delta() {
    let y_true = Vector::from_slice(&[1.0]);
    let y_pred = Vector::from_slice(&[2.0]);

    let _ = huber_loss(&y_pred, &y_true, 0.0);
}

#[test]
#[should_panic(expected = "Delta must be positive")]
fn test_huber_loss_negative_delta() {
    let y_true = Vector::from_slice(&[1.0]);
    let y_pred = Vector::from_slice(&[2.0]);

    let _ = huber_loss(&y_pred, &y_true, -1.0);
}

#[test]
fn test_huber_vs_mse_small_errors() {
    let y_true = Vector::from_slice(&[1.0, 2.0, 3.0]);
    let y_pred = Vector::from_slice(&[1.1, 2.1, 3.1]);

    let huber = huber_loss(&y_pred, &y_true, 1.0);
    let mse = mse_loss(&y_pred, &y_true);

    // For small errors, Huber should be close to MSE
    assert!((huber - mse).abs() < 0.01);
}

#[test]
fn test_huber_vs_mae_large_errors() {
    let y_true = Vector::from_slice(&[0.0, 0.0, 0.0]);
    let y_pred = Vector::from_slice(&[10.0, 10.0, 10.0]);

    let huber = huber_loss(&y_pred, &y_true, 1.0);
    let mae = mae_loss(&y_pred, &y_true);

    // For large errors, Huber should approximate MAE (with offset)
    assert!(huber < mae);
    assert!((huber - (mae - 0.5)).abs() < 0.1);
}

#[test]
fn test_mse_loss_struct() {
    let loss_fn = MSELoss;
    let y_true = Vector::from_slice(&[1.0, 2.0]);
    let y_pred = Vector::from_slice(&[1.0, 2.0]);

    let loss = loss_fn.compute(&y_pred, &y_true);
    assert!((loss - 0.0).abs() < 1e-6);
    assert_eq!(loss_fn.name(), "MSE");
}

#[test]
fn test_mae_loss_struct() {
    let loss_fn = MAELoss;
    let y_true = Vector::from_slice(&[1.0, 2.0]);
    let y_pred = Vector::from_slice(&[1.5, 2.5]);

    let loss = loss_fn.compute(&y_pred, &y_true);
    assert!((loss - 0.5).abs() < 1e-6);
    assert_eq!(loss_fn.name(), "MAE");
}

#[test]
fn test_huber_loss_struct() {
    let loss_fn = HuberLoss::new(1.0);
    let y_true = Vector::from_slice(&[1.0, 2.0]);
    let y_pred = Vector::from_slice(&[1.5, 2.5]);

    let loss = loss_fn.compute(&y_pred, &y_true);
    assert!(loss > 0.0);
    assert_eq!(loss_fn.name(), "Huber");
    assert!((loss_fn.delta() - 1.0).abs() < 1e-6);
}

#[test]
fn test_loss_trait_polymorphism() {
    let loss_fns: Vec<Box<dyn Loss>> = vec![
        Box::new(MSELoss),
        Box::new(MAELoss),
        Box::new(HuberLoss::new(1.0)),
    ];

    let y_true = Vector::from_slice(&[1.0, 2.0]);
    let y_pred = Vector::from_slice(&[1.5, 2.5]);

    for loss_fn in loss_fns {
        let loss = loss_fn.compute(&y_pred, &y_true);
        assert!(loss > 0.0);
        assert!(!loss_fn.name().is_empty());
    }
}

#[test]
fn test_negative_values() {
    let y_true = Vector::from_slice(&[-1.0, -2.0, -3.0]);
    let y_pred = Vector::from_slice(&[-1.5, -2.5, -3.5]);

    let mse = mse_loss(&y_pred, &y_true);
    let mae = mae_loss(&y_pred, &y_true);
    let huber = huber_loss(&y_pred, &y_true, 1.0);

    assert!(mse > 0.0);
    assert!(mae > 0.0);
    assert!(huber > 0.0);
}

#[test]
fn test_zero_values() {
    let y_true = Vector::from_slice(&[0.0, 0.0, 0.0]);
    let y_pred = Vector::from_slice(&[0.0, 0.0, 0.0]);

    let mse = mse_loss(&y_pred, &y_true);
    let mae = mae_loss(&y_pred, &y_true);
    let huber = huber_loss(&y_pred, &y_true, 1.0);

    assert!((mse - 0.0).abs() < 1e-6);
    assert!((mae - 0.0).abs() < 1e-6);
    assert!((huber - 0.0).abs() < 1e-6);
}

#[test]
fn test_single_value() {
    let y_true = Vector::from_slice(&[5.0]);
    let y_pred = Vector::from_slice(&[3.0]);

    let mse = mse_loss(&y_pred, &y_true);
    let mae = mae_loss(&y_pred, &y_true);
    let huber = huber_loss(&y_pred, &y_true, 1.0);

    assert!((mse - 4.0).abs() < 1e-6);
    assert!((mae - 2.0).abs() < 1e-6);
    assert!(huber > 0.0);
}

// ==========================================================================
// Contrastive Loss Tests (EXTREME TDD)
// ==========================================================================

#[test]
fn test_triplet_loss_satisfied_margin() {
    // Positive is much closer than negative - loss should be 0
    let anchor = Vector::from_slice(&[1.0, 0.0, 0.0]);
    let positive = Vector::from_slice(&[0.9, 0.1, 0.0]); // d ≈ 0.14
    let negative = Vector::from_slice(&[0.0, 1.0, 0.0]); // d ≈ 1.41

    let loss = triplet_loss(&anchor, &positive, &negative, 0.2);
    assert!(
        (loss - 0.0).abs() < 1e-6,
        "Loss should be 0 when margin satisfied"
    );
}

#[test]
fn test_triplet_loss_violated_margin() {
    // Positive and negative equidistant - should have loss = margin
    let anchor = Vector::from_slice(&[0.0, 0.0]);
    let positive = Vector::from_slice(&[1.0, 0.0]); // d = 1.0
    let negative = Vector::from_slice(&[0.0, 1.0]); // d = 1.0

    let loss = triplet_loss(&anchor, &positive, &negative, 0.5);
    // d_pos - d_neg + margin = 1.0 - 1.0 + 0.5 = 0.5
    assert!((loss - 0.5).abs() < 1e-5);
}

#[test]
fn test_triplet_loss_hard_negative() {
    // Negative closer than positive (hard case)
    let anchor = Vector::from_slice(&[0.0, 0.0]);
    let positive = Vector::from_slice(&[2.0, 0.0]); // d = 2.0
    let negative = Vector::from_slice(&[0.5, 0.0]); // d = 0.5

    let loss = triplet_loss(&anchor, &positive, &negative, 0.2);
    // d_pos - d_neg + margin = 2.0 - 0.5 + 0.2 = 1.7
    assert!((loss - 1.7).abs() < 1e-5);
}

#[test]
fn test_triplet_loss_zero_margin() {
    let anchor = Vector::from_slice(&[0.0, 0.0]);
    let positive = Vector::from_slice(&[1.0, 0.0]);
    let negative = Vector::from_slice(&[0.0, 2.0]);

    let loss = triplet_loss(&anchor, &positive, &negative, 0.0);
    // d_pos = 1.0, d_neg = 2.0, loss = max(0, 1.0 - 2.0 + 0) = 0
    assert!((loss - 0.0).abs() < 1e-6);
}

#[test]
#[should_panic(expected = "same dimension")]
fn test_triplet_loss_dimension_mismatch() {
    let anchor = Vector::from_slice(&[1.0, 0.0]);
    let positive = Vector::from_slice(&[1.0, 0.0, 0.0]);
    let negative = Vector::from_slice(&[0.0, 1.0]);

    let _ = triplet_loss(&anchor, &positive, &negative, 0.2);
}

#[test]
fn test_info_nce_loss_basic() {
    let anchor = Vector::from_slice(&[1.0, 0.0, 0.0]);
    let positive = Vector::from_slice(&[0.95, 0.05, 0.0]); // high similarity
    let negatives = vec![
        Vector::from_slice(&[0.0, 1.0, 0.0]), // low similarity
        Vector::from_slice(&[0.0, 0.0, 1.0]), // low similarity
    ];

    let loss = info_nce_loss(&anchor, &positive, &negatives, 0.1);
    // Should be positive (it's a proper loss)
    assert!(loss >= 0.0);
    // Should be relatively low since positive is very similar
    assert!(loss < 2.0);
}

#[test]
fn test_info_nce_loss_perfect_alignment() {
    // Identical vectors should give low loss
    let anchor = Vector::from_slice(&[1.0, 0.0]);
    let positive = Vector::from_slice(&[1.0, 0.0]); // identical
    let negatives = vec![
        Vector::from_slice(&[-1.0, 0.0]), // opposite
    ];

    let loss = info_nce_loss(&anchor, &positive, &negatives, 0.5);
    // With perfect positive and opposite negative, loss should be very low
    assert!(loss < 0.5);
}

#[test]
fn test_info_nce_loss_temperature_effect() {
    let anchor = Vector::from_slice(&[1.0, 0.0, 0.0]);
    let positive = Vector::from_slice(&[0.7, 0.7, 0.0]);
    let negatives = vec![Vector::from_slice(&[0.0, 1.0, 0.0])];

    let loss_low_temp = info_nce_loss(&anchor, &positive, &negatives, 0.1);
    let loss_high_temp = info_nce_loss(&anchor, &positive, &negatives, 1.0);

    // Lower temperature makes distribution sharper
    // The exact relationship depends on the similarities
    assert!(loss_low_temp != loss_high_temp);
}

#[test]
fn test_info_nce_loss_many_negatives() {
    let anchor = Vector::from_slice(&[1.0, 0.0]);
    let positive = Vector::from_slice(&[0.9, 0.1]);
    let negatives: Vec<Vector<f32>> = (0..10)
        .map(|i| {
            let angle = (i as f32) * 0.3 + 1.0; // various angles
            Vector::from_slice(&[angle.cos(), angle.sin()])
        })
        .collect();

    let loss = info_nce_loss(&anchor, &positive, &negatives, 0.2);
    // More negatives should increase difficulty
    assert!(loss > 0.0);
}

#[test]
#[should_panic(expected = "same dimension")]
fn test_info_nce_loss_dimension_mismatch() {
    let anchor = Vector::from_slice(&[1.0, 0.0]);
    let positive = Vector::from_slice(&[0.9, 0.1]);
    let negatives = vec![Vector::from_slice(&[0.0, 1.0, 0.0])]; // wrong dim

    let _ = info_nce_loss(&anchor, &positive, &negatives, 0.1);
}

#[test]
#[should_panic(expected = "Temperature must be positive")]
fn test_info_nce_loss_zero_temperature() {
    let anchor = Vector::from_slice(&[1.0, 0.0]);
    let positive = Vector::from_slice(&[0.9, 0.1]);
    let negatives = vec![Vector::from_slice(&[0.0, 1.0])];

    let _ = info_nce_loss(&anchor, &positive, &negatives, 0.0);
}

#[test]
fn test_focal_loss_confident_correct() {
    // Model is confident and correct - loss should be low
    let predictions = Vector::from_slice(&[0.99, 0.01]);
    let targets = Vector::from_slice(&[1.0, 0.0]);

    let loss = focal_loss(&predictions, &targets, 0.25, 2.0);
    // Focal loss down-weights easy examples
    assert!(loss < 0.01);
}

#[test]
fn test_focal_loss_confident_wrong() {
    // Model is confident but wrong - loss should be high
    let predictions = Vector::from_slice(&[0.01, 0.99]);
    let targets = Vector::from_slice(&[1.0, 0.0]);

    let loss = focal_loss(&predictions, &targets, 0.25, 2.0);
    // High loss for confident wrong predictions
    assert!(loss > 1.0);
}

#[test]
fn test_focal_loss_uncertain() {
    // Model is uncertain - moderate loss
    let predictions = Vector::from_slice(&[0.5, 0.5]);
    let targets = Vector::from_slice(&[1.0, 0.0]);

    let loss = focal_loss(&predictions, &targets, 0.25, 2.0);
    assert!(loss > 0.0);
    assert!(loss < 1.0);
}

#[test]
fn test_focal_loss_gamma_effect() {
    let predictions = Vector::from_slice(&[0.9]);
    let targets = Vector::from_slice(&[1.0]);

    let loss_gamma_0 = focal_loss(&predictions, &targets, 0.25, 0.0);
    let loss_gamma_2 = focal_loss(&predictions, &targets, 0.25, 2.0);

    // Higher gamma reduces loss for easy examples more
    assert!(loss_gamma_2 < loss_gamma_0);
}

// ==========================================================================
// Cross-Entropy Loss Tests (GH-280)
// ==========================================================================

#[test]
fn test_cross_entropy_loss_one_hot() {
    let logits = Vector::from_slice(&[2.0, 1.0, 0.5]);
    let targets = Vector::from_slice(&[1.0, 0.0, 0.0]); // one-hot class 0

    let loss = cross_entropy_loss(&logits, &targets);
    assert!(loss > 0.0);
    assert!(loss.is_finite());
}


include!("tests_include_01.rs");