Function Differences with torch.nn.MaxPool2d

View Source On Gitee

torch.nn.MaxPool2d

torch.nn.MaxPool2d(
    kernel_size,
    stride=None,
    padding=0,
    dilation=1,
    return_indices=False,
    ceil_mode=False
)

For more information, see torch.nn.MaxPool2d.

mindspore.nn.MaxPool2d

class mindspore.nn.MaxPool2d(
    kernel_size=1,
    stride=1,
    pad_mode="valid",
    data_format="NCHW"
)

For more information, see mindspore.nn.MaxPool2d.

Differences

PyTorch:The output shape can be adjusted through the padding parameter. If the shape of input is (N,C,Hin,Win),the shape of output is (N,C,Hout,Wout), where

Hout=Hin+2padding[0]dilation[0]×(kernel_size[0]1)1stride[0]+1
Wout=Win+2padding[1]dilation[1]×(kernel_size[1]1)1stride[1]+1

MindSpore:There is no padding parameter, the pad mode is controlled by the pad_mode parameter only. If the shape of input is (N,C,Hin,Win),the shape of output is (N,C,Hout,Wout), where

  1. pad_mode is “valid”:

    Hout=Hin(kernel_size[0]1)stride[0]
    Wout=Win(kernel_size[1]1)stride[1]
  2. pad_mode is “same”:

    Hout=Hinstride[0]
    Wout=Winstride[1]

Code Example

import mindspore as ms
import mindspore.nn as nn
import torch
import numpy as np

# In MindSpore, pad_mode="valid"
pool = nn.MaxPool2d(kernel_size=3, stride=2, pad_mode="valid")
input_x = ms.Tensor(np.random.randn(20, 16, 50, 32).astype(np.float32))
output = pool(input_x)
print(output.shape)
# Out:
# (20, 16, 24, 15)

# In MindSpore, pad_mode="same"
pool = nn.MaxPool2d(kernel_size=3, stride=2, pad_mode="same")
input_x = ms.Tensor(np.random.randn(20, 16, 50, 32).astype(np.float32))
output = pool(input_x)
print(output.shape)
# Out:
# (20, 16, 25, 16)


# In torch, padding=1
m = torch.nn.MaxPool2d(3, stride=2, padding=1)
input_x = torch.randn(20, 16, 50, 32)
output = m(input_x)
print(output.shape)
# Out:
# torch.Size([20, 16, 25, 16])