tf.sparse.from_dense

Converts a dense tensor into a sparse tensor.

Compat aliases for migration

See Migration guide for more details.

tf.compat.v1.sparse.from_dense

Used in the notebooks

Used in the guide

Only elements not equal to zero will be present in the result. The resulting SparseTensor has the same dtype and shape as the input.

sp = tf.sparse.from_dense([0, 0, 3, 0, 1])
sp.shape.as_list()
[5]
sp.values.numpy()
array([3, 1], dtype=int32)
sp.indices.numpy()
array([[2],
       [4]])

tensor A dense Tensor to be converted to a SparseTensor.
name Optional name for the op.

The SparseTensor.