Python

# PyTorch – Mean()

We will see how to return the average values from a tensor using mean() 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.

## Mean()

Mean() in PyTorch is used to return the average value of elements present in the input tensor object.

Syntax:

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

print("Mean across columns:")

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

print()

#get average along rows with mean()

print("Mean across rows:")

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

Output:

tensor([[ 1.5484, 1.4450, 0.5954, -0.1447, -1.3809],

[-0.9090, -0.6124, 0.4644, 0.3485, 0.6863],

[-1.7201, 0.4546, -0.3618, 0.4858, -1.0712]])

Mean across columns:

tensor([-0.3602, 0.4291, 0.2326, 0.2298, -0.5886])

Mean across rows:

tensor([ 0.4126, -0.0044, -0.4426])

We can see that the mean values are returned across the columns and rows.

## Example 2:

Create a Tensor with 5 * 5 matrix and return the average 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 average along columns with mean()

print("Mean across columns:")

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

print()

#get average along rows with mean()

print("Mean across rows:")

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

Output:

tensor([[-1.8994, 0.2208, -0.0023, 1.9119, 0.8428],

[-1.4042, -0.9700, 0.4683, 1.5860, -0.4229],

[-0.5011, 1.7210, -0.0949, -0.8114, -0.7528],

[ 0.1496, 0.4154, -0.5784, 0.2983, -0.2608],

[ 1.4232, 0.8856, -0.7154, -0.2667, 0.6884]])

Mean across columns:

tensor([-0.4464, 0.4546, -0.1845, 0.5436, 0.0189])

Mean across rows:

tensor([ 0.2148, -0.1486, -0.0878, 0.0048, 0.4030])

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

### Without the Dim Parameter

If we don’t specify the dim parameter, it returns the whole value’s average.

## Example 1:

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

#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 average with mean()

print("Mean :")

print(torch.mean(data))

Output:

tensor([[-1.3824, 0.5979, 0.0170, -0.1703, -0.9783],

[-0.5721, -1.0704, -0.7148, -1.4605, 0.1514],

[-1.5455, 1.5261, 1.3712, -1.3692, -1.0385],

[ 1.0159, 0.0484, -0.4317, -1.3518, 0.9220],

[-1.5225, 0.5126, -0.2473, 0.8433, 1.0807]])

Mean :

tensor(-0.2308)

## Example 2:

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

#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 average with mean()

print("Mean :")

print(torch.mean(data))

Output:

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

Mean :

tensor(30.4600)

### Work with CPU

If you want to run an argmax() 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 mean() 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 average along columns with mean()

print("Mean across columns:")

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

print()

#get average along rows with mean()

print("Mean across rows:")

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

Output:

tensor([[ 1.5484, 1.4450, 0.5954, -0.1447, -1.3809],

[-0.9090, -0.6124, 0.4644, 0.3485, 0.6863],

[-1.7201, 0.4546, -0.3618, 0.4858, -1.0712]])

Mean across columns:

tensor([-0.3602, 0.4291, 0.2326, 0.2298, -0.5886])

Mean across rows:

tensor([ 0.4126, -0.0044, -0.4426])

We can see that the mean values are returned across the columns and rows.

## Example 2:

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

print("Mean across columns:")

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

print()

#get average along rows with mean()

print("Mean across rows:")

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

Output:

tensor([[-1.8994, 0.2208, -0.0023, 1.9119, 0.8428],

[-1.4042, -0.9700, 0.4683, 1.5860, -0.4229],

[-0.5011, 1.7210, -0.0949, -0.8114, -0.7528],

[ 0.1496, 0.4154, -0.5784, 0.2983, -0.2608],

[ 1.4232, 0.8856, -0.7154, -0.2667, 0.6884]])

Mean across columns:

tensor([-0.4464, 0.4546, -0.1845, 0.5436, 0.0189])

Mean across rows:

tensor([ 0.2148, -0.1486, -0.0878, 0.0048, 0.4030])

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

## Conclusion

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

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