sklears-feature-extraction 0.1.2

Feature extraction from raw data (text, images)
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
use super::neural_types::*;
use super::neural_utilities::*;
use scirs2_core::ndarray::s;

pub fn sinusoidal_positional_encoding(seq_len: usize, d_model: usize) -> Array2<f64> {
    let mut encoding = Array2::zeros((seq_len, d_model));

    for pos in 0..seq_len {
        for i in 0..d_model {
            let angle = pos as f64 / (10000.0_f64).powf((2 * (i / 2)) as f64 / d_model as f64);
            if i % 2 == 0 {
                encoding[(pos, i)] = angle.sin();
            } else {
                encoding[(pos, i)] = angle.cos();
            }
        }
    }

    encoding
}

pub fn learned_positional_encoding(seq_len: usize, d_model: usize, random_state: Option<u64>) -> Array2<f64> {
    xavier_normal_init(seq_len, d_model, random_state)
}

pub fn relative_positional_encoding(seq_len: usize, d_model: usize, max_relative_position: usize) -> Array3<f64> {
    let mut encoding = Array3::zeros((seq_len, seq_len, d_model));

    for i in 0..seq_len {
        for j in 0..seq_len {
            let relative_pos = ((i as i32 - j as i32).abs() as usize).min(max_relative_position);

            for k in 0..d_model {
                let angle = relative_pos as f64 / (10000.0_f64).powf((2 * (k / 2)) as f64 / d_model as f64);
                if k % 2 == 0 {
                    encoding[(i, j, k)] = angle.sin();
                } else {
                    encoding[(i, j, k)] = angle.cos();
                }
            }
        }
    }

    encoding
}

pub fn rotary_positional_encoding(seq_len: usize, d_model: usize) -> Array2<f64> {
    assert_eq!(d_model % 2, 0, "d_model must be even for rotary encoding");

    let mut encoding = Array2::zeros((seq_len, d_model));

    for pos in 0..seq_len {
        for i in (0..d_model).step_by(2) {
            let theta = pos as f64 / (10000.0_f64).powf(i as f64 / d_model as f64);
            encoding[(pos, i)] = theta.cos();
            encoding[(pos, i + 1)] = theta.sin();
        }
    }

    encoding
}

pub fn apply_rotary_encoding(x: &Array2<f64>, pos_encoding: &Array2<f64>) -> Array2<f64> {
    let (seq_len, d_model) = x.dim();
    let mut result = Array2::zeros((seq_len, d_model));

    for i in 0..seq_len {
        for j in (0..d_model).step_by(2) {
            let cos_theta = pos_encoding[(i, j)];
            let sin_theta = pos_encoding[(i, j + 1)];

            let x1 = x[(i, j)];
            let x2 = x[(i, j + 1)];

            result[(i, j)] = x1 * cos_theta - x2 * sin_theta;
            result[(i, j + 1)] = x1 * sin_theta + x2 * cos_theta;
        }
    }

    result
}

pub fn alibi_positional_bias(seq_len: usize, n_heads: usize) -> Array3<f64> {
    let mut bias = Array3::zeros((n_heads, seq_len, seq_len));

    let slopes = alibi_get_slopes(n_heads);

    for h in 0..n_heads {
        for i in 0..seq_len {
            for j in 0..seq_len {
                if j <= i {
                    bias[(h, i, j)] = -slopes[h] * (i - j) as f64;
                } else {
                    bias[(h, i, j)] = f64::NEG_INFINITY;
                }
            }
        }
    }

    bias
}

fn alibi_get_slopes(n_heads: usize) -> Vec<f64> {
    let mut slopes = Vec::new();

    if n_heads.is_power_of_two() {
        let start = 2.0_f64.powf(-8.0 / n_heads as f64);
        let ratio = 2.0_f64.powf(-1.0);

        for i in 0..n_heads {
            slopes.push(start * ratio.powf(i as f64));
        }
    } else {
        let closest_power_of_2 = 2_usize.pow((n_heads as f64).log2().floor() as u32);
        let extra_heads = n_heads - closest_power_of_2;

        let base_slopes = alibi_get_slopes(closest_power_of_2);
        slopes.extend_from_slice(&base_slopes);

        let start = 2.0_f64.powf(-8.0 / (2 * closest_power_of_2) as f64);
        let ratio = 2.0_f64.powf(-2.0);

        for i in 0..extra_heads {
            slopes.push(start * ratio.powf(i as f64));
        }
    }

    slopes
}

