Learn practical skills, build real-world projects, and advance your career
Updated 4 years ago
PyTorch Tensor Functions
This is a short exploration on some of the functions used in a PyTorch tensor
-> A short introduction about PyTorch and about the chosen functions:
1.) torch.mean
2.) torch.eye
3.) torch.reshape
4.) torch.full
5.) torch.log
# Import torch and other required modules
import torch
-------> Function 1.) torch.mean
The torch.mean function returns the mean or average of your tensor.
# Example 1 - working
import torch
temp = torch.randn((4,5))
print(temp)
torch.mean(temp)
tensor([[ 1.5388, 0.0300, -1.2343, 1.1053, 0.5563],
[ 0.8353, 0.4908, 0.3069, 1.3646, -0.1550],
[ 0.7855, 1.6646, -0.0974, 0.3994, -0.8695],
[-0.9574, -1.2597, 0.0554, 1.2864, -0.2992]])
tensor(0.2773)
-> Example 1.(explanation)
As we can see in this example torch.randn has generated the tensor of 4 rows and 5 columns and the torch.mean function
is applied which is just adding all the numbers of our tensor and dividing it by the count.