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
#[macro_use(azip)]
extern crate ndarray;
use ndarray::prelude::*;
use ndarray::Data;
use ndarray::linalg::{
general_mat_mul,
general_mat_vec_mul,
};
pub struct Rls<F> {
inv_forgetting_factor: F,
gain: Array1<F>,
inverse_correlation: Array2<F>,
weight: Array1<F>,
prior_error: F,
temp_a: Array2<F>,
temp_b: Array2<F>,
}
impl<F: NdFloat> Rls<F> {
pub fn new(initialization_factor: F, forgetting_factor: F, n: usize) -> Self {
let weight = Array1::zeros(n);
Rls::with_weight(initialization_factor, forgetting_factor, weight)
}
pub fn with_weight(initialization_factor: F, forgetting_factor: F, weight: Array1<F>) -> Self {
let one = F::one();
let zero = F::zero();
let n = weight.len();
let inv_forgetting_factor = one / forgetting_factor;
let gain = Array1::zeros(n);
let prior_error = zero;
let mut inverse_correlation = Array2::eye(n);
inverse_correlation *= one/initialization_factor;
let temp_a = Array2::zeros([n,n]);
let temp_b = Array2::zeros([n,n]);
Rls {
inv_forgetting_factor,
gain,
inverse_correlation,
weight,
prior_error,
temp_a,
temp_b,
}
}
pub fn update<S>(&mut self, input: &ArrayBase<S, Ix1>, target: F)
where S: Data<Elem = F>
{
let one = F::one();
let zero = F::zero();
general_mat_vec_mul(
one,
&self.inverse_correlation,
input,
one,
&mut self.gain
);
let c = self.inv_forgetting_factor + self.gain.dot(&self.gain);
self.gain /= c;
self.prior_error = target - self.weight.dot(&input);
self.weight.scaled_add(self.prior_error, &self.gain);
azip!(mut row (self.temp_a.genrows_mut()), gain (&self.gain) in {
azip!(mut row, input (input) in {
*row = gain * input;
})});
general_mat_mul(one, &self.temp_a, &self.inverse_correlation, zero, &mut self.temp_b);
self.inverse_correlation -= &self.temp_b;
self.inverse_correlation *= self.inv_forgetting_factor;
}
}
impl<T> Rls<T> {
pub fn gain_ref(&self) -> &Array1<T> {
&self.gain
}
pub fn inverse_correlation_ref(&self) -> &Array2<T> {
&self.inverse_correlation
}
pub fn inv_forgetting_factor_ref(&self) -> &T {
&self.inv_forgetting_factor
}
pub fn weight_ref(&self) -> &Array1<T> {
&self.weight
}
pub fn prior_error_ref(&self) -> &T {
&self.prior_error
}
}