Python

PyTorch – Var()

We will see how to return the variance of a tensor using var() in this PyTorch tutorial.

PyTorch is an open-source framework available with a Python programming language. Tensor is a multidimensional array that is used to store the data. To use a tensor, we have to import the torch module. To create a tensor, the method used is tensor().

Syntax:

torch.tensor(data)

Where the data is a multi-dimensional array.

Var()

Var() in PyTorch is used to return the variance of the elements present in the input tensor object.

Syntax:

torch.var(tensor,dim)

Where:

1. The tensor is the input tensor.

2. Dim is to reduce the dimension. Dim=0 specifies the column comparison which gets the variance along a column and dim=1 specifies the row comparison which gets the variance along the row.

Example 1:

In this example, we will create a tensor with 2 dimensions that has 3 rows and 5 columns and apply the var() function on rows and columns.

#import torch module

import torch

 

#create a tensor with 2 dimensions (3 * 5)

#with random elements using randn() function

data = torch.randn(3,5)

 

#display

print(data)

 

print()

 

#get variance along columns

print("Variance across columns:")

print(torch.var(data, dim=0))

 

print()

 

#get Variance along rows

print("Variance across rows:")

print(torch.var(data, dim=1))

Output:

tensor([[ 0.8844, -0.6416, -0.5520, 0.3798, -0.0587],

[ 0.0633, 1.9403, -1.3161, -0.5293, 0.4061],

[ 1.3974, 0.9784, -0.1111, -1.7721, -0.5881]])

Variance across columns:

tensor([0.4529, 1.7027, 0.3717, 1.1669, 0.2475])

Variance across rows:

tensor([0.4115, 1.4706, 1.6017])

We can see that the variance is returned across the columns and rows.

Example 2:

Create a Tensor with 5 * 5 matrix and return the variance across the rows and columns.

#import torch module

import torch

 

#create a tensor with 2 dimensions (5 * 5)

#with random elements using randn() function

data = torch.randn(5,5)

 

#display

print(data)

 

print()

 

#get Variance along columns

print("Variance across columns:")

print(torch.var(data, dim=0))

 

print()

 

#get Variance along rows

print("Variance across rows:")

print(torch.var(data, dim=1))

Output:

tensor([[ 0.1260, 1.1681, 0.2191, -0.3685, -2.0660],

[ 0.5840, 0.2955, 0.2362, -1.2847, 1.3656],

[ 0.2925, 1.3555, -1.2881, 0.9167, 0.1882],

[-0.7466, 0.2770, 1.1448, -1.2779, -0.2764],

[-1.1316, -0.6998, 2.5051, -0.6687, -0.1624]])

Variance across columns:

tensor([0.5318, 0.6768, 1.9404, 0.8168, 1.5226])

Variance across rows:

tensor([1.4157, 0.9283, 1.0072, 0.8749, 2.1285])

We can see that the variance across the rows and columns were returned.

Without the Dim Parameter

If we don’t specify the dim parameter, it returns the variance from the entire tensor.

Example 1:

Create a 2D tensor with 5*5 matrix and return the variance.

#import torch module

import torch

 

#create a tensor with 2 dimensions (5 * 5)

#with random elements using randn() function

data = torch.randn(5,5)

 

#display

print(data)

 

print()

 

#get Variance

print("Variance :")

print(torch.var(data))

Output:

tensor([[-0.0151, -0.7915, -0.9665, -1.4131, 0.9591],

[-0.6647, 0.6698, -1.3399, -0.0513, -0.0519],

[ 1.0328, -0.3692, 1.0579, 0.5857, 0.3899],

[-0.5928, 1.2447, -0.5782, 1.4461, -0.4847],

[-1.3246, -0.7092, 0.1471, 0.5752, 3.1142]])

Variance :

tensor(1.1131)

Example 2:

Create a 1D tensor with 5 values and return the variance.

#import torch module

import torch

#create a tensor with 5 numeric values

data = torch.tensor([10.6,20.7,30.6,40.4,50.0])

#display

print(data)

print()

#get variance

print("Variance :")

print(torch.var(data))

Output:

tensor([10.6000, 20.7000, 30.6000, 40.4000, 50.0000])

Variance :

tensor(242.5780)

Work with CPU

If you want to run a var() function on the CPU, we have to create a tensor with a cpu() function. This will run on a CPU machine.

When we create a tensor, this time, we can use the cpu() function.

Syntax:

torch.tensor(data).cpu()

Example 1:

In this example, we will create a tensor with 2 dimensions that has 3 rows and 5 columns with the cpu() function and apply the var() function on rows and columns.

#import torch module

import torch

#create a tensor with 2 dimensions (3 * 5)

#with random elements using randn() function

data = torch.randn(3,5).cpu()

#display

print(data)

print()

#get Variance along columns

print("Variance across columns:")

print(torch.var(data, dim=0))

print()

#get Variance along rows

print("Variance across rows:")

print(torch.var(data, dim=1))

Output:

tensor([[ 0.1278, 1.3140, 0.5116, -0.1922, 0.9349],

[-1.1720, 1.4805, -0.2628, 0.5970, -0.6479],

[-1.7930, 0.2337, -1.3810, -0.9092, -0.0778]])

Variance across columns:

tensor([0.9607, 0.4582, 0.9053, 0.5676, 0.6427])

Variance across rows:

tensor([0.3655, 1.1027, 0.7312])

We can see that the variance is returned across the columns and rows.

Example 2:

Create a Tensor with 5 * 5 matrix with the cpu() function and return the variance across the rows and columns.

#import torch module

import torch

 

#create a tensor with 2 dimensions (5 * 5)

#with random elements using randn() function

data = torch.randn(5,5).cpu()

 

#display

print(data)

 

print()

 

#get Variance along columns

print("Variance across columns:")

print(torch.var(data, dim=0))

 

print()

 

#get Variance along rows

print("Variance across rows:")

print(torch.var(data, dim=1))

Output:

tensor([[-0.3587, 1.2200, -0.8744, -0.8184, -1.1658],

[ 0.1428, -0.6122, -0.9894, 1.8219, -0.0248],

[-1.9626, -0.3423, 1.3046, 0.9737, 1.1691],

[-0.1054, -0.3935, -0.3632, -0.7317, -0.1751],

[ 0.9651, -0.6595, 0.3152, 0.6908, -0.0370]])

Variance across columns:

tensor([1.1489, 0.6115, 0.9007, 1.2999, 0.6869])

Variance across rows:

tensor([0.9032, 1.1678, 1.9312, 0.0595, 0.4048])

We can see that the variance across the rows and columns were returned.

Conclusion

In this PyTorch lesson, we discussed about the var() function and how to apply it on a tensor to return the variance across the columns and rows.

We also created a tensor with the cpu() function and returned the variance. If the dim is not specified in two or multi-dimensional tensor, it returns the variance of the entire tensor.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain