Search

Search papers and lessons

Natural Language Processing (Almost) from Scratch

Ronan Collobert, Jason Weston, Leon Bottou et al. · 2011

One neural network over learned word vectors tags text for four tasks, and pretraining those vectors on unlabelled text closes most of the gap to hand-built systems.

On this page

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

TL;DR

Most language tagging systems in 2011 used linear classifiers fed with features that experts had designed for each task. The paper argues for a different route: one neural network that takes raw words, learns a vector for each word, and is trained end to end on each task. Its results were close to the best hand-engineered systems on part-of-speech tagging, chunking, named entity recognition and semantic role labelling. Pretraining the word vectors on about 852 million words of unlabelled text then narrowed most of the remaining gap.

Builds on: Word Embedding, Convolution, Backpropagation, Stochastic Gradient Descent · Leads to: Language Model pretraining and the learned-representation approach to NLP that followed

Why it mattered

Before this paper, a new language task meant a new feature set, often built on the output of other parsers. The paper showed that one architecture could cover four standard benchmarks with almost no task-specific engineering, and that the internal word vectors could be learned from unlabelled text and reused. The released tagger, SENNA, was fast and light enough to run on ordinary hardware, which was part of its influence. The idea that word vectors come from unlabelled data and then initialise supervised models is the pattern later pretrained language models followed, and later word-vector work such as word2vec (Mikolov et al., 2013) became the standard source of such vectors.

The paper is also a clear statement of a philosophy: treat benchmarks as indirect measures of how good the learned representations are, rather than as targets to optimise one at a time.

The problem

Four tasks label words in a sentence. Part-of-speech tagging gives each word a syntactic role. Chunking groups words into phrases such as noun phrases. Named entity recognition marks words that name a person, place or organisation. Semantic role labelling assigns roles such as ARG0 (the agent) and ARG1 (the thing acted on) to the words around a verb.

The best systems on these tasks used linear models over many engineered features. Some of those features came from external parsers, which made the systems slow and hard to combine. The paper's question is whether a model that starts from almost raw words can match them. A useful test is the gap between the current best systems and a plain neural network. The gap is small on the simplest task and large on the most complex one, because the best systems for semantic role labelling use the most engineered features.

How it works

Here is the window approach on one word, traced step by step. The paper uses the same steps for all four tasks, with a sentence-level variant for semantic role labelling.

  1. Look up a vector for each word. Every word in a dictionary has a row in a matrix WW. The lookup table returns the row for the word. The paper uses word vectors of size 50 and a dictionary of 100,000 words. Words outside the dictionary map to one special RARE vector.
  2. Gather a window. For the word to tag, take the vectors of the words in a fixed window around it. The walkthrough uses a window of three words: "the", "cat" and "sat", with "cat" the word to tag. Concatenate their vectors. Take the two-dimensional vectors (1,0)(1, 0), (0,1)(0, 1) and (1,1)(1, 1), which gives the six numbers (1,0,0,1,1,1)(1, 0, 0, 1, 1, 1).
  3. Apply a linear layer and a non-linearity. Multiply the concatenated vector by a weight vector and add a bias. Use one hidden unit with weights 0.50.5 on every entry and bias 00. Its value is 0.5×(1+0+0+1+1+1)=1.50.5 \times (1 + 0 + 0 + 1 + 1 + 1) = 1.5. The HardTanh non-linearity clips values to the range from −1-1 to 11, so the hidden value becomes 11. The clipping is cheaper than a true tanh and the paper reports no loss in accuracy from using it.
  4. Score each tag. Add an output layer with one score per tag. Take the scores fNOUN=1f_{\text{NOUN}} = 1 and fVERB=0.5f_{\text{VERB}} = 0.5 as the outputs for this window.
  5. Turn scores into probabilities. The softmax gives p(NOUN)=e1/(e1+e0.5)=0.6225p(\text{NOUN}) = e^1 / (e^1 + e^{0.5}) = 0.6225 and p(VERB)=0.3775p(\text{VERB}) = 0.3775. Check: the two numbers sum to 1.
  6. Train on the gold tag. If the gold tag is NOUN, the word-level loss is −log⁡0.6225=0.4741-\log 0.6225 = 0.4741. Stochastic gradient descent moves the weights, the word vectors and the layers to raise this probability on the next pass over that window.

