Merge pull request from Angus-Luo:master

PiperOrigin-RevId: 304490479
Change-Id: I7ea0729d31342f588adf2c42e1eb4f15bd725e60
This commit is contained in:
TensorFlower Gardener 2020-04-02 15:36:41 -07:00
commit 315ee4a1b9
29 changed files with 141 additions and 115 deletions

View File

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

View File

@ -87,7 +87,8 @@ class TextEmbeddingModel(tf.train.Checkpoint):
return tf.nn.safe_embedding_lookup_sparse(
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,
combiner="sqrtn")

View File

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

View File

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

View File

@ -100,15 +100,15 @@ def dense_to_ragged_batch(batch_size,
@tf_export("data.experimental.dense_to_sparse_batch")
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
consecutive elements of the dataset, which might have different
shapes, into a single element. The resulting element has three
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
resulting `tf.SparseTensor`, to which the effective batch size is
resulting `tf.sparse.SparseTensor`, to which the effective batch size is
prepended. For example:
```python
@ -133,9 +133,9 @@ def dense_to_sparse_batch(batch_size, row_shape):
consecutive elements of this dataset to combine in a single batch.
row_shape: A `tf.TensorShape` or `tf.int64` vector tensor-like object
representing the equivalent dense shape of a row in the resulting
`tf.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
dimension.
`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 dimension.
Returns:
A `Dataset` transformation function, which can be passed to
@ -295,7 +295,7 @@ def unbatch():
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):
"""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
any elements with length longer than `max(bucket_boundaries)`.
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
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

View File

@ -37,7 +37,7 @@ class FromSparseTensorSlicesTest(test_base.DatasetTestBase,
@combinations.generate(
combinations.combine(tf_api_version=1, mode=["graph"]))
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)
iterator = dataset_ops.make_initializable_iterator(
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.
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`.
>>> a = 1 # Integer element
@ -1456,7 +1456,7 @@ class DatasetV2(tracking_base.Trackable, composite_tensor.CompositeTensor):
array([[ 10, 100], [ 11, 12]], dtype=int32))]
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:
batch_size: A `tf.int64` scalar `tf.Tensor`, representing the number of
@ -2283,10 +2283,10 @@ class DatasetV1(DatasetV2):
@staticmethod
@deprecation.deprecated(None, "Use `tf.data.Dataset.from_tensor_slices()`.")
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:
sparse_tensor: A `tf.SparseTensor`.
sparse_tensor: A `tf.sparse.SparseTensor`.
Returns:
Dataset: A `Dataset` of rank-(N-1) sparse tensors.
@ -2891,14 +2891,14 @@ class TensorSliceDataset(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):
"""See `Dataset.from_sparse_tensor_slices()` for details."""
if not isinstance(sparse_tensor, sparse_tensor_lib.SparseTensor):
raise TypeError(
"`sparse_tensor` must be a `tf.SparseTensor` object. Was {}.".format(
sparse_tensor))
"`sparse_tensor` must be a `tf.sparse.SparseTensor` object."
"Was {}.".format(sparse_tensor))
self._sparse_tensor = sparse_tensor
indices_shape = self._sparse_tensor.indices.get_shape()

View File

@ -448,7 +448,7 @@ class Iterator(trackable.Trackable):
def output_classes(self):
"""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:
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):
"""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:
A nested structure of Python `type` objects corresponding to each

View File

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

View File

@ -1969,7 +1969,8 @@ class _CategoricalColumn(_FeatureColumn):
WARNING: Do not subclass this layer unless you know what you are doing:
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

View File

@ -2515,7 +2515,8 @@ def _create_dense_column_weighted_sum(column, transformation_cache,
class CategoricalColumn(FeatureColumn):
"""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

View File

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

View File

@ -340,7 +340,7 @@ class Tensor(tensor_like.TensorLike):
shape of a tensor at execution time.
A number of specialized tensors are available: see `tf.Variable`,
`tf.constant`, `tf.placeholder`, `tf.SparseTensor`, and
`tf.constant`, `tf.placeholder`, `tf.sparse.SparseTensor`, and
`tf.RaggedTensor`.
For more on Tensors, see the [guide](https://tensorflow.org/guide/tensor).

View File

@ -297,18 +297,18 @@ _pywrap_utils.RegisterType("SparseTensorValue", SparseTensorValue)
@tf_export("SparseTensorSpec")
class SparseTensorSpec(type_spec.BatchableTypeSpec):
"""Type specification for a `tf.SparseTensor`."""
"""Type specification for a `tf.sparse.SparseTensor`."""
__slots__ = ["_shape", "_dtype"]
value_type = property(lambda self: SparseTensor)
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:
shape: The dense shape of the `SparseTensor`, or `None` to allow
any dense shape.
shape: The dense shape of the `SparseTensor`, or `None` to allow any dense
shape.
dtype: `tf.DType` of values in the `SparseTensor`.
"""
self._shape = tensor_shape.as_shape(shape)
@ -472,13 +472,14 @@ def convert_to_tensor_or_sparse_tensor(value, dtype=None, name=None):
def is_sparse(x):
"""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`.
Args:
x: A python object to check.
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))

View File

@ -782,7 +782,7 @@ def local_conv_sparse_matmul(inputs, kernel, kernel_idxs, kernel_shape,
output_shape):
"""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
matrix-multiply. It also reshapes `inputs` to 2-D and `output` to (N+2)-D.

View File

@ -3085,7 +3085,8 @@ def sparse_placeholder(dtype, shape=None, name=None):
print(sess.run(y, feed_dict={
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)
print(sess.run(y, feed_dict={x: sp_value})) # Will succeed.
```
@ -3487,7 +3488,7 @@ def edit_distance(hypothesis, truth, normalize=True, name="edit_distance"):
# 'hypothesis' is a tensor of shape `[2, 1]` with variable-length values:
# (0,0) = ["a"]
# (1,0) = ["b"]
hypothesis = tf.SparseTensor(
hypothesis = tf.sparse.SparseTensor(
[[0, 0, 0],
[1, 0, 0]],
["a", "b"],
@ -3498,7 +3499,7 @@ def edit_distance(hypothesis, truth, normalize=True, name="edit_distance"):
# (0,1) = ["a"]
# (1,0) = ["b", "c"]
# (1,1) = ["a"]
truth = tf.SparseTensor(
truth = tf.sparse.SparseTensor(
[[0, 1, 0],
[1, 0, 0],
[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.
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])

View File

@ -106,7 +106,7 @@ def map_fn(fn,
* A `tf.DType` or `tf.TensorSpec` (to describe a `tf.Tensor`)
* 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.
#### RaggedTensors
@ -159,11 +159,11 @@ def map_fn(fn,
#### 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
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
`tf.SparseTensorSpec` to specify `fn_output_signature`. The individual
@ -171,7 +171,7 @@ def map_fn(fn,
`SparseTensor` with one more dimension.
>>> # 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.Tensor: shape=(4,), dtype=int32, numpy=array([2, 0, 7, 0], dtype=int32)>
@ -191,10 +191,15 @@ def map_fn(fn,
*rows* of a `SparseTensor`. If you wish to map a function over the nonzero
values, then you should use:
* `tf.SparseTensor(st.indices, fn(st.values), st.dense_shape)`
(if the function is expressible as TensorFlow ops)
* `tf.SparseTensor(st.indices, tf.map_fn(fn, st.values), st.dense_shape)`
(otherwise).
* If the function is expressible as TensorFlow ops, use:
```python
tf.sparse.SparseTensor(st.indices, fn(st.values), st.dense_shape)
```
* Otherwise, use:
```python
tf.sparse.SparseTensor(st.indices, tf.map_fn(fn, st.values),
st.dense_shape)
```
#### `map_fn` vs. vectorized operations
@ -276,7 +281,7 @@ def map_fn(fn,
* A `tf.DType` or `tf.TensorSpec` (to describe a `tf.Tensor`)
* 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.
Returns:

View File

@ -1541,8 +1541,8 @@ def equal(x, y, name=None):
<tf.Tensor: shape=(2,), dtype=bool, numpy=array([ True, True])>
Args:
x: A `tf.Tensor` or `tf.SparseTensor` or `tf.IndexedSlices`.
y: 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.sparse.SparseTensor` or `tf.IndexedSlices`.
name: A name for the operation (optional).
Returns:
@ -1577,8 +1577,8 @@ def not_equal(x, y, name=None):
<tf.Tensor: shape=(2,), dtype=bool, numpy=array([False, False])>
Args:
x: A `tf.Tensor` or `tf.SparseTensor` or `tf.IndexedSlices`.
y: 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.sparse.SparseTensor` or `tf.IndexedSlices`.
name: A name for the operation (optional).
Returns:
@ -3016,12 +3016,12 @@ def matmul(a,
**does not support `tf.sparse.SparseTensor`**, it just makes optimizations
that assume most values in `a` are zero.
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
**does not support `tf.sparse.SparseTensor`**, it just makes optimizations
that assume most values in `a` are zero.
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).
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)).
Args:
y: A tf.SparseTensor.
y: A tf.sparse.SparseTensor.
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 = [
self._convert_helper(t) for t in (y.indices, y.values, y.dense_shape)

View File

@ -1707,17 +1707,17 @@ class RaggedTensor(composite_tensor.CompositeTensor, tensor_like.TensorLike):
@classmethod
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
from the same row in `st_input`. `st_input` must be ragged-right. If not
it is not ragged-right, then an error will be generated.
Example:
>>> st = tf.SparseTensor(indices=[[0, 0], [0, 1], [0, 2], [1, 0], [3, 0]],
... values=[1, 2, 3, 4, 5],
... dense_shape=[4, 3])
>>> indices = [[0, 0], [0, 1], [0, 2], [1, 0], [3, 0]]
>>> st = tf.sparse.SparseTensor(indices=indices,
... values=[1, 2, 3, 4, 5],
... dense_shape=[4, 3])
>>> tf.RaggedTensor.from_sparse(st).to_list()
[[1, 2, 3], [4], [], [5]]
@ -1768,7 +1768,7 @@ class RaggedTensor(composite_tensor.CompositeTensor, tensor_like.TensorLike):
st_input.values, segment_ids, num_segments, validate=False)
def to_sparse(self, name=None):
"""Converts this `RaggedTensor` into a `tf.SparseTensor`.
"""Converts this `RaggedTensor` into a `tf.sparse.SparseTensor`.
Example:

View File

@ -156,7 +156,8 @@ def set_intersection(a, b, validate_indices=True):
((1, 1, 0), 5),
((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 = collections.OrderedDict([
@ -167,7 +168,8 @@ def set_intersection(a, b, validate_indices=True):
((1, 1, 2), 7),
((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(a, b)
@ -224,7 +226,8 @@ def set_difference(a, b, aminusb=True, validate_indices=True):
((1, 1, 0), 5),
((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}]])
b = collections.OrderedDict([
@ -238,7 +241,8 @@ def set_difference(a, b, aminusb=True, validate_indices=True):
((1, 1, 2), 7),
((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.
tf.sets.difference(a, b)
@ -302,7 +306,8 @@ def set_union(a, b, validate_indices=True):
((1, 1, 0), 5),
((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}]]
b = collections.OrderedDict([
@ -316,7 +321,8 @@ def set_union(a, b, validate_indices=True):
((1, 1, 2), 7),
((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.
tf.sets.union(a, b)

View File

@ -16,7 +16,7 @@
# pylint: disable=g-short-docstring-punctuation
"""Sparse Tensor Representation.
See also `tf.SparseTensor`.
See also `tf.sparse.SparseTensor`.
"""
from __future__ import absolute_import
@ -2510,7 +2510,7 @@ def sparse_softmax(sp_input, name=None):
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
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:
# [? 1.] [1 ?]
# [1. ? ] and [.5 .5]

View File

@ -310,8 +310,9 @@ def flatten(structure, expand_composites=False):
Args:
structure: an arbitrarily nested structure. Note, numpy arrays are
considered atoms and are not flattened.
expand_composites: If true, then composite tensors such as tf.SparseTensor
and tf.RaggedTensor are expanded into their component tensors.
expand_composites: If true, then composite tensors such as
`tf.sparse.SparseTensor` and `tf.RaggedTensor` are expanded into their
component tensors.
Returns:
A Python list, the flattened version of the input.
@ -357,15 +358,16 @@ def assert_same_structure(nest1, nest2, check_types=True,
nest1: an arbitrarily nested structure.
nest2: an arbitrarily nested structure.
check_types: if `True` (default) types of sequences are checked as well,
including the keys of dictionaries. If set to `False`, for example a
list and a tuple of objects will look the same if they have the same
size. Note that namedtuples with identical name and fields are always
considered to have the same shallow structure. Two types will also be
considered the same if they are both list subtypes (which allows "list"
and "_ListWrapper" from trackable dependency tracking to compare
equal).
expand_composites: If true, then composite tensors such as `tf.SparseTensor`
and `tf.RaggedTensor` are expanded into their component tensors.
including the keys of dictionaries. If set to `False`, for example a
list and a tuple of objects will look the same if they have the same
size. Note that namedtuples with identical name and fields are always
considered to have the same shallow structure. Two types will also be
considered the same if they are both list subtypes (which allows "list"
and "_ListWrapper" from trackable dependency tracking to compare
equal).
expand_composites: If true, then composite tensors such as
`tf.sparse.SparseTensor` and `tf.RaggedTensor` are expanded into their
component tensors.
Raises:
ValueError: If the two structures do not have the same number of elements or
@ -534,11 +536,12 @@ def pack_sequence_as(structure, flat_sequence, expand_composites=False):
Args:
structure: Nested structure, whose structure is given by nested lists,
tuples, and dicts. Note: numpy arrays and strings are considered
scalars.
tuples, and dicts. Note: numpy arrays and strings are considered
scalars.
flat_sequence: flat sequence to pack.
expand_composites: If true, then composite tensors such as `tf.SparseTensor`
and `tf.RaggedTensor` are expanded into their component tensors.
expand_composites: If true, then composite tensors such as
`tf.sparse.SparseTensor` and `tf.RaggedTensor` are expanded into their
component tensors.
Returns:
packed: `flat_sequence` converted to have the same recursive structure as
@ -574,9 +577,9 @@ def map_structure(func, *structure, **kwargs):
Note that namedtuples with identical name and fields are always
considered to have the same shallow structure.
* `expand_composites`: If set to `True`, then composite tensors such
as `tf.SparseTensor` and `tf.RaggedTensor` are expanded into their
component tensors. If `False` (the default), then composite tensors
are not expanded.
as `tf.sparse.SparseTensor` and `tf.RaggedTensor` are expanded into
their component tensors. If `False` (the default), then composite
tensors are not expanded.
Returns:
A new structure with the same arity as `structure`, whose values correspond
@ -762,8 +765,9 @@ def assert_shallow_structure(shallow_tree,
`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
name and _fields attribute to be the same class.
expand_composites: If true, then composite tensors such as tf.SparseTensor
and tf.RaggedTensor are expanded into their component tensors.
expand_composites: If true, then composite tensors such as
`tf.sparse.SparseTensor` and `tf.RaggedTensor` are expanded into their
component tensors.
Raises:
TypeError: If `shallow_tree` is a sequence but `input_tree` is not.
TypeError: If the sequence types of `shallow_tree` are different from
@ -911,8 +915,9 @@ def flatten_up_to(shallow_tree, input_tree, check_types=True,
Note, numpy arrays are considered scalars.
check_types: bool. If True, check that each node in shallow_tree has the
same type as the corresponding node in input_tree.
expand_composites: If true, then composite tensors such as tf.SparseTensor
and tf.RaggedTensor are expanded into their component tensors.
expand_composites: If true, then composite tensors such as
`tf.sparse.SparseTensor` and `tf.RaggedTensor` are expanded into their
component tensors.
Returns:
A Python list, the partially flattened version of `input_tree` according to
@ -1015,8 +1020,9 @@ def flatten_with_tuple_paths_up_to(shallow_tree,
Note, numpy arrays are considered scalars.
check_types: bool. If True, check that each node in shallow_tree has the
same type as the corresponding node in input_tree.
expand_composites: If true, then composite tensors such as tf.SparseTensor
and tf.RaggedTensor are expanded into their component tensors.
expand_composites: If true, then composite tensors such as
`tf.sparse.SparseTensor` and `tf.RaggedTensor` are expanded into their
component tensors.
Returns:
A Python list, the partially flattened version of `input_tree` according to
@ -1233,8 +1239,9 @@ def get_traverse_shallow_structure(traverse_fn, structure,
shallow structure of the same type, describing which parts of the
substructure to traverse.
structure: The structure to traverse.
expand_composites: If true, then composite tensors such as tf.SparseTensor
and tf.RaggedTensor are expanded into their component tensors.
expand_composites: If true, then composite tensors such as
`tf.sparse.SparseTensor` and `tf.RaggedTensor` are expanded into their
component tensors.
Returns:
A shallow structure containing python bools, which can be passed to
@ -1313,12 +1320,13 @@ def yield_flat_paths(nest, expand_composites=False):
Args:
nest: the value to produce a flattened paths list for.
expand_composites: If true, then composite tensors such as tf.SparseTensor
and tf.RaggedTensor are expanded into their component tensors.
expand_composites: If true, then composite tensors such as
`tf.sparse.SparseTensor` and `tf.RaggedTensor` are expanded into their
component tensors.
Yields:
Tuples containing index or key values which form the path to a specific
leaf value in the nested structure.
leaf value in the nested structure.
"""
is_seq = is_sequence_or_composite if expand_composites else is_sequence
for k, _ in _yield_flat_up_to(nest, nest, is_seq):
@ -1338,8 +1346,9 @@ def flatten_with_joined_string_paths(structure, separator="/",
structure: the nested structure to flatten.
separator: string to separate levels of hierarchy in the results, defaults
to '/'.
expand_composites: If true, then composite tensors such as tf.SparseTensor
and tf.RaggedTensor are expanded into their component tensors.
expand_composites: If true, then composite tensors such as
`tf.sparse.SparseTensor` and `tf.RaggedTensor` are expanded into their
component tensors.
Returns:
A list of (string, data element) tuples.
@ -1362,8 +1371,9 @@ def flatten_with_tuple_paths(structure, expand_composites=False):
Args:
structure: the nested structure to flatten.
expand_composites: If true, then composite tensors such as tf.SparseTensor
and tf.RaggedTensor are expanded into their component tensors.
expand_composites: If true, then composite tensors such as
`tf.sparse.SparseTensor` and `tf.RaggedTensor` are expanded into their
component tensors.
Returns:
A list of `(tuple_path, leaf_element)` tuples. Each `tuple_path` is a tuple

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
// arrays are considered scalars.
// 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.
//
// Returns:

View File

@ -244,7 +244,7 @@ PYBIND11_MODULE(_pywrap_utils, m) {
Args:
nest: an arbitrarily nested structure or a scalar object. Note, numpy
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.
Returns: