pub struct NamedTensor<T: Scalar> { /* private fields */ }Expand description
A tensor with optional names attached to each dimension.
Unnamed dimensions use None. Named dimensions must be unique within
a single tensor.
§Examples
let t = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], vec![2, 3]).unwrap();
let nt = NamedTensor::new(t, vec![Some("batch".into()), Some("feature".into())]).unwrap();
assert_eq!(nt.dim_index("batch").unwrap(), 0);
assert_eq!(nt.dim_index("feature").unwrap(), 1);Implementations§
Source§impl<T: Scalar> NamedTensor<T>
impl<T: Scalar> NamedTensor<T>
Sourcepub fn new(tensor: Tensor<T>, names: Vec<Option<String>>) -> Result<Self>
pub fn new(tensor: Tensor<T>, names: Vec<Option<String>>) -> Result<Self>
Create a named tensor, validating that the number of names matches the tensor’s rank and that named dimensions are unique.
§Errors
Returns CoreError::InvalidArgument if names.len() != tensor.ndim()
or if duplicate dimension names are found.
Sourcepub fn from_tensor(tensor: Tensor<T>) -> Self
pub fn from_tensor(tensor: Tensor<T>) -> Self
Wrap a tensor with all dimensions unnamed.
Sourcepub fn into_tensor(self) -> Tensor<T>
pub fn into_tensor(self) -> Tensor<T>
Consume the wrapper and return the plain tensor.
Sourcepub fn dim_index(&self, name: &str) -> Result<usize>
pub fn dim_index(&self, name: &str) -> Result<usize>
Look up the axis index for a named dimension.
§Errors
Returns CoreError::InvalidArgument if no dimension has the given name.
Sourcepub fn rename(&mut self, old: &str, new: &str) -> Result<()>
pub fn rename(&mut self, old: &str, new: &str) -> Result<()>
Rename a dimension from old to new.
§Errors
Returns an error if old is not found or new already exists.
Sourcepub fn set_names(&mut self, names: Vec<Option<String>>) -> Result<()>
pub fn set_names(&mut self, names: Vec<Option<String>>) -> Result<()>
Replace all dimension names at once.
§Errors
Returns an error if the length does not match the tensor rank.
Sourcepub fn align_to(&self, target_names: &[&str]) -> Result<NamedTensor<T>>
pub fn align_to(&self, target_names: &[&str]) -> Result<NamedTensor<T>>
Reorder dimensions by name, producing a permuted copy.
Every named dimension in the current tensor must appear in target_names,
and the lengths must match the tensor rank.
§Errors
Returns an error if a name is not found or if the number of names does not match the rank.
Trait Implementations§
Source§impl<T: Clone + Scalar> Clone for NamedTensor<T>
impl<T: Clone + Scalar> Clone for NamedTensor<T>
Source§fn clone(&self) -> NamedTensor<T>
fn clone(&self) -> NamedTensor<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more