torch.arange (不是 torch.range,不要用torch.range)

arange(lower, upper)
[lower, upper)
左闭右开区间。

ex1

T = torch.arange(0,5)
print(T)
T = torch.arange(0,1)
print(T)
T = torch.arange(0,0)
print(T)

输出:

tensor([0, 1, 2, 3, 4])
tensor([0])
tensor([], dtype=torch.int64)

ex2

T = torch.arange(1,5)
print(T)
T = torch.arange(1,1)
print(T)
T = torch.arange(1,0)
print(T)

输出:

tensor([1, 2, 3, 4])
tensor([], dtype=torch.int64)
Traceback (most recent call last):
  File "D:\\1119tensorrange.py", line 12, in <module>
    T = torch.arange(1,0)
RuntimeError: upper bound and larger bound inconsistent with step sign

torch.range

注意 range 是左闭右闭,右边是闭区间!!!
所以,很容易出错!
不要用!
它跟Python 自带的 range不一样

T= torch.range(0, 5,dtype=torch.int64, device='cuda:0')
print(T)

输出:

tensor([0, 1, 2, 3, 4, 5], device='cuda:0')

Python 自带的 range

for i in range(3):
    print(i)

输出:

0
1
2

torch.linspace

torch.linspace(start, end ,steps)
steps 的含义是生成的 tensor 中元素的数目

T = torch.linspace(0,1,5)
print(T)

输出

tensor([0.0000, 0.2500, 0.5000, 0.7500, 1.0000])

你可能感兴趣的:(PyTorch,python,深度学习,开发语言)