[−][src]Enum lfa::Features
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(DenseActivations)
Dense, floating-point activation vector.
Sparse(SparseActivations)
Sparse, index-based activation vector.
Implementations
impl Features
[src]
pub fn dense<I>(da: I) -> Features where
I: IntoIterator<Item = ActivationT>,
[src]
I: IntoIterator<Item = ActivationT>,
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]
I: IntoIterator<Item = (IndexT, ActivationT)>,
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]
I: IntoIterator<Item = IndexT>,
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]
self,
f_dense: impl FnOnce(DenseActivations) -> T,
f_sparse: impl FnOnce(SparseActivations) -> T
) -> T
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]
&self,
other: &Features,
f: impl Fn(ActivationT, ActivationT) -> ActivationT
) -> Features
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]
self,
other: &Features,
f: impl Fn(ActivationT, ActivationT) -> ActivationT
) -> Features
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]
&mut self,
other: &Features,
f: impl Fn(ActivationT, ActivationT) -> ActivationT
)
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]
W: Index<usize, Output = f64>,
DenseActivations: Dot<W, Output = f64>,
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]
S: Data<Elem = f64>,
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]
S: DataMut<Elem = f64>,
E: Dimension,
usize: NdIndex<E>,
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]
&self,
alpha: ActivationT,
weights: &mut ArrayBase<S, E>
) where
S: DataMut<Elem = f64>,
E: Dimension,
usize: NdIndex<E>,
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]
impl<W> Dot<W> for Features where
W: Index<usize, Output = f64>,
DenseActivations: Dot<W, Output = f64>,
[src]
W: Index<usize, Output = f64>,
DenseActivations: Dot<W, Output = f64>,
impl From<ArrayBase<OwnedRepr<f64>, Dim<[usize; 1]>>> for Features
[src]
fn from(activations: DenseActivations) -> Features
[src]
impl From<Features> for DenseActivations
[src]
fn from(phi: Features) -> DenseActivations
[src]
impl From<Features> for Vec<ActivationT>
[src]
fn from(phi: Features) -> Vec<ActivationT>
[src]
impl From<SparseActivations> for Features
[src]
fn from(sa: SparseActivations) -> Features
[src]
impl From<Vec<f64>> for Features
[src]
fn from(activations: Vec<ActivationT>) -> Features
[src]
impl From<Vec<usize>> for Features
[src]
impl FromIterator<f64> for Features
[src]
fn from_iter<I: IntoIterator<Item = ActivationT>>(iter: I) -> Self
[src]
impl Index<usize> for Features
[src]
impl Optimiser<Features> for SGD
[src]
fn step(
&mut self,
weights: &mut ArrayViewMut1<f64>,
features: &Features
) -> Result<()>
[src]
&mut self,
weights: &mut ArrayViewMut1<f64>,
features: &Features
) -> Result<()>
fn step_scaled(
&mut self,
weights: &mut ArrayViewMut1<f64>,
features: &Features,
scale_factor: f64
) -> Result<()>
[src]
&mut self,
weights: &mut ArrayViewMut1<f64>,
features: &Features,
scale_factor: f64
) -> Result<()>
fn reset(&mut self)
[src]
impl Optimiser<Features> for ISGD
[src]
fn step(
&mut self,
weights: &mut ArrayViewMut1<f64>,
features: &Features
) -> Result<()>
[src]
&mut self,
weights: &mut ArrayViewMut1<f64>,
features: &Features
) -> Result<()>
fn step_scaled(
&mut self,
weights: &mut ArrayViewMut1<f64>,
features: &Features,
scale_factor: f64
) -> Result<()>
[src]
&mut self,
weights: &mut ArrayViewMut1<f64>,
features: &Features,
scale_factor: f64
) -> Result<()>
fn reset(&mut self)
[src]
impl Optimiser<Features> for SGDMomentum
[src]
fn step_scaled(
&mut self,
weights: &mut ArrayViewMut1<f64>,
features: &Features,
scale_factor: f64
) -> Result<()>
[src]
&mut self,
weights: &mut ArrayViewMut1<f64>,
features: &Features,
scale_factor: f64
) -> Result<()>
fn reset(&mut self)
[src]
fn step(&mut self, weights: &mut ArrayViewMut1<f64>, grad: &G) -> Result<()>
[src]
impl Optimiser<Features> for NAG
[src]
fn step_scaled(
&mut self,
weights: &mut ArrayViewMut1<f64>,
features: &Features,
scale_factor: f64
) -> Result<()>
[src]
&mut self,
weights: &mut ArrayViewMut1<f64>,
features: &Features,
scale_factor: f64
) -> Result<()>
fn reset(&mut self)
[src]
fn step(&mut self, weights: &mut ArrayViewMut1<f64>, grad: &G) -> Result<()>
[src]
impl Optimiser<Features> for Adam
[src]
fn step_scaled(
&mut self,
weights: &mut ArrayViewMut1<f64>,
features: &Features,
scale_factor: f64
) -> Result<()>
[src]
&mut self,
weights: &mut ArrayViewMut1<f64>,
features: &Features,
scale_factor: f64
) -> Result<()>
fn reset(&mut self)
[src]
fn step(&mut self, weights: &mut ArrayViewMut1<f64>, grad: &G) -> Result<()>
[src]
impl Optimiser<Features> for AdaMax
[src]
fn step_scaled(
&mut self,
weights: &mut ArrayViewMut1<f64>,
features: &Features,
scale_factor: f64
) -> Result<()>
[src]
&mut self,
weights: &mut ArrayViewMut1<f64>,
features: &Features,
scale_factor: f64
) -> Result<()>
fn reset(&mut self)
[src]
fn step(&mut self, weights: &mut ArrayViewMut1<f64>, grad: &G) -> Result<()>
[src]
impl Optimiser<Features> for Adagrad
[src]
fn step_scaled(
&mut self,
weights: &mut ArrayViewMut1<f64>,
features: &Features,
scale_factor: f64
) -> Result<()>
[src]
&mut self,
weights: &mut ArrayViewMut1<f64>,
features: &Features,
scale_factor: f64
) -> Result<()>
fn reset(&mut self)
[src]
fn step(&mut self, weights: &mut ArrayViewMut1<f64>, grad: &G) -> Result<()>
[src]
impl PartialEq<Features> for Features
[src]
Auto Trait Implementations
impl RefUnwindSafe for Features
impl Send for Features
impl Sync for Features
impl Unpin for Features
impl UnwindSafe for Features
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,