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, 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
. - The default type is
float32
.
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, 4, dtype=torch.int64)
# tensor([10, 13, 16, 20], dtype=torch.int64)
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, integers 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. - 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.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, 20, 4, 100, dtype=torch.int64)
# tensor([-9223372036854775808,
# -9223372036854775808,
# -9223372036854775808,
# -9223372036854775808])
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(Default) or complex numbers from normal distribution as shown below:
*Memos:
-
normal()
can be used only fromtorch
but not from a tensor. - The default type is
float32
. - The 1st argument is
mean
. - The 2nd argument is
std
(Standard deviation). - The 3rd argument is
size
.
import torch
torch.normal(0, 1, (0,))
# tensor([])
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, (3, 2, 4), dtype=torch.complex64)
# tensor([[[0.1640-0.8105j, -0.0654+0.2241j,
# 0.1629-0.8095j, -0.2853+1.0529j],
# [-0.4795-0.5646j, -0.6884-0.1562j,
# -0.6333+0.7238j, 0.8710-1.1705j]],
# [[-1.5067+1.2063j, 0.6296-0.0607j,
# 0.7197-0.4113j, 0.1742+0.3301j],
# [-0.4388+1.0028j, 0.0297+0.2668j,
# -0.3234+0.7390j, 0.0336+0.5532j]],
# [[0.1296+0.9593j, -0.1632+0.3848j,
# -0.6574-0.0247j, -0.6997-0.0548j],
# [0.5176+1.0693j, 0.6728+1.2304j,
# -0.6519+0.0970j, -0.5081-0.4026j]]])
torch.normal(5, 4, (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([[[9.7707+5.7214j, 4.7577+12.6819j,
# 7.8147+9.3258j, 4.5580+9.2139j],
# [1.6257+3.8480j, 1.3294+4.7965j,
# 2.8914+4.5585j, 5.9573+4.1559j]],
# [[8.0873+4.8930j, 2.9459+4.8999j,
# 6.6957+3.7738j, 1.4319+7.7348j],
# [5.5551+5.8145j, 1.9599+8.7604j,
# 6.1416+5.7876j, 2.5204+3.0414j]],
# [[4.5839+6.1299j, 3.1245+3.8965j,
# 9.1091+6.3294j, 2.8272+8.0571j],
# [2.5482+3.1353j, 3.4173+2.0660j,
# 9.1401+5.8971j, 4.5078+4.3233j]]])
Top comments (0)