ReflectionPad2d
pytorch
本文字数:285 字 | 阅读时长 ≈ 1 min

ReflectionPad2d

pytorch
本文字数:285 字 | 阅读时长 ≈ 1 min

nn.ReflectionPad2d(padding)

以矩阵边缘的元素为对称轴,将矩阵中的元素对称的填充到最外围

实例

注意:如果镜像填充的数据比原来的数据多,就会报错。比如原来 H=3,pad 如果是 5,就会报错

import torch
import torch.nn as nn

input = torch.arange(9, dtype=torch.float).reshape(1, 1, 3, 3)
print(input)
'''
tensor([[[[0., 1., 2.],
          [3., 4., 5.],
          [6., 7., 8.]]]])
'''

m1 = nn.ReflectionPad2d(2)
print(m1(input))
'''
tensor([[[[8., 7., 6., 7., 8., 7., 6.],
          [5., 4., 3., 4., 5., 4., 3.],
          [2., 1., 0., 1., 2., 1., 0.],
          [5., 4., 3., 4., 5., 4., 3.],
          [8., 7., 6., 7., 8., 7., 6.],
          [5., 4., 3., 4., 5., 4., 3.],
          [2., 1., 0., 1., 2., 1., 0.]]]])
'''

m2 = nn.ReflectionPad2d((1, 1, 2, 0))
print(m2(input))
'''
tensor([[[[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.]]]])
'''

m3 = nn.ReflectionPad2d((5, 0, 0, 0))
print(m3(input))   # wrong
'''
RuntimeError: Argument #4: Padding size should be less than the corresponding 
input dimension, but got: padding (5, 0) at dimension 3 of input 4
'''
9月 09, 2024