Learn data science and machine learning by building real-world projects on Jovian
                                  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

In [1]:
# Import torch and other required modules
import torch

-------> Function 1.) torch.mean

The torch.mean function returns the mean or average of your tensor.

In [2]:
# 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]])
Out[2]:
tensor(0.2773)
In [ ]:
-> 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.
In [3]:
# Example 2 - working
import torch
temp = torch.randn((2,4))
print(temp)
torch.mean(temp, dim=0)
tensor([[-1.3686, -0.5146, 1.1132, -0.3097], [-1.5216, 0.3915, 1.4754, -0.7941]])
Out[3]:
tensor([-1.4451, -0.0615,  1.2943, -0.5519])
In [ ]:
->Example 2.(explanation)

As we can see in this example torch.randn has generated the tensor of 2 rows and 4 columns and the torch.mean function 
is applied which is just adding all the numbers of our tensor and dividing it by the count.
Here comes yet another thing to see,we have set dim parameter to 0. This is specifying that we want the average
over all rows (for each column)
In [4]:
# Example 3 -
import torch
temp = torch.randn((2,4))
print(temp)
torch.mean(temp, dim=1)
tensor([[ 0.1764, -0.0763, -1.7934, -0.7023], [-0.3834, 0.2341, -0.7218, -0.6899]])
Out[4]:
tensor([-0.5989, -0.3902])
In [ ]:
->Example 3.(explanation)

As we can see in this example torch.randn has generated the tensor of 2 rows and 4 columns and the torch.mean function 
is applied which is just adding all the numbers of our tensor and dividing it by the count.
Here comes yet another thing to see,we have set dim parameter to 1. This is specifying that we want the average
over all columns (for each row)

In [5]:
# Example 4 - breaking (to illustrate when it breaks)
x = torch.tensor([1,1,1,1,0],dtype=torch.bool)
torch.mean(x)
--------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) <ipython-input-5-19bb4e3cc91b> in <module> 1 # Example 4 - breaking (to illustrate when it breaks) 2 x = torch.tensor([1,1,1,1,0],dtype=torch.bool) ----> 3 torch.mean(x) RuntimeError: Can only calculate the mean of floating types. Got Bool instead.
In [ ]:
->Example 4.(explanation)

Applying the mean function to this boolean tensor results in a RuntimeError because you cannot get 
the mean of boolean data types. That is why the RunetimeError says it can only calculate the mean 
of floating types.

This function should be used when you need to obtain the mean of your tensor, you can also specify which dimension to calculate the mean.Also you can set dim to 0 or 1 according to your need.

-------> Function 2.) torch.eye

The torch.eye function generates a tensor that is an identity matrix,it takes the size of the matrix as a parameter

In [6]:
# Example 1 - working
import torch
temp=torch.eye(4)
print(temp)
tensor([[1., 0., 0., 0.], [0., 1., 0., 0.], [0., 0., 1., 0.], [0., 0., 0., 1.]])
In [ ]:
->Example 1.(explanation)

So,we have passed parameter 4 ,so a tensor of 4 rows and 4 columns wiil be generated,torch.eye function generates 
a tensor that is basically an identity matrix.
In [7]:
# Example 2 - working
import torch
temp=7*torch.eye(10)
print(temp)

tensor([[7., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 7., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 7., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 7., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 7., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 7., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 7., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 7., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 7., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 7.]])
In [ ]:
->Example 2.(explanation)

So,we have passed parameter 10 ,so a tensor of 10 rows and 10 columns wiil be generated,torch.eye function generates 
a tensor that is basically an identity matrix.And then we have multiplied it by 7 to get the desired tensor.
In [7]:
This function should be used when you need to obtain the identity matrix of desired size.

-------> Function 3.) torch.reshape

The torch.reshape function reshapes your tensor into another shape, if applicable.

In [8]:
# Example 1 - working
import torch
temp = torch.randn((2,10))
print(temp)
torch.reshape(temp,(4,5))
tensor([[-0.4099, -0.0077, -0.2653, -0.3445, -0.3735, -0.3870, 0.1567, -0.0146, 1.9152, 1.4409], [-0.0203, 0.3842, 1.4672, -1.0073, -0.9285, 1.5868, -1.7643, 1.0637, 0.2789, -0.3166]])
Out[8]:
tensor([[-0.4099, -0.0077, -0.2653, -0.3445, -0.3735],
        [-0.3870,  0.1567, -0.0146,  1.9152,  1.4409],
        [-0.0203,  0.3842,  1.4672, -1.0073, -0.9285],
        [ 1.5868, -1.7643,  1.0637,  0.2789, -0.3166]])
In [ ]:
->Example 1.(explanation)

As we can see in this example torch.randn has generated the tensor of 2 rows and 10 columns,now we
have reshape our tensor temp  into a (4, 5) matrix.
In [9]:
# Example 2 - working
import torch
temp = torch.randn((16,2))
print(temp)
torch.reshape(temp, (8,4))
tensor([[-0.2683, -0.3644], [ 0.0657, 0.6857], [-1.7637, -4.0172], [-1.3015, 1.7100], [ 0.7334, -0.3077], [-0.0829, -0.3588], [ 0.1995, -1.6592], [-0.5323, -1.3231], [ 0.8891, -0.3033], [-0.5240, -0.0991], [-0.6235, -0.9640], [-0.4675, 1.7694], [ 1.3199, -1.1728], [ 0.1679, 0.6113], [ 2.0312, -0.5940], [ 0.3212, -1.1665]])
Out[9]:
tensor([[-0.2683, -0.3644,  0.0657,  0.6857],
        [-1.7637, -4.0172, -1.3015,  1.7100],
        [ 0.7334, -0.3077, -0.0829, -0.3588],
        [ 0.1995, -1.6592, -0.5323, -1.3231],
        [ 0.8891, -0.3033, -0.5240, -0.0991],
        [-0.6235, -0.9640, -0.4675,  1.7694],
        [ 1.3199, -1.1728,  0.1679,  0.6113],
        [ 2.0312, -0.5940,  0.3212, -1.1665]])
