Expand description

The struct definitions for all TensorXD, Tensor trait, and more.

At a high level a tensor consists of only three parts

  1. A crate::unique_id::UniqueId to track which gradients are associated with what tensors
  2. An Nd rust array stored in a Box.
  3. A tape, which can either actually be a tape (crate::gradients::OwnedTape) or be empty (crate::gradients::NoneTape).

Creating tensors

  1. With raw rust arrays use the TensorCreator::new() method.
let t = Tensor1D::new([1.0, 2.0, 3.0]);
  1. Filled with 0s or 1s use TensorCreator::zeros() and TensorCreator::ones().
let t: Tensor1D<5> = Tensor1D::zeros();
let q: Tensor2D<3, 2> = Tensor2D::ones();
  1. Filled with random data use TensorCreator::rand() and TensorCreator::randn().
let mut rng = StdRng::seed_from_u64(0);
let a = Tensor1D::<3>::rand(&mut rng); // uniform random data
let b = Tensor2D::<4, 3>::randn(&mut rng); // gaussian random data

Accessing or modifying underlying data

Use HasArrayData::data() and HasArrayData::mut_data() to view or modify the underlying arrays.

let mut t = Tensor1D::<3>::zeros();
assert_eq!(t.data(), &[0.0, 0.0, 0.0]);
t.mut_data()[1] = 2.0;
assert_eq!(t.data(), &[0.0, 2.0, 0.0]);

Tracking gradients

Use the trace() or traced() methods to add crate::gradients::OwnedTape to the Tensor. .trace() will clone the crate::unique_id::UniqueId & data, while .traced() will take ownership of the tensor and return a version with an crate::gradients::OwnedTape.

Note that these two methods are only present for tensors without a tape already.

let t: Tensor1D<5, NoneTape> = Tensor1D::<5>::zeros();
let t_clone: Tensor1D<5, OwnedTape> = t.trace(); // copies t
let t: Tensor1D<5, OwnedTape> = t.traced(); // takes ownership of t

Cloning/copying

There are two primary methods for copying a tensor

  1. Clone is implemented for tensors without a tape. NOTE that the unique id is modified when a tensor is cloned
  2. Tensor::duplicate() is implemented for all tensors, it copies the crate::unique_id::UniqueId, and returns a tensor with no tape.

Structs

A fake tensor that holds a UniqueId and a type T that is HasArrayType. This is created and stored in GradientTape operations to access gradient data for a tensor that the GradientTape doesn’t have ownership of.

A 0d super::Tensor with shape (). Backed by data f32.

A 1d super::Tensor with shape (M, ). Backed by data [f32; M].

A 2d super::Tensor with shape (M, N). Backed by data [[f32; N]; M].

A 3d super::Tensor with shape (M, N, O). Backed by data [[[f32; O]; N]; M].

A 4d super::Tensor with shape (M, N, O, P). Backed by data [[[[f32; P]; O]; N]; M].

Traits

Something that has HasArrayType, and also can return a reference to or mutate Self::Array.

Something that can be turned into a PhantomTensor

Something that has parameters that can be randomized from a generic distribution.

The main tensor trait. A tensor consists of mainly 1. an array, 2. a device, 3. a unique id.

Something that can be created - currently only implemented for tensors with no tapes.

Functions

Transforms a NoneTape tensor to an OwnedTape tensor by cloning. Clones t using Tensor::duplicate() (to preserve id), and then inserts OwnedTape as the tape.

Transforms a NoneTape tensor to an OwnedTape by directly inserting a new OwnedTape into t.