Language models, retrieval, prompting, and language adaptation · Encoder–decoder and multilingual models · LNG021 · Architecture
Sequence-to-sequence LSTM
Maps variable-length input sequences to variable-length outputs using recurrent encoder and decoder networks. Machine translation made the approach influential, and it remains a clear foundation for understanding…
In CodeSplash
Documented in the Academy only. There's no Set up button.
- Availability
- Explained here
- Code example
- Component demo, not a trained model
- Who ran it
- CodeSplash's own run
- CodeSplash's run
- The reference's example ran in Python and R here, and they agree.
- The reference's record
- Python: The chapter's authors report running it; there is no record for this entry alone. R: Reviewed but not run: R wasn't available to the reference's authors.
Purpose
Maps variable-length input sequences to variable-length outputs using recurrent encoder and decoder networks. Machine translation made the approach influential, and it remains a clear foundation for understanding conditional generation before attention.
Question / objective
How can a fixed recurrent state support variable-length conditional text generation?
Architecture / computational structure
An LSTM encoder produces terminal states for an LSTM decoder and vocabulary head. Learned embeddings, recurrent gates, and output projections are parameters; layer widths, depth, and beam size are hyperparameters.
Assumptions
A single fixed-width state bottlenecks long inputs.
Formula / computational definition
An encoder summarizes x as state c; a decoder factorizes p(y|x)=product_t p(y_t|y_<t,c). LSTM gates update cell and hidden states; training minimizes summed target-token negative log likelihood under teacher forcing.
Parameters and notation
Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Vocabulary, embedding width, recurrent depth, hidden-state width, dropout, input ordering, decoding beam, and maximum target length.
Fitting, fine-tuning, and adaptation
Fit source/target pairs with teacher-forced conditional cross-entropy and padding-aware masks. Adapt by freezing embeddings/encoder initially or fine-tuning all recurrent weights at a lower rate. Use task-specific validation, length strata, and mixed-domain data to monitor forgetting; standard Transformer LoRA recipes do not transfer automatically.
How to use and calibrate
Use held-out conditional loss plus task-specific quality, length-stratified performance, and generation latency. For translation include human error analysis rather than only aggregate overlap scores. Use development data for settings and an independent test set for the final estimate. Report uncertainty across examples or seeds when the evaluation design supports it; model scores and self-assessments are not automatically calibrated probabilities of correctness.
Example application
Translate short structured phrases between two languages with an interpretable recurrent baseline.
Interpretation / pitfalls
Watch out
Teacher forcing exposes the decoder to gold prefixes during training but its own errors during generation; sequence length and rare vocabulary challenge generalization. The code below isolates one stated component; successful execution does not validate model quality or reproduce the cited experiment.
Python and R code examples
Full implementation route: PyTorch and R torch provide LSTM primitives; implement both networks, masks, and teacher-forcing shifts explicitly. A probability-factorization demo below illustrates the objective, not trained translation.
Example scope: Teacher-forced sequence-to-sequence conditional likelihood from supplied target probabilities.
Dependencies: Python 3 standard library; base R; shared setup above. No model downloads or external services.
Validation: Python component executed in the combined example script; R counterpart supplied but not runtime-tested here. All numbers are synthetic.
Python and R
The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.
Component demo, not a trained model — Teacher-forced sequence-to-sequence conditional likelihood from supplied target probabilities.
Shared setup for this chapter
import math
def softmax(xs):
m = max(xs)
es = [math.exp(x-m) for x in xs]
return [e/sum(es) for e in es]
def dot(a, b):
return sum(x*y for x, y in zip(a, b))
def matvec(matrix, vector):
return [dot(row, vector) for row in matrix]
def meanvec(rows):
return [sum(row[j] for row in rows)/len(rows) for j in range(len(rows[0]))]
def sigmoid(x):
return 1/(1+math.exp(-x))
def nll(probabilities):
return -sum(math.log(p) for p in probabilities)
p_gold = [0.7,0.6,0.8]
print({"sequence_probability": math.prod(p_gold), "token_nll": nll(p_gold)/3})
Component demo, not a trained model — Teacher-forced sequence-to-sequence conditional likelihood from supplied target probabilities.
Shared setup for this chapter
softmax <- function(x) {
shifted <- exp(x-max(x))
shifted/sum(shifted)
}
p_gold <- c(0.7,0.6,0.8)
print(c(sequence_probability=prod(p_gold), token_nll=-mean(log(p_gold))))
Sources
- Source 1arxiv.org
From the CodeSplash reference, entry LNG021.