scirs2-neural 0.4.2

Neural network building blocks module for SciRS2 (scirs2-neural) - Minimal Version
Documentation
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//! Extended loss functions for specialised training scenarios
//!
//! This module provides functional-style loss functions that complement the
//! struct-based losses in `scirs2_neural::losses`. All functions follow the
//! signature convention `fn loss(preds, targets, …) -> Result<f64>`.
//!
//! # Available functions
//! | Function | Use-case |
//! |---|---|
//! | [`focal_loss`]       | Class-imbalanced classification |
//! | [`dice_loss`]        | Image segmentation |
//! | [`tversky_loss`]     | Segmentation with tunable FP/FN trade-off |
//! | [`contrastive_loss`] | Metric learning with positive/negative pairs |
//! | [`triplet_loss`]     | Metric learning with anchor/positive/negative |
//!
//! All functions operate on **`ndarray::ArrayView`** slices so the caller
//! owns the data and there are no hidden copies.

use crate::error::{NeuralError, Result};
use scirs2_core::ndarray::{Array, IxDyn};

// ---------------------------------------------------------------------------
// Focal Loss (functional)
// ---------------------------------------------------------------------------

/// Focal loss for binary / multi-label classification with class imbalance.
///
/// For each element `(p, y)` in `preds` / `targets`:
/// ```text
/// FL = -α · (1 - p_t)^γ · log(p_t)
/// ```
/// where `p_t = p` when `y == 1`, `p_t = 1 - p` otherwise.
///
/// The loss is **averaged** over all elements.
///
/// # Arguments
/// * `preds`   – Predicted probabilities in `[0, 1]`, any shape (will be flattened)
/// * `targets` – Binary targets (0 or 1), same shape as `preds`
/// * `alpha`   – Scalar weighting factor (e.g. 0.25)
/// * `gamma`   – Focusing parameter (e.g. 2.0)
///
/// # Examples
/// ```
/// use scirs2_neural::losses::extended::focal_loss;
/// use scirs2_core::ndarray::array;
///
/// let preds   = array![0.9, 0.6, 0.2, 0.1].into_dyn();
/// let targets = array![1.0, 1.0, 0.0, 0.0].into_dyn();
/// let loss = focal_loss(&preds, &targets, 0.25, 2.0).expect("focal_loss failed");
/// assert!(loss >= 0.0);
/// ```
pub fn focal_loss(
    preds: &Array<f64, IxDyn>,
    targets: &Array<f64, IxDyn>,
    alpha: f64,
    gamma: f64,
) -> Result<f64> {
    if preds.shape() != targets.shape() {
        return Err(NeuralError::InferenceError(format!(
            "focal_loss: shape mismatch {:?} vs {:?}",
            preds.shape(),
            targets.shape()
        )));
    }
    let eps = 1e-12;
    let n = preds.len() as f64;
    if n == 0.0 {
        return Err(NeuralError::InferenceError(
            "focal_loss: empty tensors".to_string(),
        ));
    }
    let loss: f64 = preds
        .iter()
        .zip(targets.iter())
        .map(|(&p, &y)| {
            let p_clamped = p.max(eps).min(1.0 - eps);
            let p_t = if y > 0.5 { p_clamped } else { 1.0 - p_clamped };
            let alpha_t = if y > 0.5 { alpha } else { 1.0 - alpha };
            -alpha_t * (1.0 - p_t).powf(gamma) * p_t.ln()
        })
        .sum();
    Ok(loss / n)
}

// ---------------------------------------------------------------------------
// Dice Loss (functional)
// ---------------------------------------------------------------------------

