🔥 PyTorch & Deep Learning Masterclass
1️⃣ Deep Learning Foundations & PyTorch vs TensorFlow
Definition: Deep Learning Machine Learning ka ek advanced subset hai jo multiple layers wale Artificial Neural Networks (ANNs) ka use karke raw, unstructured data (images, audio, text) se automatically hierarchical features learn karta hai.
| Criteria | PyTorch (Meta AI) | TensorFlow / Keras (Google) |
|---|---|---|
| Computation Graph | Dynamic Graph (Define-by-Run): Debugging standard Python pdb se hoti hai. | Static Graph (Define-and-Run) via tf.function graph tracing. |
| Industry & Research | 85%+ of recent academic papers, Hugging Face aur Generative AI models me primary choice. | Legacy enterprise deployments aur mobile embedded systems (TF Lite). |
| Pythonic Feel | 100% native Python OOPs feel, NumPy jaise direct API calls. | Thoda declarative framework overhead lagta hai. |
2️⃣ PyTorch Tensors & GPU Acceleration
Tensor: Ek n-dimensional mathematical array jo GPU CUDA cores par massively parallel matrix multiplication support karta hai:
import torch
# 1. Device Agnostic Code (Automatically picks Nvidia GPU or Apple MPS or CPU)
device = "cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu"
print(f"Using compute device: {device}")
# 2. Tensor Creation & Reshaping
x = torch.randn(size=(4, 3, 224, 224), device=device) # Batch: 4, RGB: 3, 224x224 image
print(f"Tensor Shape: {x.shape} | Data Type: {x.dtype}")
# 3. Moving between Host RAM and GPU VRAM
cpu_tensor = x.to("cpu")
3️⃣ Automatic Differentiation with Autograd
PyTorch ka autograd engine reverse-mode automatic differentiation implement karta hai. Calculus ke chain rule ko background directed acyclic graph (DAG) me automatically compute karta hai:
# Track gradients with requires_grad=True
w = torch.tensor(2.0, requires_grad=True)
b = torch.tensor(1.0, requires_grad=True)
x = torch.tensor(3.0)
# Forward Pass: y = w*x + b = 2*3 + 1 = 7
y = w * x + b
# Backward Pass (Backpropagation)
y.backward()
# Gradients d(y)/d(w) = x = 3.0, d(y)/d(b) = 1.0
print(f"Weight Gradient: {w.grad}") # Output: 3.0
print(f"Bias Gradient: {b.grad}") # Output: 1.0
4️⃣ Building Custom Architectures with torch.nn.Module
PyTorch me har deep neural network nn.Module base class ko subclass karke define hota hai:
import torch.nn as nn
class MultilayerPerceptron(nn.Module):
def __init__(self, input_dim=784, hidden_dim=128, output_classes=10):
super(MultilayerPerceptron, self).__init__()
self.network = nn.Sequential(
nn.Linear(input_dim, hidden_dim),
nn.BatchNorm1d(hidden_dim),
nn.ReLU(),
nn.Dropout(p=0.2), # Regularization to prevent overfitting
nn.Linear(hidden_dim, output_classes) # Raw Logits
)
def forward(self, x):
return self.network(x)
5️⃣ The Standard 5-Step Training Loop
Deep learning models ko train karne ka universal 5-step PyTorch lifecycle:
1.
optimizer.zero_grad() → Purane iteration ke accumulated gradients flush karna.2.
outputs = model(inputs) → Forward pass calculate karna.3.
loss = criterion(outputs, targets) → Loss / Error measure karna.4.
loss.backward() → Gradients calculate karna via backpropagation.5.
optimizer.step() → Weights update karna using gradient descent step ($w = w - \alpha \nabla w$).
6️⃣ Convolutional Neural Networks (CNNs)
Computer Vision me images ke spatial features (edges, textures, shapes) extract karne ke layers:
| Layer | PyTorch Class | Primary Responsibility |
|---|---|---|
| Convolution | nn.Conv2d(in_channels, out_channels, kernel_size=3) |
Slideable filter matrix images se local spatial patterns extract karta hai. |
| Activation | nn.ReLU() |
Non-linearity introduce karta hai ($f(x) = \max(0, x)$). |
| Pooling | nn.MaxPool2d(kernel_size=2, stride=2) |
Spatial dimensions (width × height) ko half karke compute speed badhata hai. |
7️⃣ Self-Attention & Modern Transformers
LLMs (GPT, LLaMA, BERT) ke foundation architecture ka core mathematical formula:
$$\text{Attention}(Q, K, V) = \text{softmax}\left(\frac{Q K^T}{\sqrt{d_k}}\right) V$$
• Query ($Q$): "Main kya dhoondh raha hoon?"
• Key ($K$): "Mera content kisse match karta hai?"
• Value ($V$): "Mera actual semantic meaning kya hai?"
• $\sqrt{d_k}$ Scaling: Large dimensions par gradients ko vanish hone se rokta hai.
# PyTorch Built-in Multihead Attention Block
attention_layer = nn.MultiheadAttention(embed_dim=512, num_heads=8, batch_first=True)
attn_output, attn_weights = attention_layer(query=x, key=x, value=x)