Struct Array

Source
pub struct Array { /* private fields */ }
Expand description

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§

Source§

impl Array

Source

pub fn reciprocal(&self) -> Array

Computes the reciprocal of each value in the array.

Source

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

Raises the array to the specified exponent.

Source

pub fn ln(&self) -> Array

Computes the natural logarithm of all values of the array.

Source

pub fn exp(&self) -> Array

Computes the exponential of all values of the array.

Source

pub fn sum(&self, dimension_count: usize) -> Array

Sums along the last dimension_count dimensions.

Source

pub fn sum_all(&self) -> Float

Sums all the values of the array.

Source§

impl Array

Source

pub fn conv(&self, filters: &Array, stride_dimensions: (usize, usize)) -> Array

Computes the image convolution of the array with the filter.

Source§

impl Array

Source

pub fn reshape(&self, dimensions: Vec<usize>) -> Array

Reshapes the array into different dimensions

Source

pub fn axpy(alpha: Float, x: &Array, y: &Array) -> Array

Computes the element-wise alpha * x + y, for each matching dimension not multiplied.

Source

pub fn matmul(a: (&Array, bool), b: (&Array, bool), c: Option<&Array>) -> Array

Computes 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). c - The output matrix, if not initialized to a zeros matrix.

Source§

impl Array

Source

pub fn relu(&self) -> Array

Computes the ReLU of the array, defined as max(0, x) for all elements x in the array.

Source

pub fn sigmoid(&self) -> Array

Computes the sigmoid operation on each value of the array.

Source

pub fn softmax(&self) -> Array

Computes the softmax of the array.

Source§

impl Array

Source

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

Returns a copy of the dimensions of the array.

Source

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

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

Source

pub fn gradient(&self) -> Ref<'_, Option<Array>>

Returns a reference to the gradient option of the array.

Source

pub fn gradient_mut(&self) -> RefMut<'_, Option<Array>>

Returns a mutable reference to the gradient option of the array.

Source

pub fn replace_gradient(&self) -> Option<Array>

Returns the owned gradient option of the array, replacing it with nothing.

Source

pub fn tracked(self) -> Array

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().to_owned().unwrap(), arr![1.0, 2.0, 3.0]);
Source

pub fn start_tracking(&self) -> bool

Starts tracking operations for a mutable reference to an array, returning the previous value.

Source

pub fn untracked(self) -> Array

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().to_owned().unwrap(), arr![1.0, 2.0, 3.0]);
Source

pub fn stop_tracking(&self) -> bool

Stops tracking operations for a mutable reference to an array, returning the previous value. Useful for temporarily updating parameters without requiring their gradients.

Source

pub fn backward(&self, delta: Option<Array>)

Computes 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).

Source

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

Computes 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 mul: ForwardOp = Rc::new(|x: &[&Array]| {
    Array::from((x[0].dimensions().to_vec(), x[0].values().iter().zip(x[1].values()).map(|(x, y)| x * y).collect::<Vec<Float>>()))
});

let mul_clone = Rc::clone(&mul);
let backward_op: BackwardOp = Rc::new(move |children, is_tracked, delta| {
    vec![
        if is_tracked[0] {
            Some(Array::op(&[&children[1], delta], Rc::clone(&mul_clone), None))
        } else {
            None
        },
        if is_tracked[1] {
            Some(Array::op(&[&children[0], delta], Rc::clone(&mul_clone), None))
        } else {
            None
        }
    ]
});

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

Trait Implementations§

Source§

impl AbsDiffEq for Array

Source§

type Epsilon = <f64 as AbsDiffEq>::Epsilon

Used for specifying relative comparisons.
Source§

fn default_epsilon() -> <Float as AbsDiffEq>::Epsilon

The default tolerance to use when testing values that are close together. Read more
Source§

fn abs_diff_eq( &self, other: &Array, epsilon: <Float as AbsDiffEq>::Epsilon, ) -> bool

A test for equality that uses the absolute difference to compute the approximate equality of two numbers.
Source§

fn abs_diff_ne(&self, other: &Rhs, epsilon: Self::Epsilon) -> bool

