mindspore.nn.ReflectionPad1d

View Source On Gitee
class mindspore.nn.ReflectionPad1d(padding)[source]

Using a given padding to do reflection pad on the given tensor.

Parameters

padding (union[int, tuple]) – The padding size to pad the last dimension of input tensor. If padding is an integer: all directions will be padded with the same size. If padding is a tuple: uses \((pad\_left, pad\_right)\) to pad.

Inputs:
  • x (Tensor) - 2D or 3D, shape: \((C, W_{in})\) or \((N, C, W_{in})\).

Outputs:

Tensor, after padding. Shape: \((C, W_{out})\) or \((N, C, W_{out})\), where \(W_{out} = W_{in} + pad\_left + pad\_right\).

Raises
  • TypeError – If ‘padding’ is not a tuple or int.

  • TypeError – If there is an element in ‘padding’ that is not int.

  • ValueError – If the length of ‘padding’ is not divisible by 2.

  • ValueError – If there is an element in ‘padding’ that is negative.

  • ValueError – If the there is a dimension mismatch between the padding and the tensor.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import numpy as np
>>> import mindspore as ms
>>> x = ms.Tensor(np.array([[[0, 1, 2, 3], [4, 5, 6, 7]]]).astype(np.float32))
>>> # x has shape (1, 2, 4)
>>> padding = (3, 1)
>>> # The first and the second dimension of x remain the same.
>>> # The third dimension of x: W_out = W_in + pad_left + pad_right = 4 + 3 + 1 = 8
>>> pad1d = ms.nn.ReflectionPad1d(padding)
>>> out = pad1d(x)
>>> # The shape of out is (1, 2, 8)
>>> print(out)
[[[3. 2. 1. 0. 1. 2. 3. 2.]
  [7. 6. 5. 4. 5. 6. 7. 6.]]]