Function Differences with torch.ones

View Source On Gitee

torch.ones

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

For more information, see torch.ones.

mindspore.ops.ones

mindspore.ops.ones(shape, dtype=dtype) -> Tensor

For more information, see mindspore.ops.ones.

Differences

PyTorch: Generate a Tensor of size *size with a padding value of 1.

MindSpore: MindSpore API implements the same function as TensorFlow, and only the parameter names are different.

Categories

Subcategories

PyTorch

MindSpore

Difference

Parameters

Parameter 1

size

shape

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.ones(2, 2, dtype=torch.float32)
print(output.numpy())
# [[1. 1.]
#  [1. 1.]]

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

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