Differences with torch.zeros

View Source On Gitee

torch.zeros

torch.zeros(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) -> Tensor

For more information, see torch.zeros.

mindspore.ops.zeros

mindspore.ops.zeros(size, dtype=dtype) -> Tensor

For more information, see mindspore.ops.zeros.

Differences

PyTorch: Generate a Tensor of size *size whose elements are all 0.

MindSpore: Generate a Tensor of shape size whose elements are all 0.

Categories

Subcategories

PyTorch

MindSpore

Difference

Parameters

Parameter 1

size

size

MindSpore supports input of int, tuple or Tensor type

Parameter 2

out

-

Not involved

Parameter 3

dtype

dtype

The parameter is consistent.

Parameter 4

layout

-

Not involved

Parameter 5

device

-

Not involved

Parameter 6

requires_grad

-

Not involved

Code Example 1

The two APIs achieve the same function and have the same usage.

# PyTorch
import torch
from torch import tensor

output = torch.zeros(2, 2, dtype=torch.float32)
print(output.numpy())
# [[0. 0.]
#  [0. 0.]]

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

output = ops.zeros((2, 2), dtype=ms.float32).asnumpy()
print(output)
# [[0. 0.]
#  [0. 0.]]