pub struct PositionalEmbedding {
    pub encoding_type: PositionalEncodingType,
    pub seq_len: usize,
    pub d_model: usize,
    pub learned_embeddings: Option<Array2<f64>>,
    pub max_relative_position: usize,
    pub dropout_rate: f64,
}

#[derive(Debug, Clone, Copy)]
pub enum PositionalEncodingType {
    Sinusoidal,
    Learned,
    Relative,
    Rotary,
    ALiBi,
    None,
}

impl PositionalEmbedding {
    pub fn new(
        encoding_type: PositionalEncodingType,
        seq_len: usize,
        d_model: usize,
        random_state: Option<u64>,
    ) -> Self {
        let learned_embeddings = match encoding_type {
            PositionalEncodingType::Learned => Some(learned_positional_encoding(seq_len, d_model, random_state)),
            _ => None,
        };

        Self {
            encoding_type,
            seq_len,
            d_model,
            learned_embeddings,
            max_relative_position: 32,
            dropout_rate: 0.1,
        }
    }

    pub fn with_max_relative_position(mut self, max_relative_position: usize) -> Self {
        self.max_relative_position = max_relative_position;
        self
    }

    pub fn with_dropout(mut self, dropout_rate: f64) -> Self {
        self.dropout_rate = dropout_rate;
        self
    }

    pub fn get_encoding(&self, actual_seq_len: usize) -> Array2<f64> {
        let effective_seq_len = actual_seq_len.min(self.seq_len);

        match self.encoding_type {
            PositionalEncodingType::Sinusoidal => {
                sinusoidal_positional_encoding(effective_seq_len, self.d_model)
            },
            PositionalEncodingType::Learned => {
                if let Some(ref embeddings) = self.learned_embeddings {
                    embeddings.slice(s![..effective_seq_len, ..]).to_owned()
                } else {
                    Array2::zeros((effective_seq_len, self.d_model))
                }
            },
            PositionalEncodingType::Rotary => {
                rotary_positional_encoding(effective_seq_len, self.d_model)
            },
            _ => Array2::zeros((effective_seq_len, self.d_model)),
        }
    }

    pub fn apply_encoding(&self, x: &Array2<f64>) -> Array2<f64> {
        let (seq_len, d_model) = x.dim();

        if d_model != self.d_model {
            panic!("Input d_model {} does not match expected d_model {}", d_model, self.d_model);
        }

        match self.encoding_type {
            PositionalEncodingType::Sinusoidal | PositionalEncodingType::Learned => {
                let encoding = self.get_encoding(seq_len);
                x + &encoding
            },
            PositionalEncodingType::Rotary => {
                let encoding = self.get_encoding(seq_len);
                apply_rotary_encoding(x, &encoding)
            },
            PositionalEncodingType::None => x.clone(),
            _ => x.clone(),
        }
    }

    pub fn get_relative_encoding(&self, seq_len: usize) -> Array3<f64> {
        relative_positional_encoding(seq_len, self.d_model, self.max_relative_position)
    }

    pub fn get_alibi_bias(&self, seq_len: usize, n_heads: usize) -> Array3<f64> {
        alibi_positional_bias(seq_len, n_heads)
    }

    pub fn apply_dropout(&self, x: &Array2<f64>) -> Array2<f64> {
        if self.dropout_rate == 0.0 {
            return x.clone();
        }

        let mut rng = Random::seed_from_u64(42);
        let mut result = x.clone();

        for elem in result.iter_mut() {
            if rng.random_range(0.0..1.0) < self.dropout_rate {
                *elem = 0.0;
            } else {
                *elem /= 1.0 - self.dropout_rate;
            }
        }

        result
    }
}

pub fn create_position_ids(seq_len: usize, batch_size: usize) -> Array2<usize> {
    let mut position_ids = Array2::zeros((batch_size, seq_len));

    for i in 0..batch_size {
        for j in 0..seq_len {
            position_ids[(i, j)] = j;
        }
    }

    position_ids
}

pub fn create_token_type_ids(seq_lengths: &[usize], max_len: usize) -> Array2<usize> {
    let batch_size = seq_lengths.len();
    let mut token_type_ids = Array2::zeros((batch_size, max_len));

    for (i, &length) in seq_lengths.iter().enumerate() {
        for j in 0..length.min(max_len) {
            token_type_ids[(i, j)] = if j < length / 2 { 0 } else { 1 };
        }
    }

    token_type_ids
}

