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
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
//! Meta-learning algorithms for learning to learn.
//!
//! This module implements meta-learning algorithms that learn model initializations
//! or update rules that enable rapid adaptation to new tasks with minimal data.
//!
//! # Overview
//!
//! Meta-learning (or "learning to learn") aims to improve a model's ability to
//! quickly adapt to new tasks by learning across multiple related tasks. This module
//! provides implementations of state-of-the-art meta-learning algorithms:
//!
//! - **MAML** (Model-Agnostic Meta-Learning): Learns an initialization that can
//!   quickly adapt via a few gradient steps
//! - **Reptile**: Simpler first-order approximation that directly moves toward
//!   task-specific parameters
//! - **Task sampling**: Infrastructure for episodic meta-learning
//!
//! # Key Concepts
//!
//! - **Meta-training**: Outer loop that updates the meta-parameters
//! - **Task adaptation**: Inner loop that adapts to specific tasks
//! - **Support set**: Training data for task adaptation
//! - **Query set**: Validation data for meta-objective
//!
//! # Examples
//!
//! ```rust
//! use tensorlogic_train::{MAMLConfig, ReptileConfig, MetaLearner};
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Configure MAML
//! let maml_config = MAMLConfig {
//!     inner_steps: 5,
//!     inner_lr: 0.01,
//!     outer_lr: 0.001,
//!     first_order: false,
//! };
//!
//! // Configure Reptile
//! let reptile_config = ReptileConfig {
//!     inner_steps: 5,
//!     inner_lr: 0.01,
//!     outer_lr: 0.001,
//! };
//! # Ok(())
//! # }
//! ```

use crate::{TrainError, TrainResult};
use scirs2_core::ndarray::{Array1, Array2};
use std::collections::HashMap;

/// MAML (Model-Agnostic Meta-Learning) configuration.
///
/// MAML learns an initialization θ* such that a small number of gradient steps
/// on a new task yields good performance.
#[derive(Debug, Clone)]
pub struct MAMLConfig {
    /// Number of gradient steps for task adaptation (inner loop).
    pub inner_steps: usize,
    /// Learning rate for task adaptation (inner loop).
    pub inner_lr: f64,
    /// Learning rate for meta-update (outer loop).
    pub outer_lr: f64,
    /// Use first-order approximation (ignores second derivatives).
    pub first_order: bool,
}

impl Default for MAMLConfig {
    fn default() -> Self {
        Self {
            inner_steps: 5,
            inner_lr: 0.01,
            outer_lr: 0.001,
            first_order: false,
        }
    }
}

/// Reptile algorithm configuration.
///
/// Reptile is a simpler first-order alternative to MAML that repeatedly:
/// 1. Samples a task
/// 2. Trains on it to get task-specific parameters
/// 3. Moves the meta-parameters toward the task-specific parameters
#[derive(Debug, Clone)]
pub struct ReptileConfig {
    /// Number of gradient steps for task adaptation.
    pub inner_steps: usize,
    /// Learning rate for task adaptation.
    pub inner_lr: f64,
    /// Learning rate for meta-update (interpolation weight).
    pub outer_lr: f64,
}

impl Default for ReptileConfig {
    fn default() -> Self {
        Self {
            inner_steps: 10,
            inner_lr: 0.01,
            outer_lr: 0.1,
        }
    }
}

/// Meta-learning task representation.
///
/// Each task consists of a support set (for adaptation) and
/// a query set (for evaluation).
#[derive(Debug, Clone)]
pub struct MetaTask {
    /// Support set features (for training/adaptation).
    pub support_x: Array2<f64>,
    /// Support set labels.
    pub support_y: Array2<f64>,
    /// Query set features (for evaluation).
    pub query_x: Array2<f64>,
    /// Query set labels.
    pub query_y: Array2<f64>,
}

impl MetaTask {
    /// Create a new meta-learning task.
    pub fn new(
        support_x: Array2<f64>,
        support_y: Array2<f64>,
        query_x: Array2<f64>,
        query_y: Array2<f64>,
    ) -> TrainResult<Self> {
        if support_x.nrows() != support_y.nrows() {
            return Err(TrainError::InvalidParameter(format!(
                "Support X rows ({}) must match support Y rows ({})",
                support_x.nrows(),
                support_y.nrows()
            )));
        }

        if query_x.nrows() != query_y.nrows() {
            return Err(TrainError::InvalidParameter(format!(
                "Query X rows ({}) must match query Y rows ({})",
                query_x.nrows(),
                query_y.nrows()
            )));
        }

        Ok(Self {
            support_x,
            support_y,
            query_x,
            query_y,
        })
    }

