ReverseSequence#

  • Domain: ai.onnx

  • Since version: 10

Reverse batch of sequences having different lengths specified by sequence_lens.

For each slice i iterating on batch axis, the operator reverses the first sequence_lens[i] elements on time axis, and copies elements whose index’s beyond sequence_lens[i] to the output. So the output slice i contains reversed sequences on the first sequence_lens[i] elements, then have original values copied for the other elements.

Example 1:

input = [[0.0, 4.0, 8.0,  12.0],
         [1.0, 5.0, 9.0,  13.0],
         [2.0, 6.0, 10.0, 14.0],
         [3.0, 7.0, 11.0, 15.0]]
sequence_lens = [4, 3, 2, 1]
time_axis = 0
batch_axis = 1

output = [[3.0, 6.0, 9.0,  12.0],
          [2.0, 5.0, 8.0,  13.0],
          [1.0, 4.0, 10.0, 14.0],
          [0.0, 7.0, 11.0, 15.0]]

Example 2:

input = [[0.0,  1.0,  2.0,  3.0 ],
         [4.0,  5.0,  6.0,  7.0 ],
         [8.0,  9.0,  10.0, 11.0],
         [12.0, 13.0, 14.0, 15.0]]
sequence_lens = [1, 2, 3, 4]
time_axis = 1
batch_axis = 0

output = [[0.0,  1.0,  2.0,  3.0 ],
          [5.0,  4.0,  6.0,  7.0 ],
          [10.0, 9.0,  8.0,  11.0],
          [15.0, 14.0, 13.0, 12.0]]

Inputs

  • input (T): Tensor of rank r >= 2.

  • sequence_lens (tensor(int64)): Tensor specifying lengths of the sequences in a batch. It has shape [batch_size].

Outputs

  • Y (T): Tensor with same shape of input.

Attributes

  • batch_axis (int): (Optional) Specify which axis is batch axis. Must be one of 1 (default), or 0.

  • time_axis (int): (Optional) Specify which axis is time axis. Must be one of 0 (default), or 1.

Type Constraints

  • T: Input and output types can be of any tensor type. Allowed types: tensor(bool), tensor(complex128), tensor(complex64), tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8).

Examples#

test_cc_reversesequence_batch

Node:
  ReverseSequence(X, sequence_lens) -> (Y)
  Attributes:
    time_axis = 1
    batch_axis = 0
Inputs:
  X: shape=(4, 4), dtype=float32
    [[ 0.,  1.,  2.,  3.],
     [ 4.,  5.,  6.,  7.],
     [ 8.,  9., 10., 11.],
     [12., 13., 14., 15.]]
  sequence_lens: shape=(4,), dtype=int64
    [1, 2, 3, 4]

Outputs:
  Y: shape=(4, 4), dtype=float32
    [[ 0.,  1.,  2.,  3.],
     [ 5.,  4.,  6.,  7.],
     [10.,  9.,  8., 11.],
     [15., 14., 13., 12.]]

test_cc_reversesequence_default_attrs

Node:
  ReverseSequence(X, sequence_lens) -> (Y)
Inputs:
  X: shape=(3, 2), dtype=float32
    [[0., 1.],
     [2., 3.],
     [4., 5.]]
  sequence_lens: shape=(2,), dtype=int64
    [3, 2]

Outputs:
  Y: shape=(3, 2), dtype=float32
    [[4., 3.],
     [2., 1.],
     [0., 5.]]

test_cc_reversesequence_time

Node:
  ReverseSequence(X, sequence_lens) -> (Y)
  Attributes:
    time_axis = 0
    batch_axis = 1
Inputs:
  X: shape=(4, 4), dtype=float32
    [[ 0.,  4.,  8., 12.],
     [ 1.,  5.,  9., 13.],
     [ 2.,  6., 10., 14.],
     [ 3.,  7., 11., 15.]]
  sequence_lens: shape=(4,), dtype=int64
    [4, 3, 2, 1]

Outputs:
  Y: shape=(4, 4), dtype=float32
    [[ 3.,  6.,  9., 12.],
     [ 2.,  5.,  8., 13.],
     [ 1.,  4., 10., 14.],
     [ 0.,  7., 11., 15.]]

test_cc_reversesequence_with_inner_dim

Node:
  ReverseSequence(X, sequence_lens) -> (Y)
  Attributes:
    time_axis = 0
    batch_axis = 1
Inputs:
  X: shape=(3, 2, 2), dtype=float32
    [[[ 0.,  1.],
      [ 2.,  3.]],

     [[ 4.,  5.],
      [ 6.,  7.]],

     [[ 8.,  9.],
      [10., 11.]]]
  sequence_lens: shape=(2,), dtype=int64
    [3, 1]

Outputs:
  Y: shape=(3, 2, 2), dtype=float32
    [[[ 8.,  9.],
      [ 2.,  3.]],

     [[ 4.,  5.],
      [ 6.,  7.]],

     [[ 0.,  1.],
      [10., 11.]]]