 Home Deep Learning Deep Learning Framework Using torch.randn() and torch.randn_like() to create Random Tensors in PyTorch

# Using torch.randn() and torch.randn_like() to create Random Tensors in PyTorch ## Introduction

In this tutorial, we will teach how to use torch.randn() and torch.randn_like() to create PyTorch tensors with random values. Initializing tensors with random values is quite common in deep learning, hence these functions are quite useful here. Let us understand these functions in detail with examples.

## Random PyTorch Tensors with torch.randn()

torch.randn function is used to generate tensor with the random values from a normal distribution having mean 0 and variance 1.

Let us import PyTorch library and see some examples of this torch randn function.

In :

`import torch;`

### Example – 1: Creating 2 Dimensional Random Tensor with torch.randn()

In this first example, we are using the torch rand function to create 2-D random tensor of size 3×3.  There are three ways to this –

#### Method – 1:

In 1st method, we simply pass the size as 3,3 to randn function as shown below –

In :

```tensor = torch.randn(3,3)

tensor```

Out:

```tensor([[-0.2384,  1.2642, -0.5479],
[-1.3429, -0.3437,  1.0614],
[-0.8378, -0.6181, -0.1969]])```

#### Method – 2:

In the 2nd method, we pass the size 3,3 as a list to the size parameter of torch.rand function as below –

In :

```tensor = torch.randn(size=[3,3])

tensor```
Out:
```tensor([[-0.2384,  1.2642, -0.5479],
[-1.3429, -0.3437,  1.0614],
[-0.8378, -0.6181, -0.1969]])```

#### Method – 3:

In the 3rd method, we pass the size 3,3 as a tuple to the size parameter of torch.rand() function as below –

In :

```tensor = torch.randn(size=(3,3))

tensor```
Out:
```tensor([[-0.2384,  1.2642, -0.5479],
[-1.3429, -0.3437,  1.0614],
[-0.8378, -0.6181, -0.1969]])```

### Example – 2: Creating 1 Dimensional Random Tensor

In the 2nd example, we are generating a 1-D tensor with 12 random values.

In :

```tensor = torch.randn(12);

tensor```

Out:

```tensor([-1.2815, -0.9762, -0.3404, 0.1790, 0.6922, -0.0225, -1.3685, 0.1374,
-0.3351, 3.0634, -1.9558, 0.1580])```

### Example – 3: Creating 3 Dimensional Random Tensor

In this example, we generate a 3D tensor of size 2x3x3.

In :

```tensor = torch.randn(size=(2,3,4))

tensor```
Out:
```tensor([[[ 1.6177, -1.1718,  0.6330,  1.0149],
[ 1.1311, -1.6514, -1.0022,  0.5430],
[ 0.6121,  0.7355,  1.5479, -0.0983]],

[[-0.7185, -1.1925,  0.2587,  0.5498],
[ 0.2301,  0.4943,  0.3696, -0.9135],
[ 0.1081,  1.6165, -2.0171,  0.7277]]])```

### Example – 4: Using dtype parameter with torch.randn

dtype parameter of torch.randn function is used to specify the data type of random tensor as shown in below examples.

#### Using Data type as torch.float32

In :
```tensor = torch.randn(size=(3,2), dtype=torch.float32)

tensor```
Out:
```tensor([[-1.3475, -0.3550],
[-2.1005,  0.4636],
[ 0.1390, -1.2501]])```

#### Using Data type as torch.float64

In :
```tensor = torch.randn(size=(3,2), dtype=torch.float64)

tensor```
Out:
```tensor([[ 0.5041, -0.9418],
[-1.0981, -0.8508],
[ 1.0068,  1.1578]], dtype=torch.float64)```

#### Issue with Int data type with torch.randn() :

torch.randn function cannot generate tensors with random integers because it follows a normal distribution with means 0 and variance 1. It can only generate float values.

### Example – 5: Using Seed Value with torch.randn

Whenever torch.randn() function is used it generates different random values each time. However, if you want it to generate the same values every time it is called then we have to use a seed value with it as shown below.

First of all, a generator object gen is created and initialized with a seed value with manual_seed(). Then this generator object is used inside torch.rand function. Now, whenever this block of code is used it will generate tensor with the same values.

In :

```g_cpu = torch.Generator()
g_cpu.manual_seed(2847587347)

tensor = torch.randn(size=(4,3), generator = g_cpu)

tensor```
Out:
```tensor([[-0.0872, -0.2799,  1.1079],
[-0.5893, -0.5398,  0.1780],
[ 0.4049, -2.1361, -0.6413],
[ 0.5305, -1.0548, -1.1888]])```

## Random PyTorch Tensors with torch.randn_like()

You may be required to create a tensor whose size is the same as another tensor. For this, the obvious approach would be to manually get the size of the other tensor and then create the required tensor of that size. Alternatively, you can use torch.randn_like function that does not require you to calculate the size of the other tensor.

Let us understand this in detail with examples below.

### Example – 1: Creating 2 Dimensional Random Tensor with torch.randn_like()

First of all, let us create a ones tensor that will be used as a reference to create the random tensor of the same size. Then this ones tensor is passed to torch.randn_like function for reference of size.

In :

```ref_tensor = torch.ones(size=(3,4))

ref_tensor```
Out:
```tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])```
In:
```tensor = torch.randn_like(ref_tensor)

tensor```
Out:
```tensor([[ 0.9439, -1.0748, -0.4935,  0.3468],
[-1.9797, -2.5192,  2.3560,  0.1013],
[-0.8866, -0.6880, -0.1599, -0.6374]])```

### Example – 2: Creating 3 Dimensional Random Tensor with torch.randn_like()

Here we create a 3-D ones tensor which is used as a size reference to create random tensor.

In :

```ref_tensor = torch.ones(size=(2,3,4))

ref_tensor```
Out:
```tensor([[[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]],

[[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]]])```
In :
```tensor = torch.randn_like(ref_tensor)

tensor```
Out:
```tensor([[[ 1.8054, -0.1211,  0.3865, -0.2203],
[-1.1548, -0.0190,  0.4501, -1.2890],
[ 0.7608, -0.0594, -0.8486,  1.1663]],

[[ 0.2948,  1.4822,  0.6049,  0.6425],
[-0.2417, -1.1210, -0.3285,  0.3379],
[-0.6057, -1.4145, -1.0464,  0.1785]]])```