分类目录:《深入浅出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
且值全为
fill_value
的Tensor。
语法
paddle.full(shape, fill_value, dtype=None, name=None)
参数
shape
:[tuple
/list
/Tensor
] 要创建的Tensor的形状,shape
的数据类型为int32
或int64
。fill_value
:[bool
/float
/int
/Tensor
] 用于初始化输出Tensor的常量数据的值。注意:该参数不可超过输出变量数据类型的表示范围。dtype
:[可选,np.dtype
/str
] 要创建的Tensor的数据类型,可以为bool
、float16
、float32
、float64
、int32
或int64
。如果dtype
为None
,那么数据类型为float32
。name
:[可选,str
] 具体用法请参见Name
,一般无需设置,默认值为None
。
返回值
Tensor,每个元素都是
fill_value
,形状为
shape
,数据类型为
dtype
。
实例
import paddle
data1 = paddle.full(shape=[2,1], fill_value=0, dtype='int64')
#[[0]
# [0]]#attrshape is a list which contains Tensor.
positive_2 = paddle.full([1],2,"int32")
data3 = paddle.full(shape=[1, positive_2], dtype='float32', fill_value=1.5)
# [[1.51.5]]#attrshape is a Tensor.
shape = paddle.full([2],2,"int32")
data4 = paddle.full(shape=shape, dtype='bool', fill_value=True)
# [[True True]
# [True True]]#attrfill_value is a Tensor.
val = paddle.full([1],2.0,"float32")
data5 = paddle.full(shape=[2,1], fill_value=val, dtype='float32')
# [[2.0]
# [2.0]]
函数实现
def full(shape, fill_value, dtype=None, name=None):"""
Return a Tensor with the ``fill_value`` which size is same as ``shape``.
Args:shape(list|tuple|Tensor): Shape of the Tensor to be created.
The data type is ``int32`` or ``int64`` . If ``shape`` is a list or tuple,
the elements of it should be integers or Tensors with shape [1].
If ``shape`` is an Tensor, it should be an 1-D Tensor.fill_value(bool|float|int|Tensor): The constant value
used to initialize the Tensor to be created. If ``fill_value`` is an Tensor, it must be an 1-D Tensor.dtype(np.dtype|str, optional): Data type of the output Tensor
which can be float16, float32, float64, int32, int64,if dytpe is `None`, the data
type of created Tensor is `float32`.name(str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
Returns:
Tensor: Tensor which is created according to ``shape``, ``fill_value`` and ``dtype``.
Examples:.. code-block:: python
import paddle
data1 = paddle.full(shape=[2,1], fill_value=0, dtype='int64')
#[[0]
# [0]]#attrshape is a list which contains Tensor.
positive_2 = paddle.full([1],2,"int32")
data3 = paddle.full(shape=[1, positive_2], dtype='float32', fill_value=1.5)
# [[1.51.5]]#attrshape is a Tensor.
shape = paddle.full([2],2,"int32")
data4 = paddle.full(shape=shape, dtype='bool', fill_value=True)
# [[True True]
# [True True]]#attrfill_value is a Tensor.
val = paddle.full([1],2.0,"float32")
data5 = paddle.full(shape=[2,1], fill_value=val, dtype='float32')
# [[2.0]
# [2.0]]"""
if dtype is None:
dtype ='float32'returnfill_constant(shape=shape, dtype=dtype, value=fill_value, name=name)
版权归原作者 von Neumann 所有, 如有侵权,请联系我们删除。