ghostflow-ml 1.0.0

Classical ML algorithms for GhostFlow
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
//! Reinforcement Learning and Meta-Learning Architectures

use ghostflow_core::Tensor;
use crate::deep::layers::Dense;
use crate::deep::activations::{ReLU, Tanh};

/// DQN (Deep Q-Network)
pub struct DQN {
    fc1: Dense,
    fc2: Dense,
    fc3: Dense,
    num_actions: usize,
}

impl DQN {
    pub fn new(state_dim: usize, num_actions: usize) -> Self {
        DQN {
            fc1: Dense::new(state_dim, 128),
            fc2: Dense::new(128, 128),
            fc3: Dense::new(128, num_actions),
            num_actions,
        }
    }

    pub fn forward(&mut self, state: &Tensor, training: bool) -> Tensor {
        let mut out = self.fc1.forward(state, training);
        out = ReLU::new().forward(&out);
        
        out = self.fc2.forward(&out, training);
        out = ReLU::new().forward(&out);
        
        self.fc3.forward(&out, training)
    }
}

/// Dueling DQN
pub struct DuelingDQN {
    feature_layer: Dense,
    value_stream: Vec<Dense>,
    advantage_stream: Vec<Dense>,
    num_actions: usize,
}

impl DuelingDQN {
    pub fn new(state_dim: usize, num_actions: usize) -> Self {
        DuelingDQN {
            feature_layer: Dense::new(state_dim, 128),
            value_stream: vec![
                Dense::new(128, 128),
                Dense::new(128, 1),
            ],
            advantage_stream: vec![
                Dense::new(128, 128),
                Dense::new(128, num_actions),
            ],
            num_actions,
        }
    }

    pub fn forward(&mut self, state: &Tensor, training: bool) -> Tensor {
        let mut features = self.feature_layer.forward(state, training);
        features = ReLU::new().forward(&features);
        
        // Value stream
        let mut value = features.clone();
        for layer in &mut self.value_stream {
            value = layer.forward(&value, training);
        }
        
        // Advantage stream
        let mut advantage = features;
        for layer in &mut self.advantage_stream {
            advantage = layer.forward(&advantage, training);
        }
        
        // Combine: Q(s,a) = V(s) + (A(s,a) - mean(A(s,a)))
        self.combine_streams(&value, &advantage)
    }

    fn combine_streams(&self, value: &Tensor, advantage: &Tensor) -> Tensor {
        let value_data = value.data_f32();
        let adv_data = advantage.data_f32();
        
        let batch_size = advantage.dims()[0];
        let mut result = Vec::new();
        
        for b in 0..batch_size {
            let v = value_data[b];
            
            // Compute mean advantage
            let mut sum = 0.0f32;
            for a in 0..self.num_actions {
                sum += adv_data[b * self.num_actions + a];
            }
            let mean_adv = sum / self.num_actions as f32;
            
            // Q(s,a) = V(s) + (A(s,a) - mean(A))
            for a in 0..self.num_actions {
                let adv = adv_data[b * self.num_actions + a];
                result.push(v + adv - mean_adv);
            }
        }
        
        Tensor::from_slice(&result, &[batch_size, self.num_actions]).unwrap()
    }
}

/// Actor-Critic Network
pub struct ActorCritic {
    shared: Dense,
    actor: Vec<Dense>,
    critic: Vec<Dense>,
    num_actions: usize,
}

impl ActorCritic {
    pub fn new(state_dim: usize, num_actions: usize) -> Self {
        ActorCritic {
            shared: Dense::new(state_dim, 128),
            actor: vec![
                Dense::new(128, 128),
                Dense::new(128, num_actions),
            ],
            critic: vec![
                Dense::new(128, 128),
                Dense::new(128, 1),
            ],
            num_actions,
        }
    }

    pub fn forward(&mut self, state: &Tensor, training: bool) -> (Tensor, Tensor) {
        let mut shared = self.shared.forward(state, training);
        shared = ReLU::new().forward(&shared);
        
        // Actor (policy)
        let mut policy = shared.clone();
        for (i, layer) in self.actor.iter_mut().enumerate() {
            policy = layer.forward(&policy, training);
            if i < self.actor.len() - 1 {
                policy = ReLU::new().forward(&policy);
            }
        }
        policy = self.softmax(&policy);
        
        // Critic (value)
        let mut value = shared;
        for layer in &mut self.critic {
            value = layer.forward(&value, training);
        }
        
        (policy, value)
    }

    fn softmax(&self, x: &Tensor) -> Tensor {
        let data = x.data_f32();
        let batch_size = x.dims()[0];
        let num_actions = x.dims()[1];
        
        let mut result = vec![0.0f32; data.len()];
        
        for b in 0..batch_size {
            let offset = b * num_actions;
            
            // Find max for numerical stability
            let mut max_val = data[offset];
            for i in 1..num_actions {
                max_val = max_val.max(data[offset + i]);
            }
            
            // Compute exp and sum
            let mut sum = 0.0f32;
            for i in 0..num_actions {
                let exp_val = (data[offset + i] - max_val).exp();
                result[offset + i] = exp_val;
                sum += exp_val;
            }
            
            // Normalize
            for i in 0..num_actions {
                result[offset + i] /= sum;
            }
        }
        
        Tensor::from_slice(&result, x.dims()).unwrap()
    }
}

