pub struct SparseCOOTensor { /* private fields */ }Expand description
Sparse tensor in COO (Coordinate) format.
COO format stores sparse tensors as a pair of tensors:
indices: shape [sparse_dim, nnz], dtype I64 — the coordinates of non-zero elementsvalues: shape [nnz, *dense_dims] — the values at those coordinates
For a sparse tensor with shape [3, 4, 5] and sparse_dim=2:
- indices has shape [2, nnz] (row and column indices)
- values has shape [nnz, 5] (the remaining dense dimension)
The coalesced flag indicates whether duplicate indices have been merged.
A coalesced tensor has unique, sorted indices.
Implementations§
Source§impl SparseCOOTensor
impl SparseCOOTensor
Sourcepub fn new(
indices: DenseI64Tensor,
values: DenseTensor,
dense_shape: Vec<usize>,
coalesced: bool,
) -> Result<Self, SparseTensorError>
pub fn new( indices: DenseI64Tensor, values: DenseTensor, dense_shape: Vec<usize>, coalesced: bool, ) -> Result<Self, SparseTensorError>
Create a new sparse COO tensor.
§Arguments
indices- shape [sparse_dim, nnz], dtype I64values- shape [nnz, *dense_dims], any floating-point dtypedense_shape- the full shape if this were a dense tensorcoalesced- whether indices are unique and sorted
§Errors
Returns error if indices/values shapes don’t match or indices are out of bounds.
Sourcepub fn from_coords(
coords: &[Vec<i64>],
values: Vec<f64>,
dense_shape: Vec<usize>,
dtype: DType,
device: Device,
) -> Result<Self, SparseTensorError>
pub fn from_coords( coords: &[Vec<i64>], values: Vec<f64>, dense_shape: Vec<usize>, dtype: DType, device: Device, ) -> Result<Self, SparseTensorError>
Create a sparse COO tensor from coordinate lists.
§Arguments
coords- list of coordinate tuples, each of length sparse_dimvalues- flat values for each coordinatedense_shape- the full dense shapedtype- dtype for the valuesdevice- device for the tensor
pub fn id(&self) -> u64
pub fn indices(&self) -> &DenseI64Tensor
pub fn values(&self) -> &DenseTensor
pub fn dense_shape(&self) -> &[usize]
pub fn sparse_dim(&self) -> usize
pub fn nnz(&self) -> usize
pub fn is_coalesced(&self) -> bool
pub fn dtype(&self) -> DType
pub fn device(&self) -> Device
pub fn version(&self) -> u64
Sourcepub fn to_dense(&self) -> Result<DenseTensor, SparseTensorError>
pub fn to_dense(&self) -> Result<DenseTensor, SparseTensorError>
Convert this sparse tensor to a dense tensor.
This allocates a new dense tensor with zeros and fills in the non-zero values.
Trait Implementations§
Source§impl Clone for SparseCOOTensor
impl Clone for SparseCOOTensor
Source§fn clone(&self) -> SparseCOOTensor
fn clone(&self) -> SparseCOOTensor
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for SparseCOOTensor
impl Debug for SparseCOOTensor
Source§impl PartialEq for SparseCOOTensor
impl PartialEq for SparseCOOTensor
impl StructuralPartialEq for SparseCOOTensor
Auto Trait Implementations§
impl Freeze for SparseCOOTensor
impl RefUnwindSafe for SparseCOOTensor
impl Send for SparseCOOTensor
impl Sync for SparseCOOTensor
impl Unpin for SparseCOOTensor
impl UnsafeUnpin for SparseCOOTensor
impl UnwindSafe for SparseCOOTensor
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more