sparsify_tensor#

ivy.sparsify_tensor(x, card, /, *, out=None)[source]#

Zeros out all elements in the tensor except card elements with maximum absolute values.

Parameters:
  • x (Union[Array, NativeArray]) – Tensor to be sparsified

  • card (int) – Desired number of non-zero elements in the tensor

  • out (Optional[Array], default: None) – Optional output array for writing the result to.

Return type:

Array

Returns:

ivy.array of shape tensor.shape

Examples

>>> x = ivy.arange(100)
>>> x = ivy.reshape(x, (10, 10))
>>> sparsify_tensor(x, 10)
ivy.array([[ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
[ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
[ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
[ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
[ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
[ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
[ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
[ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
[ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])
Array.sparsify_tensor(self, card, /, *, out=None)[source]#

ivy.Array class method variant of ivy.sparsify_tensor. This method simply wraps the function, and so the docstring for ivy.sparsify_tensor also applies to this method with minimal changes.

Parameters:
  • self (array) – The tensor to sparsify.

  • card (int) – The number of values to keep.

  • out (array, optional) – Optional output array, for writing the result to.

Return type:

Array

Returns:

ret (array) – The sparsified tensor.

Examples

>>> x = ivy.arange(100)
>>> x = ivy.reshape(x, (10, 10))
>>> x.sparsify_tensor(10)
ivy.array([[ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
[ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
[ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
[ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
[ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
[ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
[ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
[ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
[ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])
Container.sparsify_tensor(self, card, /, *, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#

ivy.Container instance method variant of ivy.sparsify_tensor.

This method simply wraps the function, and so the docstring for ivy.sparsify_tensor also applies to this method with minimal changes.

Return type:

Container