【torch小知识点02】torch.range()和torch.arange()的区别

【torch小知识点】

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

在pytorch中 ,torch.arange更官方

  • torch.arange()左闭右开,而torch.range()是左右都闭
import torch

# 数值左开右闭
x_arange = torch.arange(0,12).reshape(3,4)
x_arange
tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]])
# 数值左右都闭
x_range = torch.range(0,11).reshape(3,4)
x_range
C:\Users\YANG\AppData\Local\Temp\ipykernel_15668\3824144836.py:1: UserWarning: torch.range is deprecated and will be removed in a future release because its behavior is inconsistent with Python's range builtin. Instead, use torch.arange, which produces values in [start, end).
  x_range12 = torch.range(0,11).reshape(3,4)





tensor([[ 0.,  1.,  2.,  3.],
        [ 4.,  5.,  6.,  7.],
        [ 8.,  9., 10., 11.]])
  • torch.arange()可以单独写一个数值,表示0开始到该数结束的张量,而torch.range()不行
x_arange = torch.arange(12).reshape(3,4)
x_arange
tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]])
#无法用单独一个数表示结尾,报错
x_range = torch.range(12).reshape(3,4)
x_range
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

Input In [17], in ()
----> 1 x_range = torch.range(12).reshape(3,4)
      2 x_range


TypeError: range() missing 1 required positional arguments: "end"

你可能感兴趣的:(小知识点,python,pytorch,深度学习)