/// PPO (Proximal Policy Optimization) Actor
pub struct PPOActor {
    fc1: Dense,
    fc2: Dense,
    mean_layer: Dense,
    log_std_layer: Dense,
}

impl PPOActor {
    pub fn new(state_dim: usize, action_dim: usize) -> Self {
        PPOActor {
            fc1: Dense::new(state_dim, 64),
            fc2: Dense::new(64, 64),
            mean_layer: Dense::new(64, action_dim),
            log_std_layer: Dense::new(64, action_dim),
        }
    }

    pub fn forward(&mut self, state: &Tensor, training: bool) -> (Tensor, Tensor) {
        let mut out = self.fc1.forward(state, training);
        out = Tanh::new().forward(&out);
        
        out = self.fc2.forward(&out, training);
        out = Tanh::new().forward(&out);
        
        let mean = self.mean_layer.forward(&out, training);
        let log_std = self.log_std_layer.forward(&out, training);
        
        (mean, log_std)
    }
}

/// PPO Critic
pub struct PPOCritic {
    fc1: Dense,
    fc2: Dense,
    value_layer: Dense,
}

impl PPOCritic {
    pub fn new(state_dim: usize) -> Self {
        PPOCritic {
            fc1: Dense::new(state_dim, 64),
            fc2: Dense::new(64, 64),
            value_layer: Dense::new(64, 1),
        }
    }

    pub fn forward(&mut self, state: &Tensor, training: bool) -> Tensor {
        let mut out = self.fc1.forward(state, training);
        out = Tanh::new().forward(&out);
        
        out = self.fc2.forward(&out, training);
        out = Tanh::new().forward(&out);
        
        self.value_layer.forward(&out, training)
    }
}

/// MAML (Model-Agnostic Meta-Learning)
pub struct MAML {
    layers: Vec<Dense>,
}

impl MAML {
    pub fn new(input_dim: usize, hidden_dim: usize, output_dim: usize) -> Self {
        MAML {
            layers: vec![
                Dense::new(input_dim, hidden_dim),
                Dense::new(hidden_dim, hidden_dim),
                Dense::new(hidden_dim, output_dim),
            ],
        }
    }

    pub fn forward(&mut self, x: &Tensor, training: bool) -> Tensor {
        let mut out = x.clone();
        
        for (i, layer) in self.layers.iter_mut().enumerate() {
            out = layer.forward(&out, training);
            if i < self.layers.len() - 1 {
                out = ReLU::new().forward(&out);
            }
        }
        
        out
    }

    pub fn clone_params(&self) -> Vec<Vec<f32>> {
        // Simplified parameter cloning
        vec![vec![0.0f32; 100]; self.layers.len()]
    }

    pub fn set_params(&mut self, _params: Vec<Vec<f32>>) {
        // Simplified parameter setting
    }
}

/// Prototypical Network
pub struct PrototypicalNetwork {
    encoder: Vec<Dense>,
}

impl PrototypicalNetwork {
    pub fn new(input_dim: usize, embedding_dim: usize) -> Self {
        PrototypicalNetwork {
            encoder: vec![
                Dense::new(input_dim, 128),
                Dense::new(128, 128),
                Dense::new(128, embedding_dim),
            ],
        }
    }

    pub fn encode(&mut self, x: &Tensor, training: bool) -> Tensor {
        let mut out = x.clone();
        
        for layer in &mut self.encoder {
            out = layer.forward(&out, training);
            out = ReLU::new().forward(&out);
        }
        
        out
    }

    pub fn compute_prototypes(&mut self, support_set: &Tensor, labels: &Tensor, training: bool) -> Tensor {
        let embeddings = self.encode(support_set, training);
        
        // Compute class prototypes (mean of embeddings per class)
        // Simplified implementation
        embeddings
    }

    pub fn classify(&mut self, query: &Tensor, prototypes: &Tensor, training: bool) -> Tensor {
        let query_embedding = self.encode(query, training);
        
        // Compute distances to prototypes
        self.euclidean_distance(&query_embedding, prototypes)
    }

    fn euclidean_distance(&self, a: &Tensor, b: &Tensor) -> Tensor {
        // Simplified distance computation
        a.clone()
    }
}

/// Matching Network
pub struct MatchingNetwork {
    encoder: Vec<Dense>,
    attention: Dense,
}

impl MatchingNetwork {
    pub fn new(input_dim: usize, embedding_dim: usize) -> Self {
        MatchingNetwork {
            encoder: vec![
                Dense::new(input_dim, 128),
                Dense::new(128, embedding_dim),
            ],
            attention: Dense::new(embedding_dim * 2, 1),
        }
    }

