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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
extern crate blas;
extern crate ndarray;
#[cfg(feature = "serde")]
extern crate serde;
#[cfg(feature = "serde")]
#[macro_use]
extern crate serde_derive;
use ndarray::prelude::*;
use ndarray::Data;
use ndarray::linalg::{
general_mat_vec_mul,
};
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone,Debug)]
pub struct Rls<F> {
inv_forgetting_factor: F,
gain: Array1<F>,
inverse_correlation: Array2<F>,
weight: Array1<F>,
prior_error: F,
temp_mat: Array2<F>,
temp_vec: Array1<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_mat = Array2::zeros([n,n]);
let temp_vec = Array1::zeros(n);
Rls {
inv_forgetting_factor,
gain,
inverse_correlation,
weight,
prior_error,
temp_mat,
temp_vec,
}
}
}
macro_rules! impl_update {
($t:ty, $fn:expr) => {
impl Rls<$t> {
pub fn update<S>(&mut self, input: &ArrayBase<S, Ix1>, target: $t)
where S: Data<Elem = $t>
{
general_mat_vec_mul(
1.0,
&self.inverse_correlation,
input,
0.0,
&mut self.gain
);
let c = self.inv_forgetting_factor + input.dot(&self.gain);
self.gain /= c;
self.prior_error = target - self.weight.dot(&input);
self.weight.scaled_add(self.prior_error, &self.gain);
general_mat_vec_mul(
1.0,
&self.inverse_correlation.t(),
input,
0.0,
&mut self.temp_vec
);
self.temp_mat.fill(0.0);
let temp_mat_stride = self.temp_mat.strides()[0];
unsafe {
$fn(
blas::c::Layout::RowMajor,
self.gain.dim() as i32,
self.temp_vec.dim() as i32,
1.0,
self.gain.as_slice().unwrap(),
self.gain.strides()[0] as i32,
self.temp_vec.as_slice().unwrap(),
self.gain.strides()[0] as i32,
self.temp_mat.as_slice_mut().unwrap(),
temp_mat_stride as i32,
);
}
self.inverse_correlation -= &self.temp_mat;
self.inverse_correlation *= self.inv_forgetting_factor;
}
}
}}
impl_update!(f32, blas::c::sger);
impl_update!(f64, blas::c::dger);
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
}
}