0


深入浅出PaddlePaddle函数——paddle.ones

分类目录:《深入浅出PaddlePaddle函数》总目录
相关文章:
· 深入浅出PaddlePaddle函数——paddle.Tensor
· 深入浅出PaddlePaddle函数——paddle.ones
· 深入浅出PaddlePaddle函数——paddle.zeros
· 深入浅出PaddlePaddle函数——paddle.full
· 深入浅出PaddlePaddle函数——paddle.ones_like
· 深入浅出PaddlePaddle函数——paddle.zeros_like
· 深入浅出PaddlePaddle函数——paddle.full_like


创建一个形状为

shape

、数据类型为

dtype

且值全为

1

的Tensor。

语法

paddle.ones(shape, dtype=None, name=None)

参数

  • shape:[tuple/list/Tensor] 要创建的Tensor的形状,shape的数据类型为int32int64
  • dtype:[可选,np.dtype/str] 要创建的Tensor的数据类型,可以为boolfloat16float32float64int32int64。如果dtypeNone,那么数据类型为float32
  • name:[可选,str] 具体用法请参见Name,一般无需设置,默认值为None

返回值

Tensor,每个元素都是

1

,形状为

shape

,数据类型为

dtype

实例

import paddle

#defaultdtype for ones OP
data1 = paddle.ones(shape=[3,2])
# [[1.1.]
#  [1.1.]
#  [1.1.]]

data2 = paddle.ones(shape=[2,2], dtype='int32')
# [[11]
#  [11]]

函数实现

def ones(shape, dtype=None, name=None):"""
    Create a Tensor of specified :attr:`shape` and :attr:`dtype` and fill it with 1.
    Args:shape(tuple|list|Tensor): Shape of the Tensor to be created, the data type of shape should be int32 or int64.dtype(np.dtype|str, optional): Data type of output Tensor, it should be one of
            bool, float16, float32, float64, int32 and int64. If it is set to None, the data type will be float32.name(str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
    Returns:
        Tensor: A Tensor of data type :attr:`dtype` with shape :attr:`shape` and all elements are 1.
    Examples:.. code-block:: python
            import paddle
            #defaultdtype for ones OP
            data1 = paddle.ones(shape=[3,2])
            # [[1.1.]
            #  [1.1.]
            #  [1.1.]]
            data2 = paddle.ones(shape=[2,2], dtype='int32')
            # [[11]
            #  [11]]#shapeis a Tensor
            shape = paddle.full(shape=[2], dtype='int32', fill_value=2)
            data3 = paddle.ones(shape=shape, dtype='int32')
            # [[11]
            #  [11]]"""
    if dtype is None:
        dtype ='float32'returnfill_constant(value=1.0, shape=shape, dtype=dtype, name=name)

本文转载自: https://blog.csdn.net/hy592070616/article/details/129409107
版权归原作者 von Neumann 所有, 如有侵权,请联系我们删除。

“深入浅出PaddlePaddle函数——paddle.ones”的评论:

还没有评论