Calculate The Sum Of All Elements In A PyTorch Tensor

Calculate the Sum of all elements in a tensor by using the PyTorch sum operation

Type: FREE   By: Sebastian Gutierrez, AIWorkbox.com Instructor Sebastian Gutierrez   Duration: 2:00   Technologies: PyTorch, Python

Page Sections: Video  |  Code  |  Transcript


< > Code:

You must be a Member to view the code for this video.

  or   Log In

Subscribe today and get access to all lessons and their code and transcripts!


Transcript:

This video will show you how to calculate the sum of all elements in a tensor by using the PyTorch sum operation.


First, we import PyTorch.

import torch


Then we print the PyTorch version we are using.

print(torch.__version__)

We are using PyTorch 0.3.1.post2.


For this example, let’s manually create a PyTorch tensor using the PyTorch FloatTensor operation.

pt_tensor_ex = torch.FloatTensor(
[
  [
    [1, 2, 3],
    [1, 2, 3],
    [1, 2, 3]
  ]
  ,
  [
    [1, 2, 3],
    [1, 2, 3],
    [1, 2, 3]
  ]
])

So torch.FloatTensor.

We pass in the construction we want it to be, and we assign it to the pt_tensor_ex Python variable.

We construct this tensor in this way so that it’s easy to manually calculate the sum.


Let’s print the pt_tensor_ex Python variable to see what we have.

print(pt_tensor_ex)

We see that we have a Python tensor with two matrices inside of it where each matrix has three columns and three rows, it’s a PyTorch FloatTensor, as we just discussed, and the size is 2x3x3.


Now, let’s calculate the sum of all the elements in the tensor by using the PyTorch sum operation.

pt_tensor_sum_ex = torch.sum(pt_tensor_ex)

So torch.sum.

We pass in the pt_tensor_ex Python variable and we assign the result to pt_tensor_sum_ex.


Let’s now print the pt_tensor_sum_ex Python variable to see what we have.

print(pt_tensor_sum_ex)

We see that we have the number 36.0.


To check that this is the right result, let’s manually calculate the sum of all the elements in our example PyTorch tensor.


Scrolling up, we see that we had one, two, three, four, five, six rows, and each row was comprised of the numbers 1, 2, and 3.


So to calculate that sum, we can do 1+2+3 and we get 6.

1 + 2 + 3


And because there were six rows – one, two, three, four, five, six – we know that 6*6 will give us the total sum.

6 * 6

We see that the sum is 36, which is what we got when we printed the pt_tensor_sum_ex Python variable.


Perfect - We were able to calculate the sum of all the elements in the tensor by using the PyTorch sum operation.


Back