分类目录:《深入浅出PaddlePaddle函数》总目录
相关文章:
· 深入浅出TensorFlow2函数——tf.range
· 深入浅出Pytorch函数——torch.arange
· 深入浅出PaddlePaddle函数——paddle.arange


语法

paddle.arange(start=0, end=None, step=1, dtype=None, name=None)

dtype表示浮点类型时,为了避免浮点计算误差,建议给end加上一个极小值epsilon,使边界可以更加明确。

返回值

返回以步长step均匀分隔给定数值区间[start , end)的一维张量,数据类型为dtype

参数

  • start: [float/int/Tensor] 区间起点(且区间包括此值)。当start类型是Tensor时,则应为形状为[1]且数据类型为int32int64float32float64Tensor。如果仅指定start,而endNone,则区间为 [ 0 , s t a r t ) [0, start) [0,start)。默认值为 0 0 0。
  • end:[可选,float/int/Tensor] 区间终点(且通常区间不包括此值)。当end类型是Tensor时,是形状为[1]且数据类型为int32int64float32float64Tensor。默认值为None
  • step:[可选,float/int/Tensor] 均匀分割的步长。当step类型是Tensor时,是形状为[1]且数据类型为int32int64float32float64Tensor。默认值为 1 1 1。
  • dtype: [可选,str/np.dtype] 输出Tensor的数据类型,支持int32int64float32float64。当该参数值为None时,输出Tensor的数据类型为int64。默认值为None
  • name: [可选,str] 具体用法参见Name,一般无需设置,默认值为None

实例

import paddleout1 = paddle.arange(5)              # [0, 1, 2, 3, 4]
out2 = paddle.arange(3, 9, 2.0)      # [3, 5, 7]# use 4.999 instead of 5.0 to avoid floating point rounding errors
out3 = paddle.arange(4.999, dtype='float32')
# [0., 1., 2., 3., 4.]start_var = paddle.to_tensor([3])
out4 = paddle.arange(start_var, 7)
# [3, 4, 5, 6]

函数实现

def arange(start=0, end=None, step=1, dtype=None, name=None):"""Returns a 1-D Tensor with spaced values within a given interval.Values are generated into the half-open interval [``start``, ``end``) withthe ``step``. (the interval including ``start`` but excluding ``end``).If ``dtype`` is float32 or float64, we advise adding a small epsilon to``end`` to avoid floating point rounding errors when comparing against ``end``.Parameters:start(float|int|Tensor): Start of interval. The interval includes thisvalue. If ``end`` is None, the half-open interval is [0, ``start``).If ``start`` is a Tensor, it is a 1-D Tensor with shape [1], withdata type int32, int64, float32, float64. Default is 0.end(float|int|Tensor, optional): End of interval. The interval does notinclude this value. If ``end`` is a Tensor, it is a 1-D Tensor withshape [1], with data type int32, int64, float32, float64. If ``end``is None, the half-open interval is [0, ``start``). Default is None.step(float|int|Tensor, optional): Spacing between values. For any out,it is the istance between two adjacent values, out[i+1] - out[i].If ``step`` is a Tensor, it is a 1-D Tensor with shape [1], withdata type int32, int64, float32, float64. Default is 1.dtype(str|np.dtype, optional): The data type of theoutput tensor. Supported data types: int32, int64, float32, float64.If ``dytpe`` is None, the data type is float32. Default is None.name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.Returns:Tensor: A 1-D Tensor with values from the interval [``start``, ``end``)taken with common difference ``step`` beginning from ``start``. Itsdata type is set by ``dtype``.Examples:.. code-block:: pythonimport paddleout1 = paddle.arange(5)# [0, 1, 2, 3, 4]out2 = paddle.arange(3, 9, 2.0)# [3, 5, 7]# use 4.999 instead of 5.0 to avoid floating point rounding errorsout3 = paddle.arange(4.999, dtype='float32')# [0., 1., 2., 3., 4.]start_var = paddle.to_tensor([3])out4 = paddle.arange(start_var, 7)# [3, 4, 5, 6]"""if dtype is None:dtype = 'int64'if end is None:end = startstart = 0out_shape = Noneif (not isinstance(start, Variable)and not isinstance(end, Variable)and not isinstance(step, Variable)):out_shape = [int(math.ceil((end - start) / step))]if not isinstance(dtype, core.VarDesc.VarType):dtype = convert_np_dtype_to_dtype_(dtype)if not isinstance(start, Variable):with device_guard("cpu"):start = fill_constant([1], dtype, start, force_cpu=True)elif start.dtype != dtype:start = paddle.cast(start, dtype)if not isinstance(end, Variable):with device_guard("cpu"):end = fill_constant([1], dtype, end, force_cpu=True)elif end.dtype != dtype:end = paddle.cast(end, dtype)if not isinstance(step, Variable):with device_guard("cpu"):step = fill_constant([1], dtype, step, force_cpu=True)elif step.dtype != dtype:step = paddle.cast(step, dtype)if in_dygraph_mode():return _C_ops.arange(start, end, step, dtype, _current_expected_place())if _in_legacy_dygraph():out = _legacy_C_ops.range(start, end, step)out.stop_gradient = Truereturn outcheck_dtype(dtype, 'dtype', ['float32', 'float64', 'int32', 'int64'], 'range/arange')helper = LayerHelper('range', **locals())out = helper.create_variable_for_type_inference(dtype, shape=out_shape)helper.append_op(type='range',inputs={'Start': start, 'End': end, 'Step': step},outputs={'Out': out},)out.stop_gradient = Trueif out_shape is not None:out.desc.set_shape(out_shape)return out

