ptorch转mindspore

from mindspore import nn
from mindspore import Parameter, Tensor
import mindspore	
from mindspore import ops as P
import numpy as np
import torch

torch.nn.Parameter

pytorch:

    pos_embed1 = torch.nn.Parameter(torch.zeros(1, 10, 10), requires_grad=False)  # fixed sin-cos embedding
    print(pos_embed1)

mindspore:

    pos_embed2 = Parameter(Tensor(np.zeros((1, 10, 10))), requires_grad=False)  # fixed sin-cos embedding
    print(pos_embed2)

Tensor.flatten & Tensor.transpose

pytorch:

x = x.flatten(2).transpose(1, 2)  # BCHW -> BNC

mindspore:

from mindspore import ops as P
b, c, h, w = x.shape
x = P.reshape(x, (b, c, h * w))  # BCHW -> BCN
x = P.transpose(x, (0, 2, 1))    # BCN -> BNC

Tensor.unsqueeze

pytorch:

    x1 = torch.zeros(10, 10)
    print(x1.shape)
    x1 = x1.unsqueeze(0)
    print(x1.shape)

mindspore:

    x2 = Tensor(np.zeros((10, 10)))
    print(x2.shape)
    x2 = P.ExpandDims()(x2, 0)
    print(x2.shape)

torch.nn.ModuleList

pytorch:

    blocks1 = torch.nn.ModuleList([
        torch.nn.Conv2d(3, 64, kernel_size=3, stride=1, bias=True)
        for i in range(10)])
    print(blocks1)

mindspore:

    blocks2 = mindspore.nn.SequentialCell([
        mindspore.nn.Conv2d(3, 64, kernel_size=3, stride=1, has_bias=True)
        for i in range(10)])
    print(blocks2)

torch.rand & torch.argsort

pytorch:

    noise1 = torch.rand(4, 4)
    print(noise1)
    ids_shuffle1 = torch.argsort(noise1, dim=1)
    print(ids_shuffle1)

mindspore:

    noise2 = P.UniformReal()((4, 4))
    print(noise2)
    ids_shuffle2 = P.Sort(axis=1)(noise2)[1]
    print(ids_shuffle2)

你可能感兴趣的:(pytorch,深度学习,人工智能)