tf.sparse.to_indicator
Stay organized with collections
Save and categorize content based on your preferences.
Converts a SparseTensor
of ids into a dense bool indicator tensor.
tf.sparse.to_indicator(
sp_input, vocab_size, name=None
)
The last dimension of sp_input.indices
is discarded and replaced with
the values of sp_input
. If sp_input.dense_shape = [D0, D1, ..., Dn, K]
,
then output.shape = [D0, D1, ..., Dn, vocab_size]
, where
output[d_0, d_1, ..., d_n, sp_input[d_0, d_1, ..., d_n, k]] = True
and False elsewhere in output
.
For example, if sp_input.dense_shape = [2, 3, 4]
with non-empty values:
[0, 0, 0]: 0
[0, 1, 0]: 10
[1, 0, 3]: 103
[1, 1, 1]: 150
[1, 1, 2]: 149
[1, 1, 3]: 150
[1, 2, 1]: 121
and vocab_size = 200
, then the output will be a [2, 3, 200]
dense bool
tensor with False everywhere except at positions
(0, 0, 0), (0, 1, 10), (1, 0, 103), (1, 1, 149), (1, 1, 150),
(1, 2, 121).
Note that repeats are allowed in the input SparseTensor.
This op is useful for converting SparseTensor
s into dense formats for
compatibility with ops that expect dense tensors.
The input SparseTensor
must be in row-major order.
Args |
sp_input
|
A SparseTensor with values property of type int32 or
int64 .
|
vocab_size
|
A scalar int64 Tensor (or Python int) containing the new size
of the last dimension, all(0 <= sp_input.values < vocab_size) .
|
name
|
A name prefix for the returned tensors (optional)
|
Returns |
A dense bool indicator tensor representing the indices with specified value.
|
Raises |
TypeError
|
If sp_input is not a SparseTensor .
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2024-04-26 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-04-26 UTC."],[],[],null,["# tf.sparse.to_indicator\n\n\u003cbr /\u003e\n\n|--------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/sparse_ops.py#L1737-L1799) |\n\nConverts a `SparseTensor` of ids into a dense bool indicator tensor.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.sparse.to_indicator`](https://www.tensorflow.org/api_docs/python/tf/sparse/to_indicator), [`tf.compat.v1.sparse_to_indicator`](https://www.tensorflow.org/api_docs/python/tf/sparse/to_indicator)\n\n\u003cbr /\u003e\n\n tf.sparse.to_indicator(\n sp_input, vocab_size, name=None\n )\n\nThe last dimension of `sp_input.indices` is discarded and replaced with\nthe values of `sp_input`. If `sp_input.dense_shape = [D0, D1, ..., Dn, K]`,\nthen `output.shape = [D0, D1, ..., Dn, vocab_size]`, where \n\n output[d_0, d_1, ..., d_n, sp_input[d_0, d_1, ..., d_n, k]] = True\n\nand False elsewhere in `output`.\n\nFor example, if `sp_input.dense_shape = [2, 3, 4]` with non-empty values: \n\n [0, 0, 0]: 0\n [0, 1, 0]: 10\n [1, 0, 3]: 103\n [1, 1, 1]: 150\n [1, 1, 2]: 149\n [1, 1, 3]: 150\n [1, 2, 1]: 121\n\nand `vocab_size = 200`, then the output will be a `[2, 3, 200]` dense bool\ntensor with False everywhere except at positions \n\n (0, 0, 0), (0, 1, 10), (1, 0, 103), (1, 1, 149), (1, 1, 150),\n (1, 2, 121).\n\nNote that repeats are allowed in the input SparseTensor.\nThis op is useful for converting `SparseTensor`s into dense formats for\ncompatibility with ops that expect dense tensors.\n\nThe input `SparseTensor` must be in row-major order.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|--------------|--------------------------------------------------------------------------------------------------------------------------------|\n| `sp_input` | A `SparseTensor` with `values` property of type `int32` or `int64`. |\n| `vocab_size` | A scalar int64 Tensor (or Python int) containing the new size of the last dimension, `all(0 \u003c= sp_input.values \u003c vocab_size)`. |\n| `name` | A name prefix for the returned tensors (optional) |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A dense bool indicator tensor representing the indices with specified value. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|-------------|----------------------------------------|\n| `TypeError` | If `sp_input` is not a `SparseTensor`. |\n\n\u003cbr /\u003e"]]