    /// Get support set size.
    pub fn support_size(&self) -> usize {
        self.support_x.nrows()
    }

    /// Get query set size.
    pub fn query_size(&self) -> usize {
        self.query_x.nrows()
    }
}

/// Meta-learner trait for different meta-learning algorithms.
pub trait MetaLearner {
    /// Perform one step of meta-training on a batch of tasks.
    ///
    /// # Arguments
    /// * `tasks` - Batch of meta-learning tasks
    /// * `parameters` - Current meta-parameters
    ///
    /// # Returns
    /// Updated meta-parameters and meta-loss
    fn meta_step(
        &self,
        tasks: &[MetaTask],
        parameters: &HashMap<String, Array1<f64>>,
    ) -> TrainResult<(HashMap<String, Array1<f64>>, f64)>;

    /// Adapt parameters to a specific task (inner loop).
    ///
    /// # Arguments
    /// * `task` - Task to adapt to
    /// * `parameters` - Initial parameters
    ///
    /// # Returns
    /// Task-adapted parameters
    fn adapt(
        &self,
        task: &MetaTask,
        parameters: &HashMap<String, Array1<f64>>,
    ) -> TrainResult<HashMap<String, Array1<f64>>>;
}

/// MAML (Model-Agnostic Meta-Learning) implementation.
///
/// MAML optimizes for a model initialization that can quickly adapt
/// to new tasks through a few gradient steps.
#[derive(Debug, Clone)]
pub struct MAML {
    config: MAMLConfig,
}

impl MAML {
    /// Create a new MAML meta-learner.
    pub fn new(config: MAMLConfig) -> Self {
        Self { config }
    }
}

impl Default for MAML {
    fn default() -> Self {
        Self::new(MAMLConfig::default())
    }
}

impl MetaLearner for MAML {
    fn meta_step(
        &self,
        tasks: &[MetaTask],
        parameters: &HashMap<String, Array1<f64>>,
    ) -> TrainResult<(HashMap<String, Array1<f64>>, f64)> {
        let mut meta_gradients: HashMap<String, Array1<f64>> = HashMap::new();
        let mut total_loss = 0.0;

        // Initialize meta-gradients to zero
        for (name, param) in parameters {
            meta_gradients.insert(name.clone(), Array1::zeros(param.len()));
        }

        // For each task in the batch
        for task in tasks {
            // 1. Adapt to the task (inner loop)
            let adapted_params = self.adapt(task, parameters)?;

            // 2. Compute loss on query set with adapted parameters
            // This is a placeholder - in practice, you'd use your model here
            let query_loss = self.compute_query_loss(task, &adapted_params)?;
            total_loss += query_loss;

            // 3. Compute gradients of query loss w.r.t. meta-parameters
            // In full MAML, we'd backprop through the adaptation process
            // For first-order MAML, we use adapted params directly
            let task_gradients = if self.config.first_order {
                self.compute_first_order_gradients(task, &adapted_params)?
            } else {
                self.compute_second_order_gradients(task, parameters, &adapted_params)?
            };

            // 4. Accumulate gradients
            for (name, grad) in task_gradients {
                if let Some(meta_grad) = meta_gradients.get_mut(&name) {
                    *meta_grad = meta_grad.clone() + grad;
                }
            }
        }

        // Average gradients and loss
        let n_tasks = tasks.len() as f64;
        for grad in meta_gradients.values_mut() {
            *grad = grad.mapv(|x| x / n_tasks);
        }
        total_loss /= n_tasks;

        // Meta-update (SGD step)
        let mut updated_params = HashMap::new();
        for (name, param) in parameters {
            if let Some(grad) = meta_gradients.get(name) {
                let updated = param - &grad.mapv(|g| g * self.config.outer_lr);
                updated_params.insert(name.clone(), updated);
            }
        }

        Ok((updated_params, total_loss))
    }

