mindspore.ops.ones

View Source On Gitee
mindspore.ops.ones(shape, dtype=None)[source]

Creates a tensor filled with value ones.

Creates a tensor with shape described by the first argument and fills it with value ones in type of the second argument.

Parameters
  • shape (Union[tuple[int], int, Tensor]) – The specified shape of output tensor. Only positive integer or tuple or Tensor containing positive integers are allowed. If it is a Tensor, it must be a 0-D or 1-D Tensor with int32 or int64 dtypes.

  • dtype (mindspore.dtype) – The specified type of output tensor. If dtype is None , mindspore.float32 will be used. Default: None .

Returns

Tensor, has the same type and shape as input shape value.

Raises

TypeError – If shape is not tuple, int or Tensor.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> from mindspore import ops
>>> output = ops.ones((2, 2), mindspore.float32)
>>> print(output)
[[1. 1.]
 [1. 1.]]