[][src]Crate lfa

lfa is a framework for online learning of linear function approximation models. Included is a suite of scalar and multi-output approximator helpers, a range of basis functions for feature construction, and a set of optimisation routines. The framework is also designed to support fallible operations for which a custom error type is provided.

Conventions

lfa adopts a few conventions to keep in mind:

  • Weights are restricted to 2-dimensional tensors based on the ndarray crate.
  • Rows are associated with features.
  • Columns are associated with outputs.

Example

The code below illustrates how one might use lfa to fit a simple linear model with SGD.

extern crate lfa;
extern crate rand;
extern crate rand_distr;

use lfa::{
    Approximator, Parameterised, ScalarFunction,
    basis::{Projector, Polynomial},
    optim::SGD,
};
use rand::{Rng, thread_rng};
use rand_distr::{Uniform, Normal};

fn main() {
    const M: f64 = 1.0;
    const C: f64 = -0.5;

    let basis = Polynomial::new(1, 1).with_constant();

    let mut fa = ScalarFunction::zeros(basis.n_features());
    let mut opt = SGD(0.01);

    let mut rng = thread_rng();
    let mut data = Uniform::new_inclusive(-1.0, 1.0);
    let mut noise = Normal::new(0.0, 0.01).unwrap();

    for x in rng.sample_iter(&data).take(10000) {
        let y_exp = M*x + C;
        let y_noisy = y_exp + rng.sample(noise);

        let x = basis.project(&vec![x]).unwrap();
        let y_apx = fa.evaluate(&x).unwrap();

        fa.update(&mut opt, &x, y_noisy - y_apx).ok();
    }

    let weights = fa.weights().column(0).to_owned();
    let rmse = (
        (weights[0] - M).powi(2) +
        (weights[1] - C).powi(2)
    ).sqrt();

    assert!(rmse < 1e-3);
}

Modules

basis

Basis function representations used to generate Features.

error

LFA error and result types.

optim

SGD-based weight optimisers.

Structs

PairFunction

Weights-Features evaluator with [f64; 2] output.

ScalarFunction

Weights-Features evaluator with f64 output.

TripleFunction

Weights-Features evaluator with [f64; 3] output.

VectorFunction

Weights-Features evaluator with Vec<f64> output.

Enums

Features

Sparse/dense feature vector representation.

Traits

Approximator

Function approximators based on Features representations.

PairApproximator

Approximators with an [f64; 2] output.

Parameterised

Types that are parameterised by a matrix of weights.

ScalarApproximator

Approximators with an f64 output.

TripleApproximator

Approximators with an [f64; 3] output.

VectorApproximator

Approximators with an Vec<f64> output.

Type Definitions

ActivationT

Internal type used to represent a feature's activation.

DenseT

Internal type used to represent a dense feature vector.

IndexT

Internal type used to represent a feature's index.

SparseT

Internal type used to represent a sparse feature vector.

Weights

Matrix populated with owned weights.

WeightsView

Matrix populated with referenced weights.

WeightsViewMut

Matrix populated with mutably referenced weights.