Language models, retrieval, prompting, and language adaptation · Contextual and sentence encoders · LNG008 · Model
ELMo
Produces context-sensitive word representations from a bidirectional recurrent language model. Unlike static vectors, the same word receives different features in different sentences, making it a useful historical and…
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
Produces context-sensitive word representations from a bidirectional recurrent language model. Unlike static vectors, the same word receives different features in different sentences, making it a useful historical and conceptual bridge to contextual transformers.
Question / objective
How can a word's representation change with its surrounding linguistic context?
Architecture / computational structure
A character-level word representation feeds forward and backward language models; task embeddings are learned scalar mixtures of contextual layers. Learned weights include recurrent networks and mixture parameters; layer widths and mixture regularization are hyperparameters.
Assumptions
Bidirectionality is appropriate for observed-input encoding, not unrestricted left-to-right prediction.
Formula / computational definition
Train forward and backward language-model log likelihoods separately. A downstream representation is gamma sum_l s_l h_{t,l}, where h_{t,l} are layer features, s_l normalized task-learned mixing weights, and gamma a learned scale.
Parameters and notation
Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Character encoder, recurrent depth/width, layer selection or mixing, dropout, and whether pretrained weights are frozen.
Fitting, fine-tuning, and adaptation
Pretrain bidirectional language modeling on text or use a compatible checkpoint. For adaptation, first learn task-layer mixtures and a downstream head with the encoder frozen. Unfreeze selectively only with adequate data and validate domain shift, sequence tagging quality, and forgetting.
How to use and calibrate
Test downstream tagging, classification, and sense-sensitive similarity against static baselines; avoid evaluating a bidirectional representation as an ordinary causal generator. 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
Improve named-entity recognition when a static vector conflates different senses of a word.
Interpretation / pitfalls
Watch out
Recurrent computation limits parallelism; library and artifact availability must be checked for legacy reproduction. 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: Use released ELMo artifacts with compatible original tooling, or implement the bidirectional language model explicitly. R commonly requires reticulate or a custom R torch implementation; legacy dependency compatibility is a real planning constraint.
Example scope: ELMo-style learned scalar mixing of supplied contextual layer vectors.
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 — ELMo-style learned scalar mixing of supplied contextual layer vectors.
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)
layers = [[1, 0], [0, 1], [0.5, 0.5]]
weights, gamma = softmax([0.0, 0.5, -0.2]), 1.2
mixed = [gamma * sum(w * h[j] for w, h in zip(weights, layers)) for j in range(2)]
print(weights, mixed)
Component demo, not a trained model — ELMo-style learned scalar mixing of supplied contextual layer vectors.
Shared setup for this chapter
softmax <- function(x) {
shifted <- exp(x-max(x))
shifted/sum(shifted)
}
layers <- rbind(c(1,0), c(0,1), c(0.5,0.5))
weights <- softmax(c(0,0.5,-0.2)); gamma <- 1.2
mixed <- gamma * colSums(layers * weights)
print(list(weights=weights, embedding=mixed))
Sources
- Source 1arxiv.org
From the CodeSplash reference, entry LNG008.