Expand description
The struct definitions for all TensorXD, Tensor trait, and more.
At a high level a tensor consists of only three parts
- A crate::unique_id::UniqueId to track which gradients are associated with what tensors
- An Nd rust array stored in a std::sync::Arc.
- A tape, which can either actually be a tape (crate::gradients::OwnedTape) or be empty (crate::gradients::NoneTape).
Creating tensors
Use the tensor function
See tensor().
let t = tensor([1.0, 2.0, 3.0]);
Use the TensorCreator trait
See TensorCreator.
- With raw rust arrays use the TensorCreator::new() method.
let t: Tensor1D<3> = TensorCreator::new([1.0, 2.0, 3.0]);
- Filled with 0s or 1s use TensorCreator::zeros() and TensorCreator::ones().
let t: Tensor1D<5> = TensorCreator::zeros();
let q: Tensor2D<3, 2> = TensorCreator::ones();
- 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 crate::arrays::HasArrayData::data() and crate::arrays::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
Structs
A fake tensor that holds a UniqueId and a type
T
that is HasArrayType.
This is created and stored in gradient operations to access gradient data
for a tensor that the operation 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 can be turned into a PhantomTensor
Enables converting this value into a Tensor. See tensor().
Changes the kind of tape inside a tensor.
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
Creates a tensor using the data based in. The return type is based
on the data you pass in. See IntoTensor for implementations.