18 Deep Learning Framework: Pytorch

  18 Deep Learning Framework: Pytorch

1. What is PyTorch?

PyTorch is an open-source deep learning framework developed by Meta (Facebook) AI. It’s widely used for building neural networks and machine learning models.

Key features:

  1. Dynamic Computation Graphs (Define-by-Run)
    • Unlike older frameworks (like TensorFlow 1.x), PyTorch builds the computation graph on the fly, which makes debugging and experimentation easier.
  2. GPU Acceleration
    • PyTorch can perform fast computations using CUDA-enabled NVIDIA GPUs, making training large models much faster.
  3. Pythonic and Flexible
    • It feels like regular Python code, which is intuitive for developers and researchers.
  4. Deep Learning Ready
    • Supports autograd (automatic differentiation), neural network modules, optimizers, and pre-trained models.

 

1. What is PyTorch?

PyTorch is an open-source deep learning framework developed by Meta (Facebook) AI. It’s widely used for building neural networks and machine learning models.

Key features:

  1. Dynamic Computation Graphs (Define-by-Run)
    • Unlike older frameworks (like TensorFlow 1.x), PyTorch builds the computation graph on the fly, which makes debugging and experimentation easier.
  2. GPU Acceleration
    • PyTorch can perform fast computations using CUDA-enabled NVIDIA GPUs, making training large models much faster.
  3. Pythonic and Flexible
    • It feels like regular Python code, which is intuitive for developers and researchers.
  4. Deep Learning Ready
    • Supports autograd (automatic differentiation), neural network modules, optimizers, and pre-trained models.

2. What are Tensors in PyTorch?

A tensor is the core data structure in PyTorch. It’s essentially a multi-dimensional array, similar to NumPy arrays, but with GPU support.

Think of tensors as the building blocks for inputs, weights, and outputs in neural networks.


Tensor Basics

Feature

Description

0D Tensor

A single number (scalar), e.g., tensor(5)

1D Tensor

A vector, e.g., [1, 2, 3]

2D Tensor

A matrix, e.g., [[1,2],[3,4]]

3D+ Tensor

Higher-dimensional data (like images, batch of images, video frames)


PyTorch Tensor Example

import torch

# 1D Tensor
a = torch.tensor([1, 2, 3])
print(a)  # tensor([1, 2, 3])

# 2D Tensor (Matrix)
b = torch.tensor([[1, 2], [3, 4]])
print(b)  # tensor([[1, 2], [3, 4]])

# Move tensor to GPU if available
if torch.cuda.is_available():
    b = b.to('cuda')


Important Features of Tensors

  1. Operations: You can do math, linear algebra, and broadcasting just like NumPy.
  2. Gradients: Tensors can track operations for automatic differentiation (requires_grad=True).
  3. GPU Support: Tensors can be stored and computed on CPU or GPU.

In short:

  • PyTorch = a framework to build deep learning models.
  • Tensors = multi-dimensional arrays that PyTorch uses to store and compute data efficiently, often on GPUs.








Comments

Popular posts from this blog

19 Google ADK Tutorial

15 Agent AI vs Agentic AI

16 Build a Free Chat App on Google Colab using RAG