Transformer को समझना

PyTorch के साथ Transformer Models

James Chapman

Curriculum Manager, DataCamp

वह पेपर जिसने सब बदल दिया...

 

  • Ashish Vaswani व अन्य का Attention Is All You Need (arXiv:1706.03762)
    • Attention mechanisms
    • Text modeling के लिए ऑप्टिमाइज़्ड
    • Large Language Models (LLMs) में उपयोग

अकादमिक पेपर "Attention Is All You Need" में दिखाया गया Transformers आर्किटेक्चर.

PyTorch के साथ Transformer Models

वह पेपर जिसने सब बदल दिया...

 

  • Ashish Vaswani व अन्य का Attention Is All You Need
    • Attention mechanisms
    • Text modeling के लिए ऑप्टिमाइज़्ड
    • Large Language Models (LLMs) में उपयोग

अकादमिक पेपर "Attention Is All You Need" में दिखाया गया Transformers आर्किटेक्चर.

PyTorch के साथ Transformer Models

वह पेपर जिसने सब बदल दिया...

 

  • Ashish Vaswani व अन्य का Attention Is All You Need
    • Attention mechanisms
    • Text modeling के लिए ऑप्टिमाइज़्ड
    • Large Language Models (LLMs) में उपयोग

अकादमिक पेपर "Attention Is All You Need" में दिखाया गया Transformers आर्किटेक्चर.

PyTorch के साथ Transformer Models

Transformer को खोलकर समझना...

 

Encoder block
  • कई एक-जैसी लेयर्स
  • इनपुट सीक्वेंस को पढ़ना और प्रोसेस करना
  • context-rich संख्यात्मक रिप्रेजेंटेशंस बनाना
  • self-attention और feed-forward networks का उपयोग

अकादमिक पेपर "Attention Is All You Need" में दिखाया गया Transformers आर्किटेक्चर.

PyTorch के साथ Transformer Models

Transformer को खोलकर समझना...

 

Decoder block
  • Encoded इनपुट सीक्वेंस → आउटपुट सीक्वेंस

अकादमिक पेपर "Attention Is All You Need" में दिखाया गया Transformers आर्किटेक्चर.

PyTorch के साथ Transformer Models

Transformer को खोलकर समझना...

 

Positional encoding
  • हर token की सीक्वेंस में position एन्कोड करना
  • क्रम सीक्वेंस मॉडलिंग के लिए अहम है

अकादमिक पेपर "Attention Is All You Need" में दिखाया गया Transformers आर्किटेक्चर.

PyTorch के साथ Transformer Models

Transformer को खोलकर समझना...

Attention mechanisms
  • ज़रूरी tokens और उनके रिश्तों पर फोकस
  • Text generation बेहतर करें

अकादमिक पेपर "Attention Is All You Need" में दिखाया गया Transformers आर्किटेक्चर.

PyTorch के साथ Transformer Models

Transformer को खोलकर समझना...

Attention mechanisms
  • ज़रूरी tokens और उनके रिश्तों पर फोकस
  • Text generation बेहतर करें
Self-attention
  • token importance का weighting
  • Long-range dependencies को पकड़ता है

अकादमिक पेपर "Attention Is All You Need" में दिखाया गया Transformers आर्किटेक्चर.

PyTorch के साथ Transformer Models

Transformer को खोलकर समझना...

Attention mechanisms
  • ज़रूरी tokens और उनके रिश्तों पर फोकस
  • Text generation बेहतर करें
Self-attention
  • token importance का weighting
  • Long-range dependencies को पकड़ता है
Multi-head attention
  • इनपुट को कई heads में बाँटता है
  • हर head अलग पैटर्न पकड़ता है, जिससे richer रिप्रेजेंटेशन मिलते हैं

अकादमिक पेपर "Attention Is All You Need" में दिखाया गया Transformers आर्किटेक्चर.

PyTorch के साथ Transformer Models

Transformer को खोलकर समझना...

 

Position-wise feed-forward networks
  • सरल NNs जो transformations लगाते हैं
  • हर token स्वतंत्र रूप से transform होता है
  • Position-independent/"position-wise"

अकादमिक पेपर "Attention Is All You Need" में दिखाया गया Transformers आर्किटेक्चर.

PyTorch के साथ Transformer Models

PyTorch में Transformers

 

  • d_model: मॉडल इनपुट की dimensionality
  • nheads: attention heads की संख्या
  • num_encoder_layers: encoder लेयर्स की संख्या
  • num_decoder_layers: decoder लेयर्स की संख्या
import torch.nn as nn


model = nn.Transformer(
d_model=512,
nhead=8,
num_encoder_layers=6,
num_decoder_layers=6
)
print(model)
PyTorch के साथ Transformer Models
Transformer(
  (encoder): TransformerEncoder(
    (layers): ModuleList(
      (0-5): 6 x TransformerEncoderLayer(
        (self_attn): MultiheadAttention(
          (out_proj): NonDynamicallyQuantizableLinear(in_features=512, out_features=512, bias=True)
        )
        (linear1): Linear(in_features=512, out_features=2048, bias=True)
        (dropout): Dropout(p=0.1, inplace=False)
        (linear2): Linear(in_features=2048, out_features=512, bias=True)
        (norm1): LayerNorm((512,), eps=1e-05, elementwise_affine=True)
        (norm2): LayerNorm((512,), eps=1e-05, elementwise_affine=True)
        (dropout1): Dropout(p=0.1, inplace=False)
        (dropout2): Dropout(p=0.1, inplace=False)
      )
    )
    (norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)
  )
  ...
PyTorch के साथ Transformer Models
  (decoder): TransformerDecoder(
    (layers): ModuleList(
      (0-5): 6 x TransformerDecoderLayer(
        (self_attn): MultiheadAttention(
          (out_proj): NonDynamicallyQuantizableLinear(in_features=512, out_features=512, bias=True)
        )
        (multihead_attn): MultiheadAttention(
          (out_proj): NonDynamicallyQuantizableLinear(in_features=512, out_features=512, bias=True)
        )
        (linear1): Linear(in_features=512, out_features=2048, bias=True)
        (dropout): Dropout(p=0.1, inplace=False)
        (linear2): Linear(in_features=2048, out_features=512, bias=True)
        (norm1): LayerNorm((512,), eps=1e-05, elementwise_affine=True)
        (norm2): LayerNorm((512,), eps=1e-05, elementwise_affine=True)
        (norm3): LayerNorm((512,), eps=1e-05, elementwise_affine=True)
        (dropout1): Dropout(p=0.1, inplace=False)
        (dropout2): Dropout(p=0.1, inplace=False)
        (dropout3): Dropout(p=0.1, inplace=False)
      )
    )
    (norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)
  )
)
PyTorch के साथ Transformer Models

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

PyTorch के साथ Transformer Models

Preparing Video For Download...