# How to compute the mean and standard deviation of a tensor in PyTorch?

A PyTorch tensor is like a numpy array. The only difference is that a tensor utilizes the GPUs to accelerate numeric computations. The mean of a tensor is computed using the torch.mean() method. It returns the mean value of all the elements in the input tensor. We can also compute the mean row-wise and column-wise, providing suitable axis or dim.

The standard deviation of a tensor is computed using torch.std(). It returns the standard deviation of all the elements in the tensor. Like mean, we can also compute the standard deviation, row or column-wise.

## Steps

• Import the required library. In all the following Python examples, the required Python library is torch. Make sure you have already installed it.

• Define a PyTorch tensor and print it.

• Compute the mean using torch.mean(input, axis). Here, the input is the tensor for which the mean should be computed and axis (or dim) is the list of dimensions. Assign the computed mean to a new variable.

• Compute the standard deviation using torch.std(input, axis). Here, input is the tensor and axis (or dim) is the list of dimensions. Assign the computed standard deviation to a new variable.

• Print the above-computed mean and standard deviation .

## Example 1

The following Python program shows how to compute the mean and standard deviation of a 1D tensor.

# Python program to compute mean and standard
# deviation of a 1D tensor
# import the library
import torch

# Create a tensor
T = torch.Tensor([2.453, 4.432, 0.754, -6.554])
print("T:", T)

# Compute the mean and standard deviation
mean = torch.mean(T)
std = torch.std(T)

# Print the computed mean and standard deviation
print("Mean:", mean)
print("Standard deviation:", std)

## Output

T: tensor([ 2.4530, 4.4320, 0.7540, -6.5540])
Mean: tensor(0.2713)
Standard deviation: tensor(4.7920)

## Example 2

The following Python program shows how to compute the mean and standard deviation of a 2D tensor in both dimensions, i.e., row-wise as well as column-wise

# import necessary library
import torch

# create a 3x4 2D tensor
T = torch.Tensor([[2,4,7,-6],
[7,33,-62,23],
[2,-6,-77,54]])
print("T:\n", T)

# compute the mean and standard deviation
mean = torch.mean(T)
std = torch.std(T)
print("Mean:", mean)
print("Standard deviation:", std)

# Compute column-wise mean and std
mean = torch.mean(T, axis = 0)
std = torch.std(T, axis = 0)
print("Column-wise Mean:\n", mean)
print("Column-wise Standard deviation:\n", std)

# Compute row-wise mean and std
mean = torch.mean(T, axis = 1)
std = torch.std(T, axis = 1)
print("Row-wise Mean:\n", mean)
print("Row-wise Standard deviation:\n", std)

## Output

T:
tensor([[ 2., 4., 7., -6.],
[ 7., 33., -62., 23.],
[ 2., -6., -77., 54.]])
Mean: tensor(-1.5833)
Standard deviation: tensor(36.2703)
Column-wise Mean:
tensor([ 3.6667, 10.3333, -44.0000, 23.6667])
Column-wise Standard deviation:
tensor([ 2.8868, 20.2567, 44.7996, 30.0056])
Row-wise Mean:
tensor([ 1.7500, 0.2500, -6.7500])
Row-wise Standard deviation:
tensor([ 5.5603, 42.8593, 53.8602])