डेरिवेटिव से मॉडल पैरामीटर अपडेट करना

PyTorch के साथ Deep Learning परिचय

Jasmin Ludolf

Senior Data Science Content Developer, DataCamp

डेरिवेटिव का एक उदाहरण

$$

डेरिवेटिव कर्व का slope दर्शाता है

$$

  • तेज़ ढलान (लाल तीर):
    • बड़े स्टेप, डेरिवेटिव high
  • हल्की ढलान (हरे तीर):
    • छोटे स्टेप, डेरिवेटिव low
  • घाटी का तल (नीला तीर):
    • समतल, डेरिवेटिव zero

$$

घाटी की एक छवि

PyTorch के साथ Deep Learning परिचय

Convex और non-convex functions

यह एक convex function है

convex function का उदाहरण

यह एक non-convex function है

global minimum हाइलाइट किया हुआ non-convex function का उदाहरण

PyTorch के साथ Deep Learning परिचय

डेरिवेटिव और मॉडल ट्रेनिंग को जोड़ना

  • ट्रेनिंग के forward pass में loss compute करें

$$ लॉस की गणना

PyTorch के साथ Deep Learning परिचय

डेरिवेटिव और मॉडल ट्रेनिंग को जोड़ना

  • Gradients loss कम करने में मदद करते हैं, लेयर के weights और biases ट्यून करते हैं
  • तब तक दोहराएँ जब तक लेयर्स tuned न हों

$$ ग्रेडिएंट्स निकालना

PyTorch के साथ Deep Learning परिचय

बैकप्रोपेगेशन के सिद्धांत

$$

  • तीन लेयर वाले नेटवर्क पर विचार करें:

    • $L2$ के लिए loss gradients से शुरू करें
    • $L2$ से $L1$ के gradients compute करें
    • सभी लेयर्स के लिए दोहराएँ ($L1$, $L0$)

बैकप्रोपेगेशन डायग्राम

PyTorch के साथ Deep Learning परिचय

PyTorch में बैकप्रोपेगेशन

# Run a forward pass 
model = nn.Sequential(nn.Linear(16, 8),
                      nn.Linear(8, 4),
                      nn.Linear(4, 2))
prediction = model(sample)


# Calculate the loss and gradients criterion = CrossEntropyLoss() loss = criterion(prediction, target) loss.backward()
# Access each layer's gradients
model[0].weight.grad
model[0].bias.grad
model[1].weight.grad
model[1].bias.grad
model[2].weight.grad
model[2].bias.grad
PyTorch के साथ Deep Learning परिचय

मॉडल पैरामीटर मैन्युअली अपडेट करना

# Learning rate is typically small
lr = 0.001

# Update the weights
weight = model[0].weight
weight_grad = model[0].weight.grad


weight = weight - lr * weight_grad
# Update the biases bias = model[0].bias bias_grad = model[0].bias.grad
bias = bias - lr * bias_grad

$$

  • हर लेयर का gradient देखें
  • उसे learning rate से गुणा करें
  • इस product को weight से घटाएँ
PyTorch के साथ Deep Learning परिचय

Gradient descent

  • Non-convex functions के लिए हम gradient descent उपयोग करेंगे

  • PyTorch इसे optimizers से सरल बनाता है

    • Stochastic gradient descent (SGD)
import torch.optim as optim

# Create the optimizer
optimizer = optim.SGD(model.parameters(), lr=0.001)

# Perform parameter updates optimizer.step()
PyTorch के साथ Deep Learning परिचय

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

PyTorch के साथ Deep Learning परिचय

Preparing Video For Download...