/// Soft Dice coefficient loss for segmentation tasks.
///
/// ```text
/// Dice = 1 - (2 · Σ(p · y) + ε) / (Σp + Σy + ε)
/// ```
///
/// # Arguments
/// * `preds`   – Predicted probabilities in `[0, 1]`
/// * `targets` – Binary ground-truth mask (0 or 1)
///
/// # Examples
/// ```
/// use scirs2_neural::losses::extended::dice_loss;
/// use scirs2_core::ndarray::array;
///
/// let preds   = array![0.9, 0.8, 0.1, 0.2].into_dyn();
/// let targets = array![1.0, 1.0, 0.0, 0.0].into_dyn();
/// let loss = dice_loss(&preds, &targets).expect("dice_loss failed");
/// assert!(loss >= 0.0 && loss <= 1.0);
/// ```
pub fn dice_loss(preds: &Array<f64, IxDyn>, targets: &Array<f64, IxDyn>) -> Result<f64> {
    if preds.shape() != targets.shape() {
        return Err(NeuralError::InferenceError(format!(
            "dice_loss: shape mismatch {:?} vs {:?}",
            preds.shape(),
            targets.shape()
        )));
    }
    if preds.is_empty() {
        return Err(NeuralError::InferenceError(
            "dice_loss: empty tensors".to_string(),
        ));
    }
    let eps = 1e-8;
    let intersection: f64 = preds.iter().zip(targets.iter()).map(|(&p, &y)| p * y).sum();
    let sum_p: f64 = preds.iter().sum();
    let sum_y: f64 = targets.iter().sum();
    let dice = (2.0 * intersection + eps) / (sum_p + sum_y + eps);
    Ok(1.0 - dice)
}

// ---------------------------------------------------------------------------
// Tversky Loss (functional)
// ---------------------------------------------------------------------------

/// Tversky loss – a generalisation of Dice that separately weights false
/// positives (FP) and false negatives (FN).
///
/// ```text
/// Tversky = 1 - (TP + ε) / (TP + α·FP + β·FN + ε)
/// ```
///
/// Setting `alpha = beta = 0.5` recovers the Dice loss.
///
/// # Arguments
/// * `preds`   – Predicted probabilities in `[0, 1]`
/// * `targets` – Binary ground-truth mask
/// * `alpha`   – Weight for false positives
/// * `beta`    – Weight for false negatives
///
/// # Examples
/// ```
/// use scirs2_neural::losses::extended::tversky_loss;
/// use scirs2_core::ndarray::array;
///
/// let preds   = array![0.9, 0.8, 0.1, 0.05].into_dyn();
/// let targets = array![1.0, 1.0, 0.0, 0.0].into_dyn();
/// let loss = tversky_loss(&preds, &targets, 0.3, 0.7).expect("tversky_loss failed");
/// assert!(loss >= 0.0 && loss <= 1.0);
/// ```
pub fn tversky_loss(
    preds: &Array<f64, IxDyn>,
    targets: &Array<f64, IxDyn>,
    alpha: f64,
    beta: f64,
) -> Result<f64> {
    if preds.shape() != targets.shape() {
        return Err(NeuralError::InferenceError(format!(
            "tversky_loss: shape mismatch {:?} vs {:?}",
            preds.shape(),
            targets.shape()
        )));
    }
    if preds.is_empty() {
        return Err(NeuralError::InferenceError(
            "tversky_loss: empty tensors".to_string(),
        ));
    }
    let eps = 1e-8;
    let mut tp = 0.0_f64;
    let mut fp = 0.0_f64;
    let mut fn_ = 0.0_f64;
    for (&p, &y) in preds.iter().zip(targets.iter()) {
        tp += p * y;
        fp += p * (1.0 - y);
        fn_ += (1.0 - p) * y;
    }
    let tversky = (tp + eps) / (tp + alpha * fp + beta * fn_ + eps);
    Ok(1.0 - tversky)
}

// ---------------------------------------------------------------------------
// Contrastive Loss (functional)
// ---------------------------------------------------------------------------

/// Contrastive loss for metric learning.
///
/// For each pair `(e1_i, e2_i)` with similarity label `y_i ∈ {0, 1}`:
/// ```text
/// L = y · d² + (1 - y) · max(0, margin - d)²
/// ```
/// where `d = ‖e1 − e2‖₂`.
///
/// # Arguments
/// * `embeddings1` – First set of embeddings, shape `[batch, dim]`
/// * `embeddings2` – Second set of embeddings, shape `[batch, dim]`
/// * `labels`      – Similarity labels: 1 = similar, 0 = dissimilar, shape `[batch]`
/// * `margin`      – Minimum distance for dissimilar pairs
///
/// # Examples
/// ```
/// use scirs2_neural::losses::extended::contrastive_loss;
/// use scirs2_core::ndarray::{array, arr2};
///
/// let e1 = arr2(&[[1.0, 0.0], [0.0, 1.0]]).into_dyn();
/// let e2 = arr2(&[[1.0, 0.0], [1.0, 0.0]]).into_dyn();
/// let labels = array![1.0, 0.0].into_dyn(); // first similar, second dissimilar
/// let loss = contrastive_loss(&e1, &e2, &labels, 1.0).expect("contrastive_loss failed");
/// assert!(loss >= 0.0);
/// ```
pub fn contrastive_loss(
    embeddings1: &Array<f64, IxDyn>,
    embeddings2: &Array<f64, IxDyn>,
    labels: &Array<f64, IxDyn>,
    margin: f64,
) -> Result<f64> {
    let s1 = embeddings1.shape();
    let s2 = embeddings2.shape();
    let sl = labels.shape();
    if s1.len() != 2 || s2.len() != 2 {
        return Err(NeuralError::InferenceError(
            "contrastive_loss: embeddings must be 2-D [batch, dim]".to_string(),
        ));
    }
    if s1 != s2 {
        return Err(NeuralError::InferenceError(format!(
            "contrastive_loss: embedding shape mismatch {:?} vs {:?}",
            s1, s2
        )));
    }
    let batch = s1[0];
    if sl != &[batch] {
        return Err(NeuralError::InferenceError(format!(
            "contrastive_loss: labels shape {:?} does not match batch size {}",
            sl, batch
        )));
    }
    let dim = s1[1];
    let n = batch as f64;
    let mut total = 0.0_f64;
    for i in 0..batch {
        let mut dist_sq = 0.0_f64;
        for j in 0..dim {
            let d = embeddings1[[i, j]] - embeddings2[[i, j]];
            dist_sq += d * d;
        }
        let dist = dist_sq.sqrt();
        let y = labels[i];
        total += y * dist_sq + (1.0 - y) * (margin - dist).max(0.0).powi(2);
    }
    Ok(total / n)
}

// ---------------------------------------------------------------------------
// Triplet Loss (functional)
// ---------------------------------------------------------------------------

