Converts this `RaggedTensor` into a `tf.SparseTensor`. Example: ```python >>> rt = ragged.constant([[1, 2, 3], [4], [], [5, 6]]) >>> rt.to_sparse().eval() SparseTensorValue(indices=[[0, 0], [0, 1], [0, 2], [1, 0], [3, 0], [3, 1]], values=[1, 2, 3, 4, 5, 6]
(self, name=None)
| 1653 | st_input.values, segment_ids, num_segments, validate=False) |
| 1654 | |
| 1655 | def to_sparse(self, name=None): |
| 1656 | """Converts this `RaggedTensor` into a `tf.SparseTensor`. |
| 1657 | |
| 1658 | Example: |
| 1659 | |
| 1660 | ```python |
| 1661 | >>> rt = ragged.constant([[1, 2, 3], [4], [], [5, 6]]) |
| 1662 | >>> rt.to_sparse().eval() |
| 1663 | SparseTensorValue(indices=[[0, 0], [0, 1], [0, 2], [1, 0], [3, 0], [3, 1]], |
| 1664 | values=[1, 2, 3, 4, 5, 6], |
| 1665 | dense_shape=[4, 3]) |
| 1666 | ``` |
| 1667 | |
| 1668 | Args: |
| 1669 | name: A name prefix for the returned tensors (optional). |
| 1670 | |
| 1671 | Returns: |
| 1672 | A SparseTensor with the same values as `self`. |
| 1673 | """ |
| 1674 | with ops.name_scope(name, "RaggedToSparse", [self]): |
| 1675 | result = gen_ragged_conversion_ops.ragged_tensor_to_sparse( |
| 1676 | self.nested_row_splits, self.flat_values, name=name) |
| 1677 | return sparse_tensor.SparseTensor(result.sparse_indices, |
| 1678 | result.sparse_values, |
| 1679 | result.sparse_dense_shape) |
| 1680 | |
| 1681 | @classmethod |
| 1682 | def _from_variant(cls, |