Jovian
⭐️
Sign In

PyTorch function

PyTorch is a library for developing deep learning models. These functions are the basic function to create tensor object.

  • function 1 - torch.as_tensor
  • function 2 - torch.from_numpy
  • function 3 - torch.zeros
  • function 4 - torch.ones
  • function 5 - torch.empty

Before we begin, let's install and import PyTorch

In [2]:
# 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
Looking in links: https://download.pytorch.org/whl/torch_stable.html Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (1.18.5) Collecting torch==1.7.0+cpu Downloading https://download.pytorch.org/whl/cpu/torch-1.7.0%2Bcpu-cp36-cp36m-linux_x86_64.whl (159.3MB) |████████████████████████████████| 159.3MB 89kB/s Collecting torchvision==0.8.1+cpu Downloading https://download.pytorch.org/whl/cpu/torchvision-0.8.1%2Bcpu-cp36-cp36m-linux_x86_64.whl (11.8MB) |████████████████████████████████| 11.8MB 28.3MB/s Collecting torchaudio==0.7.0 Downloading https://files.pythonhosted.org/packages/3f/23/6b54106b3de029d3f10cf8debc302491c17630357449c900d6209665b302/torchaudio-0.7.0-cp36-cp36m-manylinux1_x86_64.whl (7.6MB) |████████████████████████████████| 7.6MB 3.8MB/s Requirement already satisfied: dataclasses in /usr/local/lib/python3.6/dist-packages (from torch==1.7.0+cpu) (0.8) Requirement already satisfied: typing-extensions in /usr/local/lib/python3.6/dist-packages (from torch==1.7.0+cpu) (3.7.4.3) Requirement already satisfied: future in /usr/local/lib/python3.6/dist-packages (from torch==1.7.0+cpu) (0.16.0) Requirement already satisfied: pillow>=4.1.1 in /usr/local/lib/python3.6/dist-packages (from torchvision==0.8.1+cpu) (7.0.0) Installing collected packages: torch, torchvision, torchaudio Found existing installation: torch 1.7.0+cu101 Uninstalling torch-1.7.0+cu101: Successfully uninstalled torch-1.7.0+cu101 Found existing installation: torchvision 0.8.1+cu101 Uninstalling torchvision-0.8.1+cu101: Successfully uninstalled torchvision-0.8.1+cu101 Successfully installed torch-1.7.0+cpu torchaudio-0.7.0 torchvision-0.8.1+cpu
In [3]:
# Import torch and other required modules
import torch

Function 1 - torch.tensor (change this)

Add some explanations

In [4]:
# Example 1 - working
a_data = [[1,2],[3,4]]
torch.as_tensor(a_data)
Out[4]:
tensor([[1, 2],
        [3, 4]])

The function takes the data, which is the list of list, with the same dimension, as input for the function and change it to as torch.Tensor object.

In [5]:
# Example 2 - working
b_data = [[1,2],[2,3],[3,4]]
torch.as_tensor(b_data)
Out[5]:
tensor([[1, 2],
        [2, 3],
        [3, 4]])

The data can be a very long list, as long as the dimension of each list is consistent.

In [6]:
# Example 3 - breaking (to illustrate when it breaks)
torch.as_tensor([[1, 2], [3, 4, 5]])
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-6-aac81cce02a3> in <module>() 1 # Example 3 - breaking (to illustrate when it breaks) ----> 2 torch.as_tensor([[1, 2], [3, 4, 5]]) ValueError: expected sequence of length 2 at dim 1 (got 3)

If the dimension of the lists are not the same, then the error will pop out as above.

Closing comments about when to use this function

Let's save our work using Jovian before continuing.

In [7]:
!pip install jovian --upgrade --quiet
In [8]:
import jovian
In [9]:
jovian.commit(project='01-tensor-operations')
[jovian] Detected Colab notebook... [jovian] Please enter your API key ( from https://jovian.ai/ ): API KEY: ··········
[jovian] Error: The current API key is invalid or expired.
[jovian] Please enter your API key ( from https://jovian.ai/ ): API KEY: ·········· [jovian] Uploading colab notebook to Jovian... [jovian] Capturing environment.. [jovian] Committed successfully! https://jovian.ai/amirulwasim96/01-tensor-operations

Function 2 - from_numpy

Create a tensor from numpy array

In [10]:
# Example 1 - working
import numpy as np
a= np.array([[1,2,3],[3,4,5]])
torch.from_numpy(a)
Out[10]:
tensor([[1, 2, 3],
        [3, 4, 5]])

The function takes numpy array as input to create a tensor object

In [11]:
# Example 2 - working
a= np.array([[1,2,3,4],[3,4,5,6]])
torch.from_numpy(a)
Out[11]:
tensor([[1, 2, 3, 4],
        [3, 4, 5, 6]])

As long as the data has consistent dimension, the code will run smoothly.

In [13]:
# Example 3 - breaking (to illustrate when it breaks)
a = np.array([[1,2,3], [1,2]])
print(a)
torch.from_numpy(a)
[list([1, 2, 3]) list([1, 2])]
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-13-3cb6311ba9d0> in <module>() 2 a = np.array([[1,2,3], [1,2]]) 3 print(a) ----> 4 torch.from_numpy(a) TypeError: can't convert np.ndarray of type numpy.object_. The only supported types are: float64, float32, float16, complex64, complex128, int64, int32, int16, int8, uint8, and bool.

Since the diemsnion of the data is inconsistent, hence the code will have error.

Closing comments about when to use this function

In [14]:
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/amirulwasim96/01-tensor-operations

Function 3 - zeros

Create a tenso of certain sizer with zeros based on the argument size.

In [22]:
# Example 1 - working
torch.zeros(size=(2,2,3))
Out[22]:
tensor([[[0., 0., 0.],
         [0., 0., 0.]],

        [[0., 0., 0.],
         [0., 0., 0.]]])

The shape of the tensor depends on the size argument.

In [21]:
# Example 2 - working
torch.zeros(size=(1,3,2))
Out[21]:
tensor([[[0., 0.],
         [0., 0.],
         [0., 0.]]])

The shape of the tensor will change according to the size argument

In [24]:
# Example 3 - breaking (to illustrate when it breaks)
torch.zeros(size=(-1,-1))
--------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) <ipython-input-24-305df51834d1> in <module>() 1 # Example 3 - breaking (to illustrate when it breaks) ----> 2 torch.zeros(size=(-1,-1)) RuntimeError: Trying to create tensor with negative dimension -1: [-1, -1]

The function does not take negative value.

Use this function when we need to create a tensor with a certain dimension. (it can be used later to alter the value)

In [25]:
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/amirulwasim96/01-tensor-operations

Function 4 - ones

A function works similarly to zeros, except the value is one

In [26]:
# Example 1 - working
torch.ones(size=(2,2,3))
Out[26]:
tensor([[[1., 1., 1.],
         [1., 1., 1.]],

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

The dimension of the tensor is based on the size argument

In [27]:
# Example 2 - working
torch.ones(size=(1,3,2))
Out[27]:
tensor([[[1., 1.],
         [1., 1.],
         [1., 1.]]])

3 is the number of rows, 2 is the number of columns, and 1 is the number of the 3x2 matrices.

In [28]:
# Example 3 - breaking (to illustrate when it breaks)
torch.ones(-1,-1)
--------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) <ipython-input-28-a38e20d76939> in <module>() 1 # Example 3 - breaking (to illustrate when it breaks) ----> 2 torch.ones(-1,-1) RuntimeError: Trying to create tensor with negative dimension -1: [-1, -1]

Does not takes in negative values

Use this when we want to construct a tensor that can later easliy be change with multiplications or division or other operations.

In [29]:
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/amirulwasim96/01-tensor-operations

Function 5 - empty

Create a tensor object with ininitialized value

In [31]:
# Example 1 - working
torch.empty(size=(1,2,3))
Out[31]:
tensor([[[6.8440e-36, 0.0000e+00, 1.5975e-43],
         [1.3873e-43, 1.4574e-43, 6.4460e-44]]])

The function will initialize value with random value

In [32]:
# Example 2 - working
torch.empty(size=(3,2,1))
Out[32]:
tensor([[[6.8440e-36],
         [0.0000e+00]],

        [[1.5975e-43],
         [1.3873e-43]],

        [[1.4574e-43],
         [6.4460e-44]]])

The function takes in the size argument to create the tensor with the size

In [33]:
# Example 3 - breaking (to illustrate when it breaks)
torch.empty(size=(-1,-1))
--------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) <ipython-input-33-9d2ba4391655> in <module>() 1 # Example 3 - breaking (to illustrate when it breaks) ----> 2 torch.empty(size=(-1,-1)) RuntimeError: Trying to create tensor with negative dimension -1: [-1, -1]

The functions does not take in negative value

Closing comments about when to use this function

In [34]:
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/amirulwasim96/01-tensor-operations

Conclusion

Most of the functions used here are function to create tensor. Many more functions can be used.

Reference Links

Provide links to your references and other interesting articles about tensors

In [35]:
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/amirulwasim96/01-tensor-operations
In [36]:
jovian.submit(assignment="zerotogans-a1", project="01-tensor")
[jovian] Detected Colab notebook...
[jovian] Error: Please provide the project argument e.g. jovian.commit(project='my-project')
In [ ]: