Jovian
⭐️
Sign In

Assignment Instructions (delete this cell before submission)

The objective of this assignment is to develop a solid understanding of PyTorch tensors. In this assignment you will:

  1. Pick 5 interesting functions related to PyTorch tensors by reading the documentation,
  2. Edit this starter template notebook to illustrate their usage and publish your notebook to Jovian using jovian.commit. Make sure to add proper explanations too, not just code.
  3. Submit the link to your published notebook on Jovian here: https://jovian.ai/learn/deep-learning-with-pytorch-zero-to-gans/assignment/assignment-1-all-about-torch-tensor .
  4. (Optional) Write a blog post on Medium to accompany and showcase your Jupyter notebook. Embed cells from your notebook wherever necessary.
  5. (Optional) Share your work with the community and exchange feedback with other participants

The recommended way to run this notebook is to click the "Run" button at the top of this page, and select "Run on Colab". Run jovian.commit regularly to save your progress.

Try to give your notebook an interesting title e.g. "All about PyTorch tensor operations", "5 PyTorch functions you didn't know you needed", "A beginner's guide to Autograd in PyToch", "Interesting ways to create PyTorch tensors", "Trigonometic functions in PyTorch", "How to use PyTorch tensors for Linear Algebra" etc.

IMPORTANT NOTE: Make sure to submit a Jovian notebook link e.g. https://jovian.ai/aakashns/01-tensor-operations . Colab links will not be accepted.

Remove this cell containing instructions before making a submission or sharing your notebook, to make it more presentable.

All about PyTorch tensor operations

An short introduction about PyTorch and about the chosen functions.

  • numel
  • from_numpy
  • cat
  • randn
  • torch.enable_grad

Before we begin, let's install and import PyTorch

In [45]:
# Uncomment and run the appropriate command for your operating system, if required

# Linux / Binder
# !pip install numpy torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html

# Windows
# !pip install numpy torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html

# MacOS
# !pip install numpy torch torchvision torchaudio
In [46]:
# Import torch and other required modules
import torch

Function 1 - torch.numel

returns the number of elements in the tensor

In [47]:
t1 = torch.randn(1,2,3,4,5)
torch.numel(t1)
Out[47]:
120

its a 5 dimensional tensor with 1x2x3x4x5 dimensions having random numbers from 0 to 1 having 120 elements

In [48]:
t2 = torch.zeros(4,4)
torch.numel(t2)
Out[48]:
16

a tensor 4x4 dimension having 16 elements all of which are zero

In [49]:

t3 = torch.tensor([[1, 2], [3, 4]])
torch.numel(t3, t2)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-49-417ae61bc81f> in <module>() 1 2 t3 = torch.tensor([[1, 2], [3, 4]]) ----> 3 torch.numel(t3, t2) TypeError: numel() takes 1 positional argument but 2 were given

numel takes 1 argument but in this instance 2 arguments were given

can be used when we need to find the number of elements in a variable instead of explicitly calculating each time

Let's save our work using Jovian before continuing.

In [ ]:
!pip install jovian --upgrade --quiet
In [ ]:
import jovian
In [ ]:
jovian.commit(project='01-tensor-operations')

from_numpy

used to take in numpy arrays and convert them into pytorch tensors

In [ ]:
import numpy as np
a1 = np.array([1,2])
t1 = torch.from_numpy(a1)
t1

a numpy array created can be converted into tensor

In [ ]:
a2 = np.array([[1,2],[2,4]])
t2 = torch.from_numpy(a2)
t2

a 2d array gets converted into a 2 dimensional tensor

In [ ]:
a3 = np.array([[1,2],[3]])
t3 = torch.from_numpy(a3)
t3

a list can be converted in numpy but the tensor should have valid dimensions and the reason for which it not being able to be converted

this is used as most of the input datasets are taken through numpy which needs to be converted to pytorch tensors

In [ ]:
jovian.commit(project='01-tensor-operations')

Function 3 - cat

used to concatenate two tensors

In [ ]:
x = torch.randn(2,3)
t1 = torch.cat((x,x),0)
t1

concatenated over 0 dimensions

In [ ]:
x = torch.randn(2,2)
t1 = torch.cat((x,x), 1)
t1

concatenated over 1 dimension

In [ ]:
x = torch.randn(1,2)
y = torch.randn(2,2)
t1 = torch.cat((x,y), 1)
t1

both the tensors to be concatenated should be of the same dimension

Closing comments about when to use this function

In [ ]:
jovian.commit(project='01-tensor-operations')

Function 4 - randn

used to generate random numbers with mean 0 and variance 1 from a normal distribution

In [ ]:
a = torch.randn(5)
a

created a 1 dimensional tensor with 5 random numbers

In [50]:
b = torch.randn(4,3)
b
Out[50]:
tensor([[-0.9889,  1.3823,  1.0111],
        [-0.3987,  0.0031, -0.3509],
        [-2.4181,  0.6716,  1.7029],
        [ 0.2510,  0.6552,  0.6063]])

created a 2 dimensional tensor with random numbers

In [52]:
c = torch.randn(-1)
c
--------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) <ipython-input-52-179a099c7af9> in <module>() ----> 1 c = torch.randn(-1) 2 c RuntimeError: Trying to create tensor with negative dimension -1: [-1]

not possible to create a tensor with negative dimension

Closing comments about when to use this function

In [53]:
jovian.commit(project='01-tensor-operations')
[jovian] Detected Colab notebook... [jovian] Uploading colab notebook to Jovian... [jovian] Capturing environment.. [jovian] Committed successfully! https://jovian.ai/abhik375/01-tensor-operations

Function 5 - enable_grad

enables gradient calculation if it has been disabled using no_grad

In [61]:
x = torch.randn(2, requires_grad=True)
with torch.no_grad():
  with torch.enable_grad():
    y = x*2 + 2
y.requires_grad

Out[61]:
True

we can enable the gradient cancellation if it has been disabled on some outer loop

In [63]:
x = torch.randn(2, requires_grad=True)
with torch.enable_grad():
  y = x*2 + 2
y.requires_grad

Out[63]:
True

gradient is enabled

In [67]:
x = torch.randn(2, requires_grad=True)
with torch.enable_grad():
  with torch.no_grad():
  y = x*2 + 2
y.requires_grad
File "<ipython-input-67-31c83daae528>", line 4 y = x*2 + 2 ^ IndentationError: expected an indented block

Explanation about example

Closing comments about when to use this function

In [65]:
jovian.commit(project='01-tensor-operations')
[jovian] Detected Colab notebook... [jovian] Uploading colab notebook to Jovian... [jovian] Capturing environment.. [jovian] Committed successfully! https://jovian.ai/abhik375/01-tensor-operations

Conclusion

Summarize what was covered in this notebook, and where to go next

Reference Links

Provide links to your references and other interesting articles about tensors

In [66]:
jovian.commit(project='01-tensor-operations')
[jovian] Detected Colab notebook... [jovian] Uploading colab notebook to Jovian... [jovian] Capturing environment.. [jovian] Committed successfully! https://jovian.ai/abhik375/01-tensor-operations
In [66]: