示例文件 test.py
import torch
x = torch.arange(0, 6)
print(x)
print(x.type())
y = torch.range(0, 6)
print(y)
print(y.type())
终端命令行和运行结果
<user>python test.py
tensor([0, 1, 2, 3, 4, 5])
torch.LongTensor
tensor([0., 1., 2., 3., 4., 5., 6.])
torch.FloatTensor
从运算结果来看:
torch.arange(start=1, end=6)的结果不会包含end,类型是torch.LongTensor。
torch.range(start=1, end=6) 的结果会包含end,类型是torch.FloatTensor。
此外,arange(6)相当于arange(0, 6),而range(6)不能使用。
本文对比了PyTorch库中arange和range函数的使用及结果差异,详细介绍了两者在生成张量时的行为特点,包括是否包含结束值及返回张量的数据类型。
1371

被折叠的 条评论
为什么被折叠?



