Open In App

Python – Pytorch randn() method

Improve
Improve
Like Article
Like
Save
Share
Report

PyTorch torch.randn() returns a tensor defined by the variable argument size (sequence of integers defining the shape of the output tensor), containing random numbers from standard normal distribution.

Syntax: torch.randn(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)

Parameters:

  • size: sequence of integers defining the size of the output tensor. Can be a variable number of arguments or a collection like a list or tuple.
  • out: (optional) output tensor.
  • dtype: (optional) data type of output tensor.
  • layout: (optional) the desired layout of returned Tensor. Default value is torch.strided.
  • device: (optional) the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.
  • requires_grad: (optional) if set to true, autograd records operation on the output tensor.

Return: tensor filled with values from standard normal distribution.

Let’s see this concept with the help of few examples:

Example 1:

Python




# import pytorch library
import torch
 
# create a tensor of size 2 x 4
input_var = torch.randn(2,4)
 
print (input_var)


 
 

 Output:

 

tensor([[-1.4313, -0.3831, -0.8356, -1.5555],
        [-1.2749, -1.1872, -0.4983,  0.1029]])

 

This returns a tensor of size 2 × 4, filled with values from standard normal distribution, that is mean is 0 and variance is 1.

 

Example 2:

 

Python3




# import Pytorch library
import torch
 
# create a 3-dimensional tensor
# of 4 x 5
input_var =  torch.randn(3, 4, 5,
                     requires_grad = True)
print(input_var)


 
 

 Output:

 

tensor([[[-0.1097,  1.6845,  0.9375, -1.0515,  0.5767], 
        [ 0.1924, -0.7736, -0.7102, -0.2654,  0.3118], 
        [-0.5314,  0.1924, -1.1629,  0.2360,  0.8605], 
        [-0.8036, -0.0695, -0.6062,  1.4872,  0.5455]],
       [[ 1.5699, -0.7190,  1.0925,  0.8463, -0.1906], 
        [-0.0763, -0.6819, -1.0517, -0.5087, -1.4451], 
        [-2.0127,  1.0061,  0.5723, -0.1336, -0.3821], 
        [ 0.0868,  1.1556,  0.3842, -0.4168, -1.4604]],
       [[ 0.1368, -1.6240, -0.1875, -0.5964,  0.9352], 
        [ 0.4429,  0.2843, -1.2151,  1.3456, -0.4539], 
        [-0.4528,  1.9981, -1.2007,  0.0071, -0.0239], 
        [-0.1003,  0.7938, -0.0977, -1.4097,  0.1679]]], requires_grad=True)   
 

 

This returns a tensor of size 3 × 4 × 5, filled with random numbers, also recording the gradient values, when performed. 
Example 3: 

 

Python3




# import Pytorch library
import torch
 
# error occur
input_var =  torch.randn(3.0, 4.0, 5.0,
                     requires_grad = True)
print(input_var)


 
 

Output:

 

TypeError                                 Traceback (most recent call last) 
in 
     1 # import Pytorch library 
     2 import torch 
—-> 3 input =  torch.randn(3.0, 4.0, 5.0,requires_grad=True) 
     4 print( input )
TypeError: randn() received an invalid combination of arguments – got (float, float, float, requires_grad=bool), but expected one of: 
* (tuple of ints size, *, tuple of names , torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad) 
* (tuple of ints size, *, torch.Generator generator, tuple of names , torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad) 
* (tuple of ints size, *, torch.Generator generator, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad) 
* (tuple of ints size, *, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)
 

 

size parameter cannot take floating-point numbers so the error will generate.

 



Last Updated : 02 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads