[][src]Enum lfa::Features

pub enum Features {
    Dense(DenseActivations),
    Sparse(SparseActivations),
}

Sparse/dense feature vector representation.

Note: many of the methods associated with Features are based on those of the ArrayBase type provided in the ndarray crate.

Variants

Dense, floating-point activation vector.

Sparse, index-based activation vector.

Implementations

impl Features[src]

pub fn dense<I>(da: I) -> Features where
    I: IntoIterator<Item = ActivationT>, 
[src]

Construct a Dense feature vector from an iterable collection of activations.

pub fn sparse<I>(dim: usize, activations: I) -> Features where
    I: IntoIterator<Item = (IndexT, ActivationT)>, 
[src]

Construct a Sparse feature vector with given dimensionality from an iterable collection of feature-activation pairs.

pub fn unitary<I>(dim: usize, indices: I) -> Features where
    I: IntoIterator<Item = IndexT>, 
[src]

Construct a Sparse feature vector with given dimensionality from an iterable collection of feature indices.

pub fn is_dense(&self) -> bool[src]

Return true if the features is the Dense variant.

pub fn is_sparse(&self) -> bool[src]

Return true if the features is the Sparse variant.

pub fn n_features(&self) -> usize[src]

Return the number of features.

pub fn n_active(&self) -> usize[src]

Return the number of active features.

pub fn get(&self, idx: usize) -> Option<&f64>[src]

Return the activation of the feature at index idx, if defined.

Panics if the index exceeds the size of the feature vector.

pub unsafe fn uget(&self, idx: usize) -> Option<&f64>[src]

Return the activation of the feature at index idx without bounds checking.

pub fn remove(&mut self, idx: usize)[src]

Remove one feature entry from the features, if present.

For the Features::Dense variant, the feature is set to zero, and for the Features::Sparse variant, the feature index is removed entirely.

use lfa::Features;

let mut dense = Features::dense(vec![0.0, 0.2, 0.4, 0.4]);
let mut sparse = Features::unitary(16, vec![0, 10, 15]);

dense.remove(1);
sparse.remove(10);

assert_eq!(dense, vec![0.0, 0.0, 0.4, 0.4].into());
assert_eq!(sparse, vec![0, 15].into());

pub fn to_dense(&self) -> DenseActivations[src]

Clone the features and convert into a raw, dense vector of activations.

pub fn into_dense(self) -> DenseActivations[src]

Expand the features directly into a raw, dense vector of activations.

use lfa::Features;
use ndarray::Array1;

let phi = Features::unitary(5, vec![0, 2, 1, 4]);

assert_eq!(phi.into_dense(), Array1::from(vec![1.0, 1.0, 1.0, 0.0, 1.0]));

pub fn stack(self, other: Features) -> Features[src]

Stack two feature vectors together, maintaining sparsity if possible.

use lfa::Features;

assert_eq!(
    Features::stack(vec![0.0, 1.0].into(), vec![1.0, 0.0, 1.0].into()),
    vec![0.0, 1.0, 1.0, 0.0, 1.0].into()
);

pub fn map(&self, f: impl Fn(ActivationT) -> ActivationT) -> Features[src]

Map all feature activations using an operation, f, and return a new Features instance.

pub fn map_into(self, f: impl Fn(ActivationT) -> ActivationT) -> Features[src]

Map all feature activations using an operation, f, and return.

pub fn map_inplace(&mut self, f: impl Fn(ActivationT) -> ActivationT)[src]

Mutate all feature activations inplace using an operation, f.

pub fn map_dense<T>(self, f: impl FnOnce(DenseActivations) -> T) -> Option<T>[src]

Map the function f over the internal DenseActivations representation if self is the Dense variant, otherwise return None.

pub fn map_sparse<T>(self, f: impl FnOnce(SparseActivations) -> T) -> Option<T>[src]

Map the function f over the internal SparseActivations representation if self is the Dense variant, otherwise return None.

pub fn map_either<T>(
    self,
    f_dense: impl FnOnce(DenseActivations) -> T,
    f_sparse: impl FnOnce(SparseActivations) -> T
) -> T
[src]

Map the function f_dense or f_sparse based on the sparsity of the features.

