tensorlogic-train 0.1.0

Training loops, loss composition, and optimization schedules for TensorLogic
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
//! Ranking metrics.

use crate::{TrainError, TrainResult};
use scirs2_core::ndarray::{ArrayView, Ix2};

use super::Metric;

/// Top-K accuracy metric.
/// Measures whether the correct class is in the top K predictions.
#[derive(Debug, Clone)]
pub struct TopKAccuracy {
    /// Number of top predictions to consider.
    pub k: usize,
}

impl Default for TopKAccuracy {
    fn default() -> Self {
        Self { k: 5 }
    }
}

impl TopKAccuracy {
    /// Create a new Top-K accuracy metric.
    pub fn new(k: usize) -> Self {
        Self { k }
    }
}

impl Metric for TopKAccuracy {
    fn compute(
        &self,
        predictions: &ArrayView<f64, Ix2>,
        targets: &ArrayView<f64, Ix2>,
    ) -> TrainResult<f64> {
        if predictions.shape() != targets.shape() {
            return Err(TrainError::MetricsError(format!(
                "Shape mismatch: predictions {:?} vs targets {:?}",
                predictions.shape(),
                targets.shape()
            )));
        }

        let num_classes = predictions.ncols();
        if self.k > num_classes {
            return Err(TrainError::MetricsError(format!(
                "K ({}) cannot be greater than number of classes ({})",
                self.k, num_classes
            )));
        }

        let mut correct = 0;
        let total = predictions.nrows();

        for i in 0..total {
            // Find true class
            let mut true_class = 0;
            let mut max_true = targets[[i, 0]];
            for j in 1..num_classes {
                if targets[[i, j]] > max_true {
                    max_true = targets[[i, j]];
                    true_class = j;
                }
            }

            // Get top K predictions
            let mut indices: Vec<usize> = (0..num_classes).collect();
            indices.sort_by(|&a, &b| {
                predictions[[i, b]]
                    .partial_cmp(&predictions[[i, a]])
                    .unwrap_or(std::cmp::Ordering::Equal)
            });

            // Check if true class is in top K
            if indices[..self.k].contains(&true_class) {
                correct += 1;
            }
        }

        Ok(correct as f64 / total as f64)
    }

    fn name(&self) -> &str {
        "top_k_accuracy"
    }
}

/// Normalized Discounted Cumulative Gain (NDCG) metric for ranking.
///
/// NDCG measures the quality of ranking by comparing the predicted order
/// with the ideal order. It accounts for position: items ranked higher
/// contribute more to the score.
///
/// # Formula
/// DCG@k = Σᵢ₌₁ᵏ (2^relᵢ - 1) / log₂(i + 1)
/// NDCG@k = DCG@k / IDCG@k
///
/// where IDCG is the DCG of the ideal ranking.
///
/// # Use Cases
/// - Recommendation systems
/// - Search engine ranking
/// - Information retrieval
/// - Learning to rank
///
/// Reference: Järvelin & Kekäläinen "Cumulated gain-based evaluation of IR techniques" (ACM TOIS 2002)
#[derive(Debug, Clone)]
pub struct NormalizedDiscountedCumulativeGain {
    /// Number of top results to consider (k).
    pub k: usize,
}

impl Default for NormalizedDiscountedCumulativeGain {
    fn default() -> Self {
        Self { k: 10 }
    }
}

impl NormalizedDiscountedCumulativeGain {
    /// Create NDCG metric with custom k value.
    ///
    /// # Arguments
    /// * `k` - Number of top results to consider
    pub fn new(k: usize) -> Self {
        Self { k }
    }

    /// Compute DCG (Discounted Cumulative Gain) for a single ranking.
    ///
    /// # Arguments
    /// * `relevances` - Relevance scores in the predicted order
    /// * `k` - Number of positions to consider
    fn compute_dcg(relevances: &[f64], k: usize) -> f64 {
        let k = k.min(relevances.len());
        let mut dcg = 0.0;

        for (i, &rel) in relevances.iter().take(k).enumerate() {
            let position = (i + 2) as f64; // i+2 because positions start at 1 and log₂(1) = 0
            dcg += (2.0_f64.powf(rel) - 1.0) / position.log2();
        }

        dcg
    }

    /// Compute IDCG (Ideal DCG) by sorting relevances in descending order.
    fn compute_idcg(relevances: &[f64], k: usize) -> f64 {
        let mut sorted_rel = relevances.to_vec();
        sorted_rel.sort_by(|a, b| b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal));
        Self::compute_dcg(&sorted_rel, k)
    }
}

