tf.sparse.SparseTensor

View source on GitHub

Represents a sparse tensor.

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

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.

Concretely, the sparse tensor SparseTensor(indices, values, dense_shape) comprises the following components, where N and ndims are the number of values and number of dimensions in the SparseTensor, respectively:

The corresponding dense tensor satisfies:

dense.shape = dense_shape
dense[tuple(indices[i])] = values[i]

By convention, indices should be sorted in row-major order (or equivalently lexicographic order on the tuples indices[i]). This is not enforced when SparseTensor objects are constructed, but most ops assume correct ordering. If the ordering of sparse tensor st is wrong, a fixed version can be obtained by calling tf.sparse.reorder(st).

Example: The sparse tensor

SparseTensor(indices=[[0, 0], [1, 2]], values=[1, 2], dense_shape=[3, 4])

represents the dense tensor

[[1, 0, 0, 0]
 [0, 0, 2, 0]
 [0, 0, 0, 0]]

Args:

Attributes:

Methods

__div__

View source

__div__(
    sp_x, y
)

Component-wise divides a SparseTensor by a dense Tensor.

Limitation: this Op only broadcasts the dense side to the sparse side, but not the other direction.

Args:

Returns:

A Tensor. Has the same type as sp_values.

__mul__

View source

__mul__(
    sp_x, y
)

Component-wise multiplies a SparseTensor by a dense Tensor.

The output locations corresponding to the implicitly zero elements in the sparse tensor will be zero (i.e., will not take up storage space), regardless of the contents of the dense tensor (even if it's +/-INF and that INF*0 == NaN).

Limitation: this Op only broadcasts the dense side to the sparse side, but not the other direction.

Args:

Returns:

A Tensor. Has the same type as sp_values.

__truediv__

View source

__truediv__(
    sp_x, y
)

Internal helper function for 'sp_t / dense_t'.

consumers

View source

consumers()

eval

View source

eval(
    feed_dict=None, session=None
)

Evaluates this sparse tensor in a Session.

Calling this method will execute all preceding operations that produce the inputs needed for the operation that produces this tensor.

N.B. Before invoking SparseTensor.eval(), its graph must have been launched in a session, and either a default session must be available, or session must be specified explicitly.

Args:

Returns:

A SparseTensorValue object.

from_value

View source

@classmethod
from_value(
    sparse_tensor_value
)

get_shape

View source

get_shape()

Get the TensorShape representing the shape of the dense tensor.

Returns:

A TensorShape object.