oneflow.ones¶
-
oneflow.
ones
(*size: Union[int, Tuple[int, …], oneflow.Size, List[int]], dtype: Optional[oneflow._oneflow_internal.dtype] = None, device: Optional[Union[oneflow._oneflow_internal.device, str]] = None, placement: Optional[oneflow._oneflow_internal.placement] = None, sbp: Optional[Union[oneflow._oneflow_internal.sbp.sbp, List[oneflow._oneflow_internal.sbp.sbp]]] = None, requires_grad: bool = False)¶ Returns a tensor filled with the scalar value 1, with the shape defined by the variable argument size.
- Parameters
size (an integer or tuple of integer values) – defining the shape of the output tensor. Can be a variable number of arguments or a collection like a list or tuple.
dtype (flow.dtype, optional) – the desired data type of returned tensor.
device (flow.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type
placement (flow.placement, optional) – the desired placement of returned global tensor. Default: if None, the returned tensor is local one using the argument device.
sbp (flow.sbp.sbp or tuple of flow.sbp.sbp, optional) – the desired sbp descriptor of returned global tensor. Default: if None, the returned tensor is local one using the argument device.
requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.
For example:
>>> import oneflow as flow >>> y = flow.ones(5) >>> y tensor([1., 1., 1., 1., 1.], dtype=oneflow.float32) >>> y = flow.ones(2,3) # construct local tensor >>> y tensor([[1., 1., 1.], [1., 1., 1.]], dtype=oneflow.float32) >>> placement = flow.placement("cpu", ranks=[0]) >>> y = flow.ones(4, 5, placement=placement, sbp=flow.sbp.broadcast) # construct global tensor >>> y.is_global True