impl Metric for NormalizedDiscountedCumulativeGain {
    fn compute(
        &self,
        predictions: &ArrayView<f64, Ix2>,
        targets: &ArrayView<f64, Ix2>,
    ) -> TrainResult<f64> {
        if predictions.shape() != targets.shape() {
            return Err(TrainError::MetricsError(format!(
                "Shape mismatch: predictions {:?} vs targets {:?}",
                predictions.shape(),
                targets.shape()
            )));
        }

        let n_samples = predictions.nrows();
        if n_samples == 0 {
            return Ok(0.0);
        }

        let mut ndcg_sum = 0.0;

        for i in 0..n_samples {
            // Get predicted scores and true relevances for this sample
            let pred_scores: Vec<f64> = predictions.row(i).iter().copied().collect();
            let true_relevances: Vec<f64> = targets.row(i).iter().copied().collect();

            // Create indices and sort by predicted scores (descending)
            let mut indices: Vec<usize> = (0..pred_scores.len()).collect();
            indices.sort_by(|&a, &b| {
                pred_scores[b]
                    .partial_cmp(&pred_scores[a])
                    .unwrap_or(std::cmp::Ordering::Equal)
            });

            // Reorder relevances according to predicted ranking
            let ranked_relevances: Vec<f64> =
                indices.iter().map(|&idx| true_relevances[idx]).collect();

            // Compute DCG for this ranking
            let dcg = Self::compute_dcg(&ranked_relevances, self.k);

            // Compute IDCG (ideal ranking)
            let idcg = Self::compute_idcg(&true_relevances, self.k);

            // Compute NDCG (handle division by zero)
            let ndcg = if idcg > 1e-12 { dcg / idcg } else { 0.0 };

            ndcg_sum += ndcg;
        }

        Ok(ndcg_sum / n_samples as f64)
    }

    fn name(&self) -> &str {
        "ndcg"
    }
}

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

    #[test]
    fn test_top_k_accuracy() {
        let metric = TopKAccuracy::new(2);

        // Test with 3 classes
        let predictions = array![
            [0.7, 0.2, 0.1], // Correct class is 0, top-2 includes it
            [0.1, 0.6, 0.3], // Correct class is 1, top-2 includes it
            [0.3, 0.4, 0.3], // Correct class is 2, top-2 includes it (1, 0)
        ];
        let targets = array![[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]];

        let top_k = metric
            .compute(&predictions.view(), &targets.view())
            .expect("unwrap");
        assert!((0.0..=1.0).contains(&top_k));
        assert!(top_k >= 0.66); // At least 2/3 should be in top-2
    }

    #[test]
    fn test_ndcg_perfect_ranking() {
        let metric = NormalizedDiscountedCumulativeGain::new(5);

        // Perfect ranking: predicted order matches true relevance order
        let predictions = array![
            [5.0, 4.0, 3.0, 2.0, 1.0], // Pred scores: highest to lowest
        ];
        let targets = array![
            [5.0, 4.0, 3.0, 2.0, 1.0], // True relevances: match pred order
        ];

        let ndcg = metric
            .compute(&predictions.view(), &targets.view())
            .expect("unwrap");

        // Perfect ranking should give NDCG = 1.0
        assert!(
            (ndcg - 1.0).abs() < 1e-6,
            "Perfect ranking should have NDCG ≈ 1.0, got {}",
            ndcg
        );
    }

    #[test]
    fn test_ndcg_worst_ranking() {
        let metric = NormalizedDiscountedCumulativeGain::new(5);

        // Worst ranking: predicted order is reverse of true relevance
        let predictions = array![
            [1.0, 2.0, 3.0, 4.0, 5.0], // Pred scores: lowest to highest
        ];
        let targets = array![
            [5.0, 4.0, 3.0, 2.0, 1.0], // True relevances: highest to lowest
        ];

        let ndcg = metric
            .compute(&predictions.view(), &targets.view())
            .expect("unwrap");

        // Worst ranking should give low NDCG
        assert!(
            ndcg < 0.8,
            "Worst ranking should have low NDCG, got {}",
            ndcg
        );
    }

    #[test]
    fn test_ndcg_partial_match() {
        let metric = NormalizedDiscountedCumulativeGain::new(3);

        // Partial match: some items ranked correctly
        let predictions = array![
            [4.0, 5.0, 2.0, 3.0, 1.0], // Pred order: [1, 0, 3, 2, 4]
        ];
        let targets = array![
            [3.0, 5.0, 1.0, 2.0, 0.0], // True relevances
        ];

        let ndcg = metric
            .compute(&predictions.view(), &targets.view())
            .expect("unwrap");

        // Should be between 0 and 1
        assert!(
            (0.0..=1.0).contains(&ndcg),
            "NDCG should be in [0, 1], got {}",
            ndcg
        );

        // Should be reasonably high since highest relevance (5.0) is predicted correctly
        assert!(
            ndcg > 0.7,
            "NDCG should be > 0.7 for this ranking, got {}",
            ndcg
        );
    }

    #[test]
    fn test_ndcg_multiple_samples() {
        let metric = NormalizedDiscountedCumulativeGain::new(3);

        // Two samples: one perfect, one reversed
        let predictions = array![[5.0, 4.0, 3.0, 2.0], [2.0, 3.0, 4.0, 5.0],];
        let targets = array![[5.0, 4.0, 3.0, 2.0], [5.0, 4.0, 3.0, 2.0],];

        let ndcg = metric
            .compute(&predictions.view(), &targets.view())
            .expect("unwrap");

        // Average of perfect (1.0) and poor ranking
        assert!((0.0..=1.0).contains(&ndcg));
        assert!(ndcg > 0.4 && ndcg < 0.9); // Should be somewhere in between
    }

    #[test]
    fn test_ndcg_different_k_values() {
        let metric_k3 = NormalizedDiscountedCumulativeGain::new(3);
        let metric_k5 = NormalizedDiscountedCumulativeGain::new(5);

        let predictions = array![[5.0, 4.0, 3.0, 1.0, 2.0]];
        let targets = array![[5.0, 4.0, 3.0, 2.0, 1.0]];

        let ndcg_k3 = metric_k3
            .compute(&predictions.view(), &targets.view())
            .expect("unwrap");
        let ndcg_k5 = metric_k5
            .compute(&predictions.view(), &targets.view())
            .expect("unwrap");

        // k=3 should be perfect (top 3 are correct)
        assert!((ndcg_k3 - 1.0).abs() < 1e-6);

        // k=5 should be lower (last 2 are swapped)
        assert!(ndcg_k5 < ndcg_k3);
        assert!(ndcg_k5 > 0.9); // Still very good
    }

    #[test]
    fn test_ndcg_zero_relevances() {
        let metric = NormalizedDiscountedCumulativeGain::new(5);

        // All zero relevances
        let predictions = array![[1.0, 2.0, 3.0]];
        let targets = array![[0.0, 0.0, 0.0]];

        let ndcg = metric
            .compute(&predictions.view(), &targets.view())
            .expect("unwrap");

        // Should handle gracefully (IDCG = 0)
        assert!(ndcg.is_finite());
        assert_eq!(ndcg, 0.0);
    }

    #[test]
    fn test_ndcg_empty_input() {
        let metric = NormalizedDiscountedCumulativeGain::default();

        use scirs2_core::ndarray::Array;
        let empty_predictions: Array<f64, _> = Array::zeros((0, 5));
        let empty_targets: Array<f64, _> = Array::zeros((0, 5));

        let ndcg = metric
            .compute(&empty_predictions.view(), &empty_targets.view())
            .expect("unwrap");

        assert_eq!(ndcg, 0.0);
    }

    #[test]
    fn test_ndcg_shape_mismatch() {
        let metric = NormalizedDiscountedCumulativeGain::default();

        let predictions = array![[1.0, 2.0, 3.0]];
        let targets = array![[1.0, 2.0]]; // Different shape

        let result = metric.compute(&predictions.view(), &targets.view());
        assert!(result.is_err());
    }

    #[test]
    fn test_ndcg_binary_relevance() {
        let metric = NormalizedDiscountedCumulativeGain::new(5);

        // Binary relevance (0 or 1)
        let predictions = array![[0.9, 0.7, 0.5, 0.3, 0.1]];
        let targets = array![[1.0, 1.0, 0.0, 1.0, 0.0]];

        let ndcg = metric
            .compute(&predictions.view(), &targets.view())
            .expect("unwrap");

        // Should be in valid range
        assert!((0.0..=1.0).contains(&ndcg));

        // Top 2 are relevant, so should have decent NDCG
        assert!(
            ndcg > 0.6,
            "Should have decent NDCG with top-2 relevant, got {}",
            ndcg
        );
    }
}