比较与torch.nn.Unfold的功能差异

查看源文件

torch.nn.Unfold

class torch.nn.Unfold(
    kernel_size,
    dilation=1,
    padding=0,
    stride=1
)

更多内容详见torch.nn.Unfold

mindspore.nn.Unfold

class mindspore.nn.Unfold(
    ksizes,
    strides,
    rates,
    padding="valid"
)(x)

更多内容详见mindspore.nn.Unfold

使用方式

PyTorch:输出的形状,(N,C×∏(kernel_size),L) -> 输出的张量是形状为(N,C×∏(kernel_size),L)的3维张量。

MindSpore:输出张量,数据类型与x相同的4维张量,形状为[out_batch, out_depth, out_row, out_col] 其中 out_batch 与 in_batch 相同。

代码示例

from mindspore import Tensor
import mindspore.nn as nn
from mindspore import dtype as mstype
import torch
import numpy as np

unfold = torch.nn.Unfold(kernel_size=(2, 3))
input = torch.ones(2, 5, 3, 4)
output = unfold(input)
print(output.size())
# Out:
# torch.Size([2, 30, 4])

net = nn.Unfold(ksizes=[1, 2, 2, 1], strides=[1, 2, 2, 1], rates=[1, 2, 2, 1])
image = Tensor(np.ones([2, 5, 3, 4]), dtype=mstype.float16)
output = net(image)
print(output.shape)
# Out:
# (2, 20, 1, 1)