Skip to content

Language models, retrieval, prompting, and language adaptation · Encoder–decoder and multilingual models · LNG023 · Model

BART

Pretrains a bidirectional encoder and autoregressive decoder to reconstruct corrupted documents. The combination supports summarization, generation, and classification, with denoising exposing the model to several forms…

Explained here Component demo, not a trained model CodeSplash's own run

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 here in one language.
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

Pretrains a bidirectional encoder and autoregressive decoder to reconstruct corrupted documents. The combination supports summarization, generation, and classification, with denoising exposing the model to several forms of missing or disordered information.

Question / objective

Can document denoising pretraining improve both language understanding and text generation?

Architecture / computational structure

A bidirectional encoder and causal decoder share a denoising task with cross-attention between them. Learned attention, feed-forward, embedding, and output weights are parameters; corruption scheme and architecture dimensions are hyperparameters.

Assumptions

Pretraining corruption is a designed learning task, not a generative model of real document damage.

Formula / computational definition

LBART=−Ex~∼q(x~∣x)log⁡pθ(x∣x~).\mathcal L_{\mathrm{BART}}=-\mathbb E_{\widetilde x\sim q(\widetilde x\mid x)}\log p_\theta(x\mid\widetilde x).

Draw corruption x_tilde~q(x_tilde|x) and minimize -log p_theta(x|x_tilde). Text infilling replaces spans with masks; sentence permutation is another corruption described in the paper. Reconstruction predicts original tokens sequentially.

Parameters and notation

Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Corruption distribution, span lengths, masked fraction, encoder/decoder depth, vocabulary, maximum input length, and fine-tuning target format.

Fitting, fine-tuning, and adaptation

For summarization, train paired documents/summaries with conditional cross-entropy. Freeze the encoder or use compatible LoRA/adapters when data or memory are limited; full fine-tuning is possible. Validate factuality and length alongside loss, and test general-domain generation after narrow-domain adaptation.

How to use and calibrate

Use held-out reconstruction loss, downstream summarization or generation scores, human factuality checks, and out-of-domain inputs. 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

Summarize long customer-service narratives into concise case notes.

Interpretation / pitfalls

Watch out

Summaries can remain fluent while inventing facts; strong reconstruction does not establish factual reliability. 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 supports BART conditional-generation classes and tokenizers. R generally calls the Python implementation via reticulate; native R can demonstrate corruption and cross-entropy but does not thereby reproduce pretrained weights.

Example scope: BART text-infilling target construction; only a corruption example, not its full noise mixture.

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.

Python

Component demo, not a trained model — BART text-infilling target construction; only a corruption example, not its full noise mixture.

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)
original = ["the","small","red","boat","arrived"]
corrupted = original[:1] + ["<mask>"] + original[4:]
print({"encoder": corrupted, "decoder_target": original})
R

Component demo, not a trained model — BART text-infilling target construction; only a corruption example, not its full noise mixture.

Shared setup for this chapter
softmax <- function(x) {
  shifted <- exp(x-max(x))
  shifted/sum(shifted)
}
original <- c("the","small","red","boat","arrived")
corrupted <- c(original[1], "<mask>", original[5])
print(list(encoder=corrupted, decoder_target=original))

Sources

From the CodeSplash reference, entry LNG023.