Using GPUs and TPUs in Deep Learning

अब जब आप PyTorch और Keras दोनों में model बना और train कर रहे हैं, तो performance boost के लिए GPU/TPU का इस्तेमाल करना ज़रूरी हो जाता है — खासकर बड़े datasets या deep models के लिए।


🔷 1. 🔥 Why Use GPUs?

CPU vs GPUGPU Advantage
SequentialParallel computing
Few coresThousands of cores
Slower matrix opsFaster tensor operations
General-purposeSpecial for ML/DL, CUDA-optimized

🔶 PyTorch में GPU का उपयोग कैसे करें?

✅ Step 1: Check GPU Availability

import torch

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print("Using device:", device)

✅ Step 2: Model और Data को GPU पर भेजना

model = MyNet().to(device)  # Model को GPU पर भेजें
X = X.to(device)
y = y.to(device)

✅ Training Code में बदलाव:

for epoch in range(1000):
y_pred = model(X)
loss = criterion(y_pred, y)

optimizer.zero_grad()
loss.backward()
optimizer.step()

बस यह ध्यान रखें कि model और data दोनों एक ही device पर होने चाहिए


🔷 2. GPU से Prediction (Inference)

model.eval()
with torch.no_grad():
test = torch.tensor([[1., 0.]]).to(device)
output = model(test)
print("Prediction:", output.item())

🔷 3. Keras (TensorFlow) में GPU का उपयोग

TensorFlow अपने आप GPU को detect करता है और उपयोग करता है (अगर उपलब्ध हो)। बस ensure करें कि:

  • TensorFlow-GPU version install हो
  • NVIDIA Drivers + CUDA Toolkit सही से install हो

✅ Check GPU:

import tensorflow as tf
print("Num GPUs Available:", len(tf.config.list_physical_devices('GPU')))

✅ Tensor और Model GPU पर चलेंगे अपने आप:

model.fit(X, y, epochs=10)  # If GPU available, TensorFlow will use it

✅ Manually GPU select करना:

with tf.device('/GPU:0'):
model = tf.keras.Sequential([...])
model.compile(...)
model.fit(...)

🔷 4. Google Colab में GPU/TPU Use

✅ GPU Enable करें:

Runtime → Change runtime type → Hardware accelerator → GPU or TPU

✅ Check GPU/TPU:

import tensorflow as tf
print(tf.config.list_physical_devices('GPU')) # For GPU
print(tf.config.list_logical_devices('TPU')) # For TPU

🔷 5. TPUs क्या होते हैं?

AspectDetail
TPUTensor Processing Unit (Google द्वारा बनाया गया)
SpeedGPU से भी तेज़ है कुछ tasks में
Best forVery large models, production-level serving
UseMainly in Google Colab, Cloud TPUs, TensorFlow

📝 Practice Questions:

  1. PyTorch में GPU support कैसे check करते हैं?
  2. Model को GPU पर भेजने के लिए कौनसा syntax है?
  3. TensorFlow GPU vs TPU में क्या अंतर है?
  4. Google Colab में GPU enable कैसे करते हैं?
  5. क्या CPU से GPU training में फर्क आता है?

🧠 Summary Table

TaskPyTorchKeras (TF)
GPU Checktorch.cuda.is_available()tf.config.list_physical_devices()
Send to GPUx.to(device)Automatic
Model to GPUmodel.to(device)Automatic
Use Colab GPURuntime > Change > GPUSame
TPU Support❌ (Limited)✅ (Good)

Model Training, Saving, and Loading in Keras

अब हम Keras (जो TensorFlow का high-level API है) में Deep Learning Model को Train, Save, और Load करना सीखेंगे — step-by-step और practical examples के साथ।


🔷 1. ✅ Model Training in Keras (Step-by-Step)

📌 Step 1: Import Libraries

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

📌 Step 2: Define Model

model = Sequential([
Dense(8, activation='relu', input_shape=(2,)),
Dense(1, activation='sigmoid')
])

📌 Step 3: Compile Model

model.compile(
optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy']
)

📌 Step 4: Prepare Data

import numpy as np

X = np.array([[0,0], [0,1], [1,0], [1,1]])
y = np.array([0, 1, 1, 0])

📌 Step 5: Train Model

model.fit(X, y, epochs=100, batch_size=2, verbose=1)

🔷 2. 💾 Saving a Keras Model

✅ Option 1: Save Full Model (Best Practice)

model.save("my_model.h5")  # Saves architecture + weights + optimizer state

OR in newer format:

model.save("my_model.keras")  # New native format

✅ Option 2: Save Only Weights

model.save_weights("model_weights.h5")

🔷 3. 📂 Loading a Saved Model

✅ Load Full Model:

from tensorflow.keras.models import load_model

model = load_model("my_model.h5")

