megengine.functional.ones

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

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

参数

shape (int or sequence of ints) – 输出tensor的shape。

关键字参数
  • dtype (Tensor.dtype) – 输出tensor的数据类型。默认:float32

  • device (Tensor.device) – 存放新创建tensor的设备。默认:None

返回类型

Tensor

返回

一个全1的tensor。

实际案例

import megengine.functional as F

out = F.ones(5)
print(out.numpy())
out = F.ones((5, ), dtype='int32')
print(out.numpy())
out = F.ones((2, 2))
print(out.numpy())
out = F.ones([2, 1])
print(out.numpy())

输出:

[1. 1. 1. 1. 1.]
[1 1 1 1 1]
[[1. 1.]
 [1. 1.]]
[[1.]
 [1.]]