The inverse of AbsDiffEq::abs_diff_eq.
Source§

impl Add<&Array> for &Array

Source§

type Output = Array

The resulting type after applying the + operator.
Source§

fn add(self, other: &Array) -> Self::Output

Performs the + operation. Read more
Source§

impl Clone for Array

Source§

fn clone(&self) -> Array

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Array

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Div<&Array> for &Array

Source§

type Output = Array

The resulting type after applying the / operator.
Source§

fn div(self, other: &Array) -> Self::Output

Performs the / operation. Read more
Source§

impl From<(Vec<usize>, Rc<Vec<f64>>)> for Array

Source§

fn from(items: (Vec<usize>, Rc<Vec<Float>>)) -> Self

Converts to this type from the input type.
Source§

impl From<(Vec<usize>, Vec<f64>)> for Array

Implementation to construct Array structs by using Vec<usize> as the dimensions, and Vec<Float> as the values.

§Examples

let a = Array::from((vec![2, 3], vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]));
assert_eq!(a[vec![1, 2]], 6.0);
Source§

fn from(items: (Vec<usize>, Vec<Float>)) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<Array>> for Array

Implementation to construct Array structs by flattening other contained Array structs, and keeping their dimensions.

§Examples

let a = Array::from(vec![arr![1.0, 2.0, 3.0], arr![4.0, 5.0, 6.0]]);
assert_eq!(a[vec![1, 2]], 6.0);
Source§

fn from(contents: Vec<Array>) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<f64>> for Array

Implementation to construct Array structs by using Vec<Float> as the values, and by keeping flat dimensions.

§Examples

let a = Array::from(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
assert_eq!(a[vec![5]], 6.0);
Source§

fn from(values: Vec<Float>) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<usize>> for Array

Implementation to construct Array structs by using Vec<usize> as the dimensions, and filling values with zeros.

§Examples

let a = Array::from(vec![3, 2, 3]);
assert_eq!(a[vec![2, 1, 1]], 0.0);
Source§

fn from(dimensions: Vec<usize>) -> Self

Converts to this type from the input type.
Source§

impl Index<Vec<usize>> for Array

Source§

type Output = f64

The returned type after indexing.
Source§

fn index(&self, indices: Vec<usize>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl Index<usize> for Array

Source§

type Output = f64

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl Into<Vec<f64>> for Array

Source§

fn into(self) -> Vec<Float>

Converts this type into the (usually inferred) input type.
Source§

impl Mul<&Array> for &Array

Source§

type Output = Array

The resulting type after applying the * operator.
Source§

fn mul(self, other: &Array) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<&Array> for Float

Source§

type Output = Array

The resulting type after applying the * operator.
Source§

fn mul(self, other: &Array) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<f64> for &Array

Source§

type Output = Array

The resulting type after applying the * operator.
Source§

fn mul(self, other: Float) -> Self::Output

Performs the * operation. Read more
Source§

impl Neg for &Array

Source§

type Output = Array

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl PartialEq for Array

Source§

fn eq(&self, other: &Array) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl RelativeEq for Array

Source§

fn default_max_relative() -> <Float as AbsDiffEq>::Epsilon

The default relative tolerance for testing values that are far-apart. Read more
Source§

fn relative_eq( &self, other: &Array, epsilon: <Float as AbsDiffEq>::Epsilon, max_relative: <Float as AbsDiffEq>::Epsilon, ) -> bool

A test for equality that uses a relative comparison if the values are far apart.
Source§

fn relative_ne( &self, other: &Rhs, epsilon: Self::Epsilon, max_relative: Self::Epsilon, ) -> bool

The inverse of RelativeEq::relative_eq.
Source§

impl Sub<&Array> for &Array

Source§

type Output = Array

The resulting type after applying the - operator.
Source§

fn sub(self, other: &Array) -> Self::Output

Performs the - operation. Read more

Auto Trait Implementations§

§

impl !Freeze for Array

§

impl !RefUnwindSafe for Array

§

impl !Send for Array

§

impl !Sync for Array

§

impl Unpin for Array

§

impl !UnwindSafe for Array

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V