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
//! Feed-forward Neural Networks and Variants - MLP, Deep FFN, Highway Networks, etc.

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

/// Multi-Layer Perceptron (MLP)
pub struct MLP {
    layers: Vec<Dense>,
    num_layers: usize,
}

impl MLP {
    pub fn new(layer_sizes: Vec<usize>) -> Self {
        let mut layers = Vec::new();
        
        for i in 0..layer_sizes.len() - 1 {
            layers.push(Dense::new(layer_sizes[i], layer_sizes[i + 1]));
        }
        
        MLP {
            num_layers: layers.len(),
            layers,
        }
    }

    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);
            
            // Apply activation to all layers except the last
            if i < self.num_layers - 1 {
                out = ReLU::new().forward(&out);
            }
        }
        
        out
    }
}

/// Deep Feed-Forward Network with Dropout
pub struct DeepFFN {
    layers: Vec<Dense>,
    dropout_rate: f32,
}

impl DeepFFN {
    pub fn new(input_dim: usize, hidden_dims: Vec<usize>, output_dim: usize, dropout_rate: f32) -> Self {
        let mut layers = Vec::new();
        
        // Input to first hidden
        layers.push(Dense::new(input_dim, hidden_dims[0]));
        
        // Hidden to hidden
        for i in 0..hidden_dims.len() - 1 {
            layers.push(Dense::new(hidden_dims[i], hidden_dims[i + 1]));
        }
        
        // Last hidden to output
        layers.push(Dense::new(hidden_dims[hidden_dims.len() - 1], output_dim));
        
        DeepFFN {
            layers,
            dropout_rate,
        }
    }

    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);
                
                if training {
                    out = self.dropout(&out);
                }
            }
        }
        
        out
    }

    fn dropout(&self, x: &Tensor) -> Tensor {
        use rand::prelude::*;
        let mut rng = thread_rng();
        
        let data = x.data_f32();
        let result: Vec<f32> = data.iter()
            .map(|&val| {
                if rng.gen::<f32>() < self.dropout_rate {
                    0.0
                } else {
                    val / (1.0 - self.dropout_rate)
                }
            })
            .collect();
        
        Tensor::from_slice(&result, x.dims()).unwrap()
    }
}

/// Highway Network
pub struct HighwayNetwork {
    layers: Vec<HighwayLayer>,
}

struct HighwayLayer {
    transform: Dense,
    gate: Dense,
}

impl HighwayLayer {
    fn new(size: usize) -> Self {
        HighwayLayer {
            transform: Dense::new(size, size),
            gate: Dense::new(size, size),
        }
    }

    fn forward(&mut self, x: &Tensor, training: bool) -> Tensor {
        let mut h = self.transform.forward(x, training);
        h = ReLU::new().forward(&h);
        
        let mut t = self.gate.forward(x, training);
        t = Sigmoid::new().forward(&t);
        
        // Highway connection: y = h * t + x * (1 - t)
        self.highway_connection(&h, x, &t)
    }

    fn highway_connection(&self, h: &Tensor, x: &Tensor, t: &Tensor) -> Tensor {
        let h_data = h.data_f32();
        let x_data = x.data_f32();
        let t_data = t.data_f32();
        
        let result: Vec<f32> = (0..h_data.len())
            .map(|i| h_data[i] * t_data[i] + x_data[i] * (1.0 - t_data[i]))
            .collect();
        
        Tensor::from_slice(&result, h.dims()).unwrap()
    }
}

impl HighwayNetwork {
    pub fn new(size: usize, num_layers: usize) -> Self {
        HighwayNetwork {
            layers: (0..num_layers).map(|_| HighwayLayer::new(size)).collect(),
        }
    }

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

/// Residual Feed-Forward Network
pub struct ResidualFFN {
    blocks: Vec<ResidualBlock>,
    final_layer: Dense,
}

struct ResidualBlock {
    fc1: Dense,
    fc2: Dense,
}

impl ResidualBlock {
    fn new(size: usize) -> Self {
        ResidualBlock {
            fc1: Dense::new(size, size),
            fc2: Dense::new(size, size),
        }
    }

    fn forward(&mut self, x: &Tensor, training: bool) -> Tensor {
        let identity = x.clone();
        
        let mut out = self.fc1.forward(x, training);
        out = ReLU::new().forward(&out);
        
        out = self.fc2.forward(&out, training);
        
        // Add residual
        self.add_tensors(&out, &identity)
    }

    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 ResidualFFN {
    pub fn new(input_dim: usize, hidden_dim: usize, output_dim: usize, num_blocks: usize) -> Self {
        ResidualFFN {
            blocks: (0..num_blocks).map(|_| ResidualBlock::new(hidden_dim)).collect(),
            final_layer: Dense::new(hidden_dim, output_dim),
        }
    }

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

/// Mixture of Experts (MoE)
pub struct MixtureOfExperts {
    experts: Vec<Expert>,
    gating_network: Dense,
    num_experts: usize,
}

struct Expert {
    layers: Vec<Dense>,
}

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

    fn forward(&mut self, x: &Tensor, training: bool) -> Tensor {
        let mut out = self.layers[0].forward(x, training);
        out = ReLU::new().forward(&out);
        self.layers[1].forward(&out, training)
    }
}

impl MixtureOfExperts {
    pub fn new(input_dim: usize, hidden_dim: usize, output_dim: usize, num_experts: usize) -> Self {
        MixtureOfExperts {
            experts: (0..num_experts)
                .map(|_| Expert::new(input_dim, hidden_dim, output_dim))
                .collect(),
            gating_network: Dense::new(input_dim, num_experts),
            num_experts,
        }
    }

