PyTorch और ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग

इंटरमीडिएट Deep Learning with PyTorch

Michal Oleszak

Machine Learning Engineer

हम क्या सीखेंगे

मजबूत डीप लर्निंग मॉडल कैसे ट्रेन करें:

  • Optimizers से ट्रेनिंग बेहतर करें
  • Vanishing/Exploding gradients घटाएँ
  • Convolutional Neural Networks (CNNs)
  • Recurrent Neural Networks (RNNs)
  • Multi-input और multi-output मॉडल्स

 

 

PyTorch लोगो

इंटरमीडिएट Deep Learning with PyTorch

Prerequisites

कोर्स यह मानता है कि आप इन विषयों में सहज हैं:

  • Neural networks training:

    • Forward pass
    • Loss calculation
    • Backward pass (backpropagation)
  • PyTorch से मॉडल ट्रेनिंग:

    • Datasets और DataLoaders
    • Model training loop
    • Model evaluation
  • पूर्व-आवश्यक कोर्स: Introduction to Deep Learning with PyTorch

इंटरमीडिएट Deep Learning with PyTorch

Object-Oriented Programming (OOP)

  • हम OOP से इन्हें परिभाषित करेंगे:

    • PyTorch Datasets
    • PyTorch Models
  • OOP में, हम ऑब्जेक्ट बनाते हैं जिनमें:

    • क्षमताएँ (methods)
    • डेटा (attributes)
इंटरमीडिएट Deep Learning with PyTorch

Object-Oriented Programming (OOP)

class BankAccount:
    def __init__(self, balance):
        self.balance = balance
  • __init__ तब कॉल होता है जब BankAccount ऑब्जेक्ट बनता है
  • balance BankAccount ऑब्जेक्ट का attribute है
account = BankAccount(100)
print(account.balance)
100
इंटरमीडिएट Deep Learning with PyTorch

Object-Oriented Programming (OOP)

  • Methods: काम करने के लिए Python functions
  • deposit method बैलेंस बढ़ाता है
class BankAccount:
    def __init__(self, balance):
        self.balance = balance


def deposit(self, amount): self.balance += amount
account = BankAccount(100)
account.deposit(50)
print(account.balance)
150
इंटरमीडिएट Deep Learning with PyTorch

Water potability डेटासेट

Pandas DataFrame जिसमें water potability डेटा की शुरुआती और आख़िरी कुछ पंक्तियाँ दिख रही हैं.

इंटरमीडिएट Deep Learning with PyTorch

PyTorch Dataset

from torch.utils.data import Dataset

class WaterDataset(Dataset):

def __init__(self, csv_path): super().__init__() df = pd.read_csv(csv_path) self.data = df.to_numpy()
def __len__(self): return self.data.shape[0]
def __getitem__(self, idx): features = self.data[idx, :-1] label = self.data[idx, -1] return features, label
  • init: डेटा लोड करें, numpy array में स्टोर करें
    • super().__init__() सुनिश्चित करता है कि WaterDataset torch Dataset जैसा व्यवहार करे
  • len: डेटासेट का आकार लौटाएँ
  • getitem:
    • एक आर्ग्युमेंट idx लेता है
    • इंडेक्स idx पर सिंगल sample के features और label लौटाता है
इंटरमीडिएट Deep Learning with PyTorch

PyTorch DataLoader

dataset_train = WaterDataset(
    "water_train.csv"
)
from torch.utils.data import DataLoader

dataloader_train = DataLoader(
    dataset_train,
    batch_size=2,
    shuffle=True,
)
features, labels = next(iter(dataloader_train))
print(f"Features: {features},\nLabels: {labels}")
Features: tensor([
  [0.4899, 0.4180, 0.6299, 0.3496, 0.4575,
   0.3615, 0.3259, 0.5011, 0.7545],
  [0.7953, 0.6305, 0.4480, 0.6549, 0.7813,
   0.6566, 0.6340, 0.5493, 0.5789]
]),
Labels: tensor([1., 0.])
इंटरमीडिएट Deep Learning with PyTorch

PyTorch मॉडल

Sequential मॉडल परिभाषा:

net = nn.Sequential(
  nn.Linear(9, 16),
  nn.ReLU(),
  nn.Linear(16, 8),
  nn.ReLU(),
  nn.Linear(8, 1),
  nn.Sigmoid(),
)

क्लास-आधारित मॉडल परिभाषा:

class Net(nn.Module):

def __init__(self): super().__init__() self.fc1 = nn.Linear(9, 16) self.fc2 = nn.Linear(16, 8) self.fc3 = nn.Linear(8, 1)
def forward(self, x): x = nn.functional.relu(self.fc1(x)) x = nn.functional.relu(self.fc2(x)) x = nn.functional.sigmoid(self.fc3(x)) return x net = Net()
इंटरमीडिएट Deep Learning with PyTorch

अभ्यास करते हैं!

इंटरमीडिएट Deep Learning with PyTorch

Preparing Video For Download...