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
#[derive(Clone)]
pub struct EarlyExaggerationOptions {
    pub mult: f32,
    pub iterations: usize
}

#[derive(Clone)]
pub struct MomentumSchedule {
    pub early_rate: f32,
    pub late_rate: f32,
    pub crossover_iteration: usize
}

impl Default for MomentumSchedule {
    fn default() -> Self {
        MomentumSchedule {
            early_rate: 0.5,
            late_rate: 0.8,
            crossover_iteration: 250
        }
    }
}

impl MomentumSchedule {
    /// Return what the rate should be on a iteration.
    pub fn rate(&self, iter: usize) -> f32 {
        if iter > self.crossover_iteration {
            self.late_rate
        } else {
            self.early_rate
        }
    }
}

#[derive(Clone)]
pub struct TSNEOptions {
    pub perplexity: f32,
    pub initial_spread: f32,
    pub learning_rate: f32,
    pub momentum: MomentumSchedule,
    pub num_iterations: usize,
    early_compression: bool,
    pub early_exaggeration: Option<EarlyExaggerationOptions>
}

impl Default for TSNEOptions {
    fn default() -> Self {
        TSNEOptions { perplexity: 30.0,
                      learning_rate: 100.0,
                      initial_spread: 2.0,
                      num_iterations: 1000,
                      momentum: MomentumSchedule::default(),
                      early_compression: false,
                      early_exaggeration: Some(EarlyExaggerationOptions{ mult: 4.0, iterations: 50}) }
    }
}