表示整数:皆可
range, np.arange, torch.arange不包含终点
np.linspace, torch.range包含终点
np.linspace第三个参数表示点的个数,其余第三个参数表示步长
import numpy as np
import torch
# x linspace
x_linspace = np.linspace(1, 17, 17)
print("x_linspace:", x_linspace)
print("type x_linspace:", type(x_linspace))
# x arange
x_arange = np.arange(1, 17, 1)
print("x_arange:", x_arange)
print("type x_arange:", type(x_arange))
# x range
x_range = range(1, 17, 1)
print("x_range:", x_range)
print("type x_range:", type(x_range))
# torch range
torch_range = torch.range(1, 17, 1)
print("torch_range", torch_range)
print("type torch_range", torch_range.dtype)
# torch arange
torch_arange = torch.arange(1, 17, 1)
print("torch_arange", torch_arange)
print("type torch_arange", torch_arange.dtype)
output:
x_linspace: [ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17.]
type x_linspace:
x_arange: [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16]
type x_arange:
x_range: range(1, 17)
type x_range:
test.py:44: 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).
torch_range = torch.range(1, 17, 1)
torch_range tensor([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14.,
15., 16., 17.])
type torch_range torch.float32
torch_arange tensor([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])
type torch_arange torch.int64
表示浮点数:np.linspace, np.arange, torch.arange, torch.range
arange(np.arange, torch.arange)不包含终点
np.linspace, torch.range包含终点
np.linspace第三个参数表示点的个数,其余第三个参数表示步长
# x linspace_x
x_linspace = np.linspace(0.1, 1.7, 17)
print("x_linspace:", x_linspace)
print("type x_linspace:", type(x_linspace))
# x arange
x_arange = np.arange(0.1, 1.7, 0.1)
print("x_arange:", x_arange)
print("type x_arange:", type(x_arange))
# torch range
torch_range = torch.range(0.1, 1.7, 0.1)
print("torch_range", torch_range)
print("type torch_range", torch_range.dtype)
# torch arange
torch_arange = torch.arange(0.1, 1.7, 0.1)
print("torch_arange", torch_arange)
print("type torch_arange", torch_arange.dtype)
output:
x_linspace: [0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. 1.1 1.2 1.3 1.4 1.5 1.6 1.7]
type x_linspace:
x_arange: [0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. 1.1 1.2 1.3 1.4 1.5 1.6]
type x_arange:
test.py:39: 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).
torch_range = torch.range(0.1, 1.7, 0.1)
torch_range tensor([0.1000, 0.2000, 0.3000, 0.4000, 0.5000, 0.6000, 0.7000, 0.8000, 0.9000,
1.0000, 1.1000, 1.2000, 1.3000, 1.4000, 1.5000, 1.6000, 1.7000])
type torch_range torch.float32
torch_arange tensor([0.1000, 0.2000, 0.3000, 0.4000, 0.5000, 0.6000, 0.7000, 0.8000, 0.9000,
1.0000, 1.1000, 1.2000, 1.3000, 1.4000, 1.5000, 1.6000])
type torch_arange torch.float32