arange() can create the 1D tensor of zero or more integers or floating-point numbers as shown below:
*Memos:
-
arange()
can be used only from torch but not from a tensor. - The 1st argument is
start
which is0
by default. - The 2nd argument is
end
. - The 3rd argument is
step
which is1
by default. - Setting only one argument sets
0
tostart
. - There is range() which is similar to
arange()
butrange()
is deprecated.
import torch
torch.arange(5)
# tensor([0, 1, 2, 3, 4])
torch.arange(5.)
# tensor([0., 1., 2., 3., 4.])
torch.arange(5, 15)
# tensor([5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
torch.arange(5., 15.)
# tensor([5., 6., 7., 8., 9., 10., 11., 12., 13., 14.])
torch.arange(5, 15, 3)
# tensor([5, 8, 11, 14])
torch.arange(5., 15., 3.)
# tensor([5., 8., 11., 14.])
torch.arange(-5, 5)
# tensor([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4])
torch.arange(-5., 5.)
# tensor([-5., -4., -3., -2., -1., 0., 1., 2., 3., 4.])
torch.arange(-5, 5, 3)
# tensor([-5, -2, 1, 4])
torch.arange(-5., 5., 3.)
# tensor([-5., -2., 1., 4.])
torch.arange(0)
torch.arange(0, 0)
torch.arange(0, 0, 3)
# tensor([], dtype=torch.int64)
torch.arange(0.)
torch.arange(0., 0.)
torch.arange(0., 0., 3.)
# tensor([])
linspace()
can create the 1D tensor of the zero or more floating-point numbers(Default), integers or complex numbers which are evenly spaced from start
to end
as shown below:
*Memos:
-
linspace()
can be used only fromtorch
but not from a tensor. - The 1st argument(Required) is
start
. - The 2nd argument(Required) is
end
. - The 3rd argument(Required) is
steps
. - Setting
start
andend
of integer type is not enough to get the tensor of integer type so we must set integer type todtype
.
import torch
torch.linspace(10, 20, 0)
torch.linspace(10., 20., 0)
# tensor([])
torch.linspace(10, 20, 1)
torch.linspace(10., 20., 1)
tensor([10.])
torch.linspace(10, 20, 2)
torch.linspace(10., 20., 2)
# tensor([10., 20.])
torch.linspace(10, 20, 3)
torch.linspace(10., 20., 3)
# tensor([10., 15., 20.])
torch.linspace(10, 20, 4)
torch.linspace(10., 20., 4)
# tensor([10.0000, 13.3333, 16.6667, 20.0000])
torch.linspace(10, 20, 0, dtype=torch.int64)
# tensor([], dtype=torch.int64)
torch.linspace(10, 20, 1, dtype=torch.int64)
# tensor([10], dtype=torch.int64)
torch.linspace(10, 20, 2, dtype=torch.int64)
# tensor([10, 20], dtype=torch.int64)
torch.linspace(10, 20, 3, dtype=torch.int64)
# tensor([10, 15, 20], dtype=torch.int64)
torch.linspace(10, 20, 4, dtype=torch.int64)
# tensor([10, 13, 16, 20], dtype=torch.int64)
torch.linspace(10+6j, 20+3j, 0)
# tensor([], dtype=torch.complex64)
torch.linspace(10+6j, 20+3j, 1)
# tensor([10.+6.j])
torch.linspace(10+6j, 20+3j, 2)
# tensor([10.+6.j, 20.+3.j])
torch.linspace(10+6j, 20+3j, 3)
# tensor([10.+6.0000j, 15.+4.5000j, 20.+3.0000j])
torch.linspace(10+6j, 20+3j, 4)
# tensor([10.0000+6.j, 13.3333+5.j, 16.6667+4.j, 20.0000+3.j])
logspace()
can basically create the 1D tensor of the zero or more floating-point numbers(Default) or complex numbers which are evenly spaced from basestart
to baseend
as shown below:
*Memos:
-
logspace()
can be used only fromtorch
but not from a tensor. - The 1st argument(Required) is
start
. - The 2nd argument(Required) is
end
. - The 3rd argument(Required) is
steps
. - The 4th argument(Optional) is
base
which is10.0
by default. - The exponential parts
start
andend
are evenly spaced.
import torch
torch.logspace(10, 20, 0)
torch.logspace(10., 20., 0)
# tensor([])
torch.logspace(10, 20, 1)
torch.logspace(10., 20., 1)
# tensor([1.0000e+10])
torch.logspace(10, 20, 2)
torch.logspace(10., 20., 2)
# tensor([1.0000e+10, 1.0000e+20])
torch.logspace(10, 20, 3)
torch.logspace(10., 20., 3)
# tensor([1.0000e+10, 1.0000e+15, 1.0000e+20])
torch.logspace(10, 20, 4)
torch.logspace(10., 20., 4)
# tensor([1.0000e+10, 2.1544e+13, 4.6416e+16, 1.0000e+20])
torch.logspace(10, 20, 4, 100)
torch.logspace(10., 20., 4, 100)
# tensor([1.0000e+20, 4.6416e+26, 2.1544e+33, inf])
torch.logspace(10+6j, 20+3j, 0)
# tensor([], dtype=torch.complex64)
torch.logspace(10+6j, 20+3j, 1)
# tensor([3.1614e+09+9.4871e+09j])
torch.logspace(10+6j, 20+3j, 2)
# tensor([3.1614e+09+9.4871e+09j, 8.1122e+19+5.8475e+19j])
torch.logspace(10+6j, 20+3j, 3)
# tensor([3.1614e+09+9.4871e+09j,
# -5.9232e+14-8.0570e+14j,
# 8.1122e+19+5.8475e+19j])
torch.logspace(10+6j, 20+3j, 4)
# tensor([3.1614e+09+9.4871e+09j,
# 1.0655e+13-1.8725e+13j,
# -4.5353e+16+9.8772e+15j,
# 8.1122e+19+5.8475e+19j])
torch.logspace(10+6j, 20+3j, 4, 100)
# tensor([-8.0012e+19+5.9985e+19j,
# -2.3708e+26-3.9904e+26j,
# 1.9593e+33-8.9592e+32j,
# inf+infj])
normal() can create the 1D or more D tensor of zero or more random floating-point numbers from normal distribution as shown below:
*Memos:
-
normal()
can be used only fromtorch
but not from a tensor. - The 1st argument is
mean
. - The 2nd argument is
std
(Standard deviation). - The 3rd argument is
size
.
import torch
torch.normal(0, 1, (3,)) # tensor([2.4737, 1.0925, -0.2984])
torch.normal(0, 1, (3, 2)) # tensor([[ 1.5716, 0.2892],
# [-2.7665, -1.1040],
# [-0.8145, -0.0520]])
torch.normal(0, 1, (3, 2, 4))
# tensor([[[0.4032, -1.1011, -0.2356, -2.3010],
# [0.3675, -0.1115, -0.9581, -1.3232]],
# [[1.0273, 0.9534, 1.2640, -0.2971],
# [2.5688, -0.0851, 0.7737, 1.2036]],
# [[0.7446, -0.2630, -1.4919, -0.7096],
# [-0.3706, 0.0073, 0.8246, 0.9313]]])
torch.normal(0, 1, (0,)) # tensor([])
torch.normal(5, 4, (3,)) # tensor([7.2012, -1.6016, 5.2472])
torch.normal(5, 4, (3, 2)) # tensor([[9.6180, 12.7688],
# [-1.8151, 3.9245],
# [0.3551, 4.5507]])
torch.normal(5, 4, (3, 2, 4))
# tensor([[[0.7033, 7.8921, 0.8033, 8.2755],
# [1.4939, 7.6329, 9.7839, 5.8547]],
# [[-1.1205, 7.1663, 2.8457, 7.5940],
# [3.9927, 6.8923, 2.9762, 4.6035]],
# [[4.2754, 5.5594, 6.2369, 8.5229],
# [8.7253, 3.6859, -2.4835, -1.9812]]])
torch.normal(5, 4, (0,)) # tensor([])