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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//! `lfa` is a framework for online learning of linear function approximation
//! models. Included is a suite of scalar and multi-output approximation
//! helpers, a range of basis functions for feature construction, and a set of
//! optimisation routines. The framework is designed to support
//! fallible operations for which a custom error type is provided.
//!
//! # Example
//! The code below illustrates how one might use `lfa` to fit a simple linear
//! model with SGD.
//!
//! ```rust,no_run
//! extern crate lfa;
//! extern crate rand;
//! extern crate rand_distr;
//!
//! use lfa::{
//!     LFA,
//!     basis::{Basis, Polynomial},
//!     optim::SGD,
//! };
//! use rand::{Rng, thread_rng};
//! use rand_distr::{Uniform, Normal};
//! use spaces::Space;
//!
//! fn main() {
//!     const M: f64 = 1.0;
//!     const C: f64 = -0.5;
//!
//!     let mut fa = LFA::scalar(
//!         Polynomial::new(1, 1).with_zeroth(),
//!         SGD(1.0)
//!     );
//!
//!     let mut rng = thread_rng();
//!     let mut data_dist = Uniform::new_inclusive(-1.0, 1.0);
//!     let mut noise_dist = Normal::new(0.0, 0.01).unwrap();
//!
//!     for x in rng.sample_iter(&data_dist).take(1000) {
//!         let y_exp = M*x + C;
//!         let y_sample = y_exp + rng.sample(noise_dist);
//!
//!         fa.update_with(&[x], |_, y_pred| y_sample - y_pred).ok();
//!     }
//!
//!     let rmse = (
//!         (fa.weights[0] - M).powi(2) +
//!         (fa.weights[1] - C).powi(2)
//!     ).sqrt();
//!
//!     assert!(rmse < 1e-3);
//! }
//! ```
// #![warn(missing_docs)]

#[cfg_attr(feature = "serde", macro_use)]
#[cfg(feature = "serde")]
extern crate serde_crate as serde;

#[cfg(test)]
#[macro_use(quickcheck)]
extern crate quickcheck;

mod macros;
mod utils;

#[macro_use]
mod error;

mod features;
pub use self::features::*;

#[macro_use]
pub mod basis;
pub mod optim;

pub use self::error::*;

/// Linear function approximator with scalar output.
pub type ScalarLFA<B, O> = LFA<B, ndarray::Array1<f64>, O>;

/// Linear function approximator with vector output.
pub type VectorLFA<B, O> = LFA<B, ndarray::Array2<f64>, O>;

/// Linear function approximator.
#[derive(Clone, Debug)]
#[cfg_attr(
    feature = "serde",
    derive(Serialize, Deserialize),
    serde(crate = "serde_crate")
)]
pub struct LFA<B, W, O = optim::SGD> {
    /// Basis representation used to generate feature vectors.
    pub basis: B,

    /// Weights collection.
    pub weights: W,

    /// Optimisation routine used during updates.
    pub optimiser: O,
}

impl<B, W, O> LFA<B, W, O> {
    /// Construct an arbitrary `LFA` using a given basis representation, initial
    /// collection of weights and optimisation routine.
    pub fn new(basis: B, weights: W, optimiser: O) -> Self {
        LFA {
            basis,
            weights,
            optimiser,
        }
    }
}

impl<B, O> ScalarLFA<B, O> {
    /// Construct an `ScalarLFA` using a given basis representation and
    /// optimisation routine. The weights are initialised with a vector of
    /// zeros.
    pub fn scalar(basis: B, optimiser: O) -> Self
    where B: spaces::Space {
        let n: usize = basis.dim().into();
        let weights = ndarray::Array1::zeros(n);

        LFA {
            basis,
            weights,
            optimiser,
        }
    }

    /// Evaluate the function approximator for a given `input`.
    pub fn evaluate<I>(&self, input: I) -> Result<f64>
    where B: basis::Basis<I, Value = Features> {
        self.basis
            .project(input)
            .map(|b| b.dot(&self.weights.view()))
    }

    /// Update the function approximator for a given `input` and `error`.
    pub fn update<I>(&mut self, input: I, error: f64) -> Result<()>
    where
        B: basis::Basis<I, Value = Features>,
        O: optim::Optimiser,
    {
        self.basis.project(input).and_then(|ref b| {
            self.optimiser
                .step_scaled(&mut self.weights.view_mut(), b, error)
        })
    }

    /// Update the function approximator for a given `input`, deferring the
    /// error computation to a closure provided by the user.
    pub fn update_with<I>(&mut self, input: I, f: impl Fn(&Features, f64) -> f64) -> Result<()>
    where
        B: basis::Basis<I, Value = Features>,
        O: optim::Optimiser,
    {
        self.basis
            .project(input)
            .map(|b| (b.dot(&self.weights.view()), b))
            .map(|(v, b)| (f(&b, v), b))
            .and_then(|(e, b)| {
                self.optimiser
                    .step_scaled(&mut self.weights.view_mut(), &b, e)
            })
    }
}

/// An iterator over the evaluated columns of a weight matrix.
pub struct OutputIter<'a> {
    basis: Features,
    lanes: ndarray::iter::LanesIter<'a, f64, ndarray::Ix1>,
}

impl<'a> Iterator for OutputIter<'a> {
    type Item = f64;

    fn next(&mut self) -> Option<Self::Item> { self.lanes.next().map(|ref c| self.basis.dot(c)) }

    fn size_hint(&self) -> (usize, Option<usize>) { self.lanes.size_hint() }
}

impl<'a> ExactSizeIterator for OutputIter<'a> {
    fn len(&self) -> usize { self.lanes.len() }
}

impl<B, O> VectorLFA<B, O> {
    /// Construct an `VectorLFA` with a chosen number of outputs using a given
    /// basis representation and optimisation routine. The weights are
    /// initialised with a matrix of zeros.
    pub fn vector(basis: B, optimiser: O, n_outputs: usize) -> Self
    where B: spaces::Space {
        let n: usize = basis.dim().into();
        let weights = ndarray::Array2::zeros((n, n_outputs));

        LFA {
            basis,
            weights,
            optimiser,
        }
    }

    /// Return the dimensionality of the output..
    pub fn n_outputs(&self) -> usize { self.weights.ncols() }

    /// Evaluate the function approximator for a given `input`.
    pub fn evaluate<I>(&self, input: I) -> Result<ndarray::Array1<f64>>
    where B: basis::Basis<I, Value = Features> {
        self.try_iter(input).map(|it| it.collect())
    }

    /// Evaluate the `i`th output of the function approximator for a given
    /// `input`.
    pub fn evaluate_index<I>(&self, input: I, index: usize) -> Result<f64>
    where B: basis::Basis<I, Value = Features> {
        self.basis
            .project(input)
            .map(|b| b.dot(&self.weights.column(index)))
    }

    /// Iterate sequentially over the outputs of the function approximator for a
    /// given `input`.
    ///
    /// __Panics__ if the basis computation fails.
    pub fn iter<'a, I>(&'a self, input: I) -> OutputIter<'a>
    where B: basis::Basis<I, Value = Features> {
        OutputIter {
            basis: self.basis.project(input).unwrap(),
            lanes: self.weights.gencolumns().into_iter(),
        }
    }

    /// Iterate sequentially over the outputs of the function approximator for a
    /// given `input`.
    pub fn try_iter<'a, I>(&'a self, input: I) -> Result<OutputIter<'a>>
    where B: basis::Basis<I, Value = Features> {
        self.basis.project(input).map(move |basis| OutputIter {
            basis,
            lanes: self.weights.gencolumns().into_iter(),
        })
    }

    /// Update the function approximator for a given `input` and `error`.
    pub fn update<I, E>(&mut self, input: I, errors: E) -> Result<()>
    where
        B: basis::Basis<I, Value = Features>,
        O: optim::Optimiser,
        E: IntoIterator<Item = f64>,
    {
        self.basis.project(input).and_then(|ref b| {
            let opt = &mut self.optimiser;

            errors
                .into_iter()
                .zip(self.weights.gencolumns_mut().into_iter())
                .fold(Ok(()), |acc, (e, mut c)| {
                    acc.and(opt.step_scaled(&mut c, b, e))
                })
        })
    }

    /// Update the `i`th output of the function approximator for a given `input`
    /// and `error`.
    pub fn update_index<I>(&mut self, input: I, index: usize, error: f64) -> Result<()>
    where
        B: basis::Basis<I, Value = Features>,
        O: optim::Optimiser,
    {
        self.basis.project(input).and_then(|ref b| {
            self.optimiser
                .step_scaled(&mut self.weights.column_mut(index), b, error)
        })
    }

    /// Update the function approximator for a given `input`, deferring the
    /// error computation to a closure provided by the user.
    pub fn update_with<I>(
        &mut self,
        input: I,
        f: impl Fn(&Features, ndarray::Array1<f64>) -> ndarray::Array1<f64>,
    ) -> Result<()>
    where
        B: basis::Basis<I, Value = Features>,
        O: optim::Optimiser,
    {
        self.basis.project(input).and_then(|ref b| {
            let opt = &mut self.optimiser;
            let values = b.matmul(&self.weights);
            let errors = f(b, values).into_raw_vec();

            errors
                .into_iter()
                .zip(self.weights.gencolumns_mut().into_iter())
                .fold(Ok(()), |acc, (e, mut c)| {
                    acc.and(opt.step_scaled(&mut c, b, e))
                })
        })
    }
}