This will return the model ready to use — no need to recompile or redefine.


✅ Load Only Weights:

First define the model architecture same as before:

model = Sequential([
Dense(8, activation='relu', input_shape=(2,)),
Dense(1, activation='sigmoid')
])

Then load the weights:

model.load_weights("model_weights.h5")

🔷 4. 🔁 Save and Load during Training (Checkpointing)

✅ Use ModelCheckpoint Callback

from tensorflow.keras.callbacks import ModelCheckpoint

checkpoint = ModelCheckpoint("best_model.h5", save_best_only=True, monitor="loss")

model.fit(X, y, epochs=50, callbacks=[checkpoint])

🔷 5. 🧪 Inference (Prediction)

pred = model.predict(np.array([[1, 0]]))
print("Prediction:", pred[0][0])

🧠 Use .predict() method for classification, regression, or output generation.


🔧 Extra: Exporting to TF Lite (Mobile/Edge)

converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()

with open('model.tflite', 'wb') as f:
f.write(tflite_model)

📝 Practice Questions:

  1. Keras में model train करने के steps क्या हैं?
  2. .h5 और .keras में क्या अंतर है?
  3. model.save() और model.save_weights() में क्या फर्क है?
  4. Training के दौरान best model कैसे save करते हैं?
  5. Model load करने के बाद inference कैसे करते हैं?

🧠 Summary Table

TaskKeras Method
Train Modelmodel.fit()
Save Full Modelmodel.save("model.h5")
Save Only Weightsmodel.save_weights()
Load Full Modelload_model("model.h5")
Load Weights Onlymodel.load_weights()
Predict / Inferencemodel.predict(x)
Save Best during TrainingModelCheckpoint(callback)

Model Training, Saving, and Loading in PyTorch

अब हम PyTorch में Model Training, फिर उसे Save और Load करने की पूरी प्रक्रिया विस्तार से सीखते हैं —
जो किसी भी Deep Learning project का core हिस्सा है।


🔷 1. 🔁 Model Training in PyTorch

🧱 Training Steps Overview:

  1. Model बनाना (nn.Module)
  2. Loss function चुनना (nn.CrossEntropyLoss, etc.)
  3. Optimizer सेट करना (torch.optim)
  4. Forward pass करना
  5. Loss calculate करना
  6. Backward pass (loss.backward())
  7. Optimizer step (optimizer.step())

🧪 Full Example (Classifier):

import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, TensorDataset

# Sample data
X = torch.tensor([[0.,0.],[0.,1.],[1.,0.],[1.,1.]])
y = torch.tensor([[0.],[1.],[1.],[0.]])

dataset = TensorDataset(X, y)
dataloader = DataLoader(dataset, batch_size=2, shuffle=True)

# Step 1: Model
class XORNet(nn.Module):
def __init__(self):
super(XORNet, self).__init__()
self.fc1 = nn.Linear(2, 4)
self.fc2 = nn.Linear(4, 1)

def forward(self, x):
x = torch.relu(self.fc1(x))
return torch.sigmoid(self.fc2(x))

model = XORNet()

# Step 2: Loss and Optimizer
criterion = nn.BCELoss()
optimizer = optim.Adam(model.parameters(), lr=0.1)

# Step 3: Training loop
for epoch in range(500):
for xb, yb in dataloader:
y_pred = model(xb)
loss = criterion(y_pred, yb)

optimizer.zero_grad()
loss.backward()
optimizer.step()

if epoch % 100 == 0:
print(f"Epoch {epoch}, Loss: {loss.item():.4f}")

🔷 2. 💾 Saving a PyTorch Model

PyTorch में 2 तरीके हैं model save करने के:

✅ Option 1: State Dict Only (Recommended)

torch.save(model.state_dict(), "xor_model.pth")

यह केवल model के weights save करता है, architecture नहीं।


✅ Option 2: Complete Model (Not Recommended)

torch.save(model, "xor_model_full.pth")

यह पूरा model + structure save करता है, पर version compatibility issues आ सकते हैं।


🔷 3. 📂 Loading a Model

🔁 Load from State Dict (Best Practice):

model = XORNet()  # पहले architecture बनाओ
model.load_state_dict(torch.load("xor_model.pth"))
model.eval() # evaluation mode में डालना ज़रूरी

🔍 .eval() inference के समय BatchNorm / Dropout को deactivate करता है।


🧠 Bonus: GPU Compatibility (Saving/Loading)

✅ Save on GPU, Load on CPU:

# Save (from GPU)
torch.save(model.state_dict(), "model_gpu.pth")

# Load on CPU
device = torch.device("cpu")
model.load_state_dict(torch.load("model_gpu.pth", map_location=device))

🧪 Example: Inference After Loading