pub fn merge(
    &self,
    other: &Features,
    f: impl Fn(ActivationT, ActivationT) -> ActivationT
) -> Features
[src]

Merge self with another feature vector and an operation, f, returning a new instance.

pub fn merge_into(
    self,
    other: &Features,
    f: impl Fn(ActivationT, ActivationT) -> ActivationT
) -> Features
[src]

Merge self with another feature vector using a given operation, f.

pub fn merge_inplace(
    &mut self,
    other: &Features,
    f: impl Fn(ActivationT, ActivationT) -> ActivationT
)
[src]

Merge self in-place with another feature vector using an operation, f.

pub fn fold(&self, init: f64, f: impl Fn(f64, &f64) -> f64) -> f64[src]

Perform a fold operation over the feature activations.

Note: for sparse features this method will ignore zeroes.

pub fn dot<W>(&self, weights: &W) -> f64 where
    W: Index<usize, Output = f64>,
    DenseActivations: Dot<W, Output = f64>, 
[src]

Apply the dot product operation between the Features and some other Array1, typically a set of weights.

use lfa::Features;
use ndarray::Array1;

let weights = Array1::from(vec![2.0, 5.0, 1.0]);

assert_eq!(Features::dot(&vec![0.0, 0.2, 0.8].into(), &weights.view()), 1.8);
assert_eq!(Features::dot(&vec![0, 1].into(), &weights.view()), 7.0);

pub fn matmul<S>(&self, weights: &ArrayBase<S, Ix2>) -> Array1<f64> where
    S: Data<Elem = f64>, 
[src]

Apply the matrix multiplication operation between the Features and a Matrix.

extern crate ndarray;

use lfa::Features;
use ndarray::{Array1, Array2};

let weights = Array2::from_shape_vec((3, 2), vec![2.0, 5.0, 1.0, 3.0, 1.0, 3.0]).unwrap();

assert!(
    Features::matmul(&vec![0.1, 0.2, 0.7].into(), &weights.view()).all_close(
        &Array1::from(vec![1.1, 3.2]),
        1e-7 // eps
    )
);
assert_eq!(
    Features::matmul(&vec![0, 1].into(), &weights.view()),
    Array1::from(vec![3.0, 8.0])
);

pub fn sum(&self) -> ActivationT[src]

Returns the sum of all activations in the feature vector.

pub fn addto<S, E>(&self, weights: &mut ArrayBase<S, E>) where
    S: DataMut<Elem = f64>,
    E: Dimension,
    usize: NdIndex<E>, 
[src]

Perform an elementwise add of activations to a weights vector.

pub fn scaled_addto<S, E>(
    &self,
    alpha: ActivationT,
    weights: &mut ArrayBase<S, E>
) where
    S: DataMut<Elem = f64>,
    E: Dimension,
    usize: NdIndex<E>, 
[src]

Perform an elementwise add of activations (scaled by alpha) to a weights vector.

Trait Implementations

impl AsRef<Features> for Features[src]

impl Clone for Features[src]

impl Debug for Features[src]

impl Dot<Features> for Features[src]

type Output = f64

The result of the operation. Read more

impl<W> Dot<W> for Features where
    W: Index<usize, Output = f64>,
    DenseActivations: Dot<W, Output = f64>, 
[src]

type Output = f64

The result of the operation. Read more

impl From<ArrayBase<OwnedRepr<f64>, Dim<[usize; 1]>>> for Features[src]

impl From<Features> for DenseActivations[src]

impl From<Features> for Vec<ActivationT>[src]

impl From<SparseActivations> for Features[src]

impl From<Vec<f64>> for Features[src]

impl From<Vec<usize>> for Features[src]

impl FromIterator<f64> for Features[src]

impl Index<usize> for Features[src]

type Output = f64

The returned type after indexing.

impl Optimiser<Features> for SGD[src]

impl Optimiser<Features> for ISGD[src]

impl Optimiser<Features> for SGDMomentum[src]

impl Optimiser<Features> for NAG[src]

impl Optimiser<Features> for Adam[src]

impl Optimiser<Features> for AdaMax[src]

impl Optimiser<Features> for Adagrad[src]

impl PartialEq<Features> for Features[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.