megengine.functional.ones#

ones(shape, *, dtype='float32', device=None)[源代码]#

返回一个新创建的拥有指定shape且被1填充的tensor。

参数:

shape (int...) – 输出tensor的shape。

关键字参数:
  • dtype (Tensor.dtype, optional) – 输出向量的数据类型

  • device (Tensor.device, optional) – 被创建的向量所处的设备

返回类型:

Tensor

返回:

一个全1的tensor。

实际案例

>>> F.ones(5)
Tensor([1. 1. 1. 1. 1.], device=xpux:0)
>>> F.ones((5, ), dtype='int32')
Tensor([1 1 1 1 1], dtype=int32, device=xpux:0)
>>> F.ones((2, 2))
Tensor([[1. 1.]
 [1. 1.]], device=xpux:0)