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

pub struct Array { /* fields omitted */ }
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

Computes the reciprocal of each value in the array.

Raises the array to the specified exponent.

Computes the natural logarithm of all values of the array.

Computes the exponential of all values of the array.

Sums along the last dimension_count dimensions.

Sums all the values of the array.

Computes the image convolution of the array with the filter.

Reshapes the array into different dimensions

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

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.

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

Computes the sigmoid operation on each value of the array.

Computes the softmax of the array.

Returns a copy of the dimensions of the array.

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

Returns a reference to the gradient option of the array.

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

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

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]);

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

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]);

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

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

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

Used for specifying relative comparisons.

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

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

The inverse of AbsDiffEq::abs_diff_eq.

The resulting type after applying the + operator.

Performs the + operation. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

Performs the conversion.

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);

Performs the conversion.

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);

Performs the conversion.

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);

Performs the conversion.

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);

Performs the conversion.

The returned type after indexing.

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

The returned type after indexing.

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

Performs the conversion.

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the - operator.

Performs the unary - operation. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

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

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

The inverse of RelativeEq::relative_eq.

The resulting type after applying the - operator.

Performs the - operation. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.