Differences with torch.ger
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
andstd
are both Tensor, requiring the same number of members formean
andstd
. The shape of the return value matches the shape ofmean
.mean
is the float type,std
is Tensor. The shape of the return value matches the shape ofstd
.std
is the float type,mean
is Tensor. The shape of the return value matches the shape ofmean
.mean
andstd
are both float types. The shape of the return value matches the shape ofsize
.
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 |
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)