In [ ]:
->Example 2.(explanation)

As we can see in this example torch.randn has generated the tensor of 16 rows and 2 columns,now we
have reshape our tensor temp  into a (8, 4) matrix.
In [10]:
# Example 3 - breaking (to illustrate when it breaks)

import torch
temp = torch.randn((7,2))
print(temp)
torch.reshape(temp,(4,4))
tensor([[-0.2375, -1.0441], [ 1.4008, 2.1166], [ 0.0064, 0.1796], [ 0.4720, -0.9311], [ 1.1182, 0.9049], [-1.1326, 0.3653], [ 0.3877, -1.0859]])
--------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) <ipython-input-10-1c34d8219c2d> in <module> 4 temp = torch.randn((7,2)) 5 print(temp) ----> 6 torch.reshape(temp,(4,4)) RuntimeError: shape '[4, 4]' is invalid for input of size 14
In [ ]:
->Example 3.(explanation)

When specifying the reshape size, we need to make the specified size taht will fit the number of elements 
in our input tensor. Otherwise you will end up with a RuntimeError of invalid size.

This function should be used if you need to reshape your data.

-------> Function 4.) torch.full

The toch.full function generates a tensor of a specified size with all elements being equal to the number specified.

In [11]:
# Example 1 - working
import torch
temp = torch.full((4,5),7.)
temp
Out[11]:
tensor([[7., 7., 7., 7., 7.],
        [7., 7., 7., 7., 7.],
        [7., 7., 7., 7., 7.],
        [7., 7., 7., 7., 7.]])
In [ ]:
->Example 1.(explanation)

It has generated the tensor of 4 rows and 5 columns with all elements of tensor being equal to 7.
In [12]:

# Example 2 - working
import torch
temp = torch.full((2,5),4.)
temp

Out[12]:
tensor([[4., 4., 4., 4., 4.],
        [4., 4., 4., 4., 4.]])
In [ ]:
->Example 2.(explanation)

It has generated the tensor of 2 rows and 5 columns with all elements of tensor being equal to 4.

This function should be used when you want your tensor to contain all the elemnets equal to the number specified.

-------> Function 5.) torch.log

The torch.log function simply returns the natural log.

In [13]:
# Example 1 - working
import torch
temp = torch.randn((4,6))
print(temp)
torch.log(temp)

tensor([[-1.1886, 2.5196, 0.4954, -2.2004, -1.2510, 0.3114], [ 0.3581, 0.3022, -1.3220, -0.8061, -2.7201, -1.3340], [ 2.3616, -0.2577, -0.2258, -1.6656, -0.5955, -0.7136], [ 1.7774, -3.0590, -2.2129, -1.0461, -0.3430, 1.2260]])
Out[13]:
tensor([[    nan,  0.9241, -0.7023,     nan,     nan, -1.1668],
        [-1.0269, -1.1967,     nan,     nan,     nan,     nan],
        [ 0.8593,     nan,     nan,     nan,     nan,     nan],
        [ 0.5752,     nan,     nan,     nan,     nan,  0.2038]])
In [ ]:
->Example 1.(explanation)

As we can see in this example torch.randn has generated the tensor of 4 rows and 6 columns ,now
the torch.log function is applied to a (4, 6) matrix.
As We can see some resulting values labeled as 'nan' since the natural log of a negative number is undefined.
In [14]:
# Example 2 - working
import torch
temp = torch.randn((2,2))
print(temp)
torch.log(temp)

tensor([[-1.6386, -1.0503], [-0.1514, 1.1968]])
Out[14]:
tensor([[   nan,    nan],
        [   nan, 0.1797]])
In [ ]:
->Example 2.(explanation)

As we can see in this example torch.randn has generated the tensor of 4 rows and 6 columns ,now
the torch.log function is applied to a (4, 6) matrix.
As We can see some resulting values labeled as 'nan' since the natural log of a negative number is undefined.
In [15]:
# Example 3 - breaking (to illustrate when it breaks)
import torch
torch.log([.2,.5])
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-15-450dfddc0d20> in <module> 1 # Example 3 - breaking (to illustrate when it breaks) 2 import torch ----> 3 torch.log([.2,.5]) TypeError: log(): argument 'input' (position 1) must be Tensor, not list
In [ ]:
->Example 3.(explanation)

We can see in this example that the torch.log function requires the input to be a tensor object type.
We cannot simply pass in an array object or else we get a TypeError.

This function should be used when you need to take the natural log of all values in your tensor.

-------> Conclusion

In this notebook, we have only covered 5 of many different tensor functions that are in PyTorch. Those were:

1.)torch.mean 2.)torch.eye 3.)torch.reshape 4.)torch.full 5.)torch.log

For more functions:

https://pytorch.org/docs/stable/tensors.html.

Reference Links

Provide links to your references and other interesting articles about tensors

In [17]:
!pip install jovian --upgrade --quiet
In [18]:
import jovian
In [ ]:
import jovian
jovian.commit()
[jovian] Attempting to save notebook..
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]: