शुरू करेंमुफ़्त में शुरू करें

एन्कोडर ट्रांसफॉर्मर लेयर

FeedForwardSubLayer क्लास परिभाषित होने के साथ, आपके पास EncoderLayer क्लास परिभाषित करने के लिए सभी पार्ट्स मौजूद हैं। याद करें कि एन्कोडर लेयर आमतौर पर एक multi-head attention मैकेनिज़्म और एक feed-forward सबलेयर से मिलकर बनती है, जिसमें सबलेयर के इनपुट और आउटपुट पर layer normalization और dropout होता है।

जो क्लास आपने पहले से परिभाषित की हैं, वे उन्हीं नामों के साथ उपलब्ध हैं, साथ ही torch और torch.nn nn के रूप में उपलब्ध हैं।

यह अभ्यास पाठ्यक्रम का हिस्सा है

PyTorch के साथ Transformer Models

पाठ्यक्रम देखें

अभ्यास निर्देश

  • __init__ मेथड पूरा करें ताकि MultiHeadAttention, FeedForwardSubLayer, और दो layer normalization के इंस्टांस बनाए जा सकें।
  • forward() मेथड पूरा करें: multi-head attention मैकेनिज़्म और feed-forward सबलेयर जोड़ें; attention मैकेनिज़्म के लिए दिए गए src_mark और query, key, तथा value मैट्रिस के लिए इनपुट एम्बेडिंग्स x का उपयोग करें।

इंटरैक्टिव व्यावहारिक अभ्यास

इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।

class EncoderLayer(nn.Module):
    def __init__(self, d_model, num_heads, d_ff, dropout):
        super().__init__()
        # Instantiate the layers
        self.self_attn = ____(d_model, num_heads)
        self.ff_sublayer = ____(d_model, d_ff)
        self.norm1 = ____
        self.norm2 = ____
        self.dropout = nn.Dropout(dropout)

    def forward(self, x, src_mask):
        # Complete the forward method
        attn_output = self.____
        x = self.norm1(x + self.dropout(attn_output))
        ff_output = self.____
        x = self.norm2(x + self.dropout(ff_output))
        return x
कोड संपादित करें और चलाएँ