fetish_lib/
feature_collection.rs

1extern crate ndarray;
2extern crate ndarray_linalg;
3
4use ndarray::*;
5use crate::fourier_feature_collection::*;
6use crate::quadratic_feature_collection::*;
7use crate::sketched_linear_feature_collection::*;
8use crate::rand_utils::*;
9
10pub trait FeatureCollection {
11    ///Return the number of input dimensions
12    fn get_in_dimensions(&self) -> usize;
13
14    ///Return the number of dimensions in the output of get_features
15    fn get_dimension(&self) -> usize;
16
17    ///Given a vector in the input space of this feature space, return the
18    ///vector of features for this feature space
19    fn get_features(&self, in_vec: ArrayView1<f32>) -> Array1<f32>;
20
21    ///Given a vector in the input space of this feature space, return
22    ///the Jacobian matrix for the feature vector at that point
23    ///in the format f x s, for s the input space size
24    fn get_jacobian(&self, in_vec: ArrayView1<f32>) -> Array2<f32>;
25
26    ///Given a matrix whose rows are each input vectors, yields a new
27    ///matrix where every row of the output is the featurized version
28    ///of the corresponding input vector
29    fn get_features_mat(&self, in_mat : ArrayView2<f32>) -> Array2<f32> {
30        let n = in_mat.shape()[0];
31        let d = self.get_dimension();
32        let mut result = Array::zeros((n, d));
33        for i in 0..n {
34            let in_vec = in_mat.row(i).to_owned();
35            let feat_vec = self.get_features(in_vec.view());
36            result.row_mut(i).assign(&feat_vec);
37        }
38        result
39    }
40}
41
42///Gets the total number of feature dimensions in the passed [`Vec`] of [`FeatureCollection`] trait
43///objects.
44pub fn get_total_feat_dims(feature_collections : &Vec<Box<dyn FeatureCollection>>) -> usize {
45    let mut total_feat_dims : usize = 0;
46    for collection in feature_collections.iter() {
47        total_feat_dims += collection.get_dimension();
48    }
49    total_feat_dims
50}