Padding size and input dimension

I want to perform some “reflection” padding on an input but I am getting the following error:

RuntimeError: Argument #4: Padding size should be less than the corresponding input dimension, but got: padding (2, 2) at dimension 2 of input 3

This is the code example that produces the above error:

inp = torch.tensor([[1.0, 2.0], [10.0, 20.0]]).reshape(-1, 2, 2)
m = torch.nn.ReflectionPad2d(2)
m(inp)

Hi
The error is correct.

torch.nn.ReflectionPad2d() reflects the values based on the boundary. You need at least 3*3 to operate reflection padding on with 2 in the argument.
the example in the documents highlights what I say :

>>> m = nn.ReflectionPad2d(2)
>>> input = torch.arange(9, dtype=torch.float).reshape(1, 1, 3, 3)
>>> input
tensor([[[[0., 1., 2.],
          [3., 4., 5.],
          [6., 7., 8.]]]])
>>> m(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.]]]])
>>> # using different paddings for different sides
>>> m = nn.ReflectionPad2d((1, 1, 2, 0))
>>> m(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.]]]])

I missed the first line on the documentation…Apologize. I should read more carefully.