DEV Community

Cover image for PyTorch Fundamentals: A Beginner-Friendly Guide
Syed Mohammed Faham
Syed Mohammed Faham

Posted on

PyTorch Fundamentals: A Beginner-Friendly Guide

If you are starting your journey in artificial intelligence (AI) or deep learning, PyTorch is one of the best frameworks to learn. It is widely used in research and industry because of its flexibility and ease of use. PyTorch is used for tasks like computer vision, natural language processing, and reinforcement learning due to its simplicity, efficiency, and strong community support. In this blog, we will go through the basics of PyTorch in simple terms.

What is PyTorch?

PyTorch is an open-source deep learning framework developed by Facebook. It is mainly used for building neural networks and performing computations on large datasets. PyTorch is popular because:

  • It is easy to use and has a simple syntax.
  • It supports dynamic computation graphs, meaning you can modify your model while running it.
  • It integrates well with Python and NumPy, making it beginner-friendly.
  • It provides GPU acceleration, which helps in faster training of models.

Installing PyTorch

If you don't want to install PyTorch locally, you can use Google Colab, which provides a free cloud-based environment with PyTorch pre-installed. This allows you to run PyTorch code without any setup on your local machine. Simply go to Google Colab and create a new notebook.

Before using PyTorch, you need to install it. You can install PyTorch using pip:

pip install torch torchvision torchaudio 
Enter fullscreen mode Exit fullscreen mode

Or, if you are using Anaconda:

conda install pytorch torchvision torchaudio -c pytorch 
Enter fullscreen mode Exit fullscreen mode

For more details, check the official installation guide.

Tensors: The Building Blocks of PyTorch

A tensor is a fundamental data structure in PyTorch. It is similar to a NumPy array but has the added advantage of running on GPUs for faster computations.

Creating Tensors

You can create a tensor in PyTorch using:

import torch tensor1 = torch.tensor([1, 2, 3, 4]) print(tensor1) 
Enter fullscreen mode Exit fullscreen mode

For a deeper understanding of tensors, refer to the PyTorch tensor documentation.

Tensor Manipulation in PyTorch

Once you create tensors, you often need to modify or perform operations on them. This includes basic arithmetic, reshaping, concatenation, and more.

Basic Arithmetic Operations

You can perform element-wise operations like addition, subtraction, multiplication, and division.

tensor_a = torch.tensor([2, 4, 6]) tensor_b = torch.tensor([1, 2, 3]) # Addition sum_tensor = tensor_a + tensor_b print(sum_tensor) # Output: tensor([3, 6, 9])  # Subtraction diff_tensor = tensor_a - tensor_b print(diff_tensor) # Output: tensor([1, 2, 3])  # Element-wise Multiplication mul_tensor = tensor_a * tensor_b print(mul_tensor) # Output: tensor([2, 8, 18])  # Element-wise Division div_tensor = tensor_a / tensor_b print(div_tensor) # Output: tensor([2., 2., 2.]) 
Enter fullscreen mode Exit fullscreen mode

Matrix Multiplication

Matrix multiplication is different from element-wise multiplication. In PyTorch, you can use torch.matmul() or the @ operator.

matrix1 = torch.tensor([[1, 2], [3, 4]]) matrix2 = torch.tensor([[5, 6], [7, 8]]) # Matrix multiplication matmul_result = torch.matmul(matrix1, matrix2) print(matmul_result) # Alternatively, using the @ operator matmul_result2 = matrix1 @ matrix2 print(matmul_result2) 
Enter fullscreen mode Exit fullscreen mode

Reshaping Tensors

Reshaping is useful when converting data into the correct format for model training.

tensor_c = torch.arange(9) # Creates a tensor with values from 0 to 8 print(tensor_c) reshaped_tensor = tensor_c.view(3, 3) # Reshape to 3x3 print(reshaped_tensor) 
Enter fullscreen mode Exit fullscreen mode

Expanding and Concatenating Tensors

You can change the dimensions of tensors or combine multiple tensors.

tensor_d = torch.tensor([[1, 2], [3, 4]]) # Expand dimensions expanded_tensor = tensor_d.unsqueeze(0) # Adds a new dimension at index 0 print(expanded_tensor.shape) # Output: torch.Size([1, 2, 2])  # Concatenate tensors along a specific dimension tensor_e = torch.tensor([[5, 6]]) concatenated_tensor = torch.cat((tensor_d, tensor_e), dim=0) print(concatenated_tensor) 
Enter fullscreen mode Exit fullscreen mode

Indexing and Slicing Tensors

You can access specific elements of a tensor using indexing:

tensor_f = torch.tensor([[1, 2, 3], [4, 5, 6]]) # Access a single element print(tensor_f[0, 1]) # Output: 2  # Slice a tensor (get first row) print(tensor_f[0, :]) # Output: tensor([1, 2, 3]) 
Enter fullscreen mode Exit fullscreen mode

For more details, check the tensor indexing documentation.

Using GPU in PyTorch

PyTorch allows you to run tensors on a GPU for faster computations. You can check if a GPU is available using:

if torch.cuda.is_available(): print("GPU is available!") device = torch.device("cuda") else: print("Using CPU") device = torch.device("cpu") 
Enter fullscreen mode Exit fullscreen mode

You can move a tensor to GPU using:

tensor_gpu = tensor1.to(device) print(tensor_gpu) 
Enter fullscreen mode Exit fullscreen mode

For more details on GPU acceleration, visit the CUDA documentation.

Building a Simple Neural Network in PyTorch

A neural network in PyTorch is created using the torch.nn module. Here’s a simple example of a basic neural network with one hidden layer:

import torch.nn as nn # Define a simple neural network class SimpleNN(nn.Module): def __init__(self): super(SimpleNN, self).__init__() self.layer1 = nn.Linear(2, 3) self.layer2 = nn.Linear(3, 1) def forward(self, x): x = torch.relu(self.layer1(x)) x = self.layer2(x) return x # Create an instance of the model model = SimpleNN() print(model) 
Enter fullscreen mode Exit fullscreen mode

For more details on neural networks, check the PyTorch nn module documentation.

Conclusion

PyTorch is a powerful and beginner-friendly framework for deep learning. In this blog, we covered:

  • What PyTorch is and why it is useful.
  • How to install PyTorch.
  • Tensors and basic operations.
  • Tensor manipulation: arithmetic, reshaping, indexing, and matrix multiplication.
  • Using GPUs for faster computations.
  • Building a simple neural network.

This is just the beginning! In the next blogs, we will dive deeper into PyTorch by exploring:

  • Dataset and DataLoader for handling large datasets efficiently.
  • Training deep learning models step by step.
  • Advanced PyTorch features like autograd, model evaluation, and optimization techniques.

Stay tuned for the next part of this PyTorch series! Happy coding!

Top comments (0)