SlowGuess/ACL_2020_OCR huggingface dataset:
Mor Geva*
Tel Aviv University,
morgeva@mail.tau.ac.il
Ankit Gupta*
ankitgupta.iitkanpur@gmail.com
Jonathan Berant
joberant@cs.tau.ac.il
Large pre-trained language models (LMs) are known to encode substantial amounts of linguistic information. However, high-level reasoning skills, such as numerical reasoning, are difficult to learn from a language-modeling objective only. Consequently, existing models for numerical reasoning have used specialized architectures with limited flexibility. In this work, we show that numerical reasoning is amenable to automatic data generation, and thus one can inject this skill into pre-trained LMs, by generating large amounts of data, and training in a multi-task setup. We show that pre-training our model, GENBERT, on this data, dramatically improves performance on DROP  $(49.3\rightarrow 72.3\mathrm{F}_1)$ , reaching performance that matches state-of-the-art models of comparable size, while using a simple and general-purpose encoder-decoder architecture. Moreover, GENBERT generalizes well to math word problem datasets, while maintaining high performance on standard RC tasks. Our approach provides a general recipe for injecting skills into large pre-trained LMs, whenever the skill is amenable to automatic data augmentation.
Recently, models trained on large amounts of data with a language modeling (LM) objective, have shown great promise in natural language processing, exhibiting surprising amounts of knowledge and information (Peters et al., 2018; Devlin et al., 2019; Liu et al., 2019; Lan et al., 2019; Petroni et al., 2019; Hewitt and Manning, 2019). However, high-level skills, such as the ability to perform numerical reasoning over text, can be challenging to capture with a LM objective only. Consider the example in Table 1. To solve the first question (Q1), a model must capture the value of numbers in the
Figure 1: An overview of our approach for injecting numerical skills into a pre-trained LM. (a) We add two pre-training steps over large amounts of synthetic numerical data (ND) and textual data (TD); (b) we further fine-tune the model over either numerical reasoning datasets (DROP, MAWPS) or reading comprehension datasets (SQUAD).
text, compute their difference, and generate the tokens corresponding to the result, which generally do not appear in the input passage.
To make the task more manageable, state-of-the-art models have employed specialized architectures, restricting the space of possible numerical computations to a limited set. Modules were designed for counting (but only until '9') and for addition and subtraction (but of 2-3 numbers only). Such models perform well on existing datasets, such as DROP (Dua et al., 2019), but do not generalize to unsupported computations, which will require modifying the model architecture. Moreover, current models marginalize at training time over all numerical expressions that evaluate to the correct answer. Since the number of such expressions grows exponentially, scaling these approaches to arbitrary computations entails using non-differentiable operations (sampling or computing top- $K$  numerical expressions), which can lead to training difficulties.
Proceedings of the 58th Annual Meeting of the Association for Computational Linguistics, pages 946-958
Passage: Taunton has four art galleries... Hughes/ Donahue Gallery founded in 2007, a local community gallery serving local Taunton artists... Art Euphoric founded in 2008 has both visual and craft exhibits...
Q1: How many years after founding of Hughes/ Donahue was Art Euphoric founded?
A1: 1 (number)
Q2: Which gallery was founded later, Hughes/ Donahue or Art Euphoric?
A2: Art Euphoric (span)
Table 1: Example passage from DROP, and two questions with different answer types.
In this work, we propose that reasoning skills, such as numerical reasoning, are amenable to automatic data generation. Hence, one can inject that skill directly into the model by adding additional pre-training steps, allowing the model to learn the skill in an end-to-end fashion. This results in a fully-differentiable training procedure over a standard and general-purpose architecture, where the output space can be easily controlled through the data generation procedure.
Specifically (Figure 1), we add to a large pre-trained LM two pre-training steps over automatically-generated synthetic data. First, we generate numerical data of the form  $3 + 4 + 11 = 18$ . Training on this data teaches the model to compute the value of numbers from their tokens and to perform numerical operations. Second, we automatically generate question-passage pairs that require numerical reasoning using a compact grammar (textual data). Training on this data endows the model with the ability to understand computations expressed in pseudo-natural language.
In both pre-training steps, the model, GEN-BERT, generates output numbers token-by-token. Thus, the model has a standard architecture, where an answer can either be extracted from the input question and passage or generated from a decoder. Pre-training is done in a multi-task setup with a standard LM objective, in order to avoid "catastrophic forgetting" (Kirkpatrick et al., 2017) of the linguistic information in the original LM. After pre-training, the model has sufficient language and numerical skills to be directly fine-tuned on a target numerical reasoning dataset, without resorting to specialized architectures. Augmenting more numerical skills does not require changing the model, only generating additional data.
We demonstrate the validity of our approach by a series of experiments showing that:
(a) GENBERT is able to solve pre-training tasks for numerical reasoning.
(b) Pre-training on these tasks provides GEN-BERT with 1) skills to reach performance that matches state-of-the-art models of comparable size on DROP (Dua et al., 2019), a standard numerical reasoning dataset, as well as 2) the ability to generalize to math word problem (MWP) datasets (Koncel-Kedziorski et al., 2016).
(c) GENBERT learns these numerical skills while maintaining high performance on SQuAD (Rajpurkar et al., 2016), a standard reading comprehension dataset.
(d) Initializing models for numerical reasoning with GENBERT's weights improves their original performance.
To conclude, in this work we address the problem of injecting LMs with numerical reasoning skills. Our contributions are:
- A method for injecting skills into pre-trained LMs, given that automatic data generation is possible.
- GENBERT, an architecture for pre-trained LM with generative and extractive abilities.
- A framework for generating numerical and textual synthetic data for numerical reasoning.
Our code and data can be downloaded from https://github.com/ag1988/injecting_numeracy.
2 Numerical Reasoning Over Text
Numerical reasoning over text (NRoT) is commonly set up as a reading comprehension (RC) task. Given a training set of question-context-answer triples  $\{(\mathbf{q}_i, \mathbf{c}_i, a_i)\}_{i=1}^N$ , the goal is to learn a function that returns the answer  $a$  to a question  $\mathbf{q}$  given a context  $\mathbf{c}$ . However, in NRoT the answer generally requires to internally perform some numerical computation using the entities and numbers in the context. Specifically, the answer is either: (a) a span (or list of spans) from the context  $\mathbf{c}$  or question  $\mathbf{q}$ , or (b) a number that is the result of some computation (see examples in Table 1).
Two natural, yet opposing, approaches lend themselves to tackling NRoT: (a) A symbolic approach: a model can read the question and context, output a numerical expression and evaluate the answer with an external symbolic calculator. This approach is a particular case of semantic parsing (Kamath and Das, 2019), and was common in early NRoT datasets (Koncel-Kedziorski et al., 2015; Roy and Roth, 2015; Hosseini et al., 2014). How-
ever, it suffers from several drawbacks. First, because numerical expressions are discrete and their space grows combinatorially, the model must learn to search in this space using non-differentiable operations, which are usually difficult to optimize. Second, numerical expressions are limited to numerical answers, while in DROP often a numerical computation is required but the final answer is a text span. (b) A distributed approach: have a model directly generate the answer given  $(\mathbf{q},\mathbf{c})$ . When the answer is a text span, the model can extract it from the input, and when the answer is a number that is not in  $\mathbf{q}$  or  $\mathbf{c}$ , the model must generate it. While this makes training straightforward, the model must learn to perform numerical computations from the relatively small target dataset. We empirically show in §3 that this leads to low performance in general.
As a compromise, most NRoT models (Dua et al., 2019; Kinley and Lin, 2019; Hu et al., 2019; Efrat et al., 2019) have taken a hybrid approach: they augment standard extractive QA models with specialized modules for handling a limited set of numerical computations. We briefly describe this architecture, as it is the basis for our model in §3.
Given a question with  $n_1$  tokens  $\mathbf{q} = (q_1, \ldots, q_{n_1})$  and a context with  $n_2$  tokens  $\mathbf{c} = (c_1, \ldots, c_{n_2})$ , the hybrid model first computes contextualized representations for the  $n_1 + n_2 + 3$  tokens  $\langle [\mathrm{CLS}] \mathbf{q}[\mathrm{SEP}][\mathrm{C[SEP]}\rangle$  using a pretrained LM, such as BERT (Devlin et al., 2019):
$$
\mathbf {L} = \mathbf {L M} (\mathbf {q}, \mathbf {c}).
$$
The representations  $\mathbf{L}$  are then passed to multiple heads, which are small neural networks that estimate  $p(a\mid \mathbf{q},\mathbf{c},h)$ , that is, the probability of the answer given the input and conditioned on a head  $h$ , corresponding to a particular answer type:
- Context span head: computes a distribution over all spans in the context using a feed-forward network (FFN)  $\mathbf{F}\mathbf{F}_{\mathbf{c}}(\mathbf{L})$
- Question span head: computes a distribution over spans in the question using a FFN  $\mathbf{FF}_{\mathbf{q}}(\mathbf{L})$ .
- Count head: computes a distribution over the numbers  $\{0, \ldots, 9\}$  using a FF  $\mathbf{F}\mathbf{F}_{\mathrm{cnt}}(\mathbf{L})$ .
- Arithmetic head: computes a distribution over all signed combinations of numbers in the context using a FF  $\mathbf{F}\mathbf{F}_{\mathrm{cmb}}(\mathbf{L})$  (the numbers in the context are identified in a pre-processing step). While the first two heads are standard in extractive QA, the latter two heads are specialized and meant to handle answers that do not appear in the input.
Finally, for deciding which answer head to use for a given input, a type head  $\mathbf{F}\mathbf{F}_{\mathrm{typ}}(\mathbf{L})$  outputs a probability distribution  $p_{\mathrm{head}}(h\mid \mathbf{q},\mathbf{c})$  (using a FFN). Thus the model probability for an answer is
$$
p (a \mid \mathbf {q}, \mathbf {c}) = \sum_ {h \in \text {h e a d s}} p _ {\text {h e a d}} (\mathrm {h} \mid \mathbf {c}, \mathbf {q}) \cdot p (a \mid \mathbf {c}, \mathbf {q}, h).
$$
Training is done by enumerating all of the ways in which the answer can be obtained using all of the heads, and maximizing this marginal probability.
While existing models perform well on DROP, the aforementioned architecture is not flexible. First, the output space is severely constrained – the model can only count up to '9', and numerical computations are restricted to signed combinations of a few numbers. Second, expanding the space of supported numerical computations is non-trivial, because training involves marginalizing over all expressions that lead to the correct answer. Since the space of numerical expressions grows exponentially, expanding this space quickly leads to a difficult search problem. Third, delegating numerical computations to an external symbolic calculator leads to modeling challenges, since there could be interactions between text and numerical computation: Consider the DROP question "How many total yards did Phil Dawson throw for touchdowns?" Current models handle such questions by computing a sum from numbers in the text and returning the result. However, if the question was "Who threw 45 total yards for touchdowns?", the model would have to compute the sum internally, and then find the relevant span in the text. This is impossible when the computation itself is delegated to an external calculator. Thus, training models to handle such numerical questions is desirable.
Motivated by the above arguments, we wish to push the frontier of end-to-end differentiable models for numerical reasoning. Thus, we will automatically generate large amounts of data that endow a pre-trained LM with numerical skills.
3 GENBERT: A BERT-based Model for Generating Arbitrary Outputs
We now describe a simple BERT-based generative model that performs numerical computations internally, termed GENBERT. The model combines the Transformer encoder-decoder architecture (Vaswani et al., 2017) with a pre-trained LM, specifically, BERT.
Our architecture is illustrated in Figure 2. Our encoder is a standard Transformer, initialized with
BERT weights. To also enjoy BERT's representations at decoding time, we tie the weights of the decoder and the encoder. Because the Transformer decoder has source attention weights (weights for attending to the encoder representations at decoding time) that are not present in BERT, we tie these source-attention weights to the self-attention weights of the encoder (which are tied to the self-attention weights of the decoder). This fully initializes the Transformer model with BERT weights.
Since the encoder and decoder weights are tied, we make them learn distinct representations by adding a FFN  $\mathbf{F}\mathbf{F}_{\mathrm{enc}}$  that transforms the encoder contextualized representations  $\mathbf{L}_{\mathrm{enc}}$  as
$$
\mathbf {H} _ {\mathbf {e n c}} = \text {l a y e r - n o r m} (\operatorname {g e l u} (W \cdot \mathbf {L} _ {\mathbf {e n c}})),
$$
where  $W$  is a parameter matrix (Hendrycks and Gimpel, 2016; Ba et al., 2016). Analogously, we add  $\mathbf{F}\mathbf{F}_{\mathrm{dec}}$  to the decoder. To further distinguish the encoder and decoder, we use distinct start and end tokens for input and output sequences. Given  $m$  answer tokens  $\mathbf{a} = (a_1,\dots ,a_m)$ , we form an output sequence with  $m + 2$  tokens:  $\langle [\mathrm{SOS}]\mathbf{a}[\mathrm{EOS}]\rangle$ . The output tokens are passed through the decoder and  $\mathbf{F}\mathbf{F}_{\mathrm{dec}}$  to obtain  $\mathbf{H}_{\mathrm{dec}}$ .
Finally, the probability of an answer is defined in the usual manner: Let  $\langle \mathbf{a}\rangle = (a_0\cdot \cdot \cdot a_{m + 1})$  be the output sequence. The decoder outputs the probability  $p_{\mathrm{dec}}(a_{i + 1}\mid a_0,\dots a_i,\mathbf{c},\mathbf{q})$ , and the probability of an answer is:
$$
p _ {\mathrm {d e c}} (\langle \mathbf {a} \rangle \mid \mathbf {c}, \mathbf {q}) = \prod_ {i = 0} ^ {m} p _ {\mathrm {d e c}} (a _ {i + 1} \mid a _ {0},.. a _ {i}, \mathbf {c}, \mathbf {q}).
$$
As we have a generative model, we can remove the specialized count and arithmetic heads from §2. Thus, the type head  $\mathbf{FF}_{\mathrm{typ}}(\mathbf{H}_{\mathrm{enc}})$  outputs a distribution  $(p_{\mathbf{q}}, p_{\mathbf{c}}, p_{\mathbf{dec}})$  over the context span, question span, and decoder heads.
To improve pre-training on the numeric data (§4), we make two additional modifications.
Digit Tokenization (DT) Conventional wordpiece tokenization treats numbers no differently than any other token. However, computing the value of numbers should be simpler when using digits directly (Wallace et al., 2019). Hence, we tokenize numbers digit-by-digit. For example, a wordpiece  $\# \# d_{1} \cdots d_{k}$  where  $d_{i} \in \{0, \ldots, 9\}$  is further split into  $\# \# d_{1}, \ldots, \# \# d_{k}$ . We show in §5.1 that this substantially improves sample complexity when training to perform numerical operations.
(a) GenBERT
Figure 2: GENBERT's network architecture: (a) a high-level overview of the network, including a generative head (red), two span-extraction heads (yellow), and an answer type head. (b) a closer overview of GENBERT's generative head.
(b) GenBERT's decoder
Random Shift (RS) The original Transformer uses absolute positional embeddings for each token. However, in §4, we train on short inputs such as “ $1086.1 - 2.54 + 343.8$ ”. Thus, the model can potentially over-fit and learn to perform numerical reasoning only when numbers are at the beginning of an input. To prevent this, when the input length  $n_1 + n_2 + 3 < 512$ , we shift all position IDs by a random integer in  $(0,1,\ldots,512 - (n_1 + n_2 + 3))$ .
Training For each span  $(i,j)$ , a span extraction head  $h$  outputs its probability  $p_h((i,j) \mid \mathbf{c}, \mathbf{q}, h)$  of being the answer. Let  $S$  be the set of spans in the input corresponding to the gold answer. The model loss  $\mathbf{L}_{\mathrm{model}}$  marginalizes over all ways in which the answer can be predicted:
$$
- \log \left(p _ {\mathbf {d e c}} \cdot p _ {\mathbf {d e c}} (\langle \mathbf {a} \rangle) + \sum_ {\mathbf {h} \in \mathbf {q}, \mathbf {c}} p _ {\mathbf {h}} \cdot \sum_ {(i, j) \in S} p _ {h} (i, j)\right),
$$
where conditionals have been dropped for brevity.
To evaluate the ability of GENBERT to perform numerical reasoning, we initialize it with BERT and fine-tune it on DROP. GENBERT obtains 46.1 EM and  $49.3\mathrm{F}_1$  roughly 20 points lower than prior models. Thus, we conclude that acquiring numerical reasoning skills from DROP data only is difficult. To remedy this, we will automatically generate training data that will endow GENBERT with numerical skills before training it on DROP.
Figure 3: Template extraction and instantiation. A template (in red) is extracted from a MWP sentence, using categories for containers, entities, verbs, attributes and numbers, according to Hosseini et al. (2014). For generation, the categories are instantiated with a domain-specific vocabulary.
4 Pre-training Tasks for Numerical Skills
We now describe two automatically-generated datasets and the multi-task training procedure.
4.1 Generating Numerical Data (ND)
Our first dataset focuses on learning numerical values expressed by tokens and computing numerical operations, i.e., it does not involve textual content. As such, it is easy to craft templates that correspond to various numeric operations. We designed six such templates, described in Table 2. Each template consists of an expression to evaluate and its solution. Further details on their instantiation are provided in §A.1. While the numerical operations were chosen based on DROP, it is trivial to extend them to other domains (Saxton et al., 2019) with different numerical operations.
4.2 Generating Textual Data (TD)
Numeric data is easy to generate, since it does not contain any textual context. However, to tackle NRoT, a model needs to comprehend how numerical operations are expressed in text that refers to events, entities and quantities. This primes us to generate textual data from a simple grammar.
While text generation is hard in the general case, we are specifically interested in text that focuses on number manipulations. Therefore, we use the framework of Hosseini et al. (2014), who proposed to model math word problems with a simple structure. In their framework a world state consists of entities, which are objects that are being counted, and containers, which are objects that own entities. Sentences use verb categories to describe how the number of entities in a container changes, and thus a world state can be updated given a sentence.
Consider the textual example in Figure 1. the entities are soldiers and citizens, and the containers are the king and the commander. The verbs ("had" and "received") describe the entities the king holds, and how many were passed to the commander.
In this work, we use this framework to automatically generate examples. We extract templates that describe changes in the number of entities owned by containers, and automatically generate question-context pairs from these templates.
Template extraction To extract templates, we go over sentences from the corpus provided by Hosseini et al. (2014). For each sentence, we use a procedure described by Hosseini et al. (2014) to abstract its tokens to the following categories: numbers (NUM), entities (ENT), containers (CONT) and attributes (ATTR). In addition, verbs are abstracted to six categories, each corresponding to a different change in the number of entities owned by containers. Thus, each template fully specifies how to update a world state, i.e., the number of entities each container owns. The top part of Figure 3 illustrates the abstraction process. Finally, we count for each extracted template its frequency in the data, and use the top-12 templates for passage generation. Details on the abstraction process, categories used, and extracted templates are in §A.2.
Passage generation Using the extracted templates, we can generate sentences and maintain a world state of all containers and the number of entities they own. We construct a small vocabulary (<100 words) that maps categories to domain-specific words, and use the following procedure to generate passages.
We sample 3-6 templates with replacement, and instantiate them one-by-one (the bottom part of Figure 3 illustrates instantiation). Each template is instantiated by uniformly sampling values from the vocabulary with probability  $1 - p$  and from previously generated sentences with probability  $p$ . To avoid a collection of unrelated sentences, we set the probability of using previously used values to  $p = 0.7$ . An example passage is shown in Table 3.
Question generation After generating a passage, the world state holds information about all containers in the passage and the number of entities they hold. In Table 3, the state will include the number of families and rebels of different nationalities in each container (the commander, the householder, and the countries). Based on this world state, numerical reasoning questions can be asked.
To create questions, we craft 13 question templates that are instantiated with objects from the world state. The questions teach the model to track events and perform numeric and discrete operations.
Operation	Template	Example instantiation
signed float combination	s1f1s2f2s3f3s4f4	517.4 - 17484 - 10071.75 + 1013.21
min/max/avg	o(f1,f2,f3,f4)	largest(13.42, 115.5, 72.76)
arg max, arg min	arg(w1f1,w2f2,w3f3,w4f4)	arg min(highish 137.1, sightliness 43.2)
date min/max	dsup(d1,d2,d3,d4)	oldest(June 04, 959; 01 May 959)
date difference	diff in prd(d1,d2)	diff in days(05 April 112; June 01, 112)
percentage	pcent w :: w1p1%, w2p2%, w3p3%, w4p4%	percent not sunbird :: sunbird 33.2%, defector 60.77%, molehill 6.03%
Table 2: Templates for generating synthetic numerical examples and the numerical operations required to answer them. Domains (defined in App. A.1):  $s_i \in \{-, +\}$ ,  $f_i \in \mathbb{R}^+$ ,  $o \in \mathcal{O}$ : superlative words like "longest",  $arg \in \{\arg \min, \arg \max\}$ ,  $w_i \in \mathcal{W}$ : words from NTLK Words Corpus,  $d_i \in \mathcal{D}$ : dates until Sep 2019,  $dsup \in \mathcal{DSUP}$ : superlative words like "latest",  $prd \in \{\text{"days","months","years"}\}$ ,  $p_i \in (0,100)$ ,  $pcent \in \{\text{"percent","percent not"}\}$ .
P: The commander recruited 1949 Polish families in Spain. The householder recruited 1996 Japanese families in Spain. There were 10913 white rebels and 77 Chinese families in Spain. 6641 British soldiers, 476 asian rebels, and 338 Germans families were recruited in Russia.
Q: How many Japanese families were in Spain?
A:1996
Q: How many more Japanese families were in Spain than Polish families?
A:47(1996-1949)
Q: How many families of Spain were not Polish families?
A:2073 (4022-1949)
Table 3: An example synthetic passage (P) and questions. Questions (Q) were generated from templates and answers (A) were calculated based on the world state.
Examples for generated questions are shown in Table 3, where answers are computed from the world state. Overall, we create 13 question templates for 7 different "skills", provided in §A.2.
4.3 Training GENBERT on Synthetic Data