This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)
*Memos:
- [Warning]-normal() is really tricky.
- My post explains rand() and rand_like().
- My post explains randn() and randn_like().
randint() create the 1D or more D tensor of the zero or more random integers(Default) or floating-point numbers between low
and high-1
(low
<=x<=high-1
) as shown below:
*Memos:
-
randint()
can be used withtorch
but not with a tensor. - The 1st argument with
torch
islow
(Optional-Default:0
-Type:int
): *Memos:- It must be lower than
high
. - The 0D or more D tensor of one integer works.
- It must be lower than
- The 2nd argument with
torch
ishigh
(Required-Type:int
): *Memos:- It must be greater than
low
. - The 0D or more D tensor of one integer works.
- It must be greater than
- The 3rd argument with
torch
issize
(Required-Type:tuple
ofint
orlist
ofint
). - There is
dtype
argument withtorch
(Optional-Type:dtype): *Memos:- If
dtype
is not given,dtype
istorch.int64
. -
int
orfloat
can be used. -
dtype=
must be used. -
My post explains
dtype
argument.
- If
- There is
device
argument withtorch
(Optional-Type:str
,int
or device()): *Memos:-
device=
must be used. -
My post explains
device
argument.
-
- There is
requires_grad
argument withtorch
(Optional-Type:bool
): *Memos:-
requires_grad=
must be used. -
My post explains
requires_grad
argument.
-
- There is
out
argument withtorch
(Optional-Type:tensor
): *Memos:-
out=
must be used. -
My post explains
out
argument.
-
import torch
torch.randint(high=10, size=(3,))
# tensor([7, 4, 8])
torch.randint(high=10, size=(3, 2))
# tensor([[8, 9], [6, 5], [5, 2]])
torch.randint(high=10, size=(3, 2, 4))
# tensor([[[1, 5, 9, 0], [4, 6, 7, 2]],
# [[5, 2, 1, 5], [9, 3, 2, 6]],
# [[9, 3, 6, 4], [0, 4, 7, 5]]])
torch.randint(low=10, high=20, size=(3,))
# tensor([17, 12, 10])
torch.randint(low=10, high=20, size=(3, 2))
# tensor([[14, 18], [10, 19], [15, 16]])
torch.randint(low=10, high=20, size=(3, 2, 4))
# tensor([[[16, 14, 11, 19], [19, 15, 18, 13]],
# [[14, 10, 11, 13], [16, 11, 10, 16]],
# [[17, 12, 17, 10], [13, 16, 11, 10]]])
torch.randint(low=-5, high=5, size=(3,))
# tensor([-1, 2, -3])
torch.randint(low=-5, high=5, size=(3, 2))
# tensor([[-5, 4], [ 1, -1], [-4, -3]])
torch.randint(low=-5, high=5, size=(3, 2, 4))
# tensor([[[-2, 0, 1, -5], [4, -5, -3, 1]],
# [[-4, -1, -1, -1], [-3, 2, -4, -1]],
# [[4, -1, -5, -3], [2, -3, -2, 2]]])
torch.randint(low=-5, high=5, size=(3, 2, 4), dtype=torch.float32)
torch.randint(low=torch.tensor(-5),
high=torch.tensor([5]),
size=(3, 2, 4),
dtype=torch.float32)
# tensor([[[-4., 1., -1., -3.], [-3., -5., -4., 1.]],
# [[-5., 3., 3., 1.], [-1., 4., -5., 2.]],
# [[-2., -4., -5., 3.], [4., 1., -3., 3.]]])
torch.randint(high=1, size=(0,))
torch.randint(low=0, high=1, size=(0,))
torch.randint(low=10, high=20, size=(0,))
# tensor([], dtype=torch.int64)
randperm() can create the 1D tensor of zero or more random integers(Default) or floating-point numbers between 0 and n-1
(0<=x<=n-1
) as shown below:
*Memos:
-
randperm()
can be used withtorch
but not with a tensor. - The 1st argument with
torch
isn
(Required-Type:int
): *Memos:- It must be greater than or equal to 1.
- The 0D or more D tensor of one integer works.
- There is
dtype
argument withtorch
(Optional-Type:dtype): *Memos:- If
dtype
is not given,dtype
istorch.int64
. -
int
orfloat
can be used. -
dtype=
must be used. -
My post explains
dtype
argument.
- If
- There is
device
argument withtorch
(Optional-Type:str
,int
or device()): *Memos:-
device=
must be used. -
My post explains
device
argument.
-
- There is
requires_grad
argument withtorch
(Optional-Type:bool
): *Memos:-
requires_grad=
must be used. -
My post explains
requires_grad
argument.
-
- There is
out
argument withtorch
(Optional-Type:tensor
): *Memos:-
out=
must be used. -
My post explains
out
argument.
-
import torch
torch.randperm(n=0)
# tensor([], dtype=torch.int64)
torch.randperm(n=5)
# tensor([3, 0, 4, 2, 1])
torch.randperm(n=10)
# tensor([8, 6, 9, 2, 1, 3, 5, 0, 7, 4])
torch.randperm(n=10, dtype=torch.float32)
torch.randperm(n=torch.tensor([[10]]), dtype=torch.float32)
# tensor([7., 4., 2., 1., 8., 3., 0., 6., 9., 5.])
normal() can create the 1D or more D tensor of zero or more random floating-point numbers or complex numbers from normal distribution as shown below:
*Memos:
-
normal()
can be used withtorch
but not with a tensor. - The 1st argument with
torch
ismean
(Required-Type:float
orcomplex
ortensor
offloat
orcomplex
): *Memos:- Setting
mean
withoutstd
andsize
istensor
offloat
orcomplex
. - Setting
mean
andstd
withoutsize
isfloat
ortensor
offloat
orcomplex
. - Setting
mean
,std
andsize
isfloat
ortensor
offloat
. *The 0D tensor offloat
also works.
- Setting
- The 2nd argument with
torch
isstd
(Optional-Type:float
ortensor
offloat
): *Memos:- It is standard deviation.
- It must be greater than or equal to 0.
- Setting
std
withoutsize
isfloat
ortensor
offloat
. - Setting
std
withsize
isfloat
ortensor
offloat
. *The 0D tensor offloat
also works.
- The 3rd argument with
torch
issize
(Optional-Type:tuple
ofint
orlist
ofint
): *Memos:- It must be used with
std
. - It must not be negative.
- It must be used with
- There is
dtype
argument withtorch
(Optional-Type:dtype): *Memos:- If
dtype
is not given,dtype
is inferred frommean
orstd
ordtype
of set_default_dtype() is used for floating-point numbers. - Only
float
andcomplex
can be used. -
dtype=
must be used. -
My post explains
dtype
argument.
- If
- There is
device
argument withtorch
(Optional-Type:str
,int
or device()): *Memos:-
device=
must be used. -
My post explains
device
argument.
-
- There is
requires_grad
argument withtorch
(Optional-Type:bool
): *Memos:-
requires_grad=
must be used. -
My post explains
requires_grad
argument.
-
- There is
out
argument withtorch
(Optional-Type:tensor
): *Memos:-
out=
must be used. -
My post explains
out
argument.
-
import torch
torch.normal(mean=torch.tensor([1., 2., 3.]))
# tensor([1.2713, 0.7271, 3.5027])
torch.normal(mean=torch.tensor([1.+0.j, 2.+0.j, 3.+0.j]))
# tensor([1.1918-0.9001j, 2.3555+0.2956j, 2.5479-0.4672j])
torch.normal(mean=torch.tensor([1., 2., 3.]),
std=torch.tensor([4., 5., 6.]))
# tensor([2.0851, -4.3646, 6.0162])
torch.normal(mean=torch.tensor([1.+0.j, 2.+0.j, 3.+0.j]),
std=torch.tensor([4., 5., 6.]))
# tensor([1.7673-3.6004j, 3.7773+1.4781j, 0.2872-2.8034j])
torch.normal(mean=torch.tensor([1., 2., 3.]), std=4.)
# tensor([2.0851, -3.0917, 5.0108])
torch.normal(mean=torch.tensor([1.+0.j, 2.+0.j, 3.+0.j]), std=4.)
# tensor([1.7673-3.6004j, 3.4218+1.1825j, 1.1914-1.8689j])
torch.normal(mean=1., std=torch.tensor([4., 5., 6.]))
# tensor([2.0851, -5.3646, 4.0162])
torch.normal(mean=1., std=4., size=(3,))
torch.normal(mean=torch.tensor(1.), std=torch.tensor(4.), size=(3,))
# tensor([2.0851, -4.0917, 3.0108])
torch.normal(mean=1., std=4., size=(3, 2))
torch.normal(mean=torch.tensor(1.), std=torch.tensor(4.), size=(3, 2))
# tensor([[2.0851, -4.0917],
# [3.0108, 2.6723],
# [-1.5577, -1.6431]])
torch.normal(mean=1., std=4., size=(3, 2, 4))
torch.normal(mean=torch.tensor(1.), std=torch.tensor(4.), size=(3, 2, 4))
# tensor([[[-3.7568, 6.5729, 9.4236, -0.4183],
# [2.4840, 5.3827, 9.5657, 1.5267]],
# [[8.0575, -0.5000, -0.3416, 5.3502],
# [-4.3835, 1.6974, 2.6226, -1.9671]],
# [[1.1422, 1.7790, 4.5886, -0.3273],
# [2.8941, -3.3046, 1.1336, 2.8792]]])
This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito) | Sciencx (2024-07-10T13:49:24+00:00) randint(), randperm() and normal() in PyTorch. Retrieved from https://www.scien.cx/2024/07/10/randint-randperm-and-normal-in-pytorch/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.