.randn()
Published Aug 22, 2024
Contribute to Docs
The .randn()
function in PyTorch generates a tensor with random numbers drawn from a normal distribution with a mean of 0 and a standard deviation of 1. This function is particularly useful for initializing weights in neural networks and for other purposes requiring randomized data, especially in machine learning applications.
Syntax
torch.randn(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)
*size
: Specifies the shape of the output tensor. This can be one or more integers, defining the dimensions of the tensor.out
(optional): A tensor where the result will be stored. If specified, the output is stored in this tensor; otherwise, a new tensor is created.dtype
(optional): The desired data type of the returned tensor.layout
(optional): The desired layout of the returned tensor. The default istorch.strided
.device
(optional): The device on which the tensor is allocated.requires_grad
(optional): If set toTrue
, PyTorch will track operations on the tensor for automatic differentiation. Defaults toFalse
.
Example
The following example uses .randn()
to create a 4x4 tensor with random values from a normal distribution:
import torchtensor = torch.randn(4, 4)print(tensor)
This produces the following output:
tensor([[-0.7484, 1.2086, 0.3430, 0.6699],[ 0.7022, 0.0815, 0.4855, 0.1603],[-0.1214, 0.2484, 1.5672, -0.7005],[ 1.3106, -0.6518, 0.7351, -0.1027]])
Note: This code will generate a different random output each time it is run, as .randn()
produces random values from a normal distribution.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.