Python / Python Deep Learning and Neural Networks Interview Questions
What is an encoder-decoder architecture and how is it used for sequence-to-sequence tasks?
Encoder-decoder (seq2seq) architectures handle tasks where the input and output are sequences of potentially different lengths — machine translation, summarisation, speech recognition, image captioning. The encoder processes the full input sequence and produces a context representation; the decoder generates the output sequence token by token, conditioning each prediction on the context and all previously generated tokens.
In transformer-based seq2seq, the encoder uses bidirectional self-attention (each position attends to all input positions), while the decoder uses two attention mechanisms: masked self-attention (each output position can only attend to previous output positions, preserving the autoregressive property) and cross-attention (each decoder position attends to all encoder output positions to draw relevant information from the input).
import torch
import torch.nn as nn
# PyTorch's built-in Transformer (encoder-decoder)
transformer = nn.Transformer(
d_model=512,
nhead=8,
num_encoder_layers=6,
num_decoder_layers=6,
dim_feedforward=2048,
dropout=0.1,
batch_first=True
)
# Source and target sequences
src = torch.randn(4, 20, 512) # (batch, src_len, d_model)
tgt = torch.randn(4, 15, 512) # (batch, tgt_len, d_model)
# Causal mask: prevent decoder from attending to future target tokens
tgt_len = tgt.size(1)
tgt_mask = nn.Transformer.generate_square_subsequent_mask(tgt_len)
out = transformer(src, tgt, tgt_mask=tgt_mask)
print(out.shape) # (4, 15, 512)
# Teacher forcing: at training time, feed ground-truth previous tokens
# to the decoder (not its own previous predictions)
# At inference: autoregressive — use model's own previous output:
def greedy_decode(model, src, max_len, sos_idx, eos_idx):
memory = model.encoder(src)
ys = torch.tensor([[sos_idx]])
for _ in range(max_len):
mask = nn.Transformer.generate_square_subsequent_mask(ys.size(1))
out = model.decoder(ys.float(), memory, tgt_mask=mask)
next_token = out[:, -1].argmax()
ys = torch.cat([ys, next_token.unsqueeze(0).unsqueeze(0)], dim=1)
if next_token.item() == eos_idx: break
return ys
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
