PyTorch中Torch.arange函数详解

作者:_湘江夜话_ 时间:2022-04-29 02:30:50 

torch.arange函数详解

官方文档:torch.arange

函数原型

arange(start=0, end, step=1, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) -> Tensor

用法

返回大小为PyTorch中Torch.arange函数详解一维张量,其值介于区间 PyTorch中Torch.arange函数详解为步长等间隔取值

参数说明

参数类型说明
startNumber起始值,默认值:0
endNumber结束值
stepNumber步长,默认值:1

关键字参数

关键字参数类型说明
outTensor输出张量
dtypetorch.dtype期望的返回张量的数据类型。默认值:如果是None,则使用全局默认值。如果未给出 dtype,则从其他输入参数推断数据类型。如果 start、end 或 stop 中的任何一个是浮点数,则 dtype被推断为默认值,参见 get_default_dtype()。否则,dtype 被推断为 torch.int64
layouttorch.layout返回张量的期望 layout。默认值:torch.strided
devicetorch.device返回张量的期望设备。默认值:如果是None,则使用当前设备作为默认张量类型,参见torch.set_default_tensor_type()。对于 CPU 类型的张量,则 device 是 CPU ,若是 CUDA 类型的张量,则 device 是当前的 CUDA 设备
requires_gradboolautograd 是否记录返回张量上所作的操作。默认值:False

代码示例

>>> torch.arange(5)  # 默认以 0 为起点
   tensor([ 0,  1,  2,  3,  4])
   >>> torch.arange(1, 4)  # 默认间隔为 1
   tensor([ 1,  2,  3])
   >>> torch.arange(1, 2.5, 0.5)  # 指定间隔 0.5
   tensor([ 1.0000,  1.5000,  2.0000])

pyTorch中torch.range()和torch.arange()的区别

torch.range()和torch.arange()的区别

x = torch.range(-8, 8)
y = torch.arange(-8, 8)
print(x, x.dtype)
print(y, y.dtype)

output:

   tensor([-8., -7., -6., -5., -4., -3., -2., -1., 0., 1., 2., 3., 4., 5.,6., 7., 8.]) torch.float32
   tensor([-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7]) torch.int64

可以看到,torch.range()的范围是[-8, 8],类型为torch.float32

torch.arange()的范围是[-8, 8),类型为torch.int64

在梯度设置时会出现错误:

x = torch.range(-8, 8, 1, requires_grad=True)
y = torch.arange(-8, 8, 1, requires_grad=True)
print(x, x.dtype)
print(y, y.dtype)

PyTorch中Torch.arange函数详解

即只有当类型为float时才可设置requires_grad=True,故可将

y = torch.arange(-8, 8, 1, requires_grad=True)

改为以下,即手动改变数据类型即可。

y = torch.arange(-8.0, 8.0, 1.0, requires_grad=True)

output:
   tensor([-8., -7., -6., -5., -4., -3., -2., -1., 0., 1., 2., 3., 4., 5.,6., 7., 8.], requires_grad=True)
   torch.float32
   tensor([-8., -7., -6., -5., -4., -3., -2., -1., 0., 1., 2., 3., 4., 5.,6., 7.], requires_grad=True)
   torch.float32

来源:https://blog.csdn.net/weixin_44504393/article/details/127092330

标签:pytorch,torch.arange,函数
0
投稿

猜你喜欢

  • 在Python程序中操作文件之flush()方法的使用教程

    2023-12-02 16:41:31
  • TensorFlow实现AutoEncoder自编码器

    2021-08-29 02:09:58
  • python多线程编程方式分析示例详解

    2023-05-13 04:56:01
  • PDO::getAttribute讲解

    2023-06-06 02:58:22
  • 用ASP实现就MP3曲目信息的操作全攻略

    2008-05-28 12:42:00
  • FCKeditor技巧之在按钮旁边加文字

    2007-10-10 13:17:00
  • HTML5本地存储初探(二)

    2010-03-07 15:47:00
  • Python实现的求解最小公倍数算法示例

    2022-12-11 04:50:03
  • 用层模拟下拉列表框

    2013-07-01 01:19:00
  • Python keras.metrics源代码分析

    2023-10-28 20:58:14
  • 使用Canal实现PHP应用程序与MySQL数据库的实时数据同步

    2023-05-25 01:54:39
  • Python实战小项目之Mnist手写数字识别

    2023-01-20 23:24:56
  • js格式化金额可选是否带千分位以及保留精度

    2023-10-06 04:03:14
  • Python递归实现打印多重列表代码

    2023-05-28 10:38:32
  • 使用php shell命令合并图片的代码

    2023-06-21 22:34:35
  • python中sample函数的介绍与使用

    2021-02-02 15:38:56
  • 不同浏览器的兼容一些写法

    2009-03-26 12:58:00
  • 5个Python杀手级的自动化脚本分享

    2022-11-21 12:03:54
  • 在Python 字典中一键对应多个值的实例

    2023-07-25 23:45:02
  • 查看Python安装路径以及安装包路径小技巧

    2022-02-24 10:09:51
  • asp之家 网络编程 m.aspxhome.com