Represents a sparse tensor.
Represents a sparse tensor.
TensorFlow represents a sparse tensor as three separate dense tensors: indices
, values
, and denseShape
. In
Scala, the three tensors are collected into a SparseTensor class for ease of use. If you have separate
indices
, values
, and denseShape
tensors, wrap them in a SparseTensor
object before passing to the
relevant sparse tensor manipulation.
Concretely, the sparse tensor SparseTensor(indices, values, denseShape)
comprises the following components,
where N
and rank
are the number of values and number of dimensions in the SparseTensor, respectively:
indices
: Two-dimensional INT64 tensor with shape [N, rank]
, which specifies the indices of the elements
in the sparse tensor that have nonzero values (elements are zero-indexed). For example,
indices = 3], [2, 4
specifies that the elements with indexes [1, 3]
and [2, 4]
have nonzero
values.values
: One-dimensional tensor of any type, with shape [N]
, which supplies the values for each element in
indices
. For example, given indices = 3], [2, 4
, the parameter values = [18, 3.6]
specifies that
element [1, 3]
of the sparse tensor has a value of 18
, and element [2, 4]
of the tensor has a value of
3.6
.denseShape
: One-dimensional INT64 tensor with shape [rank]
, which specifies the dense shape of the
sparse tensor. For example, denseShape = [3, 6]
specifies a two-dimensional 3x6 tensor,
denseShape = [2, 3, 4]
specifies a three-dimensional 2x3x4 tensor, and denseShape = [9]
specifies a
one-dimensional tensor with 9 elements.The corresponding dense tensor, dense
, satisfies:
dense.shape == denseShape
dense(indices(i)) = values(i) // Using a somewhat loose notation with respect to indexing.
IMPORTANT NOTE: By convention, indices
should be sorted in row-major order (or equivalently lexicographic order
on 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
sparseReorder(st)
.
For example, the sparse tensor SparseTensor(indices = 0], [1, 2, values = [1, 2], denseShape = [3, 4])
,
represents the dense tensor 0, 0, 0], [0, 0, 2, 0], [0, 0, 0, 0
.
Tensor (i.e., multi-dimensional array).
Tensor (i.e., multi-dimensional array).
Tensors are the main data structure underlying all operations in TensorFlow. They represent multi-dimensional arrays of various data types (e.g., FLOAT32). Operations involving tensors can be of two types:
val a = Tensor(2.0, 4.5, 3.0, -1.2) val b = Tensor(Tensor(0.2, 0.4), Tensor(-2.3, 5.0)) a.reshape(Shape(2, 2)) + b == Tensor(Tensor(2.2, 4.9), Tensor(0.7, 3.8))
val a = tf.placeholder(FLOAT64, Shape(4)) // Symbolic placeholder for value of a val b = tf.placeholder(FLOAT64, Shape(2, 2)) // Symbolic placeholder for the value of b val add = tf.reshape(a, Shape(2, 2)) + b // Symbolic representation of the computation val result = Session.run( feeds = Map( a -> Tensor(2.0, 4.5, 3.0, -1.2), b -> Tensor(Tensor(0.2, 0.4), Tensor(-2.3, 5.0))), fetches = add) // Performs the actual computation result == Tensor(Tensor(2.2, 4.9), Tensor(0.7, 3.8))
// TODO: [OPS] Update doc when we enrich op outputs similarly to tensors.
Type trait representing types that can be converted to tensors.
Sparse representation of a set of tensor slices at given indices.
Sparse representation of a set of tensor slices at given indices.
This class if a simple wrapper for a pair (or a set of three) of Tensor objects:
indices
: A one-dimensional integer Tensor with shape [D0]
.values
: An Tensor of any data type, with shape [D0, D1, ..., Dn]
.denseShape
: Optionally, an integer Tensor with shape [LARGE0, D1, ..., Dn]
.An TensorIndexedSlices is typically used to represent a subset of a larger Output, dense
, of shape
[LARGE0, D1, ..., Dn]
, where LARGE0 >> D0
. The values in indices
are the indices in the first dimension of
the slices that have been extracted from the larger tensor.
The dense Tensor, dense
, represented by TensorIndexedSlices, slices
, has:
dense(slices.indices(i), ::, ::, ...) = slices.values(i, ::, ::, ...)
The TensorIndexedSlices class is used primarily in the definition of gradients for operations that have
sparse gradients, such as gather
.
Note that this is different than SparseTensor which uses multi-dimensional indices and scalar values.
Represents tensor-like objects.
Type trait for defining functions operating on and returning tensors.
Companion object that defines supported TensorOps implicit values.