分类目录:《深入浅出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
且值全为0
的Tensor。文章来源:https://www.toymoban.com/news/detail-457575.html
语法
paddle.zeros(shape, dtype=None, name=None)
参数
-
shape
:[tuple
/list
/Tensor
] 要创建的Tensor的形状,shape
的数据类型为int32
或int64
。 -
dtype
:[可选,np.dtype
/str
] 要创建的Tensor的数据类型,可以为bool
、float16
、float32
、float64
、int32
或int64
。如果dtype
为None
,那么数据类型为float32
。 -
name
:[可选,str
] 具体用法请参见Name
,一般无需设置,默认值为None
。
返回值
Tensor,每个元素都是0
,形状为 shape
,数据类型为dtype
。文章来源地址https://www.toymoban.com/news/detail-457575.html
实例
import paddle
data = paddle.zeros(shape=[3, 2], dtype='float32')
# [[0. 0.]
# [0. 0.]
# [0. 0.]]
data = paddle.zeros(shape=[2, 2])
# [[0. 0.]
# [0. 0.]]
# shape is a Tensor
shape = paddle.full(shape=[2], dtype='int32', fill_value=2)
data3 = paddle.zeros(shape=shape, dtype='int32')
# [[0 0]
# [0 0]]
函数实现
def zeros(shape, dtype=None, name=None):
"""
Creates a tensor of specified :attr:`shape` and :attr:`dtype`, and fills it with 0.
Args:
shape(tuple|list|Tensor): Shape of the Tensor to be created, the data type of ``shape`` is int32 or int64.
dtype(np.dtype|str, optional): Data type of output Tensor, it supports
bool, float16, float32, float64, int32 and int64. Default: if None, the date type is float32.
name(str, optional): The default value is None. Normally there is no need for user to set this
property. For more information, please refer to :ref:`api_guide_Name`.
Returns:
Tensor: A tensor of data type :attr:`dtype` with shape :attr:`shape` and all elements set to 0.
Examples:
.. code-block:: python
import paddle
data = paddle.zeros(shape=[3, 2], dtype='float32')
# [[0. 0.]
# [0. 0.]
# [0. 0.]]
data = paddle.zeros(shape=[2, 2])
# [[0. 0.]
# [0. 0.]]
# shape is a Tensor
shape = paddle.full(shape=[2], dtype='int32', fill_value=2)
data3 = paddle.zeros(shape=shape, dtype='int32')
# [[0 0]
# [0 0]]
"""
if dtype is None:
dtype = 'float32'
return fill_constant(value=0.0, shape=shape, dtype=dtype, name=name)
到了这里,关于深入浅出PaddlePaddle函数——paddle.zeros的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!