model.eval()
with torch.no_grad():
test = torch.tensor([[1., 1.]])
output = model(test)
print("Predicted:", output.item())

📦 Advanced Tip: Save Optimizer State Too

torch.save({
'model_state': model.state_dict(),
'optimizer_state': optimizer.state_dict(),
}, "checkpoint.pth")

Load Later:

checkpoint = torch.load("checkpoint.pth")
model.load_state_dict(checkpoint['model_state'])
optimizer.load_state_dict(checkpoint['optimizer_state'])

🧠 Evaluation Tips

  • हमेशा model.eval() use करें inference के लिए
  • torch.no_grad() में prediction करें (memory efficiency के लिए)
  • Large models के लिए model checkpoints का उपयोग करें

📝 Practice Questions:

  1. PyTorch में model train करने के मुख्य steps क्या हैं?
  2. State dict और full model saving में क्या अंतर है?
  3. Optimizer state क्यों save करना चाहिए?
  4. .eval() और .train() modes में क्या फर्क है?
  5. Inference में torch.no_grad() का उपयोग क्यों करें?

🧠 Summary Table

TaskMethod
🔧 TrainForward → Loss → Backward → Optimizer
💾 Save Weightstorch.save(model.state_dict(), path)
📂 Load Weightsmodel.load_state_dict(torch.load(path))
📌 Inferencemodel.eval() + with torch.no_grad()
🔁 Save with OptimizerUse checkpoint = {'model': ..., 'opt': ...}

Introduction of PyTorch

आपने TensorFlow और Keras सीख लिया — अब बारी है PyTorch की, जो research और flexibility के लिए Deep Learning community में सबसे लोकप्रिय framework है।

PyTorch एक open-source deep learning framework है जिसे Facebook AI Research Lab (FAIR) ने 2016 में विकसित किया।

यह framework खासकर researchers और advanced developers के बीच पसंदीदा है, क्योंकि यह:

  • Dynamic computation graph प्रदान करता है (runtime पर बदलता है)
  • Pythonic और NumPy जैसा syntax देता है
  • GPU acceleration आसानी से करता है

🔑 Features:

FeatureDescription
🧮 Dynamic GraphsReal-time control (ज़्यादा flexibility)
📊 Tensor LibraryNumPy जैसे operations with GPU support
🧠 AutogradAutomatic gradient calculation
🔧 Modular APINeural nets = Modules
🖥️ GPU ReadyCUDA support

🔶 2. PyTorch Installation

pip install torch torchvision

🔷 3. Tensors in PyTorch

Tensors are multi-dimensional arrays (similar to NumPy arrays) but with GPU support.

import torch

x = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
print(x.shape) # torch.Size([2, 2])
print(x + x) # Tensor addition
print(x @ x.T) # Matrix multiplication

✅ Use GPU:

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
x = x.to(device)

🔷 4. Autograd – Automatic Differentiation

x = torch.tensor(2.0, requires_grad=True)
y = x**3 + 2*x
y.backward()
print(x.grad) # dy/dx = 3x^2 + 2 = 3*2^2 + 2 = 14

🔷 5. Building a Simple Neural Network

🔨 Step 1: Import Libraries

🔨 Step 1: Import Libraries

import torch
import torch.nn as nn
import torch.optim as optim

🔨 Input and Output for XOR

X = torch.tensor([[0.,0.],[0.,1.],[1.,0.],[1.,1.]])
y = torch.tensor([[0.],[1.],[1.],[0.]])

🔨 Step 2: Define Model


class MyNet(nn.Module):
def __init__(self):
super(MyNet, self).__init__()
self.fc1 = nn.Linear(2, 4)
self.fc2 = nn.Linear(4, 1)

def forward(self, x):
x = torch.relu(self.fc1(x))
return torch.sigmoid(self.fc2(x))

🔨 Step 3: Instantiate Model

model = MyNet()

🔨 Step 4: Define Loss and Optimizer

criterion = nn.BCELoss()
optimizer = optim.Adam(model.parameters(), lr=0.01)

🔨 Step 5: Train the Model

for epoch in range(100):
y_pred = model(X)
loss = criterion(y_pred, y)

optimizer.zero_grad()
loss.backward()
optimizer.step()

print(f"Epoch {epoch}, Loss: {loss.item()}")

🔧 Important PyTorch Modules

ModuleDescription
torch.TensorMain data structure
torch.nnFor building neural nets
torch.nn.functionalActivation functions, loss functions
torch.optimOptimizers like Adam, SGD
torch.utils.dataDataset and DataLoader tools
torchvisionImage datasets and transformations

🔷 6. Example: XOR with MLP

X = torch.tensor([[0.,0.],[0.,1.],[1.,0.],[1.,1.]])
y = torch.tensor([[0.],[1.],[1.],[0.]])

