335 Star 1.5K Fork 862

MindSpore / docs

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

Function Differences with torch.nn.init.uniform_

View Source On Gitee

torch.nn.init.uniform_

torch.nn.init.uniform_(
    tensor,
    a=0.0,
    b=1.0
) -> Tensor

For more information, see torch.nn.init.uniform_.

mindspore.ops.uniform

mindspore.ops.uniform(shape, minval, maxval, seed=None, dtype=mstype.float32) -> Tensor

For more information, see mindspore.ops.uniform.

Differences

PyTorch: The upper and lower bounds of uniform distribution are specified by parameters a and b, i.e. U(-a, b).

MindSpore: The upper and lower bounds of uniform distribution are specified by parameters minval and maxval, i.e. U(minval, maxval),seed is used for random number generator to generate pseudorandom numbers.

Categories Subcategories PyTorch MindSpore Difference
Parameters Parameter 1 tensor shape The function is similar, but the input format is not.tensor is an n-dimensional tensor on Pytorch, but shape is a shape of random tensor to be generated on MindSpore platform.
Parameter 2 a minval They have different names and similar functions, which are used to specify the minimum value of random values generated.
Parameter 3 b maxval They have different names and similar functions, which are used to specify the maximum value of random values generated.
Parameter 4 - seed Seed is used as entropy source for the random number engines to generate pseudo-random numbers, must be non-negative.
Parameter 5 - dtype Specify the type of input data, and determine whether the data generated by uniform distribution is discrete or continuous according to the data type.

Code Example

# PyTorch
import torch
from torch import nn

w = torch.empty(3, 2)
output = nn.init.uniform_(w, a=1, b=4)
print(tuple(output.shape))
# (3, 2)

# MindSpore
import numpy as np
import mindspore
from mindspore import ops
from mindspore import Tensor

shape = (3,2)
minval = Tensor(1, mindspore.float32)
maxval = Tensor(4, mindspore.float32)
output = ops.uniform(shape, minval, maxval, dtype=mindspore.float32)
print(output.shape)
# Out:
# (3, 2)
1
https://gitee.com/mindspore/docs.git
git@gitee.com:mindspore/docs.git
mindspore
docs
docs
r2.0

搜索帮助

53164aa7 5694891 3bd8fe86 5694891