1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| 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)) ''' RuntimeError: Argument #4: Padding size should be less than the corresponding input dimension, but got: padding (5, 0) at dimension 3 of input 4 '''
|