Language models, retrieval, prompting, and language adaptation · Contextual and sentence encoders · LNG014 · Model
XLNet
Uses permutation-based autoregressive pretraining to learn bidirectional dependencies without inserting ordinary masked-token placeholders.
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
Uses permutation-based autoregressive pretraining to learn bidirectional dependencies without inserting ordinary masked-token placeholders. Its two-stream attention prevents target content from leaking into the representation used to predict that target.
Question / objective
Can permutation-based autoregressive objectives learn bidirectional dependencies without masking tokens?
Architecture / computational structure
A permutation language-model objective uses a Transformer-based system with two-stream attention to avoid revealing the predicted token. Learned network weights are parameters; permutation sampling and context/memory settings are hyperparameters.
Assumptions
Permuting factorization order is not shuffling the semantic input text.
Formula / computational definition
Maximize E_z sum_t log p_theta(x_{z_t}|x_{z_<t}), where z is a sampled factorization order over token positions. Query-stream features encode position/context while content-stream features contain observed token information.
Parameters and notation
Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Permutation sampling, prediction fraction, segment recurrence, memory length, attention dimensions, and fine-tuning head.
Fitting, fine-tuning, and adaptation
Pretraining requires the paper's masking and two-stream design; do not emulate it by randomly shuffling visible text alone. Fine-tune task heads, adapters, or full weights with consistent tokenization. Validate task performance and long-context handling, including memory boundary resets.
How to use and calibrate
Use held-out downstream tasks and explicitly test mask correctness, long-context behavior, and computational expense. 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
Compare an alternative encoder pretraining objective on extractive question answering.
Interpretation / pitfalls
Watch out
Implementation errors in attention masks cause leakage; the training procedure is more complex than an ordinary masked encoder. 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: Transformers provides XLNet model classes and tokenizer handling. R can use reticulate with the same model; reproducing pretraining natively requires careful two-stream attention and sampled-order masking rather than a generic encoder.
Example scope: XLNet permutation-order visibility; toy query mask excludes the predicted position.
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 — XLNet permutation-order visibility; toy query mask excludes the predicted position.
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)
order = [2, 0, 1]
rank = {position: step for step, position in enumerate(order)}
mask = [[int(rank[j] < rank[i]) for j in range(3)] for i in range(3)]
print(mask)
Component demo, not a trained model — XLNet permutation-order visibility; toy query mask excludes the predicted position.
Shared setup for this chapter
softmax <- function(x) {
shifted <- exp(x-max(x))
shifted/sum(shifted)
}
order <- c(3L,1L,2L)
rank <- match(seq_len(3), order)
mask <- outer(rank, rank, function(ri,rj) as.integer(rj < ri))
print(mask)
Sources
- Source 1arxiv.org
From the CodeSplash reference, entry LNG014.