Getting Started with PyTorch: A Guide to Creating and Initializing Tensors

RAHULRAJ P V
5 min readDec 9, 2023

PyTorch, is one of the popular open-source deep learning framework, which is short for “Python-based Torch” is an amazing tool for doing all sorts of cool things with computers. It started its journey in the world of artificial intelligence and deep learning, with a group of clever researchers and scientists developing it at Facebook’s AI Research (FAIR) lab in 2016.

PyTorch gained significant momentum in the machine learning community due to its dynamic computational graph and user-friendly interface. Researchers and developers embraced PyTorch for its flexibility, allowing for easy experimentation and rapid prototyping of deep neural networks. Now let us learn the basics of pytorch.

Tensors

In PyTorch, a tensor is a fundamental data structure that is used for representing multi-dimensional arrays. Tensors are similar to NumPy arrays, but they have additional features that make them well-suited for deep learning and GPU acceleration. Understanding tensors is crucial for working with PyTorch and building neural networks. Here are some key points about tensors in PyTorch:

  1. One Dimension: Imagine a list of numbers. It’s like a row of cells, and you can access each cell by its position (index) in the list.

2. Two Dimensions: Now, think of a grid or a table. You have rows and columns. You can access a cell by specifying its row and column, like coordinates on a map.

3. Multi Dimensions: You can keep adding dimensions, like having a bunch of 3D structures arranged in different ways. It’s like having a hyper-dimensional space for your data.

Creating and Initializing Tensors

Now, let’s dive into the practical aspect of creating and initializing tensors in PyTorch. We will use various functions provided by PyTorch to accomplish this:

Create a 0D (Scalar) Tensor:

import torch

# Creating a 0D tensor (scalar)
scalar_tensor = torch.tensor(42)

In this part, we import the PyTorch library and create a 0D tensor (scalar) with the value 42. A 0D tensor represents a single number, which is often called a scalar.

Creating a 1D Tensor:

# Creating a 1D tensor
one_dim_tensor = torch.tensor([1, 2, 3, 4, 5])

Here, we create a 1D tensor one_dim_tensor by passing a Python list [1, 2, 3, 4, 5] to torch.tensor(). This tensor contains a sequence of numbers.

Creating a 2D Tensor (Matrix):

# Creating a 2D tensor (matrix)
two_dim_tensor = torch.tensor([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])

In this part, we create a 2D tensor two_dim_tensor by passing a list of lists. Each inner list represents a row in the matrix, resulting in a 3x3 matrix. It's similar to a table or grid of numbers.

You can use these tensors for various operations and tasks in PyTorch, such as mathematical operations, neural network computations, and data processing. Each tensor type (0D, 1D, 2D, etc.) has its own use cases and applications in machine learning and deep learning.

torch.empty(size): Uninitialized Tensors

The torch.empty(size) function creates uninitialized tensors. Uninitialized means that the tensor's values are not set and may contain random or garbage values.

import torch

# Creating a 0D tensor (scalar)
scalar_tensor = torch.empty(1)

# Creating a 1D tensor (vector)
one_dim_tensor = torch.empty(3)

# Creating a 2D tensor (matrix)
two_dim_tensor = torch.empty(2, 3)

# Creating a 3D tensor
three_dim_tensor = torch. Empty(2, 2, 3)

torch.rand(size): Random Number Tensors

The torch.rand(size) function generates tensors filled with random values sampled from a uniform distribution between 0 and 1.

x = torch.rand(5, 3)  # Create a 2D tensor (5x3) with random values

torch.zeros(size) and torch.ones(size): Tensors Filled with Zeros and Ones

These functions create tensors filled with zeros or ones, respectively.

x = torch.zeros(5, 3)  # Create a 2D tensor (5x3) filled with zeros

Checking Tensor Size

print("Shape:", x.shape)

The shape of the tensor is checked using the shape attribute. The shape provides a more human-readable representation of the tensor's dimensions. It will print torch.Size([2, 3]), indicating that the tensor has 2 rows and 3 columns.

Checking Tensor Data Type

print("Data Type:", x.dtype)

This line checks the data type of the tensor using the dtype attribute. By default, tensors are of type torch.float32. It will print torch.float32 because the torch.rand() function creates tensors of this data type.

Creating a Tensor with a Different Data Type

x = torch.zeros(2, 3, dtype=torch.float64)
print("New Data Type:")
print(x)

A new tensor x is created here, filled with zeros using the torch.zeros() function. Additionally, we specify the data type as torch.float64. The tensor will have a data type of torch.float64, which is a higher precision floating-point type compared to the default torch.float32.

Creating a Tensor from a List

x = torch.tensor([1.2, 3.4, 5.6])
print("Tensor from List:")
print(x)

In this part, we create a tensor x by passing a Python list [1.2, 3.4, 5.6] to the torch.tensor() function. The tensor retains the data type of the elements in the list, which will be torch.float32 since the elements are decimal numbers.

Using requires_grad for Gradients

x = torch.tensor([2.0, 4.0], requires_grad=True)
print("Tensor with requires_grad:")
print(x)

In the final section, we create a tensor x from a list [2.0, 4.0] and set the requires_grad argument to True. This indicates that gradients need to be calculated for this tensor during optimization, which is crucial when training neural networks.

By mastering these basic tensor creation and initialization techniques, you’ve taken the first steps toward harnessing the power of PyTorch. Tensors are the building blocks of deep learning models, and being able to create tensors of different shapes and initialize them with suitable values is a fundamental skill for any PyTorch developer. Happy coding and experimenting with PyTorch!

--

--

RAHULRAJ P V
RAHULRAJ P V

Written by RAHULRAJ P V

IIITMK (DUK) MTech CSE AI '24 | IIM Kozhikode Research Intern | CSIR NPL Project Intern | MSc Physics | PGDDSA | Generative AI & Cognitive Science Learner

No responses yet