Language models, retrieval, prompting, and language adaptation · Reasoning, decoding, and discrete diffusion language · LNG077 · Algorithm
Beam search with length and coverage control
Maintains several high-scoring partial output sequences and expands them to approximate a high-probability translation or generation.
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
Maintains several high-scoring partial output sequences and expands them to approximate a high-probability translation or generation. Length normalization and coverage penalties address biases toward short outputs or incomplete source coverage.
Question / objective
How can approximate sequence search balance model likelihood, length, and source coverage?
Architecture / computational structure
A finite beam stores partial hypotheses and expands them using model probabilities; length/coverage scoring ranks candidates. Model weights remain fixed during search, while beam width and penalties are algorithm controls.
Assumptions
Search optimization does not correct a miscalibrated model; wider beams can produce worse task quality.
Formula / computational definition
Score(y)=log p(y|x)/lp(|y|)+cp(x,y), with a specified length penalty lp and coverage penalty cp. GNMT uses a particular length normalization and attention-based coverage form; these definitions must be stated rather than assuming all beam APIs match.
Parameters and notation
Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Beam width, length-penalty exponent, coverage weight, maximum/minimum length, end-token handling, and stopping heuristic.
Fitting, fine-tuning, and adaptation
No weight fitting is required for beam search itself. Tune penalties and stopping rules on development references, then evaluate unchanged settings. If the generator is fine-tuned, reassess decoding because length bias may shift. Compare task quality and omissions, not merely normalized search scores.
How to use and calibrate
Compare task quality, output length, omissions, hallucinations, runtime, and beam-width curves. 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 a source sentence while discouraging premature end-of-sequence outputs.
Interpretation / pitfalls
Watch out
Coverage from attention weights is an approximation and not proof that every source fact was preserved. 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: Supported generation libraries provide beam and length controls, but coverage penalties may require custom code. R bridges Python or implements search natively. The example ranks completed hypotheses using an explicitly defined length penalty and does not claim to perform the whole search.
Example scope: GNMT-style length-normalized completed-hypothesis scores; no coverage term or full beam expansion.
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 — GNMT-style length-normalized completed-hypothesis scores; no coverage term or full beam expansion.
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)
log_probabilities,lengths,alpha = [-3.0,-4.0],[3,6],0.6
penalties = [((5+length)/6)**alpha for length in lengths]
scores = [lp/penalty for lp,penalty in zip(log_probabilities,penalties)]
print(scores)
Component demo, not a trained model — GNMT-style length-normalized completed-hypothesis scores; no coverage term or full beam expansion.
Shared setup for this chapter
softmax <- function(x) {
shifted <- exp(x-max(x))
shifted/sum(shifted)
}
log_probabilities <- c(-3,-4); lengths <- c(3,6); alpha <- 0.6
penalties <- ((5+lengths)/6)^alpha
scores <- log_probabilities/penalties
print(scores)
Sources
- Source 1arxiv.org
From the CodeSplash reference, entry LNG077.