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)