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

tf.sparse.to_dense

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

Converts a SparseTensor into a dense tensor.

View aliases

Compat aliases for migration

SeeMigration guide for more details.

tf.compat.v1.sparse.to_dense, tf.compat.v1.sparse_tensor_to_dense

tf.sparse.to_dense(
    sp_input, default_value=None, validate_indices=True, name=None
)

Used in the notebooks

Used in the guide Used in the tutorials
Working with sparse tensors Migrate `tf.feature_column`s to Keras preprocessing layers Ragged tensors Introduction to Tensors Introduction to tensor slicing Graph-based Neural Structured Learning in TFX Text generation with an RNN TFX Estimator Component Tutorial TFX Keras Component Tutorial

For this sparse tensor with three non-empty values:

sp_input = tf.sparse.SparseTensor( dense_shape=[3, 5], values=[7, 8, 9], indices =[[0, 1], [0, 3], [2, 0]])

The output will be a dense [3, 5] tensor with values:

tf.sparse.to_dense(sp_input).numpy() array([[0, 7, 0, 8, 0], [0, 0, 0, 0, 0], [9, 0, 0, 0, 0]], dtype=int32)

Args
sp_input The input SparseTensor.
default_value Scalar value to set for indices not specified insp_input. Defaults to zero.
validate_indices A boolean value. If True, indices are checked to make sure they are sorted in lexicographic order and that there are no repeats.
name A name prefix for the returned tensors (optional).
Returns
A dense tensor with shape sp_input.dense_shape and values specified by the non-empty values in sp_input. Indices not in sp_input are assigneddefault_value.
Raises
TypeError If sp_input is not a SparseTensor.