*Memos:
- You can use manual_seed() with randint() and randperm(). *My post explains
manual_seed()
. - My post explains rand() and rand_like().
- My post explains randn() and randn_like().
- My post explains normal().
randint() can create the 0D 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 with torch 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
,list
ofint
or size()). - There is
dtype
argument withtorch
(Optional-Default:None
-Type:dtype): *Memos:- If it's
None
, it'storch.int64
. -
dtype=
must be used. - My post explains
dtype
argument.
- If it's
- There is
device
argument withtorch
(Optional-Default:None
-Type:str
,int
or device()): *Memos:- If it's
None
, get_default_device() is used. *My post explainsget_default_device()
and set_default_device(). -
device=
must be used. - My post explains
device
argument.
- If it's
- There is
requires_grad
argument withtorch
(Optional-Default:False
-Type:bool
): *Memos:-
requires_grad=
must be used. - My post explains
requires_grad
argument.
-
- There is
out
argument withtorch
(Optional-Default:None
-Type:tensor
): *Memos:-
out=
must be used. - My post explains
out
argument.
-
import torch torch.randint(high=10, size=()) torch.randint(high=10, size=torch.tensor(8).size()) # tensor(7) torch.randint(high=10, size=(3,)) torch.randint(high=10, size=torch.tensor([8, 3, 6]).size()) # tensor([7, 4, 8]) torch.randint(high=10, size=(3, 2)) torch.randint(high=10, size=torch.tensor([[8, 3], [6, 0], [2, 9]]).size()) # 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-Default:None
-Type:dtype): *Memos:- If it's
None
, it'storch.int64
. -
dtype=
must be used. - My post explains
dtype
argument.
- If it's
- There is
device
argument withtorch
(Optional-Default:None
-Type:str
,int
or device()): *Memos:- If it's
None
, get_default_device() is used. *My post explainsget_default_device()
and set_default_device(). -
device=
must be used. - My post explains
device
argument.
- If it's
- There is
requires_grad
argument withtorch
(Optional-Default:False
-Type:bool
): *Memos:-
requires_grad=
must be used. - My post explains
requires_grad
argument.
-
- There is
out
argument withtorch
(Optional-Default:None
-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.])
Top comments (0)