Create Sparse Tensor using TensorFlow 2.0 Python Tutorial

What is Sparse Tensor?

Tensor that contain mostly zero values are called sparse tensor.

When working with tensors that contain a lot of zero values, it is important to store them in a space- and time-efficient manner. Sparse tensors enable efficient storage and processing of tensors that contain a lot of zero values.
Sparse tensors are used extensively in encoding schemes like TF-IDF as part of data pre-processing in NLP applications and for pre-processing images with a lot of dark pixels in computer vision applications.

Currently, sparse tensors in TensorFlow are encoded using the coordinate list (COO) format.

The COO encoding for sparse tensors is comprised of:

values: A 1D tensor with shape [N] containing all nonzero values.
indices: A 2D tensor with shape [N, rank], containing the indices of the nonzero values.
dense_shape: A 1D tensor with shape [rank], specifying the shape of the tensor.

A nonzero value in the context of a tf.SparseTensor is a value that’s not explicitly encoded.

Syntax: tf.sparse.SparseTensor(indices, values, dense_shape)

Create TensorFlow Sparse Tensor

"""
Video Tutorial:
Create Sparse Tensor using TensorFlow 2.0 Python Tutorial | Deep Learning | Machine Learning: https: https://youtu.be/ZbhaFycySUs
"""

# # Import TensorFlow 2.X
import tensorflow as tf

# # Create TensorFlow Sparse Tensor
st1 = tf.SparseTensor(indices=[[0,3], [2,4]], values=[10,20], dense_shape=[3,10])
st1

print(st1)

# # Create Sparse Tensor From Dense
import numpy as np

np_array = np.array([[1,0,0,0],
                     [0,0,0,0],
                     [0,0,2,0],
                     [0,0,0,4]])
np_array

st2_fd = tf.sparse.from_dense(np_array)
st2_fd

# # Extract the values, indices & shape of Sparse Tensor
st2_fd.values.numpy().tolist()

st2_fd.indices.numpy().tolist()

st2_fd.dense_shape.numpy().tolist()

# # Sparse Tensor to Dense
# """If most of the elements are nonzero, then the tensor is considered dense."""

dt_fst = tf.sparse.to_dense(st2_fd)
dt_fst

dt_fst.numpy()

# # Mathematical operation on Sparse Tensor
st_add = tf.sparse.add(st2_fd, st2_fd)
st_add

print(st_add)

tf.sparse.to_dense(st_add).numpy()

REF: Working with sparse tensors: https://www.tensorflow.org/guide/sparse_tensor
tf.sparse.SparseTensor: https://www.tensorflow.org/api_docs/python/tf/sparse/SparseTensor

Leave a Reply