RNNs को ट्रेन करना और मूल्यांकन

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

Michal Oleszak

Machine Learning Engineer

Mean Squared Error Loss

  • Error:

    $$prediction - target$$

  • Squared Error:

    $$(prediction - target)^2$$

  • Mean Squared Error:

    $$avg[(prediction - target)^2]$$

त्रुटि को स्क्वेयर करना:

  • सकारात्मक और नकारात्मक त्रुटियाँ आपस में निष्प्रभावी नहीं होंगी
  • बड़ी त्रुटियों पर अधिक दंड लगता है
  • PyTorch में:
      criterion = nn.MSELoss()
    
इंटरमीडिएट Deep Learning with PyTorch

Tensors को expand करना

  • Recurrent लेयर्स इनपुट शेप (batch_size, seq_length, num_features) चाहती हैं
  • हमारे पास (batch_size, seq_length) था
  • अंत में एक डाइमेंशन जोड़ना होगा
for seqs, labels in dataloader_train:
    print(seqs.shape)
torch.Size([32, 96])
seqs = seqs.view(32, 96, 1)
print(seqs.shape)
torch.Size([32, 96, 1])
इंटरमीडिएट Deep Learning with PyTorch

Tensors को squeeze करना

  • Evaluation लूप में, training लूप में की गई reshaping वापस करनी होगी
  • Labels का शेप (batch_size) है

    for seqs, labels in test_loader:
      print(labels.shape)
    
    torch.Size([32])
    
  • Model outputs (batch_size, 1) होते हैं

    out = net(seqs)
    
    torch.Size([32, 1])
    
  • Loss फंक्शन के लिए outputs और labels के शेप मैच होने चाहिए
  • हम model outputs से आखिरी डाइमेंशन हटा सकते हैं

    out = net(seqs).squeeze()
    
    torch.Size([32])
    
इंटरमीडिएट Deep Learning with PyTorch

Training लूप

net = Net()
criterion = nn.MSELoss()
optimizer = optim.Adam(
  net.parameters(), lr=0.001
)


for epoch in range(num_epochs): for seqs, labels in dataloader_train:
seqs = seqs.view(32, 96, 1)
outputs = net(seqs) loss = criterion(outputs, labels) optimizer.zero_grad() loss.backward() optimizer.step()
  • मॉडल बनाइए, loss और optimizer तय करें
  • Epochs और डेटा बैचों पर iterate करें
  • इनपुट sequence को reshape करें
  • बाकी: सामान्य जैसा
इंटरमीडिएट Deep Learning with PyTorch

Evaluation लूप

mse = torchmetrics.MeanSquaredError()


net.eval() with torch.no_grad(): for seqs, labels in test_loader:
seqs = seqs.view(32, 96, 1)
outputs = net(seqs).squeeze()
mse(outputs, labels)
print(f"Test MSE: {mse.compute()}")
Test MSE: 0.13292162120342255
  • MSE metric सेट करें
  • Gradients के बिना टेस्ट डेटा पर iterate करें
  • मॉडल इनपुट को reshape करें
  • मॉडल outputs को squeeze करें
  • Metric अपडेट करें
  • अंतिम metric मान compute करें
इंटरमीडिएट Deep Learning with PyTorch

LSTM बनाम GRU

  • LSTM:
Test MSE: 0.13292162120342255
  • GRU:
Test MSE: 0.12187089771032333
  • GRU बेहतर पसंद: समान या बेहतर नतीजे, कम प्रोसेसिंग पावर में
इंटरमीडिएट Deep Learning with PyTorch

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

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

Preparing Video For Download...