    pub fn encode(&mut self, x: &Tensor, training: bool) -> Tensor {
        let mut out = x.clone();
        
        for layer in &mut self.encoder {
            out = layer.forward(&out, training);
            out = ReLU::new().forward(&out);
        }
        
        out
    }

    pub fn forward(&mut self, support: &Tensor, query: &Tensor, training: bool) -> Tensor {
        let support_emb = self.encode(support, training);
        let query_emb = self.encode(query, training);
        
        // Compute attention-based matching
        self.compute_attention(&query_emb, &support_emb, training)
    }

    fn compute_attention(&mut self, query: &Tensor, support: &Tensor, training: bool) -> Tensor {
        // Simplified attention computation
        query.clone()
    }
}

/// Relation Network
pub struct RelationNetwork {
    feature_encoder: Vec<Dense>,
    relation_module: Vec<Dense>,
}

impl RelationNetwork {
    pub fn new(input_dim: usize, embedding_dim: usize) -> Self {
        RelationNetwork {
            feature_encoder: vec![
                Dense::new(input_dim, 128),
                Dense::new(128, embedding_dim),
            ],
            relation_module: vec![
                Dense::new(embedding_dim * 2, 128),
                Dense::new(128, 1),
            ],
        }
    }

    pub fn encode(&mut self, x: &Tensor, training: bool) -> Tensor {
        let mut out = x.clone();
        
        for layer in &mut self.feature_encoder {
            out = layer.forward(&out, training);
            out = ReLU::new().forward(&out);
        }
        
        out
    }

    pub fn compute_relation(&mut self, x1: &Tensor, x2: &Tensor, training: bool) -> Tensor {
        let concat = self.concatenate(x1, x2);
        
        let mut out = concat;
        for layer in &mut self.relation_module {
            out = layer.forward(&out, training);
            out = ReLU::new().forward(&out);
        }
        
        out
    }

    fn concatenate(&self, a: &Tensor, b: &Tensor) -> Tensor {
        let a_data = a.data_f32();
        let b_data = b.data_f32();
        
        let mut result = Vec::new();
        result.extend_from_slice(a_data);
        result.extend_from_slice(b_data);
        
        Tensor::from_slice(&result, &[a.dims()[0], a.dims()[1] + b.dims()[1]]).unwrap()
    }
}

/// SNAIL (Simple Neural Attentive Meta-Learner)
pub struct SNAIL {
    attention_blocks: Vec<SNAILAttentionBlock>,
    fc: Dense,
}

struct SNAILAttentionBlock {
    attention: Dense,
    fc: Dense,
}

impl SNAILAttentionBlock {
    fn new(dim: usize) -> Self {
        SNAILAttentionBlock {
            attention: Dense::new(dim, dim),
            fc: Dense::new(dim, dim),
        }
    }

    fn forward(&mut self, x: &Tensor, training: bool) -> Tensor {
        let attn = self.attention.forward(x, training);
        let out = self.fc.forward(&attn, training);
        
        // Add residual
        self.add_tensors(x, &out)
    }

    fn add_tensors(&self, a: &Tensor, b: &Tensor) -> Tensor {
        let a_data = a.data_f32();
        let b_data = b.data_f32();
        let result: Vec<f32> = a_data.iter()
            .zip(b_data.iter())
            .map(|(&x, &y)| x + y)
            .collect();
        Tensor::from_slice(&result, a.dims()).unwrap()
    }
}

impl SNAIL {
    pub fn new(input_dim: usize, num_blocks: usize, output_dim: usize) -> Self {
        SNAIL {
            attention_blocks: (0..num_blocks).map(|_| SNAILAttentionBlock::new(input_dim)).collect(),
            fc: Dense::new(input_dim, output_dim),
        }
    }

    pub fn forward(&mut self, x: &Tensor, training: bool) -> Tensor {
        let mut out = x.clone();
        
        for block in &mut self.attention_blocks {
            out = block.forward(&out, training);
        }
        
        self.fc.forward(&out, training)
    }
}

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

    #[test]
    fn test_dqn() {
        let mut dqn = DQN::new(4, 2);
        let state = Tensor::from_slice(&vec![0.5f32; 1 * 4], &[1, 4]).unwrap();
        let q_values = dqn.forward(&state, false);
        assert_eq!(q_values.dims()[1], 2);
    }

    #[test]
    fn test_actor_critic() {
        let mut ac = ActorCritic::new(4, 2);
        let state = Tensor::from_slice(&vec![0.5f32; 1 * 4], &[1, 4]).unwrap();
        let (policy, value) = ac.forward(&state, false);
        assert_eq!(policy.dims()[1], 2);
        assert_eq!(value.dims()[1], 1);
    }

    #[test]
    fn test_maml() {
        let mut maml = MAML::new(10, 20, 5);
        let input = Tensor::from_slice(&vec![0.5f32; 2 * 10], &[2, 10]).unwrap();
        let output = maml.forward(&input, false);
        assert_eq!(output.dims()[1], 5);
    }
}