tf.reverse_sequence
Stay organized with collections
Save and categorize content based on your preferences.
Reverses variable length slices.
tf.reverse_sequence(
input, seq_lengths, seq_axis=None, batch_axis=None, name=None
)
This op first slices input
along the dimension batch_axis
, and for
each slice i
, reverses the first seq_lengths[i]
elements along the
dimension seq_axis
.
The elements of seq_lengths
must obey seq_lengths[i] <=
input.dims[seq_axis]
, and seq_lengths
must be a vector of length
input.dims[batch_axis]
.
The output slice i
along dimension batch_axis
is then given by
input slice i
, with the first seq_lengths[i]
slices along
dimension seq_axis
reversed.
Example usage:
seq_lengths = [7, 2, 3, 5]
input = [[1, 2, 3, 4, 5, 0, 0, 0], [1, 2, 0, 0, 0, 0, 0, 0],
[1, 2, 3, 4, 0, 0, 0, 0], [1, 2, 3, 4, 5, 6, 7, 8]]
output = tf.reverse_sequence(input, seq_lengths, seq_axis=1, batch_axis=0)
output
<tf.Tensor: shape=(4, 8), dtype=int32, numpy=
array([[0, 0, 5, 4, 3, 2, 1, 0],
[2, 1, 0, 0, 0, 0, 0, 0],
[3, 2, 1, 4, 0, 0, 0, 0],
[5, 4, 3, 2, 1, 6, 7, 8]], dtype=int32)>
Args |
input
|
A Tensor . The input to reverse.
|
seq_lengths
|
A Tensor . Must be one of the following types: int32 ,
int64 . 1-D with length input.dims(batch_axis) and max(seq_lengths) <=
input.dims(seq_axis)
|
seq_axis
|
An int . The dimension which is partially reversed.
|
batch_axis
|
An optional int . Defaults to 0 . The dimension along which
reversal is performed.
|
name
|
A name for the operation (optional).
|
Returns |
A Tensor. Has the same type as input.
|
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.reverse_sequence\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/array_ops.py#L4690-L4742) |\n\nReverses variable length slices. \n\n tf.reverse_sequence(\n input, seq_lengths, seq_axis=None, batch_axis=None, name=None\n )\n\nThis op first slices `input` along the dimension `batch_axis`, and for\neach slice `i`, reverses the first `seq_lengths[i]` elements along the\ndimension `seq_axis`.\n\nThe elements of `seq_lengths` must obey `seq_lengths[i] \u003c=\ninput.dims[seq_axis]`, and `seq_lengths` must be a vector of length\n`input.dims[batch_axis]`.\n\nThe output slice `i` along dimension `batch_axis` is then given by\ninput slice `i`, with the first `seq_lengths[i]` slices along\ndimension `seq_axis` reversed.\n\n#### Example usage:\n\n seq_lengths = [7, 2, 3, 5]\n input = [[1, 2, 3, 4, 5, 0, 0, 0], [1, 2, 0, 0, 0, 0, 0, 0],\n [1, 2, 3, 4, 0, 0, 0, 0], [1, 2, 3, 4, 5, 6, 7, 8]]\n output = tf.reverse_sequence(input, seq_lengths, seq_axis=1, batch_axis=0)\n output\n \u003ctf.Tensor: shape=(4, 8), dtype=int32, numpy=\n array([[0, 0, 5, 4, 3, 2, 1, 0],\n [2, 1, 0, 0, 0, 0, 0, 0],\n [3, 2, 1, 4, 0, 0, 0, 0],\n [5, 4, 3, 2, 1, 6, 7, 8]], dtype=int32)\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `input` | A `Tensor`. The input to reverse. |\n| `seq_lengths` | A `Tensor`. Must be one of the following types: `int32`, `int64`. 1-D with length `input.dims(batch_axis)` and `max(seq_lengths) \u003c= input.dims(seq_axis)` |\n| `seq_axis` | An `int`. The dimension which is partially reversed. |\n| `batch_axis` | An optional `int`. Defaults to `0`. The dimension along which reversal is performed. |\n| `name` | A name for the operation (optional). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A Tensor. Has the same type as input. ||\n\n\u003cbr /\u003e"]]