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_dim]
,
and seq_lengths
must be a vector of length input.dims[batch_dim]
.
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.
For example:
# Given this:
batch_dim = 0
seq_dim = 1
input.dims = (4, 8, ...)
seq_lengths = [7, 2, 3, 5]
# then slices of input are reversed on seq_dim, but only up to seq_lengths:
output[0, 0:7, :, ...] = input[0, 7:0:-1, :, ...]
output[1, 0:2, :, ...] = input[1, 2:0:-1, :, ...]
output[2, 0:3, :, ...] = input[2, 3:0:-1, :, ...]
output[3, 0:5, :, ...] = input[3, 5:0:-1, :, ...]
# while entries past seq_lens are copied through:
output[0, 7:, :, ...] = input[0, 7:, :, ...]
output[1, 2:, :, ...] = input[1, 2:, :, ...]
output[2, 3:, :, ...] = input[2, 3:, :, ...]
output[3, 2:, :, ...] = input[3, 2:, :, ...]
In contrast, if:
# Given this:
batch_dim = 2
seq_dim = 0
input.dims = (8, ?, 4, ...)
seq_lengths = [7, 2, 3, 5]
# then slices of input are reversed on seq_dim, but only up to seq_lengths:
output[0:7, :, 0, :, ...] = input[7:0:-1, :, 0, :, ...]
output[0:2, :, 1, :, ...] = input[2:0:-1, :, 1, :, ...]
output[0:3, :, 2, :, ...] = input[3:0:-1, :, 2, :, ...]
output[0:5, :, 3, :, ...] = input[5:0:-1, :, 3, :, ...]
# while entries past seq_lens are copied through:
output[7:, :, 0, :, ...] = input[7:, :, 0, :, ...]
output[2:, :, 1, :, ...] = input[2:, :, 1, :, ...]
output[3:, :, 2, :, ...] = input[3:, :, 2, :, ...]
output[2:, :, 3, :, ...] = input[2:, :, 3, :, ...]
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_dim) and
max(seq_lengths) <= input.dims(seq_dim)
|
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.
Last updated 2020-10-01 UTC.
[null,null,["Last updated 2020-10-01 UTC."],[],[],null,["# tf.reverse_sequence\n\n\u003cbr /\u003e\n\n|-----------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------|\n| [TensorFlow 1 version](/versions/r1.15/api_docs/python/tf/reverse_sequence) | [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/python/ops/array_ops.py#L3837-L3848) |\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 each\nslice `i`, reverses the first `seq_lengths[i]` elements along\nthe dimension `seq_axis`.\n\nThe elements of `seq_lengths` must obey `seq_lengths[i] \u003c= input.dims[seq_dim]`,\nand `seq_lengths` must be a vector of length `input.dims[batch_dim]`.\n\nThe output slice `i` along dimension `batch_axis` is then given by input\nslice `i`, with the first `seq_lengths[i]` slices along dimension\n`seq_axis` reversed.\n\n#### For example:\n\n # Given this:\n batch_dim = 0\n seq_dim = 1\n input.dims = (4, 8, ...)\n seq_lengths = [7, 2, 3, 5]\n\n # then slices of input are reversed on seq_dim, but only up to seq_lengths:\n output[0, 0:7, :, ...] = input[0, 7:0:-1, :, ...]\n output[1, 0:2, :, ...] = input[1, 2:0:-1, :, ...]\n output[2, 0:3, :, ...] = input[2, 3:0:-1, :, ...]\n output[3, 0:5, :, ...] = input[3, 5:0:-1, :, ...]\n\n # while entries past seq_lens are copied through:\n output[0, 7:, :, ...] = input[0, 7:, :, ...]\n output[1, 2:, :, ...] = input[1, 2:, :, ...]\n output[2, 3:, :, ...] = input[2, 3:, :, ...]\n output[3, 2:, :, ...] = input[3, 2:, :, ...]\n\nIn contrast, if: \n\n # Given this:\n batch_dim = 2\n seq_dim = 0\n input.dims = (8, ?, 4, ...)\n seq_lengths = [7, 2, 3, 5]\n\n # then slices of input are reversed on seq_dim, but only up to seq_lengths:\n output[0:7, :, 0, :, ...] = input[7:0:-1, :, 0, :, ...]\n output[0:2, :, 1, :, ...] = input[2:0:-1, :, 1, :, ...]\n output[0:3, :, 2, :, ...] = input[3:0:-1, :, 2, :, ...]\n output[0:5, :, 3, :, ...] = input[5:0:-1, :, 3, :, ...]\n\n # while entries past seq_lens are copied through:\n output[7:, :, 0, :, ...] = input[7:, :, 0, :, ...]\n output[2:, :, 1, :, ...] = input[2:, :, 1, :, ...]\n output[3:, :, 2, :, ...] = input[3:, :, 2, :, ...]\n output[2:, :, 3, :, ...] = input[2:, :, 3, :, ...]\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_dim)` and `max(seq_lengths) \u003c= input.dims(seq_dim)` |\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"]]