@@ -1907,6 +1907,10 @@ def pad(input, pad, mode='constant', value=0):
19071907 5D input tensor with padding of the form
19081908 `(padLeft, padRight, padTop, padBottom, padFront, padBack)`. No "reflect" implementation.
19091909
1910+ See :class:`torch.nn.ConstantPad2d`, :class:`torch.nn.ReflectionPad2d`, and
1911+ :class:`torch.nn.ReplicationPad2d` for concrete examples on how each of the
1912+ padding modes works.
1913+
19101914 Args:
19111915 input (Variable): `Nd` tensor
19121916 pad (tuple): m-elem tuple, where :math:`\frac{m}{2} \leq` input dimensions and :math:`m` is even.
@@ -1917,7 +1921,7 @@ def pad(input, pad, mode='constant', value=0):
19171921
19181922 >>> t4d = torch.Tensor(3, 3, 4, 2)
19191923 >>> p1d = (1, 1) # pad last dim by 1 on each side
1920- >>> out = F.pad(t4d, p1d, "constant", 0)
1924+ >>> out = F.pad(t4d, p1d, "constant", 0) # effectively zero padding
19211925 >>> print(out.data.size())
19221926 torch.Size([3, 3, 4, 4])
19231927 >>> p2d = (1, 1, 2, 2) # pad last dim by (1, 1) and 2nd to last by (2, 2)
@@ -1929,31 +1933,34 @@ def pad(input, pad, mode='constant', value=0):
19291933 >>> out = F.pad(t4d, p3d, "constant", 0)
19301934 >>> print(out.data.size())
19311935 torch.Size([3, 9, 7, 3])
1936+
19321937 """
19331938 assert len (pad ) % 2 == 0 , 'Padding length must be divisible by 2'
19341939 assert len (pad ) // 2 <= input .dim (), 'Padding length too large'
19351940 if mode == 'constant' :
19361941 return ConstantPadNd .apply (input , pad , value )
1937- elif input .dim () == 3 :
1938- assert len (pad ) == 2 , '3D tensors expect 2 values for padding'
1939- if mode == 'reflect' :
1940- return torch ._C ._nn .reflection_pad1d (input , pad )
1941- elif mode == 'replicate' :
1942- return torch ._C ._nn .replication_pad1d (input , pad )
1943- elif input .dim () == 4 :
1944- assert len (pad ) == 4 , '4D tensors expect 4 values for padding'
1945- if mode == 'reflect' :
1946- return torch ._C ._nn .reflection_pad2d (input , pad )
1947- elif mode == 'replicate' :
1948- return torch ._C ._nn .replication_pad2d (input , pad )
1949- elif input .dim () == 5 :
1950- assert len (pad ) == 6 , '5D tensors expect 6 values for padding'
1951- if mode == 'reflect' :
1952- raise NotImplementedError
1953- elif mode == 'replicate' :
1954- return torch ._C ._nn .replication_pad3d (input , pad )
19551942 else :
1956- raise NotImplementedError ("Only 3D, 4D, 5D padding with non-constant padding are supported for now" )
1943+ assert value == 0 , 'Padding mode "{}"" doesn\' t take in value argument' .format (mode )
1944+ if input .dim () == 3 :
1945+ assert len (pad ) == 2 , '3D tensors expect 2 values for padding'
1946+ if mode == 'reflect' :
1947+ return torch ._C ._nn .reflection_pad1d (input , pad )
1948+ elif mode == 'replicate' :
1949+ return torch ._C ._nn .replication_pad1d (input , pad )
1950+ elif input .dim () == 4 :
1951+ assert len (pad ) == 4 , '4D tensors expect 4 values for padding'
1952+ if mode == 'reflect' :
1953+ return torch ._C ._nn .reflection_pad2d (input , pad )
1954+ elif mode == 'replicate' :
1955+ return torch ._C ._nn .replication_pad2d (input , pad )
1956+ elif input .dim () == 5 :
1957+ assert len (pad ) == 6 , '5D tensors expect 6 values for padding'
1958+ if mode == 'reflect' :
1959+ raise NotImplementedError
1960+ elif mode == 'replicate' :
1961+ return torch ._C ._nn .replication_pad3d (input , pad )
1962+ else :
1963+ raise NotImplementedError ("Only 3D, 4D, 5D padding with non-constant padding are supported for now" )
19571964
19581965
19591966# distance
0 commit comments