334 Star 1.5K Fork 863

MindSpore / docs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
normal.md 4.92 KB
一键复制 编辑 原始数据 按行查看 历史
luojianing 提交于 2023-07-21 15:16 . replace target=blank

Differences with torch.ger

View Source On Gitee

torch.normal

torch.normal(mean, std, *, generator=None, out=None)
torch.normal(mean=0.0, std, *, out=None)
torch.normal(mean, std=1.0, *, out=None)
torch.normal(mean, std, size, *, out=None)

For more information, see torch.normal.

mindspore.ops.normal

mindspore.ops.normal(shape, mean, stddev, seed=None)

For more information, see mindspore.ops.normal.

Differences

API function of MindSpore is consistent with that of PyTorch.

PyTorch: Four interface usages are supported.

  • mean and std are both Tensor, requiring the same number of members for mean and std. The shape of the return value matches the shape of mean .
  • mean is the float type, std is Tensor. The shape of the return value matches the shape of std .
  • std is the float type, mean is Tensor. The shape of the return value matches the shape of mean .
  • mean and std are both float types. The shape of the return value matches the shape of size .

MindSpore: The data types supported by mean and std are Tensor, and the shape of the return value is broadcast by shape, mean, and stddev.

Categories Subcategories PyTorch MindSpore Differences
Parameters Parameter 1 - shape This value in MindSpore is used to broadcast the shape of the return value together with mean and stddev
Parameter 2 mean mean The data type supported in MindSpore is Tensor. Tensor and float are supported in PyTorch, corresponding to different usages
Parameter 3 std stddev The data type supported in MindSpore is Tensor. Tensor and float are supported in PyTorch, corresponding to different usages
Parameter 4 generator seed For details, see General Difference Parameter Table
Parameter 5 size - The shape of the return value in PyTorch, used under the specified interface usage
Parameter 6 out - For details, see General Difference Parameter Table

Code Example 1

In PyTorch, 'mean' and 'std' are both Tensor.

# PyTorch
import torch
import numpy as np

mean = torch.tensor(np.array([[3, 4], [5, 6]]), dtype=torch.float32)
stddev = torch.tensor(np.array([[0.2, 0.3], [0.4, 0.5]]), dtype=torch.float32)
output = torch.normal(mean, stddev)
print(output.shape)
# torch.Size([2, 2])

# MindSpore
import mindspore as ms
import numpy as np

shape = (2, 2)
mean = ms.Tensor(np.array([[3, 4], [5, 6]]), ms.float32)
stddev = ms.Tensor(np.array([[0.2, 0.3], [0.4, 0.5]]), ms.float32)
output = ms.ops.normal(shape, mean, stddev)
print(output.shape)
# (2, 2)

Code Example 2

In PyTorch, 'mean' is the float and 'std' is the Tensor.

# PyTorch
import torch
import numpy as np

mean = 3.0
stddev = torch.tensor(np.array([[0.2, 0.3], [0.4, 0.5]]), dtype=torch.float32)
output = torch.normal(mean, stddev)
print(output.shape)
# torch.Size([2, 2])

# MindSpore
import mindspore as ms
import numpy as np

shape = (2, 2)
mean = ms.Tensor(3.0, ms.float32)
stddev = ms.Tensor(np.array([[0.2, 0.3], [0.4, 0.5]]), ms.float32)
output = ms.ops.normal(shape, mean, stddev)
print(output.shape)
# (2, 2)

Code Example 3

In PyTorch, 'mean' is Tensor, and 'std' is the float.

# PyTorch
import torch
import numpy as np

mean = torch.tensor(np.array([[3, 4], [5, 6]]), dtype=torch.float32)
stddev = 1.0
output = torch.normal(mean, stddev)
print(output.shape)
# torch.Size([2, 2])

# MindSpore
import mindspore as ms
import numpy as np

shape = (2, 2)
mean = ms.Tensor(np.array([[3, 4], [5, 6]]), ms.float32)
stddev = ms.Tensor(1.0, ms.float32)
output = ms.ops.normal(shape, mean, stddev)
print(output.shape)
# (2, 2)

Code Example 4

In PyTorch, 'mean' and 'std' are both float.

# PyTorch
import torch
import numpy as np

mean = 3.0
stddev = 1.0
size = (2, 2)
output = torch.normal(mean, stddev, size)
print(output.shape)
# torch.Size([2, 2])

# MindSpore
import mindspore as ms
import numpy as np

shape = (2, 2)
mean = ms.Tensor(3.0, ms.float32)
stddev = ms.Tensor(1.0, ms.float32)
output = ms.ops.normal(shape, mean, stddev)
print(output.shape)
# (2, 2)
1
https://gitee.com/mindspore/docs.git
git@gitee.com:mindspore/docs.git
mindspore
docs
docs
r2.0

搜索帮助