Pytorch Basics

Why use PyTorch?#

PyTorch is an open-source deep learning framework developed by Facebook’s AI Research lab (FAIR). It provides a flexible and intuitive platform for building and training neural networks. Researchers and practitioners favor PyTorch due to its dynamic computation graph, which allows for more efficient and straightforward model building and debugging. Its seamless integration with Python makes it an ideal choice for rapid prototyping and experimentation.

What is a PyTorch Tensor?#

At the core of PyTorch lies the tensor, an n-dimensional array similar to NumPy arrays or TensorFlow tensors. Tensors in PyTorch are fundamental data structures essential for representing and manipulating data in machine learning tasks. A rank 0 tensor can be likened to a scalar, a rank 1 tensor to a vector, and a rank 2 tensor to a matrix. Higher-dimensional tensors extend this concept further.

Creating a tensor from a list

Creating a tensor from a list or a nested list is easy. First, we need to import the torch library and call the tensor function. 

import torch
a = torch.tensor([1 ,2, 3])
b = torch.tensor([[1], [2], [3]])


Creating a tensor from a NumPy array

import torch
import numpy as np
na = np.array([1, 2, 3])
a = torch.tensor(na)
b = torch.from_numpy(na)


Creating special tensors

PyTorch provides some useful functions to create special tensors, such as the identity tensor and tensors having all zeros or ones.

  • eye(): Creates an identity tensor with an integer.
  • zeros(): Creates a tensor with all zeros, the parameter could be an integer or a tuple that defines the shape of the tensor.
  • ones(): Creates a tensor with all ones like ones. The parameter could be an integer or a tuple that defines the shape of the tensor.

import torch

# Create a identity tensor with 3*3 shape.
eys = torch.eye(3)
print(eys)

# Create a tensor with 2*2 shape whose values are all 1.
ones = torch.ones((2, 2))
print(ones)

# Create a tensor with 3*3 shape whose values are all 0.
zeros = torch.zeros((3, 3))


Creating a random tensor

PyTorch provides some useful functions to create a tensor with a random value.

  • rand(): It creates a tensor filled with random numbers from a uniform distribution. The parameter is a sequence of integers defining the shape of the output tensor. It can be a variable number of arguments or a collection like a list or a tuple.
  • randn(): It creates a tensor filled with random numbers from a normal distribution with mean 0 and variance 1. The parameter is the same as the rand().
  • randint(): Unlike the functions above, this function creates a tensor with integer values with lowhigh and size parameters. low means the lowest value, it’s optional and the default value is 0. high means the highest value, and size is a tuple that defines the shape of the tensor.


import torch # Create a tensor with 1*10 shape with random value between 0 and 1 r0 = torch.rand(10) print(r0) print("************************************************") # Create a tensor with 10*1 shape with random value between 0 and 1 r1 = torch.rand((10, 1)) print(r1) print("************************************************") # Create a tensor with 2*2 shape with random value between 0 and 1 r2 = torch.rand((2, 2)) print(r2) print("************************************************") # Create a tensor with 2*2 shape with random value from a normal distribution. r3 = torch.randn((2,2)) print(r3) print("************************************************") # Create an integer type tensor with 3*3 shape with random value between 0 and 10. r4 = torch.randint
(high=10, size=(3, 3)) print(r4) print("************************************************") # Create an integer type tensor with 3*3 shape with random value between 5 and 10. r5 = torch.randint(low=5, high=10, size=(3, 3)) print(r5)





Creating a range tensor

PyTorch also provides a function arange that generates values in [start; end), like NumPy.

torch.arange(1, 10)

Line 3 creates a tensor by arange. It creates a 1-D dimension tensor with a length of 9

Comments

Popular Posts