How do you get the names of tensors used in a sparse tensor?

Viewed 97

TensorFlow represents a sparse tensor as three separate dense tensors: indices, values, and dense_shape. In Python, the three tensors are collected into a SparseTensor class for ease of use. If you have separate indices, values, and dense_shape tensors, wrap them in a SparseTensor object before passing to the ops below.

My question is, given a sparse tensor, or sparse tensor value, how do I retrieve the individual names for each comprising tensor, namely the indices tensor, values tensor and shape tensor? (Unless I happen to be misunderstanding the explanation in the quoted text and this isn't possible?)

1 Answers

You can access each of the three tensors within the SparseTensor object with:

# Here some_sparse_tensor is an object of the tf.sparse.SparseTensor class
some_sparse_tensor.indices
some_sparse_tensor.values
some_sparse_tensor.dense_shape

As per documentation: https://www.tensorflow.org/api_docs/python/tf/sparse/SparseTensor#attributes

You can not get their "names", but you can assign them to their own variables: values_of_sparse_tensor = some_sparse_tensor.values

Related