Search

Search papers and lessons

Nougat: Neural Optical Understanding for Academic Documents

Lukas Blecher, Guillem Cucurull, Thomas Scialom et al. · 2023

Nougat reads a scanned or PDF research page as an image and writes it out as markup, keeping the equations that plain PDF text loses.

On this page

  • Colour = topic mix
  • Architectures
  • Discrete math
  • Neural network basics
  • Calculus
  • Probability
  • Linear algebra
  • ReadReady to readNot yet

TL;DR

Nougat takes an image of one page of a scientific paper and writes out the page as lightweight markup, with headings, tables and LaTeX for the equations. It does this in one neural network, without a separate character recognizer, so it can read scanned pages as well as digital PDFs. On the paper's arXiv test set its output is much closer to the true source than the text embedded in the PDF.

Builds on: Optical Character Recognition, Donut, Data Augmentation · Leads to: Vision Transformer (ViT) models for document understanding

Why it mattered

Most of the world's scientific knowledge is published as PDFs, and a PDF records where the characters are on the page, not what they mean. A fraction written as ab\frac{a}{b} is stored as small characters at fixed positions, so a search engine or a language model reading the text sees a jumble. Nougat showed that a single model could recover the structure of a research page, including its equations, and output it in a form that software can search and reuse.

The paper released the model and its code on GitHub, so other people could convert their own papers without building the system themselves. It also described how to build a large dataset of matched page images and source text from arXiv, which is the part that made training possible.

The problem

Optical character recognition (OCR) is the classic way to turn an image into text. Engines such as Tesseract recognise characters and words from lines of text. A superscript or a subscript sits off the line, and in a mathematical expression that position is part of the meaning. A line-by-line reading can turn x2x^2 into "x2" and lose the power of the exponent.

Tools that read digital PDFs have their own gaps. GROBID parses a paper into XML with a focus on bibliographic data, and pdf2htmlEX keeps the layout, but neither recovers the meaning of an equation. The paper's own comparison shows the consequence. The text embedded in the PDF has a normalised edit distance of 0.255 on the arXiv test set, and the GROBID pipeline is worse, at 0.312.

Nougat's task is to go from the page image directly to markup, so the equations are written as LaTeX, and the tables and headings are written as markup, too.

How it works

Follow one page through the system. The steps below describe the model at inference time.

  1. Render the page. The page is rasterised at 96 DPI. The paper picks an input size of 896 by 672 pixels, which fits the aspect ratio between US Letter and A4 and the input shape the Swin Transformer encoder needs. The image is resized and padded to that size. Check: the input is a fixed 896 by 672 grid of pixels, whatever the original page size.
  2. Encode the image. A Swin Transformer, a vision transformer that works on shifted windows of patches, turns the page into a set of latent embeddings. The encoder starts from pre-trained weights, so it does not learn to see the page from scratch. Check: the output is a set of vectors that summarise where the text, equations and tables are.
  3. Decode one token at a time. A Transformer decoder with cross-attention reads the embeddings and writes markup tokens one by one. At each step it attends to the image through cross-attention and to the tokens it has already written through self-attention. Its output is a score for every word piece in the vocabulary. Check: each new token depends on the image and on the tokens before it.
  4. Choose the next token. At inference the model picks the highest-scoring token, which is greedy decoding. The output grows until the model writes an end token or reaches the length limit. Check: a sequence of 4096 tokens is the most the decoder can write for one page.
  5. Clean up. The markup is the output. Because the model was trained on one page at a time, it can make small inconsistencies across pages, which the paper leaves as future work.

A tiny trace on one formula, with invented tokens for illustration: the image shows x2x^2. After step 2 the encoder has a vector for the superscript position. Step 3 writes the token for "x", then the token for "^", then the token for "2" with the superscript structure, and step 4 chooses each highest-scoring token in turn. The output "x2x^2" is correct because the position of the 2 was kept in the encoder, not inferred from a left-to-right line of text.

flowchart LR
  A[Page image 96 DPI] --> B[Resize and pad to 896 by 672]
  B --> C[Swin Transformer encoder]
  C --> D[Transformer decoder, token by token]
  D --> E[Greedy choice of each token]
  E --> F[Markup with LaTeX math]

How the training pairs are built

The dataset is the other half of the method. The authors took 1,748,201 arXiv articles, compiled the LaTeX source to HTML with LaTeXML, and converted the HTML to markup. To get one image per page they had to match source text to the page breaks of the compiled PDF. They did this with a Bag of Words classifier on the PDF lines, a boundary search over paragraphs, and fuzzy matching of the sentences at each break. A page enters the dataset only if its page-break score averages at least 0.9, which kept about 47 percent of pages.

Here is that acceptance rule on a tiny case. Suppose the two candidate page breaks disagree by a normalised distance of 0.05. Each break then scores 1−0.05=0.951 - 0.05 = 0.95, and the page passes, because 0.95 is above 0.9. If the disagreement were 0.2, the score would be 0.8 and the page would be dropped.

The math

The decoder in symbols

Write the encoder output as z\mathbf{z} and the markup as tokens y1,…,yTy_1, \dots, y_T. The decoder models the probability of the whole markup as a product of next-token probabilities, one per step, the same autoregressive form as in autoregressive language models:

p(y1,…,yT∣z)=∏t=1Tp(yt∣y1,…,yt−1,z).p(y_1, \dots, y_T \mid \mathbf{z}) = \prod_{t=1}^{T} p(y_t \mid y_1, \dots, y_{t-1}, \mathbf{z}).

At each step the decoder produces logits ℓt∈Rv\boldsymbol{\ell}_t \in \mathbb{R}^{v}, one per vocabulary item vv, and a Softmax turns them into probabilities. Greedy decoding then chooses yt=arg⁡max⁡jℓt,jy_t = \arg\max_j \ell_{t,j}.

Training in symbols

The model is trained to maximise the probability of the true markup, which is the same as minimising the cross-entropy between the predicted and true tokens. The paper uses AdamW for 3 epochs with an effective batch size of 192. The learning rate starts at 5⋅10−55 \cdot 10^{-5} and is multiplied by 0.99960.9996 every 15 updates until it reaches 7.5⋅10−67.5 \cdot 10^{-6}.

The edit distance in symbols

The main metric is the normalised edit distance between the predicted text y^\hat{y} and the reference yy:

d(y^,y)=Levenshtein(y^,y)∣y∣,d(\hat{y}, y) = \frac{\text{Levenshtein}(\hat{y}, y)}{\lvert y \rvert},

where the Levenshtein distance counts the insertions, deletions and substitutions needed to turn one string into the other, and ∣y∣\lvert y \rvert is the number of characters in the reference. Lower is better, and 0 means an exact match.

The page-split score in symbols

When a page break is ambiguous, the paper's rule is that the two candidate split positions are compared with a normalised Levenshtein distance dd, and the page break scores 1−d1 - d. A page is kept when its average break score is at least 0.9.

Worked examples

Example
1. A tiny edit distance

The reference is x3x^3 and the prediction is x2x^2. The reference has 3 characters.

  1. Levenshtein distance: the strings differ in one character, the last one, so one substitution is needed. The distance is 1.
  2. Normalised: 1/3≈0.3331 / 3 \approx 0.333.
  3. Check: a perfect prediction would give 0, and a prediction with every character wrong would give 1 or more.

Interpretation: one wrong character in a three-character formula costs a third of the score on that formula, which is why the metric is harsh on short equations.

Example
2. The standard case: the learning rate after 150 updates

The learning rate starts at 5⋅10−55 \cdot 10^{-5} and is multiplied by 0.99960.9996 every 15 updates. After 150 updates the schedule has applied the factor 150/15=10150 / 15 = 10 times.

  1. The factor is 0.9996100.9996^{10}. Take logs: 10ln⁡0.9996≈10×(−0.0004001)=−0.00400110 \ln 0.9996 \approx 10 \times (-0.0004001) = -0.004001.
  2. e−0.004001≈0.996e^{-0.004001} \approx 0.996.
  3. The learning rate is 5⋅10−5×0.996≈4.98⋅10−55 \cdot 10^{-5} \times 0.996 \approx 4.98 \cdot 10^{-5}.
  4. Check: the learning rate has fallen by about 0.4 percent, which is a small change, as a decay of 0.9996 per 15 steps should be.

Interpretation: the schedule is gentle, and it takes thousands of decays to reach the floor of 7.5⋅10−67.5 \cdot 10^{-6}.

Example
3. Realistic: how much better than the PDF text?

