深入浅出PaddlePaddle函数——s

阅读: 评论:0

深入浅出PaddlePaddle函数——s

深入浅出PaddlePaddle函数——s

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


创建一个形状为shape、数据类型为dtype且值全为1的Tensor。

语法
s(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# default dtype for ones OP
data1 = s(shape=[3, 2])
# [[1. 1.]
#  [1. 1.]
#  [1. 1.]]data2 = s(shape=[2, 2], dtype='int32')
# [[1 1]
#  [1 1]]
函数实现
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 ofbool, 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:: pythonimport paddle# default dtype for ones OPdata1 = s(shape=[3, 2])# [[1. 1.]#  [1. 1.]#  [1. 1.]]data2 = s(shape=[2, 2], dtype='int32')# [[1 1]#  [1 1]]# shape is a Tensorshape = paddle.full(shape=[2], dtype='int32', fill_value=2)data3 = s(shape=shape, dtype='int32')# [[1 1]#  [1 1]]"""if dtype is None:dtype = 'float32'return fill_constant(value=1.0, shape=shape, dtype=dtype, name=name)

本文发布于:2024-01-30 23:14:38,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170662768123532.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23