from mindspore import nn
from mindspore import Parameter, Tensor
import mindspore
from mindspore import ops as P
import numpy as np
import torch
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)
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
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)
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)
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)