tsai_models 0.1.1

Model zoo for tsai-rs: CNN, Transformer, ROCKET, RNN, and Tabular architectures
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
//! HydraPlus: Hybrid Dictionary-based Random Architecture.
//!
//! HydraPlus combines dictionary-based random convolutions with a learnable
//! classification head, offering competitive accuracy with fast training.
//!
//! Key features:
//! - Multiple groups of random kernels with different dilations
//! - Various pooling strategies (max, mean, PPV)
//! - Learnable classification head
//! - Efficient feature extraction
//!
//! Reference: "HYDRA: Competing convolutional kernels for fast and accurate
//! time series classification" by Dempster et al. (2023)

use burn::nn::{Dropout, DropoutConfig, Linear, LinearConfig, Relu};
use burn::prelude::*;
use burn::tensor::activation::softmax;
use rand::prelude::*;
use rand_chacha::ChaCha8Rng;
use serde::{Deserialize, Serialize};

/// Pooling type for Hydra features.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum HydraPooling {
    /// Max pooling over time.
    Max,
    /// Mean pooling over time.
    Mean,
    /// Proportion of positive values (like ROCKET).
    PPV,
}

impl Default for HydraPooling {
    fn default() -> Self {
        Self::Max
    }
}

/// Configuration for HydraPlus model.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HydraPlusConfig {
    /// Number of input variables/channels.
    pub n_vars: usize,
    /// Sequence length.
    pub seq_len: usize,
    /// Number of output classes.
    pub n_classes: usize,
    /// Number of kernel groups.
    pub n_groups: usize,
    /// Number of kernels per group.
    pub kernels_per_group: usize,
    /// Kernel length.
    pub kernel_length: usize,
    /// Maximum dilation.
    pub max_dilation: usize,
    /// Pooling types to use.
    pub pooling_types: Vec<HydraPooling>,
    /// Hidden dimension for classifier.
    pub hidden_dim: usize,
    /// Dropout rate.
    pub dropout: f64,
    /// Random seed.
    pub seed: u64,
}

impl Default for HydraPlusConfig {
    fn default() -> Self {
        Self {
            n_vars: 1,
            seq_len: 100,
            n_classes: 2,
            n_groups: 8,
            kernels_per_group: 8,
            kernel_length: 9,
            max_dilation: 32,
            pooling_types: vec![HydraPooling::Max, HydraPooling::PPV],
            hidden_dim: 128,
            dropout: 0.1,
            seed: 42,
        }
    }
}

impl HydraPlusConfig {
    /// Create a new config.
    pub fn new(n_vars: usize, seq_len: usize, n_classes: usize) -> Self {
        Self {
            n_vars,
            seq_len,
            n_classes,
            ..Default::default()
        }
    }

    /// Set the number of groups.
    #[must_use]
    pub fn with_n_groups(mut self, n_groups: usize) -> Self {
        self.n_groups = n_groups;
        self
    }

    /// Set kernels per group.
    #[must_use]
    pub fn with_kernels_per_group(mut self, kernels_per_group: usize) -> Self {
        self.kernels_per_group = kernels_per_group;
        self
    }

    /// Set kernel length.
    #[must_use]
    pub fn with_kernel_length(mut self, kernel_length: usize) -> Self {
        self.kernel_length = kernel_length;
        self
    }

    /// Set pooling types.
    #[must_use]
    pub fn with_pooling_types(mut self, pooling_types: Vec<HydraPooling>) -> Self {
        self.pooling_types = pooling_types;
        self
    }

    /// Set hidden dimension.
    #[must_use]
    pub fn with_hidden_dim(mut self, hidden_dim: usize) -> Self {
        self.hidden_dim = hidden_dim;
        self
    }

    /// Set dropout rate.
    #[must_use]
    pub fn with_dropout(mut self, dropout: f64) -> Self {
        self.dropout = dropout;
        self
    }

    /// Calculate total number of features.
    pub fn n_features(&self) -> usize {
        self.n_groups * self.kernels_per_group * self.pooling_types.len() * self.n_vars
    }

    /// Initialize the model.
    pub fn init<B: Backend>(&self, device: &B::Device) -> HydraPlus<B> {
        HydraPlus::new(self.clone(), device)
    }
}

/// HydraPlus model for time series classification.
///
/// Uses dictionary-based random convolutions with learnable classification head.
///
/// # Architecture
///
/// ```text
/// Input (B, V, L) -> [Random Convolutions with Dilations]
///                 -> [Multiple Pooling Types]
///                 -> Concat Features
///                 -> Linear -> ReLU -> Dropout -> Linear -> Output
/// ```
///
/// # Example
///
/// ```rust,ignore
/// use tsai_models::rocket::HydraPlus;
///
/// let config = HydraPlusConfig::new(3, 100, 5)
///     .with_n_groups(8)
///     .with_kernels_per_group(8);
/// let model = config.init::<NdArray>(&device);
///
/// let x = Tensor::random([32, 3, 100], Distribution::Normal(0.0, 1.0), &device);
/// let output = model.forward(x);
/// // output shape: [32, 5]
/// ```
#[derive(Module, Debug)]
pub struct HydraPlus<B: Backend> {
    /// First linear layer.
    fc1: Linear<B>,
    /// Second linear layer (classifier).
    fc2: Linear<B>,
    /// Dropout.
    dropout: Dropout,
    /// Flattened kernel weights (n_groups * kernels_per_group * kernel_length).
    kernel_weights: Vec<f32>,
    /// Flattened kernel biases (n_groups * kernels_per_group).
    kernel_biases: Vec<f32>,
    /// Dilations per group.
    dilations: Vec<usize>,
    /// Kernels per group.
    kernels_per_group: usize,
    /// Pooling types.
    pooling_types_ids: Vec<usize>,
    /// Number of input variables.
    n_vars: usize,
    /// Kernel length.
    kernel_length: usize,
    /// Number of groups.
    n_groups: usize,
}

impl<B: Backend> HydraPlus<B> {
    /// Create a new HydraPlus model.
    pub fn new(config: HydraPlusConfig, device: &B::Device) -> Self {
        let mut rng = ChaCha8Rng::seed_from_u64(config.seed);

        // Flattened storage for kernels
        let mut kernel_weights = Vec::new();
        let mut kernel_biases = Vec::new();
        let mut dilations = Vec::new();

        for group_idx in 0..config.n_groups {
            // Dilation increases exponentially per group
            let dilation = 1 << (group_idx % (config.max_dilation.ilog2() as usize + 1));
            dilations.push(dilation);

            // Generate random kernels for this group
            for _ in 0..config.kernels_per_group {
                // Random kernel weights (normalized)
                let mut kernel: Vec<f32> = (0..config.kernel_length)
                    .map(|_| rng.gen_range(-1.0..1.0))
                    .collect();

                // Normalize kernel (zero mean)
                let sum: f32 = kernel.iter().sum();
                let mean = sum / config.kernel_length as f32;
                for w in &mut kernel {
                    *w -= mean;
                }

                kernel_weights.extend(kernel);

                // Random bias for PPV (quantile-like)
                kernel_biases.push(rng.gen_range(-1.0..1.0));
            }
        }

        // Convert pooling types to IDs
        let pooling_types_ids: Vec<usize> = config
            .pooling_types
            .iter()
            .map(|p| match p {
                HydraPooling::Max => 0,
                HydraPooling::Mean => 1,
                HydraPooling::PPV => 2,
            })
            .collect();

        // Classifier layers
        let n_features = config.n_features();
        let fc1 = LinearConfig::new(n_features, config.hidden_dim).init(device);
        let fc2 = LinearConfig::new(config.hidden_dim, config.n_classes).init(device);
        let dropout = DropoutConfig::new(config.dropout).init();

        Self {
            fc1,
            fc2,
            dropout,
            kernel_weights,
            kernel_biases,
            dilations,
            kernels_per_group: config.kernels_per_group,
            pooling_types_ids,
            n_vars: config.n_vars,
            kernel_length: config.kernel_length,
            n_groups: config.n_groups,
        }
    }

    /// Apply convolution with dilation (CPU computation for random kernels).
    fn apply_convolution(
        &self,
        input: &[f32],
        kernel: &[f32],
        bias: f32,
        dilation: usize,
        seq_len: usize,
    ) -> Vec<f32> {
        let kernel_len = kernel.len();
        let effective_len = (kernel_len - 1) * dilation + 1;

        if seq_len < effective_len {
            return vec![0.0; seq_len];
        }

        let output_len = seq_len - effective_len + 1;
        let mut output = Vec::with_capacity(output_len);

        for i in 0..output_len {
            let mut sum = 0.0f32;
            for (k, &w) in kernel.iter().enumerate() {
                let idx = i + k * dilation;
                if idx < seq_len {
                    sum += input[idx] * w;
                }
            }
            output.push(sum + bias);
        }

        output
    }

    /// Apply pooling to convolution output.
    fn apply_pooling(&self, output: &[f32], pooling_type: usize) -> f32 {
        if output.is_empty() {
            return 0.0;
        }

        match pooling_type {
            0 => {
                // Max pooling
                output.iter().cloned().fold(f32::NEG_INFINITY, f32::max)
            }
            1 => {
                // Mean pooling
                output.iter().sum::<f32>() / output.len() as f32
            }
            2 => {
                // PPV (proportion of positive values)
                let positive = output.iter().filter(|&&x| x > 0.0).count();
                positive as f32 / output.len() as f32
            }
            _ => 0.0,
        }
    }

    /// Extract features from input.
    fn extract_features(&self, x: &Tensor<B, 3>) -> Tensor<B, 2> {
        let [batch_size, n_vars, seq_len] = x.dims();
        let device = x.device();

        // Get input data
        let input_data = x.clone().into_data();
        let input_flat: Vec<f32> = input_data
            .as_slice()
            .map(|s| s.to_vec())
            .unwrap_or_default();

        // Calculate number of features per sample
        let n_pooling = self.pooling_types_ids.len();
        let total_kernels = self.n_groups * self.kernels_per_group;
        let features_per_sample = total_kernels * n_pooling * n_vars;

        let mut all_features = Vec::with_capacity(batch_size * features_per_sample);

        for b in 0..batch_size {
            for v in 0..n_vars {
                // Get time series for this sample and variable
                let ts_start = b * n_vars * seq_len + v * seq_len;
                let ts: Vec<f32> = input_flat[ts_start..ts_start + seq_len].to_vec();

                // Apply each kernel group
                for group_idx in 0..self.n_groups {
                    let dilation = self.dilations[group_idx];

                    for kernel_idx in 0..self.kernels_per_group {
                        // Get kernel weights from flattened storage
                        let kernel_start =
                            (group_idx * self.kernels_per_group + kernel_idx) * self.kernel_length;
                        let kernel_end = kernel_start + self.kernel_length;
                        let kernel = &self.kernel_weights[kernel_start..kernel_end];

                        // Get bias
                        let bias_idx = group_idx * self.kernels_per_group + kernel_idx;
                        let bias = self.kernel_biases[bias_idx];

                        // Apply convolution
                        let conv_output =
                            self.apply_convolution(&ts, kernel, bias, dilation, seq_len);

                        // Apply each pooling type
                        for &pooling_type in &self.pooling_types_ids {
                            let feature = self.apply_pooling(&conv_output, pooling_type);
                            all_features.push(feature);
                        }
                    }
                }
            }
        }

        // Convert to tensor
        Tensor::<B, 1>::from_floats(all_features.as_slice(), &device)
            .reshape([batch_size, features_per_sample])
    }

    /// Forward pass.
    pub fn forward(&self, x: Tensor<B, 3>) -> Tensor<B, 2> {
        // Extract features
        let features = self.extract_features(&x);

        // Classifier
        let out = self.fc1.forward(features);
        let out = Relu::new().forward(out);
        let out = self.dropout.forward(out);
        self.fc2.forward(out)
    }

    /// Forward pass returning probabilities.
    pub fn forward_probs(&self, x: Tensor<B, 3>) -> Tensor<B, 2> {
        let logits = self.forward(x);
        softmax(logits, 1)
    }

    /// Get the number of kernel groups.
    pub fn num_groups(&self) -> usize {
        self.n_groups
    }

    /// Get the total number of features.
    pub fn num_features(&self) -> usize {
        self.n_groups * self.kernels_per_group * self.pooling_types_ids.len() * self.n_vars
    }
}

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

    #[test]
    fn test_hydra_config_default() {
        let config = HydraPlusConfig::default();
        assert_eq!(config.n_groups, 8);
        assert_eq!(config.kernels_per_group, 8);
        assert_eq!(config.kernel_length, 9);
        assert_eq!(config.pooling_types.len(), 2);
    }

    #[test]
    fn test_hydra_config_new() {
        let config = HydraPlusConfig::new(3, 200, 10);
        assert_eq!(config.n_vars, 3);
        assert_eq!(config.seq_len, 200);
        assert_eq!(config.n_classes, 10);
    }

    #[test]
    fn test_hydra_config_builder() {
        let config = HydraPlusConfig::new(3, 100, 5)
            .with_n_groups(4)
            .with_kernels_per_group(16)
            .with_kernel_length(7)
            .with_pooling_types(vec![HydraPooling::Max, HydraPooling::Mean, HydraPooling::PPV])
            .with_hidden_dim(64)
            .with_dropout(0.2);

        assert_eq!(config.n_groups, 4);
        assert_eq!(config.kernels_per_group, 16);
        assert_eq!(config.kernel_length, 7);
        assert_eq!(config.pooling_types.len(), 3);
        assert_eq!(config.hidden_dim, 64);
        assert_eq!(config.dropout, 0.2);
    }

    #[test]
    fn test_n_features() {
        let config = HydraPlusConfig::new(2, 100, 5)
            .with_n_groups(4)
            .with_kernels_per_group(8)
            .with_pooling_types(vec![HydraPooling::Max, HydraPooling::PPV]);

        // 4 groups * 8 kernels * 2 pooling types * 2 variables = 128 features
        assert_eq!(config.n_features(), 128);
    }
}