    pub fn forward(&mut self, x: &Tensor, training: bool) -> Tensor {
        // Compute gating weights
        let gates = self.gating_network.forward(x, training);
        let gates_softmax = self.softmax(&gates);
        
        // Get expert outputs
        let mut expert_outputs = Vec::new();
        for expert in &mut self.experts {
            expert_outputs.push(expert.forward(x, training));
        }
        
        // Weighted combination
        self.combine_experts(&expert_outputs, &gates_softmax)
    }

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

    fn combine_experts(&self, expert_outputs: &[Tensor], gates: &Tensor) -> Tensor {
        let batch_size = expert_outputs[0].dims()[0];
        let output_dim = expert_outputs[0].dims()[1];
        let gates_data = gates.data_f32();
        
        let mut result = vec![0.0f32; batch_size * output_dim];
        
        for b in 0..batch_size {
            for e in 0..self.num_experts {
                let gate_weight = gates_data[b * self.num_experts + e];
                let expert_data = expert_outputs[e].data_f32();
                
                for d in 0..output_dim {
                    result[b * output_dim + d] += gate_weight * expert_data[b * output_dim + d];
                }
            }
        }
        
        Tensor::from_slice(&result, &[batch_size, output_dim]).unwrap()
    }
}

/// Kolmogorov-Arnold Network (KAN)
pub struct KAN {
    layers: Vec<KANLayer>,
}

struct KANLayer {
    basis_functions: Vec<Dense>,
    combination_weights: Dense,
}

impl KANLayer {
    fn new(input_dim: usize, output_dim: usize, num_basis: usize) -> Self {
        KANLayer {
            basis_functions: (0..num_basis)
                .map(|_| Dense::new(input_dim, output_dim))
                .collect(),
            combination_weights: Dense::new(num_basis, output_dim),
        }
    }

    fn forward(&mut self, x: &Tensor, training: bool) -> Tensor {
        let mut basis_outputs = Vec::new();
        
        for basis in &mut self.basis_functions {
            let out = basis.forward(x, training);
            basis_outputs.push(out);
        }
        
        // Combine basis function outputs
        self.combine_basis(&basis_outputs)
    }

    fn combine_basis(&self, outputs: &[Tensor]) -> Tensor {
        if outputs.is_empty() {
            return Tensor::from_slice(&[0.0f32], &[1, 1]).unwrap();
        }
        
        let mut result = outputs[0].data_f32().to_vec();
        
        for output in &outputs[1..] {
            let data = output.data_f32();
            for (i, &val) in data.iter().enumerate() {
                result[i] += val;
            }
        }
        
        Tensor::from_slice(&result, outputs[0].dims()).unwrap()
    }
}

impl KAN {
    pub fn new(layer_sizes: Vec<usize>, num_basis: usize) -> Self {
        let mut layers = Vec::new();
        
        for i in 0..layer_sizes.len() - 1 {
            layers.push(KANLayer::new(layer_sizes[i], layer_sizes[i + 1], num_basis));
        }
        
        KAN { layers }
    }

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

/// Radial Basis Function Network (RBF)
pub struct RBFNetwork {
    centers: Vec<Vec<f32>>,
    widths: Vec<f32>,
    output_layer: Dense,
    num_centers: usize,
}

impl RBFNetwork {
    pub fn new(input_dim: usize, num_centers: usize, output_dim: usize) -> Self {
        use rand::prelude::*;
        let mut rng = thread_rng();
        
        let mut centers = Vec::new();
        for _ in 0..num_centers {
            let center: Vec<f32> = (0..input_dim)
                .map(|_| rng.gen::<f32>() * 2.0 - 1.0)
                .collect();
            centers.push(center);
        }
        
        RBFNetwork {
            centers,
            widths: vec![1.0f32; num_centers],
            output_layer: Dense::new(num_centers, output_dim),
            num_centers,
        }
    }

    pub fn forward(&mut self, x: &Tensor, training: bool) -> Tensor {
        let rbf_activations = self.compute_rbf_activations(x);
        self.output_layer.forward(&rbf_activations, training)
    }

    fn compute_rbf_activations(&self, x: &Tensor) -> Tensor {
        let batch_size = x.dims()[0];
        let input_dim = x.dims()[1];
        let x_data = x.data_f32();
        
        let mut activations = Vec::new();
        
        for b in 0..batch_size {
            for c in 0..self.num_centers {
                let mut dist_sq = 0.0f32;
                
                for d in 0..input_dim {
                    let diff = x_data[b * input_dim + d] - self.centers[c][d];
                    dist_sq += diff * diff;
                }
                
                let activation = (-dist_sq / (2.0 * self.widths[c].powi(2))).exp();
                activations.push(activation);
            }
        }
        
        Tensor::from_slice(&activations, &[batch_size, self.num_centers]).unwrap()
    }
}

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

    #[test]
    fn test_mlp() {
        let mut mlp = MLP::new(vec![784, 256, 128, 10]);
        let input = Tensor::from_slice(&vec![0.5f32; 2 * 784], &[2, 784]).unwrap();
        let output = mlp.forward(&input, false);
        assert_eq!(output.dims()[1], 10);
    }

    #[test]
    fn test_highway_network() {
        let mut highway = HighwayNetwork::new(256, 5);
        let input = Tensor::from_slice(&vec![0.5f32; 2 * 256], &[2, 256]).unwrap();
        let output = highway.forward(&input, false);
        assert_eq!(output.dims()[1], 256);
    }

    #[test]
    fn test_mixture_of_experts() {
        let mut moe = MixtureOfExperts::new(100, 64, 10, 4);
        let input = Tensor::from_slice(&vec![0.5f32; 2 * 100], &[2, 100]).unwrap();
        let output = moe.forward(&input, false);
        assert_eq!(output.dims()[1], 10);
    }
}