mindspore.nn.ReflectionPad2d

class mindspore.nn.ReflectionPad2d(padding)[源代码]

根据 padding 对输入 x 进行填充。

参数:
  • padding (union[int, tuple]) - 填充大小,如果输入为int,则对所有边界进行相同大小的填充;如果是tuple,则顺序为 \((pad\_left, pad\_right, pad\_up, pad\_down)\)

输入:
  • x (Tensor) - 输入Tensor,shape为 \((C, H_{in}, W_{in})\)\((N, C, H_{in}, W_{in})\)

输出:

Tensor,填充后的Tensor,shape为 \((C, H_{out}, W_{out})\)\((N, C, H_{out}, W_{out})\)。其中 \(H_{out} = H_{in} + pad\_up + pad\_down\)\(W_{out} = W_{in} + pad\_left + pad\_right\)

异常:
  • TypeError - padding 不是tuple或int。

  • TypeError - padding 中存在不是int的元素。

  • ValueError - padding 是tuple,且长度不能被2整除。

  • ValueError - padding 是tuple,且存在负数。

  • ValueError - padding 是tuple,且长度和tensor的维度不匹配。

支持平台:

Ascend GPU CPU

样例:

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