tf.sparse.SparseTensor  |  TensorFlow v1.15.0 (original) (raw)

Represents a sparse tensor.

View aliases

Main aliases

tf.SparseTensor

Compat aliases for migration

SeeMigration guide for more details.

tf.compat.v1.SparseTensor, tf.compat.v1.sparse.SparseTensor, `tf.compat.v2.SparseTensor`, `tf.compat.v2.sparse.SparseTensor`

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 separateindices, values, and dense_shape tensors, wrap them in a SparseTensorobject 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 whenSparseTensor 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
indices A 2-D int64 tensor of shape [N, ndims].
values A 1-D tensor of any type and shape [N].
dense_shape A 1-D int64 tensor of shape [ndims].
Attributes
dense_shape A 1-D Tensor of int64 representing the shape of the dense tensor.
dtype The DType of elements in this tensor.
graph The Graph that contains the index, value, and dense_shape tensors.
indices The indices of non-zero values in the represented dense tensor.
op The Operation that produces values as an output.
shape Get the TensorShape representing the shape of the dense tensor.
values The non-zero values in the represented dense tensor.

Methods

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.

Args
feed_dict A dictionary that maps Tensor objects to feed values. Seetf.Session.run for a description of the valid feed values.
session (Optional.) The Session to be used to evaluate this sparse tensor. If none, the default session will be used.
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.

__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
sp_indices A Tensor of type int64. 2-D. N x R matrix with the indices of non-empty values in a SparseTensor, possibly not in canonical ordering.
sp_values A Tensor. Must be one of the following types: float32, float64, int32, uint8, int16, int8, complex64, int64, qint8, quint8, qint32, bfloat16, uint16, complex128, half, uint32, uint64. 1-D. N non-empty values corresponding to sp_indices.
sp_shape A Tensor of type int64. 1-D. Shape of the input SparseTensor.
dense A Tensor. Must have the same type as sp_values.R-D. The dense Tensor operand.
name A name for the operation (optional).
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
sp_indices A Tensor of type int64. 2-D. N x R matrix with the indices of non-empty values in a SparseTensor, possibly not in canonical ordering.
sp_values A Tensor. Must be one of the following types: float32, float64, int32, uint8, int16, int8, complex64, int64, qint8, quint8, qint32, bfloat16, uint16, complex128, half, uint32, uint64. 1-D. N non-empty values corresponding to sp_indices.
sp_shape A Tensor of type int64. 1-D. Shape of the input SparseTensor.
dense A Tensor. Must have the same type as sp_values.R-D. The dense Tensor operand.
name A name for the operation (optional).
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'.