PyTorch Basics Part 1

  • by

PyTorch is an open source deep learning frame work used to implement deep learning models. The core of PyTorch is Tensor. A tensor is a multi-dimensional data holding unit. A tensor with order 0 is a ‘scalar’ i.e. a number. Order 1 tensor is a series of numbers called ‘vector’, and order 2 of tensor is an array of vectors called ‘matrix’. From 3 and above its generalized as N-Tensor where N is the order or dimension.

In this page we will see about

1. Creating Tensor

2. Operations with Tensor

3. Indexing, Slicing and Joining Tensors

4. CUDA Tensors with GPU

PyTorch in Jupyter LAB

To run/use PyTorch you need to install it from the website. But the first step is to create a conda environment in a particular path where you want to run and store all your jupyter files for the current project.


  1. create a conda environment in a path. Eg: I want to store and run my project in path (E:PYtorch) run this command in anaconda prompt in that particular path to create environment. pytorch_blog is the name of environment.

2. After creation activate the environment

3. Install Jupyter lab and pytorch in that Environment

for jupyter Lab:

Trending Bot Articles:

1. Understanding DialogFlow ES and CX

2. Deploying Transformer Models

3. TensorFlow installation with GPU Support on Python 3.7 in Windows

4. Conversation Designers: who are they and what do they do?

then find your CUDA version through command prompt by running command: nvidia-smi

my version is 10.2.

for Pytorch:

command varies based on OS and CUDA version as from Pytorch official website my command to run is

Creating Tensor:

You can start utilizing PyTorch by importing the Torch package. The Torch package helps us to create tensors in many ways one simple way is use torch.Tensor function to get a random tensor of specified size. Before looking at different ways of initializing or creating a tensor we need to three printable features of tensors.

1. Type e.g.: Float, Long, Double

2. Size i.e.: no of rows and columns

3. values in the tensor

we can just create a function and pass tensor as input and print these details inside the function.

For torch.Tesnor we can pass the size as parameters. Eg: 3 rows and 2 columns

Instead of shape we can pass matrix elements

we can randomly initialize the tensor with either uniformly distributed values with rand function or normal distribution with randn.

We looked at generating a random tensor now we see about single valued tensor like all values in tensor are same. We can create zero, one tensors also custom single value tensor. For custom mode we need a basic zero or one tensor on which we use fill_ to get our custom tensor.

Till now in all the examples we have seen Float Tensor we also have Long Tensor and Double Tensor. We can directly call them from torch.

We can also use numpy to generate a tensor. This convertible relation between Numpy and PyTorch will be useful when we have variables of numpy format. we use torch.from_numpy to convert from numpy to torch.

Operations on tensors and other remaining we look in PyTorch Basics Part2.

Don’t forget to give us your 👏 !

PyTorch Basics Part 1 was originally published in Chatbots Life on Medium, where people are continuing the conversation by highlighting and responding to this story.

Leave a Reply

Your email address will not be published. Required fields are marked *