mindspore.ops.ReverseSequence

class mindspore.ops.ReverseSequence(seq_dim, batch_dim=0)[源代码]

对输入序列进行部分反转。

参数:
  • seq_dim (int) - 指定反转的维度,此值为必填参数。

  • batch_dim (int) - 指定切片维度。默认值: 0

输入:
  • x (Tensor) - 输入需反转的数据,其数据类型支持包括bool在内的所有数值型。

  • seq_lengths (Tensor) - 指定反转长度,为一维向量,其数据类型为int32或int64。

输出:

Tensor,shape和数据类型与输入 x 相同。

异常:
  • TypeError - seq_dimbatch_dim 不是int。

  • ValueError - 如果 \(len(seq\_lengths) != x.shape[batch\_dim]\)

  • ValueError - 如果 \(batch\_dim == seq\_dim\)

  • ValueError - 如果 \(seq\_dim < 0\)\(seq\_dim >= len(x.shape)\)

  • ValueError - 如果 \(batch\_dim < 0\)\(batch\_dim >= len(x.shape)\)

  • RuntimeError - 如果 seq_lengths 中的任意一个值小于0。

  • RuntimeError - 如果 seq_lengths 中的任意一个值大于 x.shape[seq_dim]

支持平台:

Ascend GPU CPU

样例:

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> x = Tensor(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), mindspore.float32)
>>> seq_lengths = Tensor(np.array([1, 2, 3]))
>>> reverse_sequence = ops.ReverseSequence(seq_dim=1)
>>> output = reverse_sequence(x, seq_lengths)
>>> print(output)
[[1. 2. 3.]
 [5. 4. 6.]
 [9. 8. 7.]]
>>> x = Tensor(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), mindspore.float32)
>>> seq_lengths = Tensor(np.array([1, 2, 3]))
>>> reverse_sequence = ops.ReverseSequence(seq_dim=0, batch_dim=1)
>>> output = reverse_sequence(x, seq_lengths)
>>> print(output)
[[1. 5. 9.]
 [4. 2. 6.]
 [7. 8. 3.]]
>>> x = Tensor(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), mindspore.float32)
>>> seq_lengths = Tensor(np.array([2, 2, 3]))
>>> reverse_sequence = ops.ReverseSequence(seq_dim=1)
>>> output = reverse_sequence(x, seq_lengths)
>>> print(output)
[[2. 1. 3.]
 [5. 4. 6.]
 [9. 8. 7.]]
>>> x = Tensor(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), mindspore.float32)
>>> seq_lengths = Tensor(np.array([3, 2, 3]))
>>> reverse_sequence = ops.ReverseSequence(seq_dim=1)
>>> output = reverse_sequence(x, seq_lengths)
>>> print(output)
[[3. 2. 1.]
 [5. 4. 6.]
 [9. 8. 7.]]
>>> x = Tensor(np.array([[1, 2, 3, 4], [5, 6, 7, 8]]), mindspore.float32)
>>> seq_lengths = Tensor(np.array([4, 4]))
>>> reverse_sequence = ops.ReverseSequence(seq_dim=1)
>>> output = reverse_sequence(x, seq_lengths)
>>> print(output)
[[4. 3. 2. 1.]
 [8. 7. 6. 5.]]