The paper's Table 1 gives a normalised edit distance of 0.255 for the text embedded in the PDF and 0.071 for Nougat base, both on all content of the arXiv test set.

  1. Difference: 0.255−0.071=0.1840.255 - 0.071 = 0.184.
  2. Relative reduction: 0.184/0.255≈0.720.184 / 0.255 \approx 0.72.
  3. Check: a reduction of 0.72 means Nougat base makes about 28 percent of the PDF text's error count, measured as this normalised distance.
  4. Interpretation: most of the distance to the true source is removed. The paper's Table 1 gives no separate PDF-text score for mathematics, so the math figures cannot be compared like for like: Nougat base reaches 0.128 on math, against 0.727 for GROBID with the LaTeX-OCR formula recogniser.

Try it. Nougat base has a repetition rate of about 1.5 percent of pages in the test set. Out of 1,000 pages, roughly how many loop? Answer: 1000×0.015=151000 \times 0.015 = 15 pages.

Results

The paper evaluates on an arXiv test set with the normalised edit distance, BLEU and METEOR (both borrowed from machine translation), and precision, recall and F1 scores.

MethodEdit distance (lower is better)BLEUF1
Text embedded in the PDF0.25565.879.2
GROBID0.31255.673.0
Nougat small (250M parameters)0.07388.992.9
Nougat base (350M parameters)0.07189.193.1

The two Nougat models are close, and the smaller one is on par with the larger. On math alone, Nougat base reaches an edit distance of 0.128, against 0.727 for GROBID with the external formula recogniser LaTeX-OCR.

The paper also reports three limits. First, the model sometimes falls into a loop, repeating the same sentence or alternating two sentences; this happens on about 1.5 percent of test pages and more often on documents unlike the training set. The authors flag a loop when a variance signal computed from the model's logits drops below 6.75 and stays there. Second, the model works best on documents that look like research papers, and performance on scanned pages is noticeably worse than on digital ones. Third, pages are processed one at a time, so the bibliography can contain inconsistent styles or invented section numbers.

Reading guide

  1. Abstract and Introduction. Read for the motivation: PDFs lose meaning in equations, and line-by-line OCR does not recover it. Needs Optical Character Recognition.
  2. Related Work. Skim. It places Nougat between OCR for handwritten formulas and layout models like LayoutLM. Needs Transformer for the encoder and decoder that the section mentions.
  3. Model (Section 3). The core. The encoder is a Swin Transformer and the decoder is based on the Donut setup. The training uses AdamW Optimizer and Learning Rate decay.
  4. Data augmentation (Section 3.2). Needs Data Augmentation: erosion, blur, noise and distortion make digital pages look scanned. The token perturbation is the part that reduces repetition.
  5. Datasets (Section 4). The arXiv pipeline and page splitting. Needs Bag of Words for the page classifier and Support Vector Machine for the linear classifier it uses.
  6. Results (Section 5). Read Table 1 and the metric definitions in 5.1. Needs Edit Distance, and the BLEU and METEOR definitions are given in the section itself.
  7. Repetitions and utility. The failure modes, which matter for anyone who uses the model.

Check yourself

QuestionRecall: why does Nougat use a single network from page image to markup, rather than a separate OCR step followed by a parser?Show answer

A line-by-line OCR engine loses the positions of superscripts, subscripts and fractions, and those positions carry the meaning of a formula. A single encoder-decoder can keep the layout of each symbol and write the structure directly as LaTeX, so no separate step has to rebuild it from plain text.

QuestionComputation: the reference markup has 40 characters, and the prediction differs by 4 insertions and 2 substitutions. What is the normalised edit distance?Show answer

The Levenshtein distance is 4+2=64 + 2 = 6. Dividing by the 40 characters of the reference gives 6/40=0.156 / 40 = 0.15.

QuestionWhy: why does the paper add random token replacements to the ground truth during training?Show answer

The model tends to fall into loops at inference, repeating a sentence over and over under greedy decoding. Replacing some tokens in the training targets makes the model learn to recover from a small error rather than assume its own output is always correct. The paper reports that this reduces the collapse into repetition significantly.

QuestionApplied: a lab wants to convert 20,000 old scanned journal pages to searchable text. The paper's results are on digital arXiv pages. What should they check before trusting the output?Show answer

They should run the model on a sample of their own scanned pages and measure the error on them, since the paper reports that scanned pages score noticeably worse than digital ones. They should also check for repetition loops, which rise on documents unlike the training data, and review pages with long equations or tables by hand. A small labelled sample of their own pages gives them an error estimate they can rely on.

Sources

Read the full paper

Lukas Blecher, Guillem Cucurull, Thomas Scialom et al., 2023. arXiv:2308.13418