    fn adapt(
        &self,
        task: &MetaTask,
        parameters: &HashMap<String, Array1<f64>>,
    ) -> TrainResult<HashMap<String, Array1<f64>>> {
        let mut adapted_params = parameters.clone();

        // Perform inner loop updates
        for _ in 0..self.config.inner_steps {
            // Compute loss and gradients on support set
            let gradients = self.compute_support_gradients(task, &adapted_params)?;

            // SGD update
            for (name, param) in &mut adapted_params {
                if let Some(grad) = gradients.get(name) {
                    *param = param.clone() - &grad.mapv(|g| g * self.config.inner_lr);
                }
            }
        }

        Ok(adapted_params)
    }
}

impl MAML {
    /// Compute gradients on support set (placeholder).
    fn compute_support_gradients(
        &self,
        task: &MetaTask,
        _parameters: &HashMap<String, Array1<f64>>,
    ) -> TrainResult<HashMap<String, Array1<f64>>> {
        // This is a simplified placeholder
        // In practice, you'd compute actual gradients through your model
        let mut gradients = HashMap::new();
        gradients.insert("weights".to_string(), Array1::zeros(task.support_x.ncols()));
        Ok(gradients)
    }

    /// Compute loss on query set (placeholder).
    fn compute_query_loss(
        &self,
        task: &MetaTask,
        _parameters: &HashMap<String, Array1<f64>>,
    ) -> TrainResult<f64> {
        // This is a simplified placeholder
        // In practice, you'd compute actual loss through your model
        Ok(task.query_size() as f64 * 0.1)
    }

    /// Compute first-order gradients (placeholder).
    fn compute_first_order_gradients(
        &self,
        task: &MetaTask,
        _parameters: &HashMap<String, Array1<f64>>,
    ) -> TrainResult<HashMap<String, Array1<f64>>> {
        // This is a simplified placeholder
        let mut gradients = HashMap::new();
        gradients.insert("weights".to_string(), Array1::zeros(task.query_x.ncols()));
        Ok(gradients)
    }

    /// Compute second-order gradients through adaptation (placeholder).
    fn compute_second_order_gradients(
        &self,
        task: &MetaTask,
        _meta_params: &HashMap<String, Array1<f64>>,
        _adapted_params: &HashMap<String, Array1<f64>>,
    ) -> TrainResult<HashMap<String, Array1<f64>>> {
        // This is a simplified placeholder
        // In full MAML, we'd backprop through the inner loop
        let mut gradients = HashMap::new();
        gradients.insert("weights".to_string(), Array1::zeros(task.query_x.ncols()));
        Ok(gradients)
    }
}

/// Reptile meta-learning algorithm.
///
/// Reptile is a simpler first-order algorithm that:
/// 1. Samples a task
/// 2. Trains on it via SGD to get φ
/// 3. Updates θ ← θ + ε(φ - θ)
#[derive(Debug, Clone)]
pub struct Reptile {
    config: ReptileConfig,
}

impl Reptile {
    /// Create a new Reptile meta-learner.
    pub fn new(config: ReptileConfig) -> Self {
        Self { config }
    }
}

impl Default for Reptile {
    fn default() -> Self {
        Self::new(ReptileConfig::default())
    }
}

impl MetaLearner for Reptile {
    fn meta_step(
        &self,
        tasks: &[MetaTask],
        parameters: &HashMap<String, Array1<f64>>,
    ) -> TrainResult<(HashMap<String, Array1<f64>>, f64)> {
        let mut total_loss = 0.0;
        let mut accumulated_delta: HashMap<String, Array1<f64>> = HashMap::new();

        // Initialize accumulated delta to zero
        for (name, param) in parameters {
            accumulated_delta.insert(name.clone(), Array1::zeros(param.len()));
        }

        // For each task in the batch
        for task in tasks {
            // 1. Adapt to the task (train on support set)
            let task_params = self.adapt(task, parameters)?;

            // 2. Compute task loss (for monitoring)
            let task_loss = self.compute_task_loss(task, &task_params)?;
            total_loss += task_loss;

            // 3. Compute direction: φ - θ
            for (name, param) in parameters {
                if let Some(task_param) = task_params.get(name) {
                    let delta = task_param - param;
                    if let Some(acc_delta) = accumulated_delta.get_mut(name) {
                        *acc_delta = acc_delta.clone() + delta;
                    }
                }
            }
        }

        // Average delta and loss
        let n_tasks = tasks.len() as f64;
        for delta in accumulated_delta.values_mut() {
            *delta = delta.mapv(|x| x / n_tasks);
        }
        total_loss /= n_tasks;

        // Meta-update: θ ← θ + ε * average_delta
        let mut updated_params = HashMap::new();
        for (name, param) in parameters {
            if let Some(delta) = accumulated_delta.get(name) {
                let updated = param + &delta.mapv(|d| d * self.config.outer_lr);
                updated_params.insert(name.clone(), updated);
            }
        }

        Ok((updated_params, total_loss))
    }

