Function Differences with torch.nn.Conv2d

View Source On Gitee

torch.nn.Conv2d

torch.nn.Conv2d(
    in_channels=120,
    out_channels=240,
    kernel_size=4,
    stride=1,
    padding=0,
    padding_mode='zeros',
    dilation=1,
    groups=1,
    bias=True
)

For more information, see torch.nn.Conv2d.

mindspore.nn.Conv2d

class mindspore.nn.Conv2d(
    in_channels=120,
    out_channels=240,
    kernel_size=4,
    stride=1,
    pad_mode='same',
    padding=0,
    dilation=1,
    group=1,
    has_bias=False,
    weight_init='normal',
    bias_init='zeros',
    data_format='NCHW'
)(input_x)

For more information, see mindspore.nn.Conv2d.

Differences

PyTorch: No padding is applied to the input by default. bias is set to True by default.

MindSpore: Padding is applied to the input so the output’s dimensions match with input’s dimensions by default. If no padding is needed, set pad_mode to ‘valid’. has_bias is set to False by default.

Code Example

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

# In MindSpore
net = nn.Conv2d(120, 240, 4, stride=2, has_bias=True, weight_init='normal')
x = ms.Tensor(np.ones([1, 120, 1024, 640]), ms.float32)
output = net(x).shape
print(output)
# Out:
# (1, 240, 512, 320)

# In MindSpore
net = nn.Conv2d(120, 240, 4, stride=2, pad_mode='valid', has_bias=True, weight_init='normal')
x = ms.Tensor(np.ones([1, 120, 1024, 640]), ms.float32)
output = net(x).shape
print(output)
# Out:
# (1, 240, 511, 319)

# In PyTorch
m = torch.nn.Conv2d(120, 240, 4, stride=2)
input = torch.rand(1, 120, 1024, 640)
output = m(input)
print(output.shape)
# Out:
# torch.Size([1, 240, 511, 319])