Learn data science and machine learning by building real-world projects on Jovian

# AOabdouldjalilo/01-tensor-operations

a year ago

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

## Title Here

• function 1
• function 2
• function 3
• function 4
• function 5

Before we begin, let's install and import PyTorch

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

# Linux / Binder

# Windows

# MacOS
# !pip install numpy torch torchvision torchaudio``````
```Looking in links: https://download.pytorch.org/whl/torch_stable.html Requirement already satisfied: numpy in /opt/conda/lib/python3.8/site-packages (1.19.2) Requirement already satisfied: torch==1.7.0+cpu in /opt/conda/lib/python3.8/site-packages (1.7.0+cpu) Requirement already satisfied: torchvision==0.8.1+cpu in /opt/conda/lib/python3.8/site-packages (0.8.1+cpu) Requirement already satisfied: torchaudio==0.7.0 in /opt/conda/lib/python3.8/site-packages (0.7.0) Requirement already satisfied: dataclasses in /opt/conda/lib/python3.8/site-packages (from torch==1.7.0+cpu) (0.6) Requirement already satisfied: typing-extensions in /opt/conda/lib/python3.8/site-packages (from torch==1.7.0+cpu) (3.7.4.3) Requirement already satisfied: future in /opt/conda/lib/python3.8/site-packages (from torch==1.7.0+cpu) (0.18.2) Requirement already satisfied: pillow>=4.1.1 in /opt/conda/lib/python3.8/site-packages (from torchvision==0.8.1+cpu) (8.0.0) ```
In [2]:
``````# Import torch and other required modules
import torch``````

### Function 1 - torch.ones()

Returns a tensor filled with the scalar value 1, with the shape defined by the variable argument size.

In [3]:
``````# torch.ones() - filled tensor with the scalar value 1
a = torch.ones(1, 2, 3)
a
``````
Out[3]:
``````tensor([[[1., 1., 1.],
[1., 1., 1.]]])``````
In [4]:
``````# torch.ones_like() - filled a tensor with the scalar value 1 with the same size as input
x = torch.empty(2, 3, 3)
b = torch.ones_like(x)
b
``````
Out[4]:
``````tensor([[[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]],

[[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]]])``````

In [17]:
``````# torch.ones_like(torch.ones) - taking torch.ones as input of torch.ones_like
b = torch.ones_like(a)
b
``````
Out[17]:
``````tensor([[[1., 1., 1.],
[1., 1., 1.]]])``````

Let's save our work using Jovian before continuing.

In [5]:
``!pip install jovian --upgrade --quiet``
In [6]:
``import jovian``
In [7]:
``jovian.commit(project='01-tensor-operations')``
```[jovian] Attempting to save notebook.. [jovian] Updating notebook "abdouldjalilo/01-tensor-operations" on https://jovian.ai [jovian] Uploading notebook.. [jovian] Uploading additional files... [jovian] Committed successfully! https://jovian.ai/abdouldjalilo/01-tensor-operations ```
Out[7]:
``'https://jovian.ai/abdouldjalilo/01-tensor-operations'``

### Function 2 - torch.range()

Returns a 1-D tensor of siz ((end - start)/step)with values from the interval (start, end) taken with common difference step beginning from start.

In [21]:
``````# torch.range() - taking a values and return a float tensor
torch.range(3, 7)``````
```<ipython-input-21-3b3474b81c4a>:2: UserWarning: torch.range is deprecated and will be removed in a future release because its behavior is inconsistent with Python's range builtin. Instead, use torch.arange, which produces values in [start, end). torch.range(3, 7) ```
Out[21]:
``tensor([3., 4., 5., 6., 7.])``

Returns a 1-D tensor of value size with values from start to end with step step. Step is the gap between two values in the tensor

In [26]:
``````# torch.arange() - taking one value and returns tensor from 1-7 with step 2
torch.arange(1,9,2)``````
Out[26]:
``tensor([1, 3, 5, 7])``

In [8]:
``# Example 3 - breaking (to illustrate when it breaks)``

In [ ]:
``jovian.commit(project='01-tensor-operations')``
```[jovian] Attempting to save notebook.. ```

### Function 3 - ???

In [9]:
``# Example 1 - working``

In [10]:
``# Example 2 - working``

In [11]:
``# Example 3 - breaking (to illustrate when it breaks)``

In [17]:
``jovian.commit(project='01-tensor-operations')``
```[jovian] Attempting to save notebook.. [jovian] Updating notebook "aakashns/01-tensor-operations" on https://jovian.ai/ [jovian] Uploading notebook.. [jovian] Capturing environment.. [jovian] Committed successfully! https://jovian.ai/aakashns/01-tensor-operations ```
Out[17]:
``'https://jovian.ai/aakashns/01-tensor-operations'``

### Function 4 - ???

In [12]:
``# Example 1 - working``

In [13]:
``# Example 2 - working``

In [14]:
``# Example 3 - breaking (to illustrate when it breaks)``

In [18]:
``jovian.commit(project='01-tensor-operations')``
```[jovian] Attempting to save notebook.. [jovian] Updating notebook "aakashns/01-tensor-operations" on https://jovian.ai/ [jovian] Uploading notebook.. [jovian] Capturing environment.. [jovian] Committed successfully! https://jovian.ai/aakashns/01-tensor-operations ```
Out[18]:
``'https://jovian.ai/aakashns/01-tensor-operations'``

### Function 5 - ???

In [14]:
``# Example 1 - working``

In [15]:
``# Example 2 - working``

In [16]:
``# Example 3 - breaking (to illustrate when it breaks)``

In [19]:
``jovian.commit(project='01-tensor-operations')``
```[jovian] Attempting to save notebook.. [jovian] Updating notebook "aakashns/01-tensor-operations" on https://jovian.ai/ [jovian] Uploading notebook.. [jovian] Capturing environment.. [jovian] Committed successfully! https://jovian.ai/aakashns/01-tensor-operations ```
Out[19]:
``'https://jovian.ai/aakashns/01-tensor-operations'``

### Conclusion

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

``jovian.commit(project='01-tensor-operations')``
```[jovian] Attempting to save notebook.. ```
`` ``