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:
Where the data is a multi-dimensional array.
Sum()
Sum() in PyTorch is used to return the total sum of the elements present in the input tensor object.
Syntax:
Where:
1. The tensor is the input tensor.
2. Dim is to reduce the dimension. Dim=0 specifies the column comparison which gets the total sum of values along a column and dim=1 specifies the row comparison which gets the total sum of values along the row.
Example 1:
In this example, we will create a tensor with 3 dimensions that has 3 rows and 5 columns and apply the sum() function on rows and columns.
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 Sum values along columns
print("Sum values across columns:")
print(torch.sum(data, dim=0))
print()
#get Sum values along rows
print("Sum values across rows:")
print(torch.sum(data, dim=1))
Output:
[-0.6305, 0.2493, -1.8812, 1.3837, 0.7238],
[ 1.7078, -0.8948, -1.2484, -0.2079, -0.9078]])
Sum values across columns:
tensor([ 1.0217, -0.2247, -1.1220, 1.4399, -1.4447])
Sum values across rows:
tensor([ 1.3762, -0.1548, -1.5512])
We can see that the sum of values are returned across the columns and rows.
Example 2:
Create a Tensor with 5 * 5 matrix and return the total sum of values across the rows and columns.
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 Sum of values along columns
print("Sum of values across columns:")
print(torch.sum(data, dim=0))
print()
#get Sum of values along rows
print("Sum of values across rows:")
print(torch.sum(data, dim=1))
Output:
[-0.2578, 0.8914, 1.1879, -1.4176, -1.6000],
[ 0.2300, -0.8414, 0.7395, 0.2362, 0.9471],
[-0.1933, -0.3221, 1.6938, 1.0898, -1.1636],
[ 1.4314, -1.3938, 0.6046, 0.7937, 1.9621]])
Sum of values across columns:
tensor([ 0.1631, -1.1084, 3.8663, 1.9308, -0.1275])
Sum of values across rows:
tensor([ 0.1063, -1.1960, 1.3114, 1.1046, 3.3980])
We can see the sum of values across the rows and columns.
Without the Dim Parameter
If we don’t specify the dim parameter, it returns the total sum from the entire tensor.
Example 1:
Create a 2D tensor with 5*5 matrix and return the total sum.
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 total sum
print("Total sum :")
print(torch.sum(data))
Output:
[-0.6120, 0.1565, -0.3482, -0.9082, -1.2066],
[ 0.5195, 0.3678, 1.1712, -0.3106, -0.1575],
[ 1.7759, -0.1936, 1.7604, -0.5895, 1.9677],
[ 1.5080, -0.1691, 0.2007, -0.7224, 0.0071]])
Total sum :
tensor(2.6937)
Example 2:
Create a 1D tensor with 5 values and return the total sum.
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 total sum
print("MTotal sum :")
print(torch.sum(data))
Output:
MTotal sum :
tensor(152.3000)
Work with CPU
If you want to run a sum() 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:
Example 1:
In this example, we will create a tensor with 3 dimensions that has 3 rows and 5 columns with a cpu() function and apply the sum() on rows and columns.
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 Sum of values along columns
print("Sum of values across columns:")
print(torch.sum(data, dim=0))
print()
#get Sum of values along rows
print("Sum of values across rows:")
print(torch.sum(data, dim=1))
Output:
[ 0.4140, 0.6337, 0.8007, -0.8377, -0.7650],
[ 0.8471, 0.6988, 0.2508, 0.2901, -0.4939]])
Sum of values across columns:
tensor([ 1.0483, 2.0339, 2.6334, -1.3006, -2.1824])
Sum of values across rows:
tensor([0.3939, 0.2457, 1.5930])
We can see that the sum of values are returned across the columns and rows.
Example 2:
Create a Tensor with 5 * 5 matrix with the cpu() function and return the sum of values across the rows and columns.
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 sum of values along columns
print("sum of values across columns:")
print(torch.sum(data, dim=0))
print()
#get sum of values along rows
print("sum of values across rows:")
print(torch.sum(data, dim=1))
Output:
[-1.4990, -0.8262, -1.2368, 0.0618, 1.0522],
[-0.7371, 0.6237, -0.8857, -0.4474, -1.7985],
[ 0.0569, 1.4520, -1.6996, 1.2843, 0.6789],
[-1.8241, 0.4399, 0.1749, -2.5850, 1.3348]])
sum of values across columns:
tensor([-3.7080, 1.5432, -2.2781, -1.5064, 1.5163])
sum of values across rows:
tensor([ 1.9471, -2.4480, -3.2450, 1.7725, -2.4595])
We can see that the sum of values across the rows and columns were returned.
Conclusion
In this PyTorch lesson, we learned about the sum() function and how to apply it on a tensor to return the total sum of values across the columns and rows. We also created a tensor with the cpu() function and returned the sum of all values. If the dim is not specified in two or multi-dimensional tensor, it returns the total sum from the entire tensor.