get_slice#
- iskra.sparse.get_slice(x: SparseTensor, *indices: None | slice | int | Tensor | tuple[int, ...]) SparseTensor[SOURCE]#
Slices a sparse tensor.
Warning
The behavior of this function is not the same PyTorch’s [] operator. This function only slices a sparse Tensor via masking.
Behavior with indices of types int, slice, and None is the same as dense tensor indexing. Indexing with tensors and tuples works differently. Specifically, if there are two or more indices which are tensors or tuples, it does _not_ use them to pick out the individual elements, rather it picks out the entire row or column selected by those indices. Therefore, it cannot be used with arbitrary integer indices to reorder and repeat certain elements. For example, if given: ``` a =
- [[2., 0., 0., 0.],
[6., 3., 0., 0.], [0., 0., 4., 0.], [0., 0., 0., 5.]]
` indexing `SparseTensor` gives: `a[(0, 1), :] = a[0:2, :] = a[(True, True, False, False), :] =- [[2., 0., 0., 0.],
[6., 3., 0., 0.]]
and
a[(0, 1), (1, 2)] = a[0:2, 1:3] = a[(True, True, False, False), (False, True, True, False)] =
- [[0., 0.],
[3., 0.]]
` whereas indexing `Tensor` gives: `a[(0, 1), :] = a[0:2, :] = a[(True, True, False, False), :] =- [[2., 0., 0., 0.],
[6., 3., 0., 0.]]
and
a[[(0, 1), (1, 2)]] = a[[(True, False, True, False), (True, False, True, False)]] =
[0., 0.]
but
- a[0:2, 1:3] =
- [[0., 0.],
[3., 0.]]
Moreover, unlike dense tensors, where certain indexing operations can be achieved by modifying only the view of a tensor, all of sparse indexing operations produce copies.
- Parameters:
x (
SparseTensor) – Sparse tensor to be sliced.*indices (None | slice | int |
Tensor[bool | int, …] | tuple[int, …]) – Slicing masks, one per dimension of input tensor.
- Returns:
(
SparseTensor) – Sliced sparse tensor.