# Function Differences with torch.nn.functional.pad

View Source On Gitee

torch.nn.functional.pad

class torch.nn.functional.pad(
    input
    pad,
    mode='constant',
    value=0.0
)

For more information, see torch.nn.functional.pad.

mindspore.nn.Pad

class mindspore.nn.Pad(
    paddings,
    mode="CONSTANT"
)(x)

For more information, see mindspore.nn.Pad.

Differences

PyTorch:The pad parameter is a tuple with m values, m/2 is less than or equal to the dimension of the input data, and m is even. Negative dimensions are supported.

MindSpore:The paddings parameter is a tuple whose shape is (n, 2), n is the dimension of the input data. Negative dimensions are not supported currently, and can be cut into smaller slice by ops.Slice.

Code Example

# In MindSpore.
import numpy as np
import torch
import mindspore.nn as nn
from mindspore import Tensor

x = Tensor(np.ones(3, 3).astype(np.float32))
pad_op = nn.Pad(paddings=((0, 0), (1, 1)))
output = pad_op(x)
print(output.shape)
# Out:
# (3, 5)

# In Pytorch.
x = torch.empty(3, 3)
pad = (1, 1)
output = torch.nn.functional.pad(x, pad)
print(output.size())
# Out:
# (3, 5)