Struct corgi::array::Array[][src]

pub struct Array { /* fields omitted */ }

An n-dimensional differentiable array. Stored in row-major order.

Examples


use corgi::array::*;

let a = arr![arr![1.0, 2.0, 3.0], arr![4.0, 5.0, 6.0]];
let b = arr![arr![3.0, 2.0, 1.0], arr![6.0, 5.0, 4.0]];

let mut p = &a * &b;
p.backward(None);

Implementations

impl Array[src]

pub fn dimensions(&self) -> Vec<usize>[src]

Returns a copy of the dimensions of the array.

pub fn values(&self) -> &Vec<Float>[src]

Returns an immutable reference to the values of the array in row-major order.

pub fn gradient(&self) -> Option<Array>[src]

Returns a copy of the gradient of the array.

Panics

Panics if unable to obtain a lock on the Mutex.

pub fn gradient_mut(&mut self) -> MutexGuard<'_, Option<Array>>[src]

Returns a guard for the gradient option of the array.

Panics

Panics if unable to obtain a lock on the Mutex.

pub fn tracked(self) -> Array[src]

Enables tracking of operations for the backward pass, meaning the backward pass will compute, and store gradients for the current array, and any children arrays which are tracked.

An operation with any positive number of tracked children will always output a tracked array.

This does not persist through threads, or through being set on a clone, meaning any tracked clones will not affect tracking of the array, apart from clones of the clone.

Examples

// only the gradient for `b`, will be stored
let mut a = arr![1.0, 2.0, 3.0].untracked();
let b = arr![3.0, 2.0, 1.0].tracked();
let mut c = &a * &b;
c.backward(None);
assert_eq!(b.gradient().unwrap(), arr![1.0, 2.0, 3.0]);

pub fn start_tracking(&mut self)[src]

Starts tracking operations for a mutable reference to an array.

pub fn untracked(self) -> Array[src]

Prevents tracking of operations for the backward pass, meaning the backward pass will skip computation of gradients for the current array, and any children arrays.

Any operation with every child untracked will always output an untracked array, and will not store any subgraph information.

This does not persist through threads, or through being set on a clone, meaning any tracked clones will not affect tracking of the array, apart from clones of the clone.

Examples

// only the gradient for `b`, will be stored
let mut a = arr![1.0, 2.0, 3.0].untracked();
let b = arr![3.0, 2.0, 1.0].tracked();
let mut c = &a * &b;
c.backward(None);
assert_eq!(b.gradient().unwrap(), arr![1.0, 2.0, 3.0]);

pub fn stop_tracking(&mut self)[src]

Stops tracking operations for a mutable reference to an array. Useful for temporarily updating parameters without requiring their gradients.

pub fn matmul(a: (&Array, bool), b: (&Array, bool)) -> Array[src]

Performs matrix multiplications on two arrays, for each matching dimension not multiplied.

Arguments

  • a - The LHS matrix, and whether to transpose it: (a, a_transpose).
  • b - The RHS matrix, and whether to transpose it: (b, b_transpose).

pub fn op(
    arrays: &[&Array],
    op: ForwardOp,
    backward_op: Option<BackwardOp>
) -> Array
[src]

Performs an operation on arrays.

Arguments

  • arrays - The arrays to perform the operations on.
  • op - The ForwardOp, which takes in the arrays, and outputs another Array.
  • backward_op - The BackwardOp, which takes in the arrays, and the delta, and outputs a new delta, with respect to each input. It is recommended that any array operations here are untracked, unless interested in higher order derivatives.

Examples

let op: ForwardOp = Arc::new(|x: &[&Array]| {
    Arrays::new((x[0].dimensions(), x[0].values().iter().zip(x[1].values()).map(|(x, y)| x * y).collect::<Vec<Float>>()))
});

let op_clone = Arc::clone(&op);
let backward_op: BackwardOp = Arc::new(move |c: &mut Vec<Array>, x: &Array| {
    vec![
        Some(Array::op(&[&c[1], x], Arc::clone(&op_clone), None)),
        Some(Array::op(&[&c[0], x], Arc::clone(&op_clone), None)),
    ]
});

let a = arr![1.0, 2.0, 3.0];
let b = arr![3.0, 2.0, 1.0];

let mut product = Array::op(&vec![&a, &b], op, Some(backward_op));
assert_eq!(product, arr![3.0, 4.0, 3.0]);
product.backward(None);
assert_eq!(product.gradient().unwrap(), arr![1.0, 1.0, 1.0]);
assert_eq!(b.gradient().unwrap(), arr![1.0, 2.0, 3.0]);
assert_eq!(a.gradient().unwrap(), arr![3.0, 2.0, 1.0]);

pub fn powf(&self, exponent: Float) -> Array[src]

Raises the array to the specified exponent.

pub fn sigmoid(&self) -> Array[src]

Performs the sigmoid operation on each value of the array.

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

Sums the values of the array.

pub fn backward(&mut self, delta: Option<Array>)[src]

Performs the backward pass, computing gradients for all descendants, and propagating consumer counts if requested.

Panics

Panics if the current node has children, but is not a differentiable function (is not a leaf).

Trait Implementations

impl<'a, 'b> Add<&'b Array> for &'a Array[src]

type Output = Array

The resulting type after applying the + operator.

impl Clone for Array[src]

impl Debug for Array[src]

impl Drop for Array[src]

impl Index<Vec<usize, Global>> for Array[src]

type Output = Float

The returned type after indexing.

impl Index<usize> for Array[src]

type Output = Float

The returned type after indexing.

impl Mul<&'_ Array> for Float[src]

type Output = Array

The resulting type after applying the * operator.

impl<'a, 'b> Mul<&'b Array> for &'a Array[src]

type Output = Array

The resulting type after applying the * operator.

impl<'a> Mul<f64> for &'a Array[src]

type Output = Array

The resulting type after applying the * operator.

impl<'a> Neg for &'a Array[src]

type Output = Array

The resulting type after applying the - operator.

impl PartialEq<Array> for Array[src]

impl<'a, 'b> Sub<&'b Array> for &'a Array[src]

type Output = Array

The resulting type after applying the - operator.

Auto Trait Implementations

impl !RefUnwindSafe for Array

impl Send for Array

impl Sync for Array

impl Unpin for Array

impl !UnwindSafe for Array

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.