tf.random.experimental.index_shuffle
Stay organized with collections
Save and categorize content based on your preferences.
Outputs the position of index
in a permutation of [0, ..., max_index].
tf.random.experimental.index_shuffle(
index, seed, max_index
)
For each possible seed
and max_index
there is one pseudorandom permutation
of the sequence S=[0, ..., max_index]. Instead of materializing the full array
we can compute the new position of any single element in S. This can be useful
for very large max_index
s.
The input index
and output can be used as indices to shuffle a vector.
For example:
vector = tf.constant(['e0', 'e1', 'e2', 'e3'])
indices = tf.random.experimental.index_shuffle(tf.range(4), [5, 9], 3)
shuffled_vector = tf.gather(vector, indices)
print(shuffled_vector)
tf.Tensor([b'e2' b'e0' b'e1' b'e3'], shape=(4,), dtype=string)
More usefully, it can be used in a streaming (aka online) scenario such as
tf.data
, where each element of vector
is processed individually and the
whole vector
is never materialized in memory.
dataset = tf.data.Dataset.range(10)
dataset = dataset.map(
lambda idx: tf.random.experimental.index_shuffle(idx, [5, 8], 9))
print(list(dataset.as_numpy_iterator()))
[3, 8, 0, 1, 2, 7, 6, 9, 4, 5]
This operation is stateless (like other tf.random.stateless_*
functions),
meaning the output is fully determined by the seed
(other inputs being
equal).
Each seed
choice corresponds to one permutation, so when calling this
function
multiple times for the same shuffling, please make sure to use the same
seed
. For example:
seed = [5, 9]
idx0 = tf.random.experimental.index_shuffle(0, seed, 3)
idx1 = tf.random.experimental.index_shuffle(1, seed, 3)
idx2 = tf.random.experimental.index_shuffle(2, seed, 3)
idx3 = tf.random.experimental.index_shuffle(3, seed, 3)
shuffled_vector = tf.gather(vector, [idx0, idx1, idx2, idx3])
print(shuffled_vector)
tf.Tensor([b'e2' b'e0' b'e1' b'e3'], shape=(4,), dtype=string)
Args |
index
|
An integer scalar tensor or vector with values in [0, max_index ].
It can be seen as either a value v in the sequence S =[0, ...,
max_index ] to be permutated, or as an index of an element e in a
shuffled vector.
|
seed
|
A tensor of shape [2] or [n, 2] with dtype int32/uint32/int64/uint64.
The RNG seed. If the rank is unknown during graph building it must be 1 at
runtime.
|
max_index
|
A non-negative tensor with the same shape and dtype as index .
The upper bound (inclusive).
|
Returns |
If all inputs were scalar (shape [2] for seed ) the output will be a scalar
with the same dtype as index . The output can be seen as the new position
of v in S , or as the index of e in the vector before shuffling.
If one or multiple inputs were vectors (shape [n, 2] for seed ) then the
output will be a vector of the same size which each element shuffled
independently. Scalar values are broadcasted in this case.
|
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 2023-03-23 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 2023-03-23 UTC."],[],[],null,["# tf.random.experimental.index_shuffle\n\n\u003cbr /\u003e\n\n|----------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.11.1/tensorflow/python/ops/stateless_random_ops.py#L259-L335) |\n\nOutputs the position of `index` in a permutation of \\[0, ..., max_index\\].\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.random.experimental.index_shuffle`](https://www.tensorflow.org/api_docs/python/tf/random/experimental/index_shuffle)\n\n\u003cbr /\u003e\n\n tf.random.experimental.index_shuffle(\n index, seed, max_index\n )\n\nFor each possible `seed` and `max_index` there is one pseudorandom permutation\nof the sequence S=\\[0, ..., max_index\\]. Instead of materializing the full array\nwe can compute the new position of any single element in S. This can be useful\nfor very large `max_index`s.\n\nThe input `index` and output can be used as indices to shuffle a vector.\nFor example: \n\n vector = tf.constant(['e0', 'e1', 'e2', 'e3'])\n indices = tf.random.experimental.index_shuffle(tf.range(4), [5, 9], 3)\n shuffled_vector = tf.gather(vector, indices)\n print(shuffled_vector)\n tf.Tensor([b'e2' b'e0' b'e1' b'e3'], shape=(4,), dtype=string)\n\nMore usefully, it can be used in a streaming (aka online) scenario such as\n[`tf.data`](../../../tf/data), where each element of `vector` is processed individually and the\nwhole `vector` is never materialized in memory. \n\n dataset = tf.data.Dataset.range(10)\n dataset = dataset.map(\n lambda idx: tf.random.experimental.index_shuffle(idx, [5, 8], 9))\n print(list(dataset.as_numpy_iterator()))\n [3, 8, 0, 1, 2, 7, 6, 9, 4, 5]\n\nThis operation is stateless (like other `tf.random.stateless_*` functions),\nmeaning the output is fully determined by the `seed` (other inputs being\nequal).\nEach `seed` choice corresponds to one permutation, so when calling this\nfunction\nmultiple times for the same shuffling, please make sure to use the same\n`seed`. For example: \n\n seed = [5, 9]\n idx0 = tf.random.experimental.index_shuffle(0, seed, 3)\n idx1 = tf.random.experimental.index_shuffle(1, seed, 3)\n idx2 = tf.random.experimental.index_shuffle(2, seed, 3)\n idx3 = tf.random.experimental.index_shuffle(3, seed, 3)\n shuffled_vector = tf.gather(vector, [idx0, idx1, idx2, idx3])\n print(shuffled_vector)\n tf.Tensor([b'e2' b'e0' b'e1' b'e3'], shape=(4,), dtype=string)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `index` | An integer scalar tensor or vector with values in \\[0, `max_index`\\]. It can be seen as either a value `v` in the sequence `S`=\\[0, ..., `max_index`\\] to be permutated, or as an index of an element `e` in a shuffled vector. |\n| `seed` | A tensor of shape \\[2\\] or \\[n, 2\\] with dtype int32/uint32/int64/uint64. The RNG seed. If the rank is unknown during graph building it must be 1 at runtime. |\n| `max_index` | A non-negative tensor with the same shape and dtype as `index`. The upper bound (inclusive). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| If all inputs were scalar (shape \\[2\\] for `seed`) the output will be a scalar with the same dtype as `index`. The output can be seen as the new position of `v` in `S`, or as the index of `e` in the vector before shuffling. If one or multiple inputs were vectors (shape \\[n, 2\\] for `seed`) then the output will be a vector of the same size which each element shuffled independently. Scalar values are broadcasted in this case. ||\n\n\u003cbr /\u003e"]]