深入浅出PaddlePaddle函数——paddle.arange相关推荐

  1. 深入浅出PaddlePaddle函数——paddle.Tensor

    分类目录:<深入浅出PaddlePaddle函数>总目录 相关文章: · 深入浅出TensorFlow2函数--tf.Tensor · 深入浅出Pytorch函数--torch.Tenso ...

  2. 深入浅出PaddlePaddle函数——paddle.ones_like

    分类目录:<深入浅出PaddlePaddle函数>总目录 相关文章: · 深入浅出PaddlePaddle函数--paddle.Tensor · 深入浅出PaddlePaddle函数--p ...

  3. 深入浅出PaddlePaddle函数——paddle.numel

    分类目录:<深入浅出PaddlePaddle函数>总目录 相关文章: · 深入浅出TensorFlow2函数--tf.size · 深入浅出Pytorch函数--torch.numel · ...

  4. 深入浅出PaddlePaddle函数——paddle.shape

    分类目录:<深入浅出PaddlePaddle函数>总目录 语法 paddle.shape(input) 参数 input:[Tensor] 输入的多维Tensor或SelectedRows ...

  5. 深入浅出Pytorch函数——torch.arange

    分类目录:<深入浅出Pytorch函数>总目录 相关文章: · 深入浅出TensorFlow2函数--tf.range · 深入浅出Pytorch函数--torch.arange · 深入 ...

  6. 深入浅出Pytorch函数——torch.exp

    分类目录:<深入浅出Pytorch函数>总目录 相关文章: · 深入浅出TensorFlow2函数--tf.exp · 深入浅出TensorFlow2函数--tf.math.exp · 深 ...

  7. 深入浅出TensorFlow2函数——tf.constant

    分类目录:<深入浅出TensorFlow2函数>总目录 相关文章: · 深入浅出TensorFlow2函数--tf.constant · 深入浅出TensorFlow2函数--tf.Ten ...

  8. 深入浅出Pytorch函数——torch.numel

    分类目录:<深入浅出Pytorch函数>总目录 相关文章: · 深入浅出TensorFlow2函数--tf.size · 深入浅出Pytorch函数--torch.numel · 深入浅出 ...

  9. 深入浅出Pytorch函数——torch.zeros

    分类目录:<深入浅出Pytorch函数>总目录 相关文章: · 深入浅出Pytorch函数--torch.Tensor · 深入浅出Pytorch函数--torch.ones · 深入浅出 ...

最新文章

  1. 大规模集群中Docker镜像如何分发管理?试试Uber刚开源的Kraken
  2. Kubernetes从懵圈到熟练:认证与调度
  3. Docker exec 命令执行出错, 显示 the input device is not aTTY 的解决办法
  4. java之跳转_java学习之五种跳转关于jsp的
  5. python如何读取tfrecord_tensorflow读取tfrecords格式文件
  6. python处理ppt的插件_几款PPT神器插件,千万不能错过!
  7. java和ajax超时_java – 如何在不重置tomcat的会话超时的情况下执行经过身份验证的AJAX请求?...
  8. VisualStudio quick tips -- 快速在多个打开的代码文件间切换
  9. Python学习Day7
  10. 6.3 tensorflow2实现FM推荐系统——Python实战
  11. mysql为何不建议使用外键
  12. 在线EXCEL编辑器-Luckysheet
  13. 服务器系统不用关机,云服务器不用了要关机吗
  14. 抖音视频如何发到快手?短视频如何一键发布?
  15. Flink on Yarn的两种模式及HA
  16. 九度OJ-1163:素数(未关联)
  17. 钻石DIAMOND英语源于DIAMAUND钻石
  18. 用好CUDA加速 6款视频软件评测与指南
  19. JavaWeb技术归档大全
  20. 痞子衡嵌入式:嵌入式里通用微秒(microseconds)计时函数框架设计与实现

热门文章

  1. 物联网如何为患者带来安全医疗保健服务
  2. 个税计算器,用BigDecimal实现
  3. 前端web3入门脚本三:一键完成与dex的交互,羊毛党必备
  4. 18位身份证号码的校验
  5. 我有10个职场经验,价值100万,但今天免费|咪蒙
  6. $(...).on is not a function 解决方案
  7. HTML中行内元素块级元素 行内块元素
  8. Tomcat配置JDK和JRE
  9. 手机视频硬解码和软解码的区别
  10. html网页制作期末大作业成品:基于HTML+CSS+JavaScript简洁汽车网站(7页)