Python

PyTorch – Std()

We will see how to return the standard deviation of a tensor using std() 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.

Std()

Std() in PyTorch is used to return the standard deviation of the elements present in the input tensor object.

Syntax:

torch.std(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 standard deviation along a column and dim=1 specifies the row comparison which gets the standard deviation 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 std() 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 standard deviation along columns

print("Standard deviation across columns:")

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

 

print()

 

#get standard deviation along rows

print("Standard deviation across rows:")

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

Output:

tensor([[ 0.6548, 1.0587, -0.1196, 0.9985, -0.2190],

[ 0.3791, 1.5435, -0.5304, 0.8167, 3.5842],

[-0.1122, -0.2159, 0.3844, -0.6877, -0.7479]])

Standard deviation across columns:

tensor([0.3886, 0.9088, 0.4582, 0.9255, 2.3633])

Standard deviation across rows:

tensor([0.6088, 1.5499, 0.4633])

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

Example 2:

Create a Tensor with 5 * 5 matrix and return the standard deviation 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 Standard deviation along columns

print("Standard deviation across columns:")

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

 

print()

 

#get Standard deviation along rows

print("Standard deviation across rows:")

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

Output:

tensor([[-0.2092, 0.2423, -0.6894, 0.4194, -0.3451],

[ 0.0026, 0.0415, 0.0787, 0.3679, 0.6610],

[ 1.1111, -1.2749, -0.5760, 0.0788, -0.7471],

[-0.9320, -0.4619, -0.4667, 0.7881, 0.4340],

[ 0.6366, -1.0388, -1.3156, 0.3060, 0.7883]])

Standard deviation across columns:

tensor([0.7871, 0.6589, 0.4997, 0.2568, 0.6706])

Standard deviation across rows:

tensor([0.4486, 0.2806, 0.9164, 0.7120, 0.9814])

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

Without the Dim Parameter

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

Example 1:

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

#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 standard deviation

print("Standard deviation :")

print(torch.std(data))

Output:

tensor([[ 0.7371, 0.9772, -0.7774, 0.6982, -1.6117],

[-0.3546, 0.0951, 0.0059, 0.5024, -1.1832],

[ 0.0237, 1.0456, 1.6042, 0.6445, -0.9371],

[ 0.7644, -0.8274, 0.8999, 0.3538, -0.0928],

[ 1.4303, 0.8764, -1.6896, 0.0271, -0.1859]])

Standard deviation :

tensor(0.9011)

Example 2:

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

#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 Standard deviation

print("Standard deviation :")

print(torch.std(data))

Output:

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

Standard deviation :

tensor(15.5749)

Work with CPU

If you want to run an std() 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 std() 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 Standard deviation along columns

print("Standard deviation across columns:")

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

 

print()

 

#get Standard deviation along rows

print("Standard deviation across rows:")

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

Output:

tensor([[-0.6536, -0.4777, 1.6667, 0.0299, 0.1223],

[-1.8604, -0.3503, 0.7509, -0.2912, -1.5708],

[ 0.1468, 1.2626, 0.6741, 1.8651, 0.1632]])

Standard deviation across columns:

tensor([1.0104, 0.9701, 0.5523, 1.1633, 0.9895])

Standard deviation across rows:

tensor([0.9158, 1.0598, 0.7406])

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

Example 2:

Create a Tensor with 5 * 5 matrix with the cpu() function and return the standard deviation 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 Standard deviation along columns

print("Standard deviation across columns:")

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

 

print()

 

#get Standard deviation along rows

print("Standard deviation across rows:")

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

Output:

tensor([[-1.3900, 1.3594, -0.3603, 1.6448, -0.2708],

[-0.6731, 0.9022, 1.0914, -0.0416, -1.1494],

[ 0.1134, 1.0007, 0.5488, -1.6023, -1.2196],

[ 0.4858, 0.2534, -2.2222, -0.1260, -0.0746],

[-0.2175, -1.6167, -1.1183, 0.2427, -0.1219]])

Standard deviation across columns:

tensor([0.7273, 1.1853, 1.3192, 1.1561, 0.5686])

Standard deviation across rows:

tensor([1.2743, 0.9718, 1.1293, 1.0831, 0.7716])

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

Conclusion

In this PyTorch lesson, we learned about the std() function and how to apply it on a tensor to return the standard deviation across the columns and rows.

We also created a tensor with the cpu() function and returned the standard deviation. If the dim is not specified in two or multi-dimensional tensor, it returns the standard deviation 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