14 releases (breaking)
new 0.14.1 | Nov 16, 2024 |
---|---|
0.13.1 | Aug 30, 2024 |
0.12.0 | Jul 30, 2024 |
0.6.0 | Mar 31, 2024 |
0.1.0 | Dec 31, 2023 |
#530 in Machine learning
1,903 downloads per month
Used in 10 crates
(7 directly)
350KB
8K
SLoC
rten-tensor
rten-tensor is the foundational library that provides multi-dimensional arrays used by RTen. It is similar to ndarray but tailored for use in the RTen library.
lib.rs
:
rten_tensor provides multi-dimensional arrays, commonly referred to as tensors in a machine learning context.
Each tensor is a combination of data and a layout. The data can be owned,
borrowed or mutably borrowed. This is analagous to Vec<T>
, &[T]
and
&mut [T]
for 1D arrays. The layout determines the number of dimensions
(the rank), the size of each dimension, and the strides (gap between
successive indices along a given dimension).
Key types and traits
The base type for all tensors is [TensorBase]. This is not normally used directly but instead via a type alias, depending on whether the number of dimensions (the rank) of the tensor is known at compile time or only at runtime, as well as whether the tensor owns, borrows or mutably borrows its data.
Rank | Owned (like Vec<T> ) |
Borrowed (like &[T] ) |
Mutably borrowed |
---|---|---|---|
Static | [NdTensor] | [NdTensorView] | [NdTensorViewMut] |
Dynamic | [Tensor] | [TensorView] | [TensorViewMut] |
All tensors implement the [Layout] trait, which provide methods to query
the shape, dimension count and strides of the tensor. Tensor views provide
various methods for indexing, iterating, slicing and transforming them.
The [AsView] trait provides access to these methods for owned and mutably
borrowed tensors. Conceptually it is similar to how Deref
allows accesing methods for &[T]
on a Vec<T>
. The preferred way to
import the traits is via the prelude:
use rten_tensor::prelude::*;
use rten_tensor::NdTensor;
let tensor = NdTensor::from([[1, 2], [3, 4]]);
let transposed_elems: Vec<_> = tensor.transposed().iter().copied().collect();
assert_eq!(transposed_elems, [1, 3, 2, 4]);
Serialization
Tensors can be serialized and deserialized using serde
if the serde
feature is enabled. The serialized representation of a
tensor includes its shape and elements in row-major (C) order. The JSON
serialization of a matrix (NdTensor<f32, 2>
) looks like this for example:
{
"shape": [2, 2],
"data": [0.5, 1.0, 1.5, 2.0]
}
Dependencies
~230KB