Complete the code to import the TransformerEncoder class from PyTorch.
from torch.nn import [1]
The TransformerEncoder class is imported from torch.nn to build transformer encoder layers.
Complete the code to create a TransformerEncoderLayer with 512 embedding size.
encoder_layer = torch.nn.TransformerEncoderLayer(d_model=[1], nhead=8)
d_model with number of heads.The d_model parameter sets the embedding size. For this task, it should be 512.
Fix the error in the code by completing the blank to create a TransformerEncoder with 6 layers.
transformer_encoder = torch.nn.TransformerEncoder(encoder_layer, num_layers=[1])num_layers with number of attention heads.The num_layers parameter sets how many encoder layers to stack. The correct value here is 6.
Fill both blanks to create a mask tensor of shape (10, 10) filled with -inf for masked positions.
mask = torch.full((10, 10), [1]) mask = mask.masked_fill(mask == [2], float('-inf'))
The mask is first filled with 1s, then positions equal to 1 are replaced with -inf to mask them.
Fill all three blanks to create a TransformerEncoderLayer, pass input through it, and get output shape.
encoder_layer = torch.nn.TransformerEncoderLayer(d_model=[1], nhead=[2]) input_tensor = torch.rand(5, 32, [3]) # (sequence_length, batch_size, embedding_dim) output = encoder_layer(input_tensor) output_shape = output.shape
The embedding size d_model and input embedding dimension must match (512). The number of heads is 8.
The input tensor shape is (sequence_length=5, batch_size=32, embedding_dim=512).