Learn practical skills, build real-world projects, and advance your career

Pytorch ZERO to GAN

Assignment 1: Tensor-operations

PyTorch is an open source machine learning library based on the Torch library,used for applications such as computer vision and natural language processing primarily developed by Facebook's AI Research lab (FAIR). It is free and open-source software released under the Modified BSD license. Although the Python interface is more polished and the primary focus of development, PyTorch also has a C++ interface. in this assignment, five basic functions will presented.

  • function 1: create tensor with pytorch
  • function 2: Resizing a Tensor
  • function 3: Mathematical Operation
  • function 4: Standard Deviation and Mean
  • function 5: Variables and Gradient
# Import torch and other required modules
import torch

Function 1 - torch.tensor

There are three ways to create Tensor

  1. Create PyTorch Tensor with an array
  2. Create a Tensor with all ones and random number
  3. Create Tensor from numpy array
# Example 1
#1. with an array
arr = [[3, 4], [8, 5]]   
pyTensor = torch.Tensor(arr)  
print(pyTensor)  
 
tensor([[3., 4.], [8., 5.]])

In this method, we have first to define the array and then pass that array in Tensor method of the torch as an argument.