    fn adapt(
        &self,
        task: &MetaTask,
        parameters: &HashMap<String, Array1<f64>>,
    ) -> TrainResult<HashMap<String, Array1<f64>>> {
        let mut task_params = parameters.clone();

        // Perform SGD steps on support set
        for _ in 0..self.config.inner_steps {
            // Compute loss and gradients on support set
            let gradients = self.compute_support_gradients(task, &task_params)?;

            // SGD update
            for (name, param) in &mut task_params {
                if let Some(grad) = gradients.get(name) {
                    *param = param.clone() - &grad.mapv(|g| g * self.config.inner_lr);
                }
            }
        }

        Ok(task_params)
    }
}

impl Reptile {
    /// Compute gradients on support set (placeholder).
    fn compute_support_gradients(
        &self,
        task: &MetaTask,
        _parameters: &HashMap<String, Array1<f64>>,
    ) -> TrainResult<HashMap<String, Array1<f64>>> {
        // This is a simplified placeholder
        let mut gradients = HashMap::new();
        gradients.insert("weights".to_string(), Array1::zeros(task.support_x.ncols()));
        Ok(gradients)
    }

    /// Compute task loss (placeholder).
    fn compute_task_loss(
        &self,
        task: &MetaTask,
        _parameters: &HashMap<String, Array1<f64>>,
    ) -> TrainResult<f64> {
        // This is a simplified placeholder
        Ok(task.query_size() as f64 * 0.1)
    }
}

/// Meta-learning statistics tracker.
#[derive(Debug, Clone, Default)]
pub struct MetaStats {
    /// Meta-training losses over time.
    pub meta_losses: Vec<f64>,
    /// Task adaptation losses.
    pub task_losses: Vec<Vec<f64>>,
    /// Number of meta-iterations completed.
    pub iterations: usize,
}

impl MetaStats {
    /// Create a new statistics tracker.
    pub fn new() -> Self {
        Self::default()
    }

    /// Record a meta-training step.
    pub fn record_meta_step(&mut self, meta_loss: f64) {
        self.meta_losses.push(meta_loss);
        self.iterations += 1;
    }

    /// Record task adaptation.
    pub fn record_task_adaptation(&mut self, task_id: usize, losses: Vec<f64>) {
        while self.task_losses.len() <= task_id {
            self.task_losses.push(Vec::new());
        }
        self.task_losses[task_id] = losses;
    }

    /// Get average meta-loss over last N steps.
    pub fn avg_meta_loss(&self, last_n: usize) -> f64 {
        if self.meta_losses.is_empty() {
            return 0.0;
        }

        let n = last_n.min(self.meta_losses.len());
        let start = self.meta_losses.len() - n;
        self.meta_losses[start..].iter().sum::<f64>() / n as f64
    }

    /// Check if meta-training is improving (loss decreasing).
    pub fn is_improving(&self, window: usize) -> bool {
        if self.meta_losses.len() < window * 2 {
            return false;
        }

        let recent = self.avg_meta_loss(window);
        let previous = {
            let start = self.meta_losses.len() - window * 2;
            let end = self.meta_losses.len() - window;
            self.meta_losses[start..end].iter().sum::<f64>() / window as f64
        };

        recent < previous
    }
}

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

    #[test]
    fn test_maml_config_default() {
        let config = MAMLConfig::default();
        assert_eq!(config.inner_steps, 5);
        assert_eq!(config.inner_lr, 0.01);
        assert_eq!(config.outer_lr, 0.001);
        assert!(!config.first_order);
    }

    #[test]
    fn test_reptile_config_default() {
        let config = ReptileConfig::default();
        assert_eq!(config.inner_steps, 10);
        assert_eq!(config.inner_lr, 0.01);
        assert_eq!(config.outer_lr, 0.1);
    }

    #[test]
    fn test_meta_task_creation() {
        let support_x = Array2::zeros((5, 10));
        let support_y = Array2::zeros((5, 2));
        let query_x = Array2::zeros((15, 10));
        let query_y = Array2::zeros((15, 2));

        let task = MetaTask::new(support_x, support_y, query_x, query_y).expect("unwrap");
        assert_eq!(task.support_size(), 5);
        assert_eq!(task.query_size(), 15);
    }

    #[test]
    fn test_meta_task_validation() {
        let support_x = Array2::zeros((5, 10));
        let support_y = Array2::zeros((4, 2)); // Mismatch!
        let query_x = Array2::zeros((15, 10));
        let query_y = Array2::zeros((15, 2));

        let result = MetaTask::new(support_x, support_y, query_x, query_y);
        assert!(result.is_err());
    }

    #[test]
    fn test_maml_creation() {
        let config = MAMLConfig::default();
        let maml = MAML::new(config);
        assert_eq!(maml.config.inner_steps, 5);
    }

    #[test]
    fn test_maml_default() {
        let maml = MAML::default();
        assert_eq!(maml.config.inner_steps, 5);
    }

    #[test]
    fn test_reptile_creation() {
        let config = ReptileConfig::default();
        let reptile = Reptile::new(config);
        assert_eq!(reptile.config.inner_steps, 10);
    }

    #[test]
    fn test_reptile_default() {
        let reptile = Reptile::default();
        assert_eq!(reptile.config.inner_steps, 10);
    }

    #[test]
    fn test_maml_adapt() {
        let maml = MAML::default();

        let task = create_dummy_task();
        let mut params = HashMap::new();
        params.insert("weights".to_string(), Array1::zeros(10));

        let adapted = maml.adapt(&task, &params).expect("unwrap");
        assert!(adapted.contains_key("weights"));
    }

    #[test]
    fn test_reptile_adapt() {
        let reptile = Reptile::default();

        let task = create_dummy_task();
        let mut params = HashMap::new();
        params.insert("weights".to_string(), Array1::zeros(10));

        let adapted = reptile.adapt(&task, &params).expect("unwrap");
        assert!(adapted.contains_key("weights"));
    }

    #[test]
    fn test_maml_meta_step() {
        let maml = MAML::default();

        let tasks = vec![create_dummy_task(), create_dummy_task()];
        let mut params = HashMap::new();
        params.insert("weights".to_string(), Array1::zeros(10));

        let (updated_params, loss) = maml.meta_step(&tasks, &params).expect("unwrap");
        assert!(updated_params.contains_key("weights"));
        assert!(loss >= 0.0);
    }

    #[test]
    fn test_reptile_meta_step() {
        let reptile = Reptile::default();

        let tasks = vec![create_dummy_task(), create_dummy_task()];
        let mut params = HashMap::new();
        params.insert("weights".to_string(), Array1::zeros(10));

        let (updated_params, loss) = reptile.meta_step(&tasks, &params).expect("unwrap");
        assert!(updated_params.contains_key("weights"));
        assert!(loss >= 0.0);
    }

    #[test]
    fn test_meta_stats() {
        let mut stats = MetaStats::new();

        stats.record_meta_step(1.0);
        stats.record_meta_step(0.8);
        stats.record_meta_step(0.6);

        assert_eq!(stats.iterations, 3);
        assert_eq!(stats.meta_losses.len(), 3);
        assert_eq!(stats.avg_meta_loss(2), 0.7);
    }

    #[test]
    fn test_meta_stats_improvement() {
        let mut stats = MetaStats::new();

        // Add decreasing losses
        for i in 0..20 {
            stats.record_meta_step(1.0 - i as f64 * 0.01);
        }

        assert!(stats.is_improving(5));
    }

    #[test]
    fn test_meta_stats_task_adaptation() {
        let mut stats = MetaStats::new();

        stats.record_task_adaptation(0, vec![1.0, 0.8, 0.6]);
        stats.record_task_adaptation(1, vec![1.2, 0.9, 0.7]);

        assert_eq!(stats.task_losses.len(), 2);
        assert_eq!(stats.task_losses[0].len(), 3);
    }

    // Helper function
    fn create_dummy_task() -> MetaTask {
        let support_x = Array2::zeros((5, 10));
        let support_y = Array2::zeros((5, 2));
        let query_x = Array2::zeros((15, 10));
        let query_y = Array2::zeros((15, 2));
        MetaTask::new(support_x, support_y, query_x, query_y).expect("unwrap")
    }
}