栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Pytorch——四种创建张量linspace,range,arange,logspace的用法及区别

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Pytorch——四种创建张量linspace,range,arange,logspace的用法及区别

linspace、range、 arange、logspace的用法及区别
  • torch.linspace()
  • torch.range()
  • torch.arange()
  • torch.logspace()
  • 可能会导致代码数据类型出错的原因

arange、logspace的用法及区别)

torch.linspace()

作用是返回一个一维的tensor(张量),其中dtype是返回的数据类型。

torch.linspace(start, end, steps, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

常用参数:

  • start (float) – the starting value for the set of points (起始的数)
  • end (float) – the ending value for the set of points (结束的数)
  • steps (int) – size of the constructed tensor (定义数据生成的个数)
  • dtype (torch.dpython:type, optional) – the data type to perform the computation in. Default: if None, uses the global default dtype (see torch.get_default_dtype()) when both start and end are real, and corresponding complex dtype when either is complex. (返回的数据类型)

注意:

  1. 该方法为左闭右闭,即[start, end]。
  2. steps并非步长,而是将数据切分成几段的段数

举例:

import torch

A = torch.linspace(0,10,6).reshape(2,3)
# 从0开始,到10结束,将这段数据分成6份 得到0,2,4,6,8,10。
# 再将数据调整形状为[2,3]
B = torch.linspace(1,5,4)
# 从1开始,到5结束,将这段数据分成4份。

print(A)
print(B)
# tensor([[ 0.,  2.,  4.],
#        [ 6.,  8., 10.]])
# tensor([1.0000, 2.3333, 3.6667, 5.0000])
torch.range()

返回一维张量,不过现在已经弃用了,通常使用torch.arange()

注意:该方法为左闭右闭,即[start, end]

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

常用参数:

  • start (float) – the starting value for the set of points. Default: 0.
    (起始的数,默认为0)
  • end (float) – the ending value for the set of points (结束的数)
  • step (float) – the gap between each pair of adjacent points. Default: 1.
    (步长,默认为1)
  • dtype (torch.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see torch.set_default_tensor_type()). If dtype is not given, infer the data type from the other input arguments. If any of start, end, or stop are floating-point, the dtype is inferred to be the default dtype, see get_default_dtype(). Otherwise, the dtype is inferred to be torch.int64.
    (返回张量的所需数据类型。默认值:如果None,使用全局默认值(请参阅torch.set_default_tensor_type())。如果未给出dtype ,则从其他输入参数推断数据类型。如果start、end或stop中的任何一个是浮点数,则 dtype被推断为默认 dtype,请参阅 get_default_dtype()。否则,dtype被推断为torch.int64。)

举例:

import torch

A = torch.range(1,3)
B = torch.range(1,3,0.5)
print(A)
print(B)
# tensor([1., 2., 3.])
# tensor([1.0000, 1.5000, 2.0000, 2.5000, 3.0000])
torch.arange()

注意:torch.arange()为左闭右开,即[start, end)

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

常用参数:

  • start (Number) – the starting value for the set of points. Default: 0.
  • end (Number) – the ending value for the set of points
  • step (Number) – the gap between each pair of adjacent points. Default: 1.
  • dtype (torch.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see torch.set_default_tensor_type()). If dtype is not given, infer the data type from the other input arguments. If any of start, end, or stop are floating-point, the dtype is inferred to be the default dtype, see get_default_dtype(). Otherwise, the dtype is inferred to be torch.int64.
    (返回张量的所需数据类型。默认值:如果,使用全局默认值(请参阅)。如果未给出dtype ,则从其他输入参数推断数据类型。如果start、end或stop中的任何一个是浮点数,则 dtype被推断为默认 dtype,请参阅 。否则,dtype被推断为torch .int64。)

举例:

import torch

A = torch.arange(1,3)
B = torch.arange(1,3,0.5)
print(A)
print(B)
# tensor([1, 2])
# tensor([1.0000, 1.5000, 2.0000, 2.5000])
torch.logspace()

创建对数均分的一维张量

torch.logspace(start, end, steps, base=10.0, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

常用参数:

  • start (float) – the starting value for the set of points
  • end (float) – the ending value for the set of points
  • steps (int) – size of the constructed tensor
  • base (float, optional) – base of the logarithm function. Default:
    10.0.
  • dtype (torch.dpython:type, optional) – the data type to perform the computation in. Default: if None, uses the global default dtype (see torch.get_default_dtype()) when both start and end are real, and corresponding complex dtype when either is complex.
    (执行计算的数据类型。默认值:如果为 None ,则使用全局默认 dtype(请参阅 torch.get_default_dtype()),当两者start都是end实数时,以及对应的复杂 dtype 时很复杂。)

注意:

  1. 该方法为左闭右闭,即[start, end]。
  2. 与linspace一样,steps是多少,返回的tensor中就有多少个数。
可能会导致代码数据类型出错的原因

看一下实例:

A = torch.arange(1,3)		# torch.int64
B = torch.arange(1,3,0.5) 	# torch.float32

C = torch.range(1,3)  		# torch.float32
D = torch.range(1,3,0.5) 	# torch.float32

print(A.dtype)
print(B.dtype)
print(C.dtype)
print(D.dtype)

对比A和C,同样数据输入为整数,但是torch.arange返回的数据类型为torch.int64,而torch.range返回的数据类型为torch.float32。当输入数据带有浮点类型的数值时,二者没有区别,都返回torch.float32。

要认真对待数据类型!

可以往上翻着重新看一下每个的dtype类型的介绍。


官方文档:

TORCH.LINSPACE
TORCH.RANGE
TORCH.ARANGE
TORCH.LOGSPACE

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/931822.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号