डिकोडर लेयर
एन्कोडर ट्रांसफॉर्मर्स की तरह, डिकोडर ट्रांसफॉर्मर्स भी कई लेयर्स से बने होते हैं जिनमें multi-head attention और feed-forward सबलेयर्स का उपयोग होता है. इन कंपोनेंट्स को जोड़कर एक DecoderLayer क्लास बनाने की कोशिश कीजिए.
MultiHeadAttention और FeedForwardSubLayer क्लास आपके उपयोग के लिए उपलब्ध हैं, साथ ही वह tgt_mask भी जो आपने बनाया था.
यह अभ्यास पाठ्यक्रम का हिस्सा है
PyTorch के साथ Transformer Models
अभ्यास निर्देश
forward() मेथड को पूरा कीजिए ताकि __init__ मेथड में परिभाषित लेयर्स से इनपुट embeddings पास हों:
- दिए गए
tgt_maskऔर इनपुट embeddingsxका उपयोग करते हुए query, key, और value मैट्रिसेज़ के लिए attention कैलकुलेशन करें. dropoutऔर पहली layer normalization,norm1, लागू करें.- feed-forward सबलेयर,
ff_sublayer, से पास कराएँ. dropoutऔर दूसरी layer normalization,norm2, लागू करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
class DecoderLayer(nn.Module):
def __init__(self, d_model, num_heads, d_ff, dropout):
super().__init__()
self.self_attn = MultiHeadAttention(d_model, num_heads)
self.ff_sublayer = FeedForwardSubLayer(d_model, d_ff)
self.norm1 = nn.LayerNorm(d_model)
self.norm2 = nn.LayerNorm(d_model)
self.dropout = nn.Dropout(dropout)
def forward(self, x, tgt_mask):
# Perform the attention calculation
attn_output = self.____
# Apply dropout and the first layer normalization
x = self.____(x + self.____(attn_output))
# Pass through the feed-forward sublayer
ff_output = self.____(x)
# Apply dropout and the second layer normalization
x = self.____(x + self.____(ff_output))
return x