Attention Is All You Need · Section 6.3

6.3 English constituency parsing

Is the Transformer only good at translation? The paper tries a very different task, drawing the grammatical structure of English sentences, almost without tuning.

A sentence and its structure

The old man reads a letter .

The constituency tree of “The old man reads a letter .”DTTheJJoldNNmanNPVBZreadsDTaNNletterNPVP..S

Input: 7 words. Output written out: 29 tokens.

  • S sentence
  • NP noun phrase
  • VP verb phrase
  • DT determiner
  • JJ adjective
  • NN noun
  • VBZ verb, third person singular

An English sentence of our models' vocabulary and its constituency tree: the sentence splits into a noun phrase and a verb phrase, those into smaller phrases, down to each word's part of speech. Switch to the same tree written out as one sequence of brackets and labels, the form a translation model can produce, and compare its length with the sentence's.

What this section says

Constituency parsing turns a sentence into a tree of its phrases: a sentence (S) is a noun phrase (NP) and a verb phrase (VP), a verb phrase is a verb and another noun phrase, and so on down to the words. Written out as a sequence of brackets and labels, a tree becomes an output a translation model can learn to write, a trick from earlier work the paper cites. The task is hard in two ways the paper names: the output must follow strict rules (every bracket closed, labels in the right places), and it is much longer than the input.

The paper trained a 4-layer Transformer with d_model = 1024 on the Wall Street Journal part of the Penn Treebank, about 40,000 sentences, and also with about 17 million extra, automatically parsed sentences. It changed almost nothing from the translation model: a few experiments on dropout, learning rate and beam size (21, with α = 0.3, and room for outputs 300 tokens longer than the input).

Table 4 reports the F1 score on section 23 of the Wall Street Journal, a measure from 0 to 100 of how many phrases match the human tree. Trained on the 40,000 sentences alone it scores 91.3, second among the models trained that way to the Recurrent Neural Network Grammar (91.7), and, unlike recurrent sequence-to-sequence models, better than the BerkeleyParser with that little data. With the extra data it scores 92.7. The paper sums up: better than all previously reported models except the Recurrent Neural Network Grammar (Table 4 also lists a multi-task model at 93.0).

In the paper's words

“This task presents specific challenges: the output is subject to strong structural constraints and is significantly longer than the input.”

Section 6.3 In plain words: Parsing is hard because the output must follow strict rules and is much longer than the input.

“In contrast to RNN sequence-to-sequence models [37], the Transformer outperforms the BerkeleyParser [29] even when training only on the WSJ training set of 40K sentences.”

Section 6.3 In plain words: Unlike recurrent models, the Transformer beats the BerkeleyParser even with only 40,000 training sentences.

Quoted from Vaswani et al., Attention Is All You Need, arXiv:1706.03762v7(opens in a new tab). The figures on this page are redrawn, not copied.

The notation

The symbols of this section, in the paper and in our model
Symbol Meaning In the paper Our miniature
\(\text{F1}\) how many phrases of the predicted tree match the human one, from 0 to 100 91.3 (WSJ only), 92.7 (semi-supervised) not trained: our models only translate
\(N,\ d_{\text{model}}\) layers and vector size of the parsing model 4, 1024 not trained
\(\text{beam size},\ \alpha\) beam search's width and length penalty for parsing 21, 0.3 not trained

Try it

Try this Our model has one task, English to German. Try a sentence in the translation playground: the same design, a different output language, just as parsing is a different output language for the paper's.

How our miniature differs

We did not train a parser: the lab's two models translate and predict the next word. The figure's tree is drawn by hand for a sentence our models know. Written out, its 7 words become 29 output tokens, which shows why parsing outputs are long.

Check yourself

Three questions a reader of this section should now be able to answer. The lab checks your answer against the paper and our models.

How long the output is

Written out as brackets, labels and words, how many output tokens does the figure's 7-word sentence become?

Your answer
A whole number

Why it is hard

What two challenges of constituency parsing does the paper name?

The result

How did the Transformer do on constituency parsing, according to Table 4?

An unhandled error has occurred. Reload 🗙

Reconnecting…

Your connection to the Transformers and Attention Lab dropped, probably a Wi-Fi blip. Hold on while it reconnects.

The connection could not be restored. The page will reload in a few seconds.