tf.SparseTensor to tf.sparse.SparseTensor

This commit is contained in:
Angus-Luo 2020-03-24 13:40:16 +08:00
parent f1efe7d568
commit 652a4b64be
29 changed files with 81 additions and 81 deletions

View File

@ -45,7 +45,7 @@ Usage example:
with tf.Session() as sess: with tf.Session() as sess:
# Define (COO format) SparseTensor over Numpy array. # Define (COO format) SparseTensor over Numpy array.
a_st = tf.SparseTensor(a_indices, a_values, a_dense_shape) a_st = tf.sparse.SparseTensor(a_indices, a_values, a_dense_shape)
# Convert SparseTensors to CSR SparseMatrix. # Convert SparseTensors to CSR SparseMatrix.
a_sm = sparse_csr_matrix_ops.sparse_tensor_to_csr_sparse_matrix( a_sm = sparse_csr_matrix_ops.sparse_tensor_to_csr_sparse_matrix(

View File

@ -58,7 +58,7 @@ Usage example:
with tf.Session() as sess: with tf.Session() as sess:
# Define (COO format) SparseTensor over Numpy array. # Define (COO format) SparseTensor over Numpy array.
a_st = tf.SparseTensor(a_indices, a_values, a_dense_shape) a_st = tf.sparse.SparseTensor(a_indices, a_values, a_dense_shape)
# Convert SparseTensors to CSR SparseMatrix. # Convert SparseTensors to CSR SparseMatrix.
a_sm = sparse_csr_matrix_ops.sparse_tensor_to_csr_sparse_matrix( a_sm = sparse_csr_matrix_ops.sparse_tensor_to_csr_sparse_matrix(

View File

@ -71,8 +71,8 @@ Usage example:
with tf.Session() as sess: with tf.Session() as sess:
# Define (COO format) Sparse Tensors over Numpy arrays # Define (COO format) Sparse Tensors over Numpy arrays
a_st = tf.SparseTensor(a_indices, a_values, a_dense_shape) a_st = tf.sparse.SparseTensor(a_indices, a_values, a_dense_shape)
b_st = tf.SparseTensor(b_indices, b_values, b_dense_shape) b_st = tf.sparse.SparseTensor(b_indices, b_values, b_dense_shape)
# Convert SparseTensors to CSR SparseMatrix # Convert SparseTensors to CSR SparseMatrix
a_sm = sparse_csr_matrix_ops.sparse_tensor_to_csr_sparse_matrix( a_sm = sparse_csr_matrix_ops.sparse_tensor_to_csr_sparse_matrix(

View File

@ -87,7 +87,7 @@ class TextEmbeddingModel(tf.train.Checkpoint):
return tf.nn.safe_embedding_lookup_sparse( return tf.nn.safe_embedding_lookup_sparse(
embedding_weights=self.embeddings, embedding_weights=self.embeddings,
sparse_ids=tf.SparseTensor(token_ids, token_values, token_dense_shape), sparse_ids=tf.sparse.SparseTensor(token_ids, token_values, token_dense_shape),
sparse_weights=None, sparse_weights=None,
combiner="sqrtn") combiner="sqrtn")

View File

@ -20740,7 +20740,7 @@ func Print(scope *Scope, input tf.Output, data []tf.Output, optional ...PrintAtt
// //
// with tf.Session() as sess: // with tf.Session() as sess:
// # Define (COO format) SparseTensor over Numpy array. // # Define (COO format) SparseTensor over Numpy array.
// a_st = tf.SparseTensor(a_indices, a_values, a_dense_shape) // a_st = tf.sparse.SparseTensor(a_indices, a_values, a_dense_shape)
// //
// # Convert SparseTensors to CSR SparseMatrix. // # Convert SparseTensors to CSR SparseMatrix.
// a_sm = sparse_csr_matrix_ops.sparse_tensor_to_csr_sparse_matrix( // a_sm = sparse_csr_matrix_ops.sparse_tensor_to_csr_sparse_matrix(
@ -32089,8 +32089,8 @@ func SparseMatrixSparseMatMulAdjointB(value bool) SparseMatrixSparseMatMulAttr {
// //
// with tf.Session() as sess: // with tf.Session() as sess:
// # Define (COO format) Sparse Tensors over Numpy arrays // # Define (COO format) Sparse Tensors over Numpy arrays
// a_st = tf.SparseTensor(a_indices, a_values, a_dense_shape) // a_st = tf.sparse.SparseTensor(a_indices, a_values, a_dense_shape)
// b_st = tf.SparseTensor(b_indices, b_values, b_dense_shape) // b_st = tf.sparse.SparseTensor(b_indices, b_values, b_dense_shape)
// //
// # Convert SparseTensors to CSR SparseMatrix // # Convert SparseTensors to CSR SparseMatrix
// a_sm = sparse_csr_matrix_ops.sparse_tensor_to_csr_sparse_matrix( // a_sm = sparse_csr_matrix_ops.sparse_tensor_to_csr_sparse_matrix(
@ -37606,7 +37606,7 @@ func RecvTPUEmbeddingActivations(scope *Scope, num_outputs int64, config string)
// //
// with tf.Session() as sess: // with tf.Session() as sess:
// # Define (COO format) SparseTensor over Numpy array. // # Define (COO format) SparseTensor over Numpy array.
// a_st = tf.SparseTensor(a_indices, a_values, a_dense_shape) // a_st = tf.sparse.SparseTensor(a_indices, a_values, a_dense_shape)
// //
// # Convert SparseTensors to CSR SparseMatrix. // # Convert SparseTensors to CSR SparseMatrix.
// a_sm = sparse_csr_matrix_ops.sparse_tensor_to_csr_sparse_matrix( // a_sm = sparse_csr_matrix_ops.sparse_tensor_to_csr_sparse_matrix(

View File

@ -862,7 +862,7 @@ class BaseSession(SessionInterface):
* A `tf.Tensor`. * A `tf.Tensor`.
The corresponding fetched value will be a numpy ndarray containing the The corresponding fetched value will be a numpy ndarray containing the
value of that tensor. value of that tensor.
* A `tf.SparseTensor`. * A `tf.sparse.SparseTensor`.
The corresponding fetched value will be a The corresponding fetched value will be a
`tf.compat.v1.SparseTensorValue` `tf.compat.v1.SparseTensorValue`
containing the value of that sparse tensor. containing the value of that sparse tensor.
@ -907,7 +907,7 @@ class BaseSession(SessionInterface):
`tf.compat.v1.placeholder`, the shape of `tf.compat.v1.placeholder`, the shape of
the value will be checked for compatibility with the placeholder. the value will be checked for compatibility with the placeholder.
* If the key is a * If the key is a
`tf.SparseTensor`, `tf.sparse.SparseTensor`,
the value should be a the value should be a
`tf.compat.v1.SparseTensorValue`. `tf.compat.v1.SparseTensorValue`.
* If the key is a nested tuple of `Tensor`s or `SparseTensor`s, the value * If the key is a nested tuple of `Tensor`s or `SparseTensor`s, the value

View File

@ -88,15 +88,15 @@ def dense_to_ragged_batch(batch_size,
@tf_export("data.experimental.dense_to_sparse_batch") @tf_export("data.experimental.dense_to_sparse_batch")
def dense_to_sparse_batch(batch_size, row_shape): def dense_to_sparse_batch(batch_size, row_shape):
"""A transformation that batches ragged elements into `tf.SparseTensor`s. """A transformation that batches ragged elements into `tf.sparse.SparseTensor`s.
Like `Dataset.padded_batch()`, this transformation combines multiple Like `Dataset.padded_batch()`, this transformation combines multiple
consecutive elements of the dataset, which might have different consecutive elements of the dataset, which might have different
shapes, into a single element. The resulting element has three shapes, into a single element. The resulting element has three
components (`indices`, `values`, and `dense_shape`), which components (`indices`, `values`, and `dense_shape`), which
comprise a `tf.SparseTensor` that represents the same data. The comprise a `tf.sparse.SparseTensor` that represents the same data. The
`row_shape` represents the dense shape of each row in the `row_shape` represents the dense shape of each row in the
resulting `tf.SparseTensor`, to which the effective batch size is resulting `tf.sparse.SparseTensor`, to which the effective batch size is
prepended. For example: prepended. For example:
```python ```python
@ -121,7 +121,7 @@ def dense_to_sparse_batch(batch_size, row_shape):
consecutive elements of this dataset to combine in a single batch. consecutive elements of this dataset to combine in a single batch.
row_shape: A `tf.TensorShape` or `tf.int64` vector tensor-like object row_shape: A `tf.TensorShape` or `tf.int64` vector tensor-like object
representing the equivalent dense shape of a row in the resulting representing the equivalent dense shape of a row in the resulting
`tf.SparseTensor`. Each element of this dataset must have the same rank as `tf.sparse.SparseTensor`. Each element of this dataset must have the same rank as
`row_shape`, and must have size less than or equal to `row_shape` in each `row_shape`, and must have size less than or equal to `row_shape` in each
dimension. dimension.
@ -283,7 +283,7 @@ def unbatch():
class _DenseToSparseBatchDataset(dataset_ops.UnaryDataset): class _DenseToSparseBatchDataset(dataset_ops.UnaryDataset):
"""A `Dataset` that batches ragged dense elements into `tf.SparseTensor`s.""" """A `Dataset` that batches ragged dense elements into `tf.sparse.SparseTensor`s."""
def __init__(self, input_dataset, batch_size, row_shape): def __init__(self, input_dataset, batch_size, row_shape):
"""See `Dataset.dense_to_sparse_batch()` for more details.""" """See `Dataset.dense_to_sparse_batch()` for more details."""

View File

@ -161,7 +161,7 @@ def bucket_by_sequence_length(element_length_func,
bucket), and caller must ensure that the source `Dataset` does not contain bucket), and caller must ensure that the source `Dataset` does not contain
any elements with length longer than `max(bucket_boundaries)`. any elements with length longer than `max(bucket_boundaries)`.
no_padding: `bool`, indicates whether to pad the batch features (features no_padding: `bool`, indicates whether to pad the batch features (features
need to be either of type `tf.SparseTensor` or of same shape). need to be either of type `tf.sparse.SparseTensor` or of same shape).
drop_remainder: (Optional.) A `tf.bool` scalar `tf.Tensor`, representing drop_remainder: (Optional.) A `tf.bool` scalar `tf.Tensor`, representing
whether the last batch should be dropped in the case it has fewer than whether the last batch should be dropped in the case it has fewer than
`batch_size` elements; the default behavior is not to drop the smaller `batch_size` elements; the default behavior is not to drop the smaller

View File

@ -37,7 +37,7 @@ class FromSparseTensorSlicesTest(test_base.DatasetTestBase,
@combinations.generate( @combinations.generate(
combinations.combine(tf_api_version=1, mode=["graph"])) combinations.combine(tf_api_version=1, mode=["graph"]))
def testFromSparseTensorSlices(self): def testFromSparseTensorSlices(self):
"""Test a dataset based on slices of a `tf.SparseTensor`.""" """Test a dataset based on slices of a `tf.sparse.SparseTensor`."""
st = array_ops.sparse_placeholder(dtypes.float64) st = array_ops.sparse_placeholder(dtypes.float64)
iterator = dataset_ops.make_initializable_iterator( iterator = dataset_ops.make_initializable_iterator(
dataset_ops.Dataset.from_sparse_tensor_slices(st)) dataset_ops.Dataset.from_sparse_tensor_slices(st))

View File

@ -158,7 +158,7 @@ class DatasetV2(tracking_base.Trackable, composite_tensor.CompositeTensor):
Elements can be nested structures of tuples, named tuples, and dictionaries. Elements can be nested structures of tuples, named tuples, and dictionaries.
Element components can be of any type representable by `tf.TypeSpec`, Element components can be of any type representable by `tf.TypeSpec`,
including `tf.Tensor`, `tf.data.Dataset`, `tf.SparseTensor`, including `tf.Tensor`, `tf.data.Dataset`, `tf.sparse.SparseTensor`,
`tf.RaggedTensor`, and `tf.TensorArray`. `tf.RaggedTensor`, and `tf.TensorArray`.
>>> a = 1 # Integer element >>> a = 1 # Integer element
@ -1486,7 +1486,7 @@ class DatasetV2(tracking_base.Trackable, composite_tensor.CompositeTensor):
array([[ 10, 100], [ 11, 12]], dtype=int32))] array([[ 10, 100], [ 11, 12]], dtype=int32))]
See also `tf.data.experimental.dense_to_sparse_batch`, which combines See also `tf.data.experimental.dense_to_sparse_batch`, which combines
elements that may have different shapes into a `tf.SparseTensor`. elements that may have different shapes into a `tf.sparse.SparseTensor`.
Args: Args:
batch_size: A `tf.int64` scalar `tf.Tensor`, representing the number of batch_size: A `tf.int64` scalar `tf.Tensor`, representing the number of
@ -2296,10 +2296,10 @@ class DatasetV1(DatasetV2):
@staticmethod @staticmethod
@deprecation.deprecated(None, "Use `tf.data.Dataset.from_tensor_slices()`.") @deprecation.deprecated(None, "Use `tf.data.Dataset.from_tensor_slices()`.")
def from_sparse_tensor_slices(sparse_tensor): def from_sparse_tensor_slices(sparse_tensor):
"""Splits each rank-N `tf.SparseTensor` in this dataset row-wise. """Splits each rank-N `tf.sparse.SparseTensor` in this dataset row-wise.
Args: Args:
sparse_tensor: A `tf.SparseTensor`. sparse_tensor: A `tf.sparse.SparseTensor`.
Returns: Returns:
Dataset: A `Dataset` of rank-(N-1) sparse tensors. Dataset: A `Dataset` of rank-(N-1) sparse tensors.
@ -2909,13 +2909,13 @@ class TensorSliceDataset(DatasetSource):
class SparseTensorSliceDataset(DatasetSource): class SparseTensorSliceDataset(DatasetSource):
"""A `Dataset` that splits a rank-N `tf.SparseTensor` into its rows.""" """A `Dataset` that splits a rank-N `tf.sparse.SparseTensor` into its rows."""
def __init__(self, sparse_tensor): def __init__(self, sparse_tensor):
"""See `Dataset.from_sparse_tensor_slices()` for details.""" """See `Dataset.from_sparse_tensor_slices()` for details."""
if not isinstance(sparse_tensor, sparse_tensor_lib.SparseTensor): if not isinstance(sparse_tensor, sparse_tensor_lib.SparseTensor):
raise TypeError( raise TypeError(
"`sparse_tensor` must be a `tf.SparseTensor` object. Was {}.".format( "`sparse_tensor` must be a `tf.sparse.SparseTensor` object. Was {}.".format(
sparse_tensor)) sparse_tensor))
self._sparse_tensor = sparse_tensor self._sparse_tensor = sparse_tensor

View File

@ -448,7 +448,7 @@ class Iterator(trackable.Trackable):
def output_classes(self): def output_classes(self):
"""Returns the class of each component of an element of this iterator. """Returns the class of each component of an element of this iterator.
The expected values are `tf.Tensor` and `tf.SparseTensor`. The expected values are `tf.Tensor` and `tf.sparse.SparseTensor`.
Returns: Returns:
A nested structure of Python `type` objects corresponding to each A nested structure of Python `type` objects corresponding to each
@ -677,7 +677,7 @@ class OwnedIterator(trackable.Trackable, composite_tensor.CompositeTensor):
def output_classes(self): def output_classes(self):
"""Returns the class of each component of an element of this iterator. """Returns the class of each component of an element of this iterator.
The expected values are `tf.Tensor` and `tf.SparseTensor`. The expected values are `tf.Tensor` and `tf.sparse.SparseTensor`.
Returns: Returns:
A nested structure of Python `type` objects corresponding to each A nested structure of Python `type` objects corresponding to each

View File

@ -47,7 +47,7 @@ def as_dense_shapes(shapes, classes):
Returns: Returns:
a structure matching the nested structure of `shapes`, containing a structure matching the nested structure of `shapes`, containing
`tensor_shape.unknown_shape()` at positions where `classes` contains `tensor_shape.unknown_shape()` at positions where `classes` contains
`tf.SparseTensor` and matching contents of `shapes` otherwise `tf.sparse.SparseTensor` and matching contents of `shapes` otherwise
""" """
ret = nest.pack_sequence_as(shapes, [ ret = nest.pack_sequence_as(shapes, [
tensor_shape.unknown_shape() if c is sparse_tensor.SparseTensor else shape tensor_shape.unknown_shape() if c is sparse_tensor.SparseTensor else shape
@ -65,7 +65,7 @@ def as_dense_types(types, classes):
Returns: Returns:
a structure matching the nested structure of `types`, containing a structure matching the nested structure of `types`, containing
`dtypes.variant` at positions where `classes` contains `tf.SparseTensor` and `dtypes.variant` at positions where `classes` contains `tf.sparse.SparseTensor` and
matching contents of `types` otherwise matching contents of `types` otherwise
""" """
ret = nest.pack_sequence_as(types, [ ret = nest.pack_sequence_as(types, [
@ -106,7 +106,7 @@ def get_classes(tensors):
Returns: Returns:
a structure matching the nested structure of `tensors`, containing a structure matching the nested structure of `tensors`, containing
`tf.SparseTensor` at positions where `tensors` contains a sparse tensor and `tf.sparse.SparseTensor` at positions where `tensors` contains a sparse tensor and
`tf.Tensor` otherwise `tf.Tensor` otherwise
""" """
return nest.pack_sequence_as(tensors, [ return nest.pack_sequence_as(tensors, [

View File

@ -1969,7 +1969,7 @@ class _CategoricalColumn(_FeatureColumn):
WARNING: Do not subclass this layer unless you know what you are doing: WARNING: Do not subclass this layer unless you know what you are doing:
the API is subject to future changes. the API is subject to future changes.
A categorical feature typically handled with a `tf.SparseTensor` of IDs. A categorical feature typically handled with a `tf.sparse.SparseTensor` of IDs.
""" """
IdWeightPair = collections.namedtuple( # pylint: disable=invalid-name IdWeightPair = collections.namedtuple( # pylint: disable=invalid-name

View File

@ -2515,7 +2515,7 @@ def _create_dense_column_weighted_sum(column, transformation_cache,
class CategoricalColumn(FeatureColumn): class CategoricalColumn(FeatureColumn):
"""Represents a categorical feature. """Represents a categorical feature.
A categorical feature typically handled with a `tf.SparseTensor` of IDs. A categorical feature typically handled with a `tf.sparse.SparseTensor` of IDs.
""" """
IdWeightPair = collections.namedtuple( # pylint: disable=invalid-name IdWeightPair = collections.namedtuple( # pylint: disable=invalid-name

View File

@ -84,7 +84,7 @@ class IndexedSlices(_TensorLike, composite_tensor.CompositeTensor):
(e.g. `tf.gather`). (e.g. `tf.gather`).
Contrast this representation with Contrast this representation with
`tf.SparseTensor`, `tf.sparse.SparseTensor`,
which uses multi-dimensional indices and scalar values. which uses multi-dimensional indices and scalar values.
""" """

View File

@ -338,7 +338,7 @@ class Tensor(_TensorLike):
shape of a tensor at execution time. shape of a tensor at execution time.
There are specialized tensors; for these, see `tf.Variable`, `tf.constant`, There are specialized tensors; for these, see `tf.Variable`, `tf.constant`,
`tf.placeholder`, `tf.SparseTensor`, and `tf.RaggedTensor`. `tf.placeholder`, `tf.sparse.SparseTensor`, and `tf.RaggedTensor`.
For more on Tensors, see the [guide](https://tensorflow.org/guide/tensor`). For more on Tensors, see the [guide](https://tensorflow.org/guide/tensor`).
""" """

View File

@ -298,14 +298,14 @@ _pywrap_utils.RegisterType("SparseTensorValue", SparseTensorValue)
@tf_export("SparseTensorSpec") @tf_export("SparseTensorSpec")
class SparseTensorSpec(type_spec.BatchableTypeSpec): class SparseTensorSpec(type_spec.BatchableTypeSpec):
"""Type specification for a `tf.SparseTensor`.""" """Type specification for a `tf.sparse.SparseTensor`."""
__slots__ = ["_shape", "_dtype"] __slots__ = ["_shape", "_dtype"]
value_type = property(lambda self: SparseTensor) value_type = property(lambda self: SparseTensor)
def __init__(self, shape=None, dtype=dtypes.float32): def __init__(self, shape=None, dtype=dtypes.float32):
"""Constructs a type specification for a `tf.SparseTensor`. """Constructs a type specification for a `tf.sparse.SparseTensor`.
Args: Args:
shape: The dense shape of the `SparseTensor`, or `None` to allow shape: The dense shape of the `SparseTensor`, or `None` to allow
@ -473,13 +473,13 @@ def convert_to_tensor_or_sparse_tensor(value, dtype=None, name=None):
def is_sparse(x): def is_sparse(x):
"""Check whether `x` is sparse. """Check whether `x` is sparse.
Check whether an object is a `tf.SparseTensor` or Check whether an object is a `tf.sparse.SparseTensor` or
`tf.compat.v1.SparseTensorValue`. `tf.compat.v1.SparseTensorValue`.
Args: Args:
x: A python object to check. x: A python object to check.
Returns: Returns:
`True` iff `x` is a `tf.SparseTensor` or `tf.compat.v1.SparseTensorValue`. `True` iff `x` is a `tf.sparse.SparseTensor` or `tf.compat.v1.SparseTensorValue`.
""" """
return isinstance(x, (SparseTensor, SparseTensorValue)) return isinstance(x, (SparseTensor, SparseTensorValue))

View File

@ -782,7 +782,7 @@ def local_conv_sparse_matmul(inputs, kernel, kernel_idxs, kernel_shape,
output_shape): output_shape):
"""Apply N-D convolution with un-shared weights using a single sparse matmul. """Apply N-D convolution with un-shared weights using a single sparse matmul.
This method outputs `inputs . tf.SparseTensor(indices=kernel_idxs, This method outputs `inputs . tf.sparse.SparseTensor(indices=kernel_idxs,
values=kernel, dense_shape=kernel_shape)`, with `.` standing for values=kernel, dense_shape=kernel_shape)`, with `.` standing for
matrix-multiply. It also reshapes `inputs` to 2-D and `output` to (N+2)-D. matrix-multiply. It also reshapes `inputs` to 2-D and `output` to (N+2)-D.

View File

@ -3069,7 +3069,7 @@ def sparse_placeholder(dtype, shape=None, name=None):
print(sess.run(y, feed_dict={ print(sess.run(y, feed_dict={
x: (indices, values, shape)})) # Will succeed. x: (indices, values, shape)})) # Will succeed.
sp = tf.SparseTensor(indices=indices, values=values, dense_shape=shape) sp = tf.sparse.SparseTensor(indices=indices, values=values, dense_shape=shape)
sp_value = sp.eval(session=sess) sp_value = sp.eval(session=sess)
print(sess.run(y, feed_dict={x: sp_value})) # Will succeed. print(sess.run(y, feed_dict={x: sp_value})) # Will succeed.
``` ```
@ -3471,7 +3471,7 @@ def edit_distance(hypothesis, truth, normalize=True, name="edit_distance"):
# 'hypothesis' is a tensor of shape `[2, 1]` with variable-length values: # 'hypothesis' is a tensor of shape `[2, 1]` with variable-length values:
# (0,0) = ["a"] # (0,0) = ["a"]
# (1,0) = ["b"] # (1,0) = ["b"]
hypothesis = tf.SparseTensor( hypothesis = tf.sparse.SparseTensor(
[[0, 0, 0], [[0, 0, 0],
[1, 0, 0]], [1, 0, 0]],
["a", "b"], ["a", "b"],
@ -3482,7 +3482,7 @@ def edit_distance(hypothesis, truth, normalize=True, name="edit_distance"):
# (0,1) = ["a"] # (0,1) = ["a"]
# (1,0) = ["b", "c"] # (1,0) = ["b", "c"]
# (1,1) = ["a"] # (1,1) = ["a"]
truth = tf.SparseTensor( truth = tf.sparse.SparseTensor(
[[0, 1, 0], [[0, 1, 0],
[1, 0, 0], [1, 0, 0],
[1, 0, 1], [1, 0, 1],

View File

@ -1126,7 +1126,7 @@ def dense_labels_to_sparse(dense, length):
length: int tensor of shape [batch] The length of each sequence in dense. length: int tensor of shape [batch] The length of each sequence in dense.
Returns: Returns:
tf.SparseTensor with values only for the valid elements of sequences. tf.sparse.SparseTensor with values only for the valid elements of sequences.
""" """
flat_values = array_ops.reshape(dense, [-1]) flat_values = array_ops.reshape(dense, [-1])

View File

@ -106,7 +106,7 @@ def map_fn(fn,
* A `tf.DType` or `tf.TensorSpec` (to describe a `tf.Tensor`) * A `tf.DType` or `tf.TensorSpec` (to describe a `tf.Tensor`)
* A `tf.RaggedTensorSpec` (to describe a `tf.RaggedTensor`) * A `tf.RaggedTensorSpec` (to describe a `tf.RaggedTensor`)
* A `tf.SparseTensorSpec` (to describe a `tf.SparseTensor`) * A `tf.SparseTensorSpec` (to describe a `tf.sparse.SparseTensor`)
* A (possibly nested) tuple, list, or dict containing the above types. * A (possibly nested) tuple, list, or dict containing the above types.
#### RaggedTensors #### RaggedTensors
@ -159,11 +159,11 @@ def map_fn(fn,
#### SparseTensors #### SparseTensors
`map_fn` supports `tf.SparseTensor` inputs and outputs. In particular: `map_fn` supports `tf.sparse.SparseTensor` inputs and outputs. In particular:
* If `elems` is a `SparseTensor`, then `fn` will be called with each row * If `elems` is a `SparseTensor`, then `fn` will be called with each row
of that sparse tensor. In particular, the value passed to `fn` will be a of that sparse tensor. In particular, the value passed to `fn` will be a
`tf.SparseTensor` with one fewer dimension than `elems`. `tf.sparse.SparseTensor` with one fewer dimension than `elems`.
* If the result of `map_fn` should be a `SparseTensor`, then use a * If the result of `map_fn` should be a `SparseTensor`, then use a
`tf.SparseTensorSpec` to specify `fn_output_signature`. The individual `tf.SparseTensorSpec` to specify `fn_output_signature`. The individual
@ -171,7 +171,7 @@ def map_fn(fn,
`SparseTensor` with one more dimension. `SparseTensor` with one more dimension.
>>> # Example: SparseTensor input >>> # Example: SparseTensor input
>>> st = tf.SparseTensor([[0, 0], [2, 0], [2, 1]], [2, 3, 4], [4, 4]) >>> st = tf.sparse.SparseTensor([[0, 0], [2, 0], [2, 1]], [2, 3, 4], [4, 4])
>>> tf.map_fn(tf.sparse.reduce_sum, st, fn_output_signature=tf.int32) >>> tf.map_fn(tf.sparse.reduce_sum, st, fn_output_signature=tf.int32)
<tf.Tensor: shape=(4,), dtype=int32, numpy=array([2, 0, 7, 0], dtype=int32)> <tf.Tensor: shape=(4,), dtype=int32, numpy=array([2, 0, 7, 0], dtype=int32)>
@ -191,9 +191,9 @@ def map_fn(fn,
*rows* of a `SparseTensor`. If you wish to map a function over the nonzero *rows* of a `SparseTensor`. If you wish to map a function over the nonzero
values, then you should use: values, then you should use:
* `tf.SparseTensor(st.indices, fn(st.values), st.dense_shape)` * `tf.sparse.SparseTensor(st.indices, fn(st.values), st.dense_shape)`
(if the function is expressible as TensorFlow ops) (if the function is expressible as TensorFlow ops)
* `tf.SparseTensor(st.indices, tf.map_fn(fn, st.values), st.dense_shape)` * `tf.sparse.SparseTensor(st.indices, tf.map_fn(fn, st.values), st.dense_shape)`
(otherwise). (otherwise).
#### `map_fn` vs. vectorized operations #### `map_fn` vs. vectorized operations
@ -276,7 +276,7 @@ def map_fn(fn,
* A `tf.DType` or `tf.TensorSpec` (to describe a `tf.Tensor`) * A `tf.DType` or `tf.TensorSpec` (to describe a `tf.Tensor`)
* A `tf.RaggedTensorSpec` (to describe a `tf.RaggedTensor`) * A `tf.RaggedTensorSpec` (to describe a `tf.RaggedTensor`)
* A `tf.SparseTensorSpec` (to describe a `tf.SparseTensor`) * A `tf.SparseTensorSpec` (to describe a `tf.sparse.SparseTensor`)
* A (possibly nested) tuple, list, or dict containing the above types. * A (possibly nested) tuple, list, or dict containing the above types.
Returns: Returns:

View File

@ -1432,8 +1432,8 @@ def equal(x, y, name=None):
<tf.Tensor: shape=(2,), dtype=bool, numpy=array([ True, True])> <tf.Tensor: shape=(2,), dtype=bool, numpy=array([ True, True])>
Args: Args:
x: A `tf.Tensor` or `tf.SparseTensor` or `tf.IndexedSlices`. x: A `tf.Tensor` or `tf.sparse.SparseTensor` or `tf.IndexedSlices`.
y: A `tf.Tensor` or `tf.SparseTensor` or `tf.IndexedSlices`. y: A `tf.Tensor` or `tf.sparse.SparseTensor` or `tf.IndexedSlices`.
name: A name for the operation (optional). name: A name for the operation (optional).
Returns: Returns:
@ -1468,8 +1468,8 @@ def not_equal(x, y, name=None):
<tf.Tensor: shape=(2,), dtype=bool, numpy=array([False, False])> <tf.Tensor: shape=(2,), dtype=bool, numpy=array([False, False])>
Args: Args:
x: A `tf.Tensor` or `tf.SparseTensor` or `tf.IndexedSlices`. x: A `tf.Tensor` or `tf.sparse.SparseTensor` or `tf.IndexedSlices`.
y: A `tf.Tensor` or `tf.SparseTensor` or `tf.IndexedSlices`. y: A `tf.Tensor` or `tf.sparse.SparseTensor` or `tf.IndexedSlices`.
name: A name for the operation (optional). name: A name for the operation (optional).
Returns: Returns:
@ -2907,12 +2907,12 @@ def matmul(a,
**does not support `tf.sparse.SparseTensor`**, it just makes optimizations **does not support `tf.sparse.SparseTensor`**, it just makes optimizations
that assume most values in `a` are zero. that assume most values in `a` are zero.
See `tf.sparse.sparse_dense_matmul` See `tf.sparse.sparse_dense_matmul`
for some support for `tf.SparseTensor` multiplication. for some support for `tf.sparse.SparseTensor` multiplication.
b_is_sparse: If `True`, `b` is treated as a sparse matrix. Notice, this b_is_sparse: If `True`, `b` is treated as a sparse matrix. Notice, this
**does not support `tf.sparse.SparseTensor`**, it just makes optimizations **does not support `tf.sparse.SparseTensor`**, it just makes optimizations
that assume most values in `a` are zero. that assume most values in `a` are zero.
See `tf.sparse.sparse_dense_matmul` See `tf.sparse.sparse_dense_matmul`
for some support for `tf.SparseTensor` multiplication. for some support for `tf.sparse.SparseTensor` multiplication.
name: Name for the operation (optional). name: Name for the operation (optional).
Returns: Returns:

View File

@ -1217,10 +1217,10 @@ class PFor(object):
the new dense shape will be (N, max_i(x_i), max_i(y_i), max_i(z_i)). the new dense shape will be (N, max_i(x_i), max_i(y_i), max_i(z_i)).
Args: Args:
y: A tf.SparseTensor. y: A tf.sparse.SparseTensor.
Returns: Returns:
A tf.SparseTensor that is the converted value corresponding to y. A tf.sparse.SparseTensor that is the converted value corresponding to y.
""" """
outputs = [ outputs = [
self._convert_helper(t) for t in (y.indices, y.values, y.dense_shape) self._convert_helper(t) for t in (y.indices, y.values, y.dense_shape)

View File

@ -1629,7 +1629,7 @@ class RaggedTensor(composite_tensor.CompositeTensor):
@classmethod @classmethod
def from_sparse(cls, st_input, name=None, row_splits_dtype=dtypes.int64): def from_sparse(cls, st_input, name=None, row_splits_dtype=dtypes.int64):
"""Converts a 2D `tf.SparseTensor` to a `RaggedTensor`. """Converts a 2D `tf.sparse.SparseTensor` to a `RaggedTensor`.
Each row of the `output` `RaggedTensor` will contain the explicit values Each row of the `output` `RaggedTensor` will contain the explicit values
from the same row in `st_input`. `st_input` must be ragged-right. If not from the same row in `st_input`. `st_input` must be ragged-right. If not
@ -1637,7 +1637,7 @@ class RaggedTensor(composite_tensor.CompositeTensor):
Example: Example:
>>> st = tf.SparseTensor(indices=[[0, 0], [0, 1], [0, 2], [1, 0], [3, 0]], >>> st = tf.sparse.SparseTensor(indices=[[0, 0], [0, 1], [0, 2], [1, 0], [3, 0]],
... values=[1, 2, 3, 4, 5], ... values=[1, 2, 3, 4, 5],
... dense_shape=[4, 3]) ... dense_shape=[4, 3])
>>> tf.RaggedTensor.from_sparse(st).to_list() >>> tf.RaggedTensor.from_sparse(st).to_list()
@ -1690,7 +1690,7 @@ class RaggedTensor(composite_tensor.CompositeTensor):
st_input.values, segment_ids, num_segments, validate=False) st_input.values, segment_ids, num_segments, validate=False)
def to_sparse(self, name=None): def to_sparse(self, name=None):
"""Converts this `RaggedTensor` into a `tf.SparseTensor`. """Converts this `RaggedTensor` into a `tf.sparse.SparseTensor`.
Example: Example:

View File

@ -156,7 +156,7 @@ def set_intersection(a, b, validate_indices=True):
((1, 1, 0), 5), ((1, 1, 0), 5),
((1, 1, 1), 6), ((1, 1, 1), 6),
]) ])
a = tf.SparseTensor(list(a.keys()), list(a.values()), dense_shape=[2,2,2]) a = tf.sparse.SparseTensor(list(a.keys()), list(a.values()), dense_shape=[2,2,2])
# b = np.array([[{1}, {}], [{4}, {5, 6, 7, 8}]]) # b = np.array([[{1}, {}], [{4}, {5, 6, 7, 8}]])
b = collections.OrderedDict([ b = collections.OrderedDict([
@ -167,7 +167,7 @@ def set_intersection(a, b, validate_indices=True):
((1, 1, 2), 7), ((1, 1, 2), 7),
((1, 1, 3), 8), ((1, 1, 3), 8),
]) ])
b = tf.SparseTensor(list(b.keys()), list(b.values()), dense_shape=[2, 2, 4]) b = tf.sparse.SparseTensor(list(b.keys()), list(b.values()), dense_shape=[2, 2, 4])
# `tf.sets.intersection` is applied to each aligned pair of sets. # `tf.sets.intersection` is applied to each aligned pair of sets.
tf.sets.intersection(a, b) tf.sets.intersection(a, b)
@ -224,7 +224,7 @@ def set_difference(a, b, aminusb=True, validate_indices=True):
((1, 1, 0), 5), ((1, 1, 0), 5),
((1, 1, 1), 6), ((1, 1, 1), 6),
]) ])
a = tf.SparseTensor(list(a.keys()), list(a.values()), dense_shape=[2, 2, 2]) a = tf.sparse.SparseTensor(list(a.keys()), list(a.values()), dense_shape=[2, 2, 2])
# np.array([[{1, 3}, {2}], [{4, 5}, {5, 6, 7, 8}]]) # np.array([[{1, 3}, {2}], [{4, 5}, {5, 6, 7, 8}]])
b = collections.OrderedDict([ b = collections.OrderedDict([
@ -238,7 +238,7 @@ def set_difference(a, b, aminusb=True, validate_indices=True):
((1, 1, 2), 7), ((1, 1, 2), 7),
((1, 1, 3), 8), ((1, 1, 3), 8),
]) ])
b = tf.SparseTensor(list(b.keys()), list(b.values()), dense_shape=[2, 2, 4]) b = tf.sparse.SparseTensor(list(b.keys()), list(b.values()), dense_shape=[2, 2, 4])
# `set_difference` is applied to each aligned pair of sets. # `set_difference` is applied to each aligned pair of sets.
tf.sets.difference(a, b) tf.sets.difference(a, b)
@ -302,7 +302,7 @@ def set_union(a, b, validate_indices=True):
((1, 1, 0), 5), ((1, 1, 0), 5),
((1, 1, 1), 6), ((1, 1, 1), 6),
]) ])
a = tf.SparseTensor(list(a.keys()), list(a.values()), dense_shape=[2, 2, 2]) a = tf.sparse.SparseTensor(list(a.keys()), list(a.values()), dense_shape=[2, 2, 2])
# [[{1, 3}, {2}], [{4, 5}, {5, 6, 7, 8}]] # [[{1, 3}, {2}], [{4, 5}, {5, 6, 7, 8}]]
b = collections.OrderedDict([ b = collections.OrderedDict([
@ -316,7 +316,7 @@ def set_union(a, b, validate_indices=True):
((1, 1, 2), 7), ((1, 1, 2), 7),
((1, 1, 3), 8), ((1, 1, 3), 8),
]) ])
b = tf.SparseTensor(list(b.keys()), list(b.values()), dense_shape=[2, 2, 4]) b = tf.sparse.SparseTensor(list(b.keys()), list(b.values()), dense_shape=[2, 2, 4])
# `set_union` is applied to each aligned pair of sets. # `set_union` is applied to each aligned pair of sets.
tf.sets.union(a, b) tf.sets.union(a, b)

View File

@ -16,7 +16,7 @@
# pylint: disable=g-short-docstring-punctuation # pylint: disable=g-short-docstring-punctuation
"""Sparse Tensor Representation. """Sparse Tensor Representation.
See also `tf.SparseTensor`. See also `tf.sparse.SparseTensor`.
""" """
from __future__ import absolute_import from __future__ import absolute_import
@ -2460,7 +2460,7 @@ def sparse_softmax(sp_input, name=None):
values = np.asarray([[[0., np.e], [1., 0.]], [[np.e, 0.], [np.e, np.e]]]) values = np.asarray([[[0., np.e], [1., 0.]], [[np.e, 0.], [np.e, np.e]]])
indices = np.vstack(np.where(values)).astype(np.int64).T indices = np.vstack(np.where(values)).astype(np.int64).T
result = tf.sparse.softmax(tf.SparseTensor(indices, values, shape)) result = tf.sparse.softmax(tf.sparse.SparseTensor(indices, values, shape))
# ...returning a 3-D SparseTensor, equivalent to: # ...returning a 3-D SparseTensor, equivalent to:
# [? 1.] [1 ?] # [? 1.] [1 ?]
# [1. ? ] and [.5 .5] # [1. ? ] and [.5 .5]

View File

@ -310,7 +310,7 @@ def flatten(structure, expand_composites=False):
Args: Args:
structure: an arbitrarily nested structure. Note, numpy arrays are structure: an arbitrarily nested structure. Note, numpy arrays are
considered atoms and are not flattened. considered atoms and are not flattened.
expand_composites: If true, then composite tensors such as tf.SparseTensor expand_composites: If true, then composite tensors such as tf.sparse.SparseTensor
and tf.RaggedTensor are expanded into their component tensors. and tf.RaggedTensor are expanded into their component tensors.
Returns: Returns:
@ -364,7 +364,7 @@ def assert_same_structure(nest1, nest2, check_types=True,
considered the same if they are both list subtypes (which allows "list" considered the same if they are both list subtypes (which allows "list"
and "_ListWrapper" from trackable dependency tracking to compare and "_ListWrapper" from trackable dependency tracking to compare
equal). equal).
expand_composites: If true, then composite tensors such as `tf.SparseTensor` expand_composites: If true, then composite tensors such as `tf.sparse.SparseTensor`
and `tf.RaggedTensor` are expanded into their component tensors. and `tf.RaggedTensor` are expanded into their component tensors.
Raises: Raises:
@ -537,7 +537,7 @@ def pack_sequence_as(structure, flat_sequence, expand_composites=False):
tuples, and dicts. Note: numpy arrays and strings are considered tuples, and dicts. Note: numpy arrays and strings are considered
scalars. scalars.
flat_sequence: flat sequence to pack. flat_sequence: flat sequence to pack.
expand_composites: If true, then composite tensors such as `tf.SparseTensor` expand_composites: If true, then composite tensors such as `tf.sparse.SparseTensor`
and `tf.RaggedTensor` are expanded into their component tensors. and `tf.RaggedTensor` are expanded into their component tensors.
Returns: Returns:
@ -574,7 +574,7 @@ def map_structure(func, *structure, **kwargs):
Note that namedtuples with identical name and fields are always Note that namedtuples with identical name and fields are always
considered to have the same shallow structure. considered to have the same shallow structure.
* `expand_composites`: If set to `True`, then composite tensors such * `expand_composites`: If set to `True`, then composite tensors such
as `tf.SparseTensor` and `tf.RaggedTensor` are expanded into their as `tf.sparse.SparseTensor` and `tf.RaggedTensor` are expanded into their
component tensors. If `False` (the default), then composite tensors component tensors. If `False` (the default), then composite tensors
are not expanded. are not expanded.
@ -762,7 +762,7 @@ def assert_shallow_structure(shallow_tree,
`input_tree` have to be the same. Note that even with check_types==True, `input_tree` have to be the same. Note that even with check_types==True,
this function will consider two different namedtuple classes with the same this function will consider two different namedtuple classes with the same
name and _fields attribute to be the same class. name and _fields attribute to be the same class.
expand_composites: If true, then composite tensors such as tf.SparseTensor expand_composites: If true, then composite tensors such as tf.sparse.SparseTensor
and tf.RaggedTensor are expanded into their component tensors. and tf.RaggedTensor are expanded into their component tensors.
Raises: Raises:
TypeError: If `shallow_tree` is a sequence but `input_tree` is not. TypeError: If `shallow_tree` is a sequence but `input_tree` is not.
@ -911,7 +911,7 @@ def flatten_up_to(shallow_tree, input_tree, check_types=True,
Note, numpy arrays are considered scalars. Note, numpy arrays are considered scalars.
check_types: bool. If True, check that each node in shallow_tree has the check_types: bool. If True, check that each node in shallow_tree has the
same type as the corresponding node in input_tree. same type as the corresponding node in input_tree.
expand_composites: If true, then composite tensors such as tf.SparseTensor expand_composites: If true, then composite tensors such as tf.sparse.SparseTensor
and tf.RaggedTensor are expanded into their component tensors. and tf.RaggedTensor are expanded into their component tensors.
Returns: Returns:
@ -1015,7 +1015,7 @@ def flatten_with_tuple_paths_up_to(shallow_tree,
Note, numpy arrays are considered scalars. Note, numpy arrays are considered scalars.
check_types: bool. If True, check that each node in shallow_tree has the check_types: bool. If True, check that each node in shallow_tree has the
same type as the corresponding node in input_tree. same type as the corresponding node in input_tree.
expand_composites: If true, then composite tensors such as tf.SparseTensor expand_composites: If true, then composite tensors such as tf.sparse.SparseTensor
and tf.RaggedTensor are expanded into their component tensors. and tf.RaggedTensor are expanded into their component tensors.
Returns: Returns:
@ -1233,7 +1233,7 @@ def get_traverse_shallow_structure(traverse_fn, structure,
shallow structure of the same type, describing which parts of the shallow structure of the same type, describing which parts of the
substructure to traverse. substructure to traverse.
structure: The structure to traverse. structure: The structure to traverse.
expand_composites: If true, then composite tensors such as tf.SparseTensor expand_composites: If true, then composite tensors such as tf.sparse.SparseTensor
and tf.RaggedTensor are expanded into their component tensors. and tf.RaggedTensor are expanded into their component tensors.
Returns: Returns:
@ -1313,7 +1313,7 @@ def yield_flat_paths(nest, expand_composites=False):
Args: Args:
nest: the value to produce a flattened paths list for. nest: the value to produce a flattened paths list for.
expand_composites: If true, then composite tensors such as tf.SparseTensor expand_composites: If true, then composite tensors such as tf.sparse.SparseTensor
and tf.RaggedTensor are expanded into their component tensors. and tf.RaggedTensor are expanded into their component tensors.
Yields: Yields:
@ -1338,7 +1338,7 @@ def flatten_with_joined_string_paths(structure, separator="/",
structure: the nested structure to flatten. structure: the nested structure to flatten.
separator: string to separate levels of hierarchy in the results, defaults separator: string to separate levels of hierarchy in the results, defaults
to '/'. to '/'.
expand_composites: If true, then composite tensors such as tf.SparseTensor expand_composites: If true, then composite tensors such as tf.sparse.SparseTensor
and tf.RaggedTensor are expanded into their component tensors. and tf.RaggedTensor are expanded into their component tensors.
Returns: Returns:
@ -1362,7 +1362,7 @@ def flatten_with_tuple_paths(structure, expand_composites=False):
Args: Args:
structure: the nested structure to flatten. structure: the nested structure to flatten.
expand_composites: If true, then composite tensors such as tf.SparseTensor expand_composites: If true, then composite tensors such as tf.sparse.SparseTensor
and tf.RaggedTensor are expanded into their component tensors. and tf.RaggedTensor are expanded into their component tensors.
Returns: Returns:

View File

@ -234,7 +234,7 @@ PyObject* AssertSameStructure(PyObject* o1, PyObject* o2, bool check_types,
// nest: an arbitrarily nested structure or a scalar object. Note, numpy // nest: an arbitrarily nested structure or a scalar object. Note, numpy
// arrays are considered scalars. // arrays are considered scalars.
// expand_composites: If true, then composite tensors (such as // expand_composites: If true, then composite tensors (such as
// `tf.SparseTensor` and `tf.RaggedTensor` are flattened into their // `tf.sparse.SparseTensor` and `tf.RaggedTensor` are flattened into their
// component tensors. // component tensors.
// //
// Returns: // Returns:

View File

@ -244,7 +244,7 @@ PYBIND11_MODULE(_pywrap_utils, m) {
Args: Args:
nest: an arbitrarily nested structure or a scalar object. Note, numpy nest: an arbitrarily nested structure or a scalar object. Note, numpy
arrays are considered scalars. arrays are considered scalars.
expand_composites: If true, then composite tensors such as `tf.SparseTensor` expand_composites: If true, then composite tensors such as `tf.sparse.SparseTensor`
and `tf.RaggedTensor` are expanded into their component tensors. and `tf.RaggedTensor` are expanded into their component tensors.
Returns: Returns: