tf.sparse.from_dense  |  TensorFlow v2.16.1 (original) (raw)

tf.sparse.from_dense

Stay organized with collections Save and categorize content based on your preferences.

Converts a dense tensor into a sparse tensor.

View aliases

Compat aliases for migration

SeeMigration guide for more details.

tf.compat.v1.sparse.from_dense

tf.sparse.from_dense(
    tensor, name=None
)

Used in the notebooks

Used in the guide
Working with sparse tensors

Only elements not equal to zero will be present in the result. The resultingSparseTensor 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]])

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

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.

Last updated 2024-04-26 UTC.