DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Updated on

randn and randn_like in PyTorch

Buy Me a Coffee

*Memos:

randn() can create the 0D or more D tensor of the zero or more random floating-point numbers(Default) or complex numbers most of the time about between 2 and -2 from standard normal distribution as shown below:

*Memos:

  • randn() can be used with torch but not with a tensor.
  • The 1st or more arguments with torch are size(Required-Type:int, tuple of int, list of int or size()).
  • There is dtype argument with torch(Optional-Default:None-Type:dtype): *Memos:
  • There is device argument with torch(Optional-Default:None-Type:str, int or device()): *Memos:
  • There is requires_grad argument with torch(Optional-Default:False-Type:bool): *Memos:
    • requires_grad= must be used.
    • My post explains requires_grad argument.
  • There is out argument with torch(Optional-Default:None-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
import torch

torch.randn(size=())
torch.randn(size=torch.tensor(8).size())
# tensor(0.3306)

torch.randn(size=(0,))
torch.randn(0)
torch.randn(size=torch.tensor([]).size())
# tensor([])

torch.randn(size=(3,))
torch.randn(3)
torch.randn(size=torch.tensor([8, 3, 6]).size())
# tensor([-0.6635,  1.6257, -2.0568])

torch.randn(size=(3, 2))
torch.randn(3, 2)
torch.randn(size=torch.tensor([[8, 3], [6, 0], [2, 9]]).size())
# tensor([[-1.9382, 1.1300], [0.4138, 0.8232], [-0.9810, -0.1410]])

torch.randn(size=(3, 2, 4))
torch.randn(3, 2, 4)
# tensor([[[-0.1585, 0.6327, -0.2487, -0.0963],
#          [-0.2869,  2.4429,  0.5098, -1.1569]],
#         [[ 0.2823,  1.0742,  1.0531, -0.2384],
#          [-0.2275, -2.6246,  0.3347, -1.6514]],
#         [[ 0.2934, -1.2568,  0.1798,  1.1989],
#          [ 0.5166, -1.2155,  0.5101, -0.7396]]])

torch.randn(size=(3, 2, 4), dtype=torch.complex64)
torch.randn(3, 2, 4, dtype=torch.complex64)
# tensor([[[0.5911+0.4230j, -0.1695-0.5487j,
#           -0.5253-1.3477j, 0.2331+1.9928j],
#          [1.1815+0.8113j, 0.1307+2.0229j,
#           0.5588+0.6502j, 0.8352-1.1519j]],
#         [[1.5789+0.5048j, -1.2516-1.0842j,
#           0.6233+0.7139j, 0.0393-0.4259j],
#          [0.1236+0.9666j, 0.2755+0.1572j,
#           -0.5766-0.0394j, -0.3715-0.2530j]],
#         [[0.6339-1.1397j, -0.1932-0.8051j,
#           1.3219-0.4141j, 0.0786+0.1172j],
#          [-0.9245+0.9382j, -1.0984+0.4884j,
#           -0.3895-0.0102j, -0.0171+0.9639j]]])
Enter fullscreen mode Exit fullscreen mode

randn_like() can replace the zero or more floating-point numbers or complex numbers of a 0D or more D tensor with the zero or more random floating-point numbers or complex numbers most of the time about between 2 and -2 from standard normal distribution as shown below:

*Memos:

  • randn_like() can be used with torch but not with a tensor.
  • The 1st argument with torch is input(Required-Type:tensor of float or complex).
  • There is dtype argument with torch(Optional-Default:None-Type:dtype): *Memos:
    • If it's None, it's inferred from input.
    • dtype= must be used.
    • My post explains dtype argument.
  • There is device argument with torch(Optional-Default:None-Type:str, int or device()): *Memos:
    • If it's None, it's inferred from input.
    • device= must be used.
    • My post explains device argument.
  • There is requires_grad argument with torch(Optional-Default:False-Type:bool): *Memos:
    • requires_grad= must be used.
    • My post explains requires_grad argument.
import torch

my_tensor = torch.tensor(7.)

torch.randn_like(input=my_tensor)
# tensor(-2.3177)

my_tensor = torch.tensor([7., 4., 5.])

torch.randn_like(input=my_tensor)
# tensor([-0.4706, -0.0940, 1.7397])

my_tensor = torch.tensor([[7., 4., 5.], [2., 8., 3.]])

torch.randn_like(input=my_tensor)
# tensor([[-0.4152, 0.8599, -2.4599],
#         [-1.1088, -0.4828, 2.3003]])

my_tensor = torch.tensor([[[7., 4., 5.], [2., 8., 3.]],
                          [[6., 0., 1.], [5., 9., 4.]]])
torch.randn_like(input=my_tensor)
# tensor([[[-1.0316, 1.5852, -1.1012], [-2.3114, 0.5584, 2.3333]],
#         [[-0.5730, -0.3688, 0.0671], [1.2493, -0.0072, 0.1905]]])

my_tensor = torch.tensor([[[7.+4.j, 4.+2.j, 5.+3.j],
                           [2.+5.j, 8.+1.j, 3.+9.j]],
                          [[6.+9.j, 0.+3.j, 1.+8.j],
                           [5.+3.j, 9.+4.j, 4.+6.j]]])
torch.randn_like(input=my_tensor)
# tensor([[[-0.3193-0.2821j, -0.8942+0.7886j, 0.2968-0.0608j],
#          [-0.5996+0.8685j, 0.7314-0.5378j, 1.7339-0.8352j]],
#         [[-0.0787-0.1357j, -1.4519-0.0295j, 0.1233-0.1794j],
#          [ 0.4109-0.2422j, 0.3973+0.3363j, -1.0303-1.4719j]]])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)