#### 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.
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

• numel
• from_numpy
• cat
• randn

Before we begin, let's install and import PyTorch

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

# Linux / Binder

# Windows

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

### Function 1 - torch.numel

returns the number of elements in the tensor

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

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

In :
``````t2 = torch.zeros(4,4)
torch.numel(t2)
``````
Out:
``16``

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

In :
``````
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],])
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

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 :
``````b = torch.randn(4,3)
b
``````
Out:
``````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 :
``````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

In :
``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 ```
Out:
``'https://jovian.ai/abhik375/01-tensor-operations'``

In :
``````x = torch.randn(2, requires_grad=True)
y = x*2 + 2

``````
Out:
``True``

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

In :
``````x = torch.randn(2, requires_grad=True)
y = x*2 + 2

``````
Out:
``True``

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

In :
``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 ```
Out:
``'https://jovian.ai/abhik375/01-tensor-operations'``

### Conclusion

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

``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 ```
``'https://jovian.ai/abhik375/01-tensor-operations'``
`` ``