pub fn interpolate_positional_encoding(
    original_encoding: &Array2<f64>,
    new_seq_len: usize,
) -> Array2<f64> {
    let (original_seq_len, d_model) = original_encoding.dim();

    if new_seq_len == original_seq_len {
        return original_encoding.clone();
    }

    let mut interpolated = Array2::zeros((new_seq_len, d_model));

    for i in 0..new_seq_len {
        let position = (i as f64 * (original_seq_len - 1) as f64) / (new_seq_len - 1) as f64;
        let lower_idx = position.floor() as usize;
        let upper_idx = (lower_idx + 1).min(original_seq_len - 1);
        let weight = position - position.floor();

        for j in 0..d_model {
            if lower_idx == upper_idx {
                interpolated[(i, j)] = original_encoding[(lower_idx, j)];
            } else {
                interpolated[(i, j)] = (1.0 - weight) * original_encoding[(lower_idx, j)]
                    + weight * original_encoding[(upper_idx, j)];
            }
        }
    }

    interpolated
}

pub fn extrapolate_positional_encoding(
    original_encoding: &Array2<f64>,
    new_seq_len: usize,
) -> Array2<f64> {
    let (original_seq_len, d_model) = original_encoding.dim();

    if new_seq_len <= original_seq_len {
        return interpolate_positional_encoding(original_encoding, new_seq_len);
    }

    let mut extrapolated = Array2::zeros((new_seq_len, d_model));

    for j in 0..d_model {
        let last_value = original_encoding[(original_seq_len - 1, j)];
        let second_last_value = if original_seq_len > 1 {
            original_encoding[(original_seq_len - 2, j)]
        } else {
            0.0
        };
        let diff = last_value - second_last_value;

        for i in 0..original_seq_len {
            extrapolated[(i, j)] = original_encoding[(i, j)];
        }

        for i in original_seq_len..new_seq_len {
            let steps = (i - original_seq_len + 1) as f64;
            extrapolated[(i, j)] = last_value + diff * steps;
        }
    }

    extrapolated
}

pub fn compute_positional_similarity(
    pos1: usize,
    pos2: usize,
    encoding: &Array2<f64>,
) -> f64 {
    if pos1 >= encoding.nrows() || pos2 >= encoding.nrows() {
        return 0.0;
    }

    let vec1 = encoding.row(pos1);
    let vec2 = encoding.row(pos2);

    cosine_similarity(&vec1.to_owned(), &vec2.to_owned())
}

pub fn positional_encoding_temperature_scaling(
    encoding: &Array2<f64>,
    temperature: f64,
) -> Array2<f64> {
    encoding / temperature
}

pub fn adaptive_positional_encoding(
    seq_len: usize,
    d_model: usize,
    adaptation_factor: f64,
) -> Array2<f64> {
    let base_encoding = sinusoidal_positional_encoding(seq_len, d_model);
    let mut adaptive_encoding = Array2::zeros((seq_len, d_model));

    for i in 0..seq_len {
        let position_weight = 1.0 + adaptation_factor * (i as f64 / seq_len as f64);
        for j in 0..d_model {
            adaptive_encoding[(i, j)] = base_encoding[(i, j)] * position_weight;
        }
    }

    adaptive_encoding
}

pub fn hierarchical_positional_encoding(
    seq_len: usize,
    d_model: usize,
    hierarchy_levels: usize,
) -> Array2<f64> {
    let mut encoding = Array2::zeros((seq_len, d_model));
    let dims_per_level = d_model / hierarchy_levels;

    for level in 0..hierarchy_levels {
        let scale = 2_usize.pow(level as u32);
        let start_dim = level * dims_per_level;
        let end_dim = ((level + 1) * dims_per_level).min(d_model);

        for pos in 0..seq_len {
            for i in start_dim..end_dim {
                let dim_in_level = i - start_dim;
                let angle = (pos / scale) as f64 / (10000.0_f64).powf((2 * (dim_in_level / 2)) as f64 / dims_per_level as f64);

                if dim_in_level % 2 == 0 {
                    encoding[(pos, i)] = angle.sin();
                } else {
                    encoding[(pos, i)] = angle.cos();
                }
            }
        }
    }

    encoding
}

pub fn contextual_positional_encoding(
    context_vectors: &Array2<f64>,
    d_model: usize,
    random_state: Option<u64>,
) -> Array2<f64> {
    let seq_len = context_vectors.nrows();
    let context_dim = context_vectors.ncols();

    let projection_matrix = xavier_normal_init(context_dim, d_model, random_state);
    let base_encoding = sinusoidal_positional_encoding(seq_len, d_model);

    let projected_context = context_vectors.dot(&projection_matrix);
    &base_encoding + &projected_context
}