Each word is tagged this way. The sentence-level version in the paper replaces step 5 with a score for the whole sequence of tags, which includes transition scores between neighbouring tags.

−2−1.5−1−0.500.511.52−1.5−1−0.500.511.5pre-activationoutput
  • HardTanh(x)
  • tanh(x), for comparison
HardTanh is a clipped line; it matches tanh near zero and costs less to compute. The point at 1.5 is the clipped value from step 3.
flowchart LR
  W["window of word indices"] --> L["lookup table: one vector per word"]
  L --> C["concatenate window vectors"]
  C --> H["linear layer + HardTanh"]
  H --> S["one score per tag"]
  S --> P["softmax over tags"]

Worked examples

Example
1. Tiny case: the lookup and concatenation

Use the vectors from the walkthrough, vthe=(1,0)\mathbf{v}_{\text{the}} = (1, 0), vcat=(0,1)\mathbf{v}_{\text{cat}} = (0, 1) and vsat=(1,1)\mathbf{v}_{\text{sat}} = (1, 1), and a window of size 1 centred on "cat".

  1. Look up the vector for "cat": (0,1)(0, 1).
  2. The window contains one vector, so the concatenated feature has two numbers: (0,1)(0, 1).
  3. Look up the vector for "the" and "sat" for the size-3 window instead: concatenation gives (1,0,0,1,1,1)(1, 0, 0, 1, 1, 1), which is six numbers.

Interpretation: a larger window gives the linear layer more context, and its input grows with the window size times the word-vector size. The paper's window was 5 words for the supervised tasks, so its input had 5×50=2505 \times 50 = 250 numbers, plus the capital-letter features it adds.

Example
2. Standard case: the hidden unit and the softmax

Use the window (1,0,0,1,1,1)(1, 0, 0, 1, 1, 1), weights 0.50.5 on each of the six inputs, bias 00, and the two tag scores from the walkthrough.

  1. Weighted sum: 0.5×1+0.5×0+0.5×0+0.5×1+0.5×1+0.5×1=1.50.5 \times 1 + 0.5 \times 0 + 0.5 \times 0 + 0.5 \times 1 + 0.5 \times 1 + 0.5 \times 1 = 1.5.
  2. HardTanh: 1.5>11.5 > 1, so the output is 11.
  3. Scores: fNOUN=1f_{\text{NOUN}} = 1, fVERB=0.5f_{\text{VERB}} = 0.5.
  4. Softmax: e1=2.7183e^{1} = 2.7183 and e0.5=1.6487e^{0.5} = 1.6487, sum 4.36704.3670, so p(NOUN)=2.7183/4.3670=0.6225p(\text{NOUN}) = 2.7183 / 4.3670 = 0.6225.
  5. Loss if the gold tag is NOUN: −ln⁡0.6225=0.4741-\ln 0.6225 = 0.4741.
  6. If the gold tag were VERB, the loss would be −ln⁡0.3775=0.9741-\ln 0.3775 = 0.9741, so the gradient is larger for the wrong tag.

Interpretation: the softmax turns scores into probabilities that always sum to 1, and the loss grows as the probability of the gold tag shrinks. Training pushes the gold tag's score up.

Example
3. Realistic case: the gap closed by pretraining

Table 8 reports the supervised network with word-level likelihood, with and without embeddings pretrained on unlabelled text. The LM2 column uses the larger language model. The benchmark row is the best published system for each task.

  1. Chunking (F1): benchmark 94.2994.29; network without pretraining 89.1389.13; network with LM2 embeddings 92.0492.04. Gain from pretraining: 92.04−89.13=2.9192.04 - 89.13 = 2.91. Gap left to the benchmark: 94.29−92.04=2.2594.29 - 92.04 = 2.25.
  2. Named entity recognition (F1): benchmark 89.3189.31; without pretraining 79.5379.53; with LM2 86.9686.96. Gain: 86.96−79.53=7.4386.96 - 79.53 = 7.43. Gap left: 89.31−86.96=2.3589.31 - 86.96 = 2.35.
  3. Semantic role labelling (F1): benchmark 77.9277.92; without pretraining 55.4055.40; with LM2 58.3458.34. Gain: 58.34−55.40=2.9458.34 - 55.40 = 2.94. Gap left: 77.92−58.34=19.5877.92 - 58.34 = 19.58.
  4. Part-of-speech tagging (per-word accuracy): benchmark 97.2497.24; without pretraining 96.3196.31; with LM2 97.1497.14. Gain: 0.830.83. Gap left: 0.100.10.
  5. The sentence-level likelihood with LM2 gives 93.6393.63 for chunking and 74.1574.15 for SRL, so for SRL the sentence-level model is about 16 points ahead of the word-level model with the same embeddings.

