एन्कोडर ट्रांसफॉर्मर बॉडी
आपका केवल-एन्कोडर ट्रांसफॉर्मर बॉडी लगभग तैयार है! अब समय है कि आप पहले बनाई गई InputEmbeddings, PositionalEncoding, और EncoderLayer क्लासेस को मिलाकर एक TransformerEncoder क्लास बनाएँ.
यह अभ्यास पाठ्यक्रम का हिस्सा है
PyTorch के साथ Transformer Models
अभ्यास निर्देश
- टोकन एम्बेडिंग, पोज़िशनल एनकोडिंग, और एन्कोडर लेयर्स परिभाषित करें (लिस्ट कम्प्रिहेंशन का उपयोग करके
num_layersएन्कोडर लेयर्स बनाएँ). - इन लेयर्स के जरिए फॉरवर्ड पास करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
class TransformerEncoder(nn.Module):
def __init__(self, vocab_size, d_model, num_layers, num_heads, d_ff, dropout, max_seq_length):
super().__init__()
# Define the embedding, positional encoding, and encoder layers
self.embedding = ____(vocab_size, d_model)
self.positional_encoding = ____(d_model, max_seq_length)
self.layers = nn.____([____(d_model, num_heads, d_ff, dropout) for _ in range(num_layers)])
def forward(self, x, src_mask):
# Perform the forward pass through the layers
x = self.____(x)
x = self.____(x)
for layer in ____:
x = layer(x, src_mask)
return x