model = nn.Sequential(
nn.Linear(2, 4),
nn.ReLU(),
nn.Linear(4, 1),
nn.Sigmoid()
)

loss_fn = nn.BCELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.1)

for epoch in range(2000):
y_pred = model(X)
loss = loss_fn(y_pred, y)

optimizer.zero_grad()
loss.backward()
optimizer.step()

if epoch % 200 == 0:
print(f"Epoch {epoch}, Loss: {loss.item()}")

📝 Practice Questions:

  1. PyTorch और TensorFlow में मुख्य अंतर क्या है?
  2. PyTorch में tensor कैसे बनाते हैं?
  3. Autograd का उपयोग gradient के लिए कैसे करते हैं?
  4. एक simple model class कैसे बनाते हैं?
  5. Sequential API और custom model class में क्या फर्क है?

🧠 Summary Table

ConceptExplanation
TensorPyTorch का data container (NumPy + GPU)
AutogradAutomatic differentiation
nn.ModuleNeural network architecture का base class
OptimizerParameters को update करता है
Loss FunctionModel की गलती measure करता है

TensorFlow and Keras Basics

अब जब आपने Deep Learning के theory और models (जैसे CNN, RNN, BERT) अच्छे से समझ लिए हैं —
तो अगला practical step है:
⚙️ TensorFlow और Keras के साथ Deep Learning models बनाना सीखना।


🔷 1. TensorFlow क्या है?

TensorFlow एक open-source deep learning framework है जिसे Google ने बनाया है।
यह numerical computation और large-scale machine learning models के लिए design किया गया है।

🧠 TensorFlow का नाम “Tensor” (data structure) + “Flow” (computation graph) से आया है।


✅ Key Features:

FeatureDetail
📊 Automatic DifferentiationGradient calculation
🧮 GPU/TPU Supportतेज़ computation
🧠 High-level + Low-level APIsFlexibility
🔧 DeploymentAndroid, Web, Edge devices
🤝 EcosystemTF Hub, TF Lite, TF.js, TF-Serving

🔷 2. Keras क्या है?

Keras एक high-level deep learning API है, जो TensorFlow के ऊपर चलता है।
यह models को लिखना, train करना और debug करना बहुत आसान बना देता है।

🎯 “Keras = Simplicity + Productivity + Modularity”


✅ Keras क्यों चुनें?

BenefitReason
🚀 Easy to LearnPythonic syntax
🧩 ModularLayers, Optimizers, Loss अलग-अलग
🧠 PowerfulAdvanced models possible
🔧 Fast prototypingजल्दी result देखने के लिए
🔌 TF BackendTensorFlow की ताकत use करता है

🔷 3. Tensor, Model, and Layer Basics

🔹 Tensor:

Multidimensional array (जैसे NumPy array, लेकिन GPU-compatible)

import tensorflow as tf
x = tf.constant([[1, 2], [3, 4]])
print(x.shape) # (2, 2)

🔹 Layer:

Neural network का एक building block (Dense, Conv2D, LSTM)

from tensorflow.keras.layers import Dense
dense = Dense(units=64, activation='relu')

🔹 Model:

Input से Output तक का पूरा network architecture

from tensorflow.keras.models import Sequential

model = Sequential([
Dense(128, activation='relu'),
Dense(10, activation='softmax')
])

🔷 4. Keras में Model बनाना (Step-by-Step)

✅ Step 1: Import

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

✅ Step 2: Model Define

model = Sequential([
Dense(64, activation='relu', input_shape=(100,)),
Dense(10, activation='softmax')
])

✅ Step 3: Compile

model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)

✅ Step 4: Train

model.fit(x_train, y_train, epochs=10, batch_size=32)

✅ Step 5: Evaluate

model.evaluate(x_test, y_test)

🧪 Example: Simple Binary Classifier

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

model = Sequential([
Dense(16, activation='relu', input_shape=(2,)),
Dense(1, activation='sigmoid')
])

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

model.fit(X_train, y_train, epochs=20)

🔧 Useful Layers in Keras

LayerUse
DenseFully connected layer
Conv2DImage convolution layer
LSTM / GRUSequence modeling
DropoutRegularization
FlattenInput flattening
EmbeddingWord embedding for NLP

🧠 Visualization: Model Summary

model.summary()

📝 Practice Questions:

  1. TensorFlow और Keras में क्या अंतर है?
  2. Sequential model क्या होता है?
  3. Model को compile करने में किन चीज़ों की ज़रूरत होती है?
  4. Dense layer क्या है?
  5. एक simple 3-layer model का कोड लिखिए।

🧠 Summary Table

ConceptDescription
TensorFlowGoogle का ML framework
KerasEasy high-level API
TensorMultidimensional data
LayerModel का हिस्सा (Dense, Conv)
ModelComplete NN architecture