Interpretation: pretraining helps every task, and the largest gain is on named entity recognition (7.43 points). Semantic role labelling stays furthest from its benchmark, 19.58 points behind even with pretraining.

Try it: using the sentence-level numbers with LM2, what is the gap between the benchmark and the network on named entity recognition, and how does it compare with the gap for chunking? Answer: 89.31−88.67=0.6489.31 - 88.67 = 0.64 for NER and 94.29−93.63=0.6694.29 - 93.63 = 0.66 for chunking. The two gaps are nearly equal, so the sentence-level model with pretrained embeddings is almost level with the benchmark on both.

Common mistakes and tradeoffs

It is tempting to think that a neural network with raw words needs no linguistic knowledge. In fact the paper keeps two small features on purpose, a lower-cased word and a capitalisation feature, and it replaces numbers with a single token. Those choices reduce the dictionary and carry some information the lower-casing loses.

It is tempting to think that the learned word vectors capture meaning from the labelled data alone. The paper's nearest-neighbour table for the SRL network trained from scratch (Table 6) gives neighbours of "france" such as "persuade" and "faw", which are not semantically related, which is why the paper turns to unlabelled data. The same vectors after language-model pretraining (Table 7) give neighbours such as "austria", "belgium" and "germany" for "france".

It is tempting to think that a sentence-level model is always better. In fact the paper finds that the sentence-level likelihood helps chunking, NER and SRL but has little advantage for POS (96.37 against 96.31 without pretraining). The window model is the simpler choice for POS.

It is tempting to think that the windowed model handles any task. The paper says it fails for SRL, where the tag depends on a verb that may fall outside the window. That is why SRL uses a convolutional sentence model with max pooling over the sentence.

When to use this approach: you have a sequence-labelling task, a fair amount of labelled data and a large unlabelled corpus in the same language, and you want a single model with few engineered features. When not to use it: the labelled data is tiny and no unlabelled text is available, or the task needs structure the model cannot represent without features from a parser. Alternatives include conditional random fields and hidden Markov models with engineered features, which the paper cites as the main earlier approach and which need their own feature design.

The math

Definition: Window network with sentence-level likelihood

For a sentence [x]1T[x]_1^T and a tag path [i]1T[i]_1^T, the network produces a score matrix [fθ]i,t[f_\theta]_{i,t}, and the path score adds transition scores [A]i,j[A]_{i,j} between neighbouring tags. The conditional probability of the gold path [y]1T[y]_1^T is the exponential of its score divided by the sum over all paths. The log-probability is the gold score minus a log-sum over all paths.

Step 1 and 2 in symbols

The lookup table returns the column ⟨W⟩w\langle W \rangle_w of a dwrd×∣D∣d_{\text{wrd}} \times |\mathcal{D}| matrix for word ww. A window of dwind_{\text{win}} words gives a concatenated vector ⟨⋅⟩tdwin\langle \cdot \rangle_t^{d_{\text{win}}} of length dwrd×dwind_{\text{wrd}} \times d_{\text{win}}. In the walkthrough dwrd=2d_{\text{wrd}} = 2 and dwin=3d_{\text{win}} = 3, so the concatenated vector has six entries.

Step 3 in symbols

A linear layer computes fl=Wlfl−1+bl\mathbf{f}^l = W^l \mathbf{f}^{l-1} + \mathbf{b}^l, and HardTanh is applied element by element:

HardTanh(x)={−1x<−1x−1≤x≤11x>1\mathrm{HardTanh}(x) = \begin{cases} -1 & x < -1 \\ x & -1 \leq x \leq 1 \\ 1 & x > 1 \end{cases}

In the walkthrough the pre-activation 1.51.5 falls in the top case, so HardTanh returns 11.

Step 5 in symbols

The softmax over the KK tags is p(i∣x,θ)=e[fθ]i/∑je[fθ]jp(i \mid x, \theta) = e^{[f_\theta]_i} / \sum_j e^{[f_\theta]_j}. Its log is log⁡p(y∣x,θ)=[fθ]y−log⁡∑je[fθ]j\log p(y \mid x, \theta) = [f_\theta]_y - \log\sum_j e^{[f_\theta]_j}. In the walkthrough this gives log⁡p(NOUN)=1−log⁡(e1+e0.5)=1−1.4741=−0.4741\log p(\text{NOUN}) = 1 - \log(e^1 + e^{0.5}) = 1 - 1.4741 = -0.4741, which matches the loss in step 6.

Sentence-level form

The path score is s([x]1T,[i]1T,θ~)=∑t=1T([A][i]t−1,[i]t+[fθ][i]t,t)s([x]_1^T, [i]_1^T, \tilde\theta) = \sum_{t=1}^T ([A]_{[i]_{t-1}, [i]_t} + [f_\theta]_{[i]_t, t}), and the log-likelihood of the gold path is s(⋅,[y]1T)−logadd∀[j]1Ts(⋅,[j]1T)s(\cdot, [y]_1^T) - \mathrm{logadd}_{\forall [j]_1^T} s(\cdot, [j]_1^T). The log-add over all paths is computed with the recursion δt(k)=[fθ]k,t+logaddi(δt−1(i)+[A]i,k)\delta_t(k) = [f_\theta]_{k,t} + \mathrm{logadd}_i(\delta_{t-1}(i) + [A]_{i,k}), followed by logaddiδT(i)\mathrm{logadd}_i \delta_T(i). The recursion costs time linear in the sentence length, although the number of paths grows exponentially. Inference replaces the log-add with a max, which is the Viterbi algorithm (see Viterbi Algorithm).

Ranking criterion for unlabelled text

For the language model, each window xx of real text should score higher than the same window with its centre word replaced by any other dictionary word ww. The paper minimises

∑x∈X∑w∈Dmax⁡{0, 1−fθ(x)+fθ(x(w))}.\sum_{x \in \mathcal{X}} \sum_{w \in \mathcal{D}} \max\{0,\, 1 - f_\theta(x) + f_\theta(x^{(w)})\}.

This is a hinge loss over pairs (see Hinge Loss). It needs no normalisation over the dictionary, which is the expensive part of an entropy criterion, and it does not favour frequent phrases over rare but legal ones.

Results

The paper reports three kinds of result: supervised benchmarks, semi-supervised benchmarks and multitask training.

Supervised. Table 4 compares the vanilla network with the benchmark systems. Without unlabelled data the network reaches POS 96.3796.37 (sentence-level), chunking 90.3390.33, NER 81.4781.47 and SRL 70.9970.99, all below the benchmarks of 97.2497.24, 94.2994.29, 89.3189.31 and 77.9277.92. The paper calls these results behind the benchmarks but in the ballpark of published systems.

Semi-supervised. Table 8 initialises the lookup tables with embeddings from LM1 (window 11, 100 hidden units, trained about four weeks on Wikipedia with a dictionary of 100,000 words) or LM2 (the same model, trained three more weeks on Wikipedia and Reuters with a 130,000-word dictionary). With the sentence-level likelihood and LM2, the scores are POS 97.2097.20, chunking 93.6393.63, NER 88.6788.67 and SRL 74.1574.15. The worked example above gives the gains. The paper states that a larger language model gave better results, which suggests that more unlabelled text would help further.

Multitask. Section 5 shares the lookup tables and first hidden layer across tasks. Training POS, chunking and NER jointly with the window network gives 97.22, 94.10 and 88.62, against 97.20, 93.63 and 88.67 for the single-task model with LM2 embeddings (Table 9). The gains are small, and NER gets slightly worse.

What the results do not show: the benchmarks are the paper's chosen reference systems, and the paper notes that many of the top CoNLL systems used external labelled data. The hidden-layer sizes were chosen on validation data, and the paper picked the smallest network with reasonable performance rather than the top single run, because of the variance from initialisation. The training times are long: chunking and NER about an hour, POS a few hours and SRL about three days. The paper also reports the time the pretrained language models took, about four weeks and then three more weeks, which is a large cost before any supervised model is trained.

Reading guide

  • Section 1 (Introduction). Needs Word Embedding and the idea of a benchmark task. Read for the philosophy: benchmarks as indirect measures of representation quality.
  • Section 2 (The Benchmark Tasks). Needs the vocabulary of each task: POS tags, chunks, named entities and PropBank roles. Table 1 and Table 2 give the datasets and the state of the art. Skim the prior-system descriptions unless you want the history.
  • Section 3.1 (Transforming Words into Feature Vectors). Needs One-Hot Encoding and Word Embedding. The lookup table is the only trainable part of the first layer.
  • Section 3.2 (Extracting Higher Level Features). Needs Convolution and Max Pooling. The window approach is a one-layer case; the sentence approach uses convolution and a max over positions.
  • Section 3.3 (Training). Needs Softmax, Maximum Likelihood Estimation and Stochastic Gradient Descent. Work through the sentence-level recursion with Conditional Random Field in mind, since the paper compares the two.
  • Section 3.4 (Supervised Benchmark Results). Read Table 4 and Table 5 together with the hyper-parameter list in the text.
  • Section 4 (Lots of Unlabeled Data). Needs Language Model and Semi-Supervised Learning. Read the ranking criterion and Table 8. Table 7 gives the neighbour lists that show what pretraining changes.
  • Sections 5 and 6 (Multitask and task-specific engineering). Needs Transfer Learning and Support Vector Machine for the comparison points. Optional on first read.

Recap

  • A single network with a learned word table can be trained end to end on four tagging tasks, with few engineered features.
  • The window approach tags each word from its neighbours; the convolutional sentence approach handles tasks where the tag depends on a verb outside the window.
  • The sentence-level likelihood, computed with a linear-time recursion, helps structured tags such as chunks and entities.
  • Word vectors learned from a language model on about 852 million unlabelled words make the supervised tasks much better, and the nearest-neighbour lists become semantically meaningful.
  • The largest remaining gap is in semantic role labelling, which the paper's own results leave open.

Read next: Language Model for the training objective behind the pretrained vectors, and Structured Prediction for the sequence-level view taken in Section 3.3.

Check yourself

QuestionRecall: why does the paper use a HardTanh layer instead of a plain linear layer stack?Show answer

Without a non-linearity, a stack of linear layers is still one linear function of the input, so it cannot extract non-linear features. HardTanh is a cheap non-linearity that clips values to the range from −1-1 to 11, and the paper reports that it does not hurt generalisation compared with the exact tanh.

QuestionCompute: a network gives scores fA=2f_{\text{A}} = 2 and fB=0f_{\text{B}} = 0 for two tags, and the gold tag is A. What is the word-level loss −log⁡p(A)-\log p(A)?Show answer

The softmax gives p(A)=e2/(e2+e0)=7.3891/(7.3891+1)=7.3891/8.3891=0.8808p(A) = e^2 / (e^2 + e^0) = 7.3891 / (7.3891 + 1) = 7.3891 / 8.3891 = 0.8808. The loss is −ln⁡0.8808=0.1269-\ln 0.8808 = 0.1269.

QuestionWhy does the paper prefer a pairwise ranking criterion to an entropy criterion for the language model?Show answer

An entropy criterion needs the probability of each word given its context, and normalising over a dictionary of about 100,000 words is expensive. The entropy also weights frequent phrases heavily, which leaves little signal for rare but legal ones. The ranking criterion only compares a real window with corrupted windows, so it needs no full normalisation and treats rare legal phrases as important as common ones.

QuestionApplied: a team wants to tag tweets for named entities. They have 2,000 labelled tweets and a large unlabelled tweet corpus. Using the paper's results, which step should they copy first, and what should they expect?Show answer

They should copy the pretraining step first: train word embeddings on the large unlabelled corpus with a language model, then initialise the supervised lookup table with them. The paper shows this gives the largest gain on named entity recognition, moving the word-level network from 79.5379.53 to 86.9686.96 F1 on CoNLL 2003. Tweets differ from the Reuters and Wikipedia text the paper used, so the team should expect a smaller gain than the paper's and should check on held-out tweets.

Sources

  • Collobert, R., Weston, J., Bottou, L., Karlen, M., Kavukcuoglu, K. and Kuksa, P. (2011). Natural Language Processing (almost) from Scratch. arXiv:1103.0398. https://arxiv.org/abs/1103.0398
  • Mikolov, T., Chen, K., Corrado, G. and Dean, J. (2013). Efficient Estimation of Word Representations in Vector Space. arXiv:1301.3781. https://arxiv.org/abs/1301.3781

Read the full paper

Ronan Collobert, Jason Weston, Leon Bottou et al., 2011. arXiv:1103.0398