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

use super::super::activation::Activation;



/// multiply two vectors element wise
#[inline]
pub fn element_multiply(one: &mut Vec<f32>, two: &Vec<f32>) {
    assert!(one.len() == two.len(), "Element multiply vector shapes don't match");
    one.iter_mut()
        .zip(two.iter())
        .for_each(|(a, b)| {
            *a *= b
        });
}



/// invert a vector that is already holding values between 0 and 1
#[inline]
pub fn element_invert(one: &mut Vec<f32>) {
    one.iter_mut()
        .for_each(|a| *a = 1.0 - *a);
}



/// add elements from vectors together element wise
#[inline]
pub fn element_add(one: &mut Vec<f32>, two: &Vec<f32>) {
    assert!(one.len() == two.len(), "Element add vector shapes don't match");
    one.iter_mut()
        .zip(two.iter())
        .for_each(|(a, b)| {
            *a += b
        });
}


#[inline]
pub fn element_activate(one: &Vec<f32>, func: Activation) -> Vec<f32> {
    one.iter()
        .map(|x| {
            func.activate(*x)
        })
        .collect()
}


#[inline]
pub fn element_deactivate(one: &Vec<f32>, func: Activation) -> Vec<f32> {
    one.iter()
        .map(|x| {
            func.deactivate(*x)
        })
        .collect()
}


#[inline]
pub fn product(one: &Vec<f32>, two: &Vec<f32>) -> Vec<f32> {
    assert!(one.len() == two.len(), "Product dimensions do not match");
    one.iter()
        .zip(two.iter())
        .map(|(o, t)| o * t)
        .collect::<Vec<_>>()
}


#[inline]
pub fn subtract(one: &Vec<f32>, two: &Vec<f32>) -> Vec<f32> {
    assert!(one.len() == two.len(), "Subtract lengths do not match");
    one.iter()
        .zip(two.iter())
        .map(|(tar, pre)| tar - pre)
        .collect::<Vec<_>>()
}


#[inline]
pub fn softmax(one: &Vec<f32>) -> Vec<f32> {
    let ex = one   
        .iter()
        .map(|x| x.exp())
        .collect::<Vec<_>>();
    let sum = ex.iter().sum::<f32>();
    ex.iter()
        .map(|x| x / sum)
        .collect()
}


#[inline]
pub fn d_softmax(one: &Vec<f32>) -> Vec<f32> {
    one.iter()
        .map(|x| x - 1.0)
        .collect()
}