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
use crate::prelude::*;
use rand::{distributions::Distribution, Rng};
#[derive(Default, Debug, Clone)]
pub struct Linear<const I: usize, const O: usize> {
pub weight: Tensor2D<I, O, NoTape>,
pub bias: Tensor1D<O, NoTape>,
}
impl<const I: usize, const O: usize> CanUpdateWithGradients for Linear<I, O> {
fn update<G: GradientProvider>(&mut self, grads: &mut G) {
self.weight.update(grads);
self.bias.update(grads);
}
}
impl<const I: usize, const O: usize> Randomize<f32> for Linear<I, O> {
fn randomize<R: Rng, D: Distribution<f32>>(&mut self, rng: &mut R, dist: &D) {
self.weight.randomize(rng, dist);
self.bias.randomize(rng, dist);
}
}
impl<const I: usize, const O: usize, H: TapeHolder> Module<Tensor1D<I, H>> for Linear<I, O> {
type Output = Tensor1D<O, H>;
fn forward(&self, x: Tensor1D<I, H>) -> Self::Output {
add(&self.bias, vecmat_mul(x, &self.weight))
}
}
impl<const B: usize, const I: usize, const O: usize, H: TapeHolder> Module<Tensor2D<B, I, H>>
for Linear<I, O>
{
type Output = Tensor2D<B, O, H>;
fn forward(&self, x: Tensor2D<B, I, H>) -> Self::Output {
broadcast_outer_add(matmul(x, &self.weight), &self.bias)
}
}
#[cfg(test)]
mod tests {
use super::*;
const W: [[f32; 2]; 5] = [
[-0.34588930, 0.11733949],
[-0.30371523, 0.14059687],
[-0.37120569, -0.10670426],
[0.14303583, -0.09373143],
[-0.02689660, 0.18974298],
];
const B: [f32; 2] = [0.37653649, -0.29071701];
#[test]
fn test_forward_1d() {
let model: Linear<5, 2> = Linear {
weight: Tensor2D::new(W),
bias: Tensor1D::new(B),
};
let x = Tensor1D::new([-0.88080013, 2.41853333, 2.24783349, 0.05652110, 2.03129911]);
let y = model.forward(x.trace());
assert_eq!(y.data(), &[-0.93430865, 0.08624211]);
let loss = y.square().mean();
let gradients = loss.backward();
assert_eq!(
gradients.ref_gradient(&model.weight),
&[
[0.82293916, -0.07596206],
[-2.25965667, 0.20857942],
[-2.10017037, 0.19385791],
[-0.05280815, 0.004874499],
[-1.89786029, 0.17518352]
]
);
assert_eq!(
gradients.ref_gradient(&model.bias),
&[-0.93430865, 0.08624211]
);
}
#[test]
fn test_forward_2d() {
let model: Linear<5, 2> = Linear {
weight: Tensor2D::new(W),
bias: Tensor1D::new(B),
};
let x = Tensor2D::new([
[-1.94686651, 1.46117854, -1.66989815, 1.40886295, 1.34256434],
[
-1.33998311,
3.05106783,
-0.17936817,
-0.04943254,
-0.80527049,
],
[
-0.82914120,
0.07691376,
-0.26538327,
0.90017676,
-1.87904549,
],
]);
let y = model.forward(x.trace());
assert_eq!(
y.data(),
&[
[1.39143777, -0.012851536],
[-0.005462587, -0.14800104],
[0.9177769, -0.7897872]
]
);
let loss = y.square().mean();
let gradients = loss.backward();
assert_eq!(
gradients.ref_gradient(&model.weight),
&[
[-1.15419686, 0.29272807],
[0.69568729, -0.17702839],
[-0.85538071, 0.08586791],
[0.92892551, -0.24057935],
[0.04931633, 0.52865762]
]
);
assert_eq!(
gradients.ref_gradient(&model.bias),
&[0.76791739, -0.31687993]
);
}
}