model को train और save कर लिया है — अब बारी है उसे दुनिया के सामने पेश करने की 🎯 यानि model को API या Web App के ज़रिए serve करना।
🧩 दो मुख्य तरीकें:
तरीका
विवरण
उदाहरण
✅ API-Based
Model को backend में serve करें
Flask / FastAPI
✅ Web App
Model के ऊपर UI बनाएं
Streamlit / Gradio
🔶 1. ✅ Flask API से Model Serve करना (PyTorch Example)
🛠️ Step-by-step Code
📌 app.py:
from flask import Flask, request, jsonify import torch import torch.nn as nn
# Your trained model class 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))
# Load model model = MyNet() model.load_state_dict(torch.load("model_weights.pth")) model.eval()
# Create Flask app app = Flask(__name__)
@app.route('/predict', methods=['POST']) def predict(): data = request.get_json() input_tensor = torch.tensor(data["input"]).float() with torch.no_grad(): output = model(input_tensor).item() return jsonify({"prediction": round(output)})
अब हम Deep Learning में सबसे ज़रूरी और ज़्यादा इस्तेमाल होने वाले Evaluation Metrics — विशेषकर Confusion Matrix, Precision, और Recall — को पूरी गहराई से समझेंगे।
🔷 1. What Are Evaluation Metrics?
Evaluation metrics यह मापने के लिए होते हैं कि trained model कितनी सही predictions कर पा रहा है — खासकर classification problems में।
🔶 2. Confusion Matrix (गड़बड़ी मैट्रिक्स)
Confusion matrix classification prediction को चार हिस्सों में तोड़ता है:
Predicted Positive
Predicted Negative
Actual Positive (1)
✅ TP (True Positive)
❌ FN (False Negative)
Actual Negative (0)
❌ FP (False Positive)
✅ TN (True Negative)
🤖 ये matrix binary और multi-class दोनों में काम करता है।
अब जब आप PyTorch और Keras दोनों में model बना और train कर रहे हैं, तो performance boost के लिए GPU/TPU का इस्तेमाल करना ज़रूरी हो जाता है — खासकर बड़े datasets या deep models के लिए।
🔷 1. 🔥 Why Use GPUs?
CPU vs GPU
GPU Advantage
Sequential
Parallel computing
Few cores
Thousands of cores
Slower matrix ops
Faster tensor operations
General-purpose
Special 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)