/// Triplet loss for metric learning.
///
/// For each triplet `(anchor_i, positive_i, negative_i)`:
/// ```text
/// L = max(0, d(a, p)² − d(a, n)² + margin)
/// ```
/// where `d` is the Euclidean distance.
///
/// # Arguments
/// * `anchor`   – Anchor embeddings, shape `[batch, dim]`
/// * `positive` – Positive embeddings (same class as anchor), shape `[batch, dim]`
/// * `negative` – Negative embeddings (different class), shape `[batch, dim]`
/// * `margin`   – Minimum margin between positive and negative distances
///
/// # Examples
/// ```
/// use scirs2_neural::losses::extended::triplet_loss;
/// use scirs2_core::ndarray::arr2;
///
/// let a = arr2(&[[1.0, 0.0], [0.0, 1.0]]).into_dyn();
/// let p = arr2(&[[0.9, 0.1], [0.1, 0.9]]).into_dyn();
/// let n = arr2(&[[0.0, 1.0], [1.0, 0.0]]).into_dyn();
/// let loss = triplet_loss(&a, &p, &n, 0.5).expect("triplet_loss failed");
/// assert!(loss >= 0.0);
/// ```
pub fn triplet_loss(
    anchor: &Array<f64, IxDyn>,
    positive: &Array<f64, IxDyn>,
    negative: &Array<f64, IxDyn>,
    margin: f64,
) -> Result<f64> {
    let sa = anchor.shape();
    let sp = positive.shape();
    let sn = negative.shape();
    if sa.len() != 2 {
        return Err(NeuralError::InferenceError(
            "triplet_loss: inputs must be 2-D [batch, dim]".to_string(),
        ));
    }
    if sa != sp || sa != sn {
        return Err(NeuralError::InferenceError(format!(
            "triplet_loss: shape mismatch anchor {:?}, positive {:?}, negative {:?}",
            sa, sp, sn
        )));
    }
    let batch = sa[0];
    let dim = sa[1];
    let n = batch as f64;
    let mut total = 0.0_f64;
    for i in 0..batch {
        let mut d_ap = 0.0_f64;
        let mut d_an = 0.0_f64;
        for j in 0..dim {
            let dap = anchor[[i, j]] - positive[[i, j]];
            let dan = anchor[[i, j]] - negative[[i, j]];
            d_ap += dap * dap;
            d_an += dan * dan;
        }
        total += (d_ap - d_an + margin).max(0.0);
    }
    Ok(total / n)
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use scirs2_core::ndarray::{array, arr2};

    // ---- focal_loss ----

    #[test]
    fn test_focal_loss_perfect_predictions() {
        // Perfect predictions → very low focal loss
        let preds = array![0.9999, 0.9999, 0.0001, 0.0001].into_dyn();
        let targets = array![1.0, 1.0, 0.0, 0.0].into_dyn();
        let loss = focal_loss(&preds, &targets, 0.25, 2.0).expect("focal_loss failed");
        assert!(loss < 1e-3, "Perfect predictions should have near-zero focal loss, got {}", loss);
    }

    #[test]
    fn test_focal_loss_hard_examples() {
        // Mis-classified examples → higher focal loss than standard cross-entropy
        let preds = array![0.1, 0.1].into_dyn();
        let targets = array![1.0, 1.0].into_dyn();
        let loss = focal_loss(&preds, &targets, 1.0, 0.0).expect("focal_loss failed");
        // gamma=0 recovers standard CE
        assert!(loss > 0.0, "Wrong predictions should have positive loss");
    }

    #[test]
    fn test_focal_loss_shape_mismatch() {
        let preds = array![0.5, 0.5, 0.5].into_dyn();
        let targets = array![1.0, 0.0].into_dyn();
        assert!(focal_loss(&preds, &targets, 0.25, 2.0).is_err());
    }

    // ---- dice_loss ----

    #[test]
    fn test_dice_loss_perfect() {
        let preds = array![1.0, 1.0, 0.0, 0.0].into_dyn();
        let targets = array![1.0, 1.0, 0.0, 0.0].into_dyn();
        let loss = dice_loss(&preds, &targets).expect("dice_loss failed");
        assert!(loss < 1e-6, "Perfect dice should be ~0, got {}", loss);
    }

    #[test]
    fn test_dice_loss_worst() {
        // Completely wrong: predict all 0 for all-1 target
        let preds = array![0.0, 0.0].into_dyn();
        let targets = array![1.0, 1.0].into_dyn();
        let loss = dice_loss(&preds, &targets).expect("dice_loss failed");
        assert!(loss > 0.9, "Worst dice should be near 1, got {}", loss);
    }

    #[test]
    fn test_dice_loss_range() {
        let preds = array![0.7, 0.3, 0.2, 0.8].into_dyn();
        let targets = array![1.0, 0.0, 0.0, 1.0].into_dyn();
        let loss = dice_loss(&preds, &targets).expect("dice_loss failed");
        assert!((0.0..=1.0).contains(&loss), "Dice loss must be in [0, 1], got {}", loss);
    }

    // ---- tversky_loss ----

    #[test]
    fn test_tversky_loss_symmetric_equals_dice() {
        let preds = array![0.8, 0.9, 0.1, 0.2].into_dyn();
        let targets = array![1.0, 1.0, 0.0, 0.0].into_dyn();
        let tversky = tversky_loss(&preds, &targets, 0.5, 0.5).expect("tversky failed");
        let dice = dice_loss(&preds, &targets).expect("dice failed");
        assert!((tversky - dice).abs() < 1e-6, "Tversky(0.5,0.5) should equal Dice");
    }

    #[test]
    fn test_tversky_loss_range() {
        let preds = array![0.6, 0.4, 0.3, 0.7].into_dyn();
        let targets = array![1.0, 1.0, 0.0, 0.0].into_dyn();
        let loss = tversky_loss(&preds, &targets, 0.3, 0.7).expect("tversky failed");
        assert!((0.0..=1.0).contains(&loss), "Tversky must be in [0, 1], got {}", loss);
    }

    #[test]
    fn test_tversky_loss_shape_mismatch() {
        let preds = array![0.5, 0.5].into_dyn();
        let targets = array![1.0, 0.0, 0.0].into_dyn();
        assert!(tversky_loss(&preds, &targets, 0.5, 0.5).is_err());
    }

    // ---- contrastive_loss ----

    #[test]
    fn test_contrastive_loss_identical_similar() {
        // Identical embeddings marked as similar → d=0 → loss=0
        let e1 = arr2(&[[1.0, 0.0]]).into_dyn();
        let e2 = arr2(&[[1.0, 0.0]]).into_dyn();
        let labels = array![1.0].into_dyn();
        let loss = contrastive_loss(&e1, &e2, &labels, 1.0).expect("contrastive failed");
        assert!(loss.abs() < 1e-10, "Identical similar pair should have ~0 loss, got {}", loss);
    }

    #[test]
    fn test_contrastive_loss_far_dissimilar() {
        // Embeddings far apart, marked dissimilar → low loss (margin satisfied)
        let e1 = arr2(&[[10.0, 0.0]]).into_dyn();
        let e2 = arr2(&[[0.0, 0.0]]).into_dyn();
        let labels = array![0.0].into_dyn();
        // margin = 1.0; d = 10.0 > margin → max(0, 1 - 10)^2 = 0
        let loss = contrastive_loss(&e1, &e2, &labels, 1.0).expect("contrastive failed");
        assert!(loss.abs() < 1e-10, "Far dissimilar pair should have ~0 loss, got {}", loss);
    }

    #[test]
    fn test_contrastive_loss_shape_error() {
        let e1 = arr2(&[[1.0, 0.0], [0.0, 1.0]]).into_dyn();
        let e2 = arr2(&[[1.0, 0.0]]).into_dyn(); // wrong batch
        let labels = array![1.0, 0.0].into_dyn();
        assert!(contrastive_loss(&e1, &e2, &labels, 1.0).is_err());
    }

    // ---- triplet_loss ----

    #[test]
    fn test_triplet_loss_positive_closer() {
        // positive much closer to anchor than negative → loss = 0 (margin satisfied)
        let anchor = arr2(&[[0.0, 0.0]]).into_dyn();
        let positive = arr2(&[[0.01, 0.0]]).into_dyn();
        let negative = arr2(&[[10.0, 0.0]]).into_dyn();
        let loss = triplet_loss(&anchor, &positive, &negative, 0.5)
            .expect("triplet failed");
        assert!(loss.abs() < 1e-6, "Easy triplet should have ~0 loss, got {}", loss);
    }

    #[test]
    fn test_triplet_loss_violation() {
        // negative is as close as positive → large loss
        let anchor = arr2(&[[0.0, 0.0]]).into_dyn();
        let positive = arr2(&[[1.0, 0.0]]).into_dyn();
        let negative = arr2(&[[1.0, 0.0]]).into_dyn();
        // d_ap == d_an → loss = max(0, 0 + margin) = margin
        let margin = 0.5;
        let loss = triplet_loss(&anchor, &positive, &negative, margin)
            .expect("triplet failed");
        assert!((loss - margin).abs() < 1e-6, "Expected loss = margin = {}, got {}", margin, loss);
    }

    #[test]
    fn test_triplet_loss_non_negative() {
        let anchor = arr2(&[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]).into_dyn();
        let positive = arr2(&[[1.1, 2.0, 3.0], [4.1, 5.0, 6.0]]).into_dyn();
        let negative = arr2(&[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]).into_dyn();
        let loss = triplet_loss(&anchor, &positive, &negative, 1.0)
            .expect("triplet failed");
        assert!(loss >= 0.0, "Triplet loss must be non-negative, got {}", loss);
    }

    #[test]
    fn test_triplet_loss_shape_error() {
        let anchor = arr2(&[[0.0, 0.0]]).into_dyn();
        let positive = arr2(&[[0.1, 0.0], [0.2, 0.0]]).into_dyn(); // wrong batch
        let negative = arr2(&[[1.0, 0.0]]).into_dyn();
        assert!(triplet_loss(&anchor, &positive, &negative, 1.0).is_err());
    }
}