Teaching Machines to Code: Neural Markup …Teaching Machines to Code: Neural Markup Generation with...

14
Teaching Machines to Code: Neural Markup Generation with Interpretable Attention Sumeet S. Singh Independent Researcher Saratoga, CA 95070 [email protected] Abstract We present a neural transducer model with visual attention that learns to generate L A T E X markup of a real-world math formula given its image. Applying sequence modeling and transduction techniques that have been very successful across modal- ities such as natural language, image, handwriting, speech and audio; we construct an image-to-markup model that learns to produce syntactically and semantically correct L A T E X markup code over 150 words long and achieves a BLEU score of 89%; improving upon the previous state-of-art for the Im2Latex problem. We also demonstrate with heat-map visualization how attention helps in interpreting the model and can pinpoint (localize) symbols on the image accurately despite having been trained without any bounding box data. 1 Introduction In the past decade, deep neural network models based on RNNs 1 , CNNs 2 and ‘attention’ [29] have been shown to be very powerful sequence modelers and transducers. Their ability to model joint distributions of real-world data has been demonstrated through remarkable achievements in a broad spectrum of generative tasks such as; image synthesis [27, 28, 22, 25], image description [16, 31, 14, 21, 30], video description [7], speech and audio synthesis [26], handwriting recognition [12, 2], handwriting synthesis [9], machine translation [5, 1, 15, 24], speech recognition [10, 4, 11], etc. [8, 29] One class of sequence models employ the so-called encoder-decoder [5] or sequence-to-sequence [24] architecture, wherein an encoder encodes a source sequence into feature vectors, which a decoder employs to produce the target sequence. The source and target sequences may either belong to the same modality (e.g. in machine translation use-cases) or different modalities (e.g. in image-to-text, text-to-image, speech-to-text); the encoder / decoder sub-models being constructed accordingly. The entire model is trained end-to-end using supervised-learning techniques. In recent years, this architecture has been augmented with an attention and alignment model which selects a subset of the feature vectors for decoding. It has been shown to help with longer sequences [1, 19]. Among other things, this architecture has been used for image-captioning [31]. In our work we employ a encoder-decoder architecture with attention, to map images of math formulas into corresponding L A T E X markup code. The contributions of this paper are: 1) Solves the Im2Latex problem 100 and improves over the previous best reported BLEU score by 1.27% BLEU, 2) Pushes the boundaries of the neural encoder-decoder architecture with visual attention, 3) Analyses variations of the model and cost function. Specifically we note the changes to the base model [31] and what impact those had on performance, 4) Demonstrates the use of attention visualization for model interpretation and 1 Recurrent Neural Network. 2 Convolutional Neural Networks and variants such as dilated CNNs [32]. Preprint. Work in progress. arXiv:1802.05415v2 [cs.LG] 15 Jun 2018

Transcript of Teaching Machines to Code: Neural Markup …Teaching Machines to Code: Neural Markup Generation with...

Page 1: Teaching Machines to Code: Neural Markup …Teaching Machines to Code: Neural Markup Generation with Interpretable Attention Sumeet S. Singh Independent Researcher Saratoga, CA 95070

Teaching Machines to Code:Neural Markup Generation with Interpretable

Attention

Sumeet S. SinghIndependent Researcher

Saratoga, CA [email protected]

Abstract

We present a neural transducer model with visual attention that learns to generateLATEX markup of a real-world math formula given its image. Applying sequencemodeling and transduction techniques that have been very successful across modal-ities such as natural language, image, handwriting, speech and audio; we constructan image-to-markup model that learns to produce syntactically and semanticallycorrect LATEX markup code over 150 words long and achieves a BLEU score of89%; improving upon the previous state-of-art for the Im2Latex problem. We alsodemonstrate with heat-map visualization how attention helps in interpreting themodel and can pinpoint (localize) symbols on the image accurately despite havingbeen trained without any bounding box data.

1 Introduction

In the past decade, deep neural network models based on RNNs1, CNNs2 and ‘attention’ [29] havebeen shown to be very powerful sequence modelers and transducers. Their ability to model jointdistributions of real-world data has been demonstrated through remarkable achievements in a broadspectrum of generative tasks such as; image synthesis [27, 28, 22, 25], image description [16, 31,14, 21, 30], video description [7], speech and audio synthesis [26], handwriting recognition [12, 2],handwriting synthesis [9], machine translation [5, 1, 15, 24], speech recognition [10, 4, 11], etc. [8, 29]

One class of sequence models employ the so-called encoder-decoder [5] or sequence-to-sequence[24] architecture, wherein an encoder encodes a source sequence into feature vectors, which a decoderemploys to produce the target sequence. The source and target sequences may either belong to thesame modality (e.g. in machine translation use-cases) or different modalities (e.g. in image-to-text,text-to-image, speech-to-text); the encoder / decoder sub-models being constructed accordingly.The entire model is trained end-to-end using supervised-learning techniques. In recent years, thisarchitecture has been augmented with an attention and alignment model which selects a subset ofthe feature vectors for decoding. It has been shown to help with longer sequences [1, 19]. Amongother things, this architecture has been used for image-captioning [31]. In our work we employ aencoder-decoder architecture with attention, to map images of math formulas into correspondingLATEX markup code. The contributions of this paper are: 1) Solves the Im2Latex problem100 andimproves over the previous best reported BLEU score by 1.27% BLEU, 2) Pushes the boundaries ofthe neural encoder-decoder architecture with visual attention, 3) Analyses variations of the modeland cost function. Specifically we note the changes to the base model [31] and what impact thosehad on performance, 4) Demonstrates the use of attention visualization for model interpretation and

1Recurrent Neural Network.2Convolutional Neural Networks and variants such as dilated CNNs [32].

Preprint. Work in progress.

arX

iv:1

802.

0541

5v2

[cs

.LG

] 1

5 Ju

n 20

18

Page 2: Teaching Machines to Code: Neural Markup …Teaching Machines to Code: Neural Markup Generation with Interpretable Attention Sumeet S. Singh Independent Researcher Saratoga, CA 95070

5) Demonstrates how attention can be used to localize objects (symbols) in an image despite havingbeen trained without bounding box data.

1.1 The IM2LATEX problem

The IM2LATEX Problem is a request for research proposed by OpenAI. The challenge is to builda Neural Markup Generation model that can be trained end-to-end to generate the LATEX markupof a math formula given its image. Data for this problem was produced by rendering single-linereal-world LATEX formulas obtained from the KDD Cup 2003 dataset. The resulting grayscaleimages were used as the input samples while the original markup was used as the label/targetsequence. Each training/test sample (Figure 1) is comprised of an input image x and a cor-

S0 =∑l

1

2∆2l

Trφal φa−l +

∑l

1

2ε2lTr fal f

a−l +

∑r

1

grTr ψarψ

ar .

S _ 0 = \sum _ l \ frac 1 2 \Delta _ l ^ 2 \mathrm T r \, \phi _ l ^ a \phi _ − l ^ a + \sum _ l

\ frac 1 2 \ epsilon _ l ^ 2 \mathrm T r \, f _ l ^ a f _ − l ^ a + \sum _ r \ frac 1 g _ r \

mathrm T r \, \bar \ psi _ r ^ a \ psi _ r ^ a \, .

S _ 0 = \sum _ l \ frac 1 2 \Delta _ l ^ 2 \mathrm T r \, \phi _ l ^ a \phi _ − l ^ a + \sum _ l

\ frac 1 2 \ epsilon _ l ^ 2 \mathrm T r \, f _ l ^ a f _ − i ^ a + \sum _ r \ frac 1 g _ r

\mathrm T r \, \ psi _ r ^ a \ psi _ r ^ a \, .

Figure 1: A training sample: At the top is the input image x, middle the target sequence y (τ = 145)and bottom the predicted sequence y (τ = 148). Each space-separated word in y and y ∈ V

responding target LATEX-sequence y of length τ . Each word y of the target sequence, belongsto the vocabulary of the dataset plus two special tokens: beginning-of-sequence <bos> and end-of-sequence <eos>. Denoting image dimensions as HI ,WI and CI and the vocabulary as a setV of K words, we represent x ∈ RHI×WI×CI , V := LATEX tokens,<eos>,<bos>; |V | = K andy := (y1, . . . ,yτ ); yt ∈ 1, . . . ,K. The task is to generate markup that a LATEX compiler willrender back to the original image. Therefore, our model needs to generate syntactically and seman-tically correct markup, by simply ‘looking’ at the image: i.e. it should jointly model vision andlanguage.

2 Image to markup model

Our model (Figure 2a) has the same basic architecture as [31] (which we call our baseline model) inthe way the encoder, decoder and a visual attention interact. However there are significant differencesin the sub-models which we notate in the remainder of this paper and in the appendix.

2.1 Encoder

All images are standardized to a fixed size by centering and padding with whitepixels. Then they are linearly transformed (whitened) to lie in the range [-0.5,0.5]. A deep CNN then encodes the whitened image into a visual feature gridA, having H × W (i.e. height × width) visual feature vectors a(h,w) ∈ RD.

A :=

a(1,1) . . . a(1,W )

......

...a(H,1) . . . a(H,W )

(1)

The visual feature vectors are then concatenated(pooled) together in strides of shape [SH , SW ]; beget-ting pooled feature vectors a(h,w) ∈ RD, whereD = D · SH · SW . The resulting feature mapA, has acorrespondingly shrunken shape [H,W ]; where H =

H/SH and W = SW /W .

Each pooled feature vector can be viewed as a rectangular window into the image, bounded byits receptive field.3 The idea behind this is to partition the image into spatially localized regionalencodings and setup a decoder architecture (Section 2.2) that selects/emphasizes only the relevant

3Neighboring regions overlap but each region is distinct overall.

2

Page 3: Teaching Machines to Code: Neural Markup …Teaching Machines to Code: Neural Markup Generation with Interpretable Attention Sumeet S. Singh Independent Researcher Saratoga, CA 95070

Enco

der

Deco

der

RN

NCtyt-1, Ct-1

Pooling

CNN

Deep Output Layer

Init State Model EmbeddingC

ALS

TM

Attention

LSTM-Stack

Beam Search or CTC Decoding

pt

Ht

I

Á

ŷt

a

(a)

CtCt-1

Deep Output Layer

Init State Model

CALSTM

Attention Model

pt

Ht

a

LSTM Stack

LSTM1

LSTMQ ctQ

ct1

ht1

htQ

ht-1Q, ct-1

Q

ht-11, ct-1

1

αtht-1

Q

zt

zt

C0

βta

E

DRNN

(b)

Figure 2: (a) Model outline showing major parts of the model. Beam search decoder is only usedduring inferencing, not training. LSTM-Stack and Attention model jointly form a ConditionedAttentive LSTM stack (CALSTM) which can itself be stacked. (b) Expanded view of Decoder RNNshowing its sub-models. There are three nested RNN cells in all: The decoder RNN (DRNN) at thetop level, nesting the CALSTM which nests the LSTM-Stack. The Init Model does not participate inrecurrence, therefore its is shown outside the box.

regions at each time-step t, while filtering-out/de-emphasizing the rest. Bahdanau et al. 1 showedthat such piecewise encoding enables modeling longer sequences as opposed to models that encodethe entire input into a single feature vector [24, 5]. 4 Pooling allows us to construct encoders withdifferent receptive field sizes. We share results of two such models: I2L-NOPOOL with no featurepooling and pooled feature grid shape [4,34] and I2L-STRIPS having stride [4,1] and pooled featuregrid shape [1,34]. Finally, for convenience we representA as a flattened sequence a (Equation 2).See the appendix for more details.

a := (a1, . . . ,aL) ; al ∈ RD; l = H(h− 1) + w; L = HW (2)

2.2 Decoder

pt : 1, . . . ,K → [0, 1]

yt ∼ ptpt(yt) := Pr(yt|y<t,a) (3)

Pr(y|a) =

τ∏t=1

pt (yt) (4)

The decoder is a language modeler and generator. It isa Recurrent Neural Network (DRNN in Figure 2b) thatmodels the discrete probably distribution pt, of the outputword yt, conditioned on the sequence of previous wordsy<t and relevant regions of the encoded image a5 (Equa-tions 3). Probability of the entire output sequence y givenimage a is therefore given by Equation 4.

4That said, Bahdanau et al. 1 employ a bidirectional-LSTM [8] encoder whose receptive field does encompassthe entire input anyway! (Although that does not necessarily mean that the bi-LSTM will encode the entireimage). Likewise Deng et al. 6 who also solve the IM2LATEX problem also employ a bi-directional LSTMstacked on top of a CNN-encoder in order to get full view of the image. In contrast, our visual feature vectorshold only spatially local information which we found are sufficient to achieve good accuracy. This is probablyowing to the nature of the problem; i.e. transcribing a one-line math formula into LATEXsequence requires onlylocal information at each step.

5This is now a very standard way to model sequence (sentence) probabilities in neural sequence-generators.See [24] for example.

3

Page 4: Teaching Machines to Code: Neural Markup …Teaching Machines to Code: Neural Markup Generation with Interpretable Attention Sumeet S. Singh Independent Researcher Saratoga, CA 95070

DRNN : a; yt−1; Ct−1 → pt; Ct (5)

The DRNN receives the previous word yt−1 and encoded image a as inputs. In addition, it maintainsan internal state Ct that propagates information (features) extracted from an initial state, the outputsequence unrolled thus far and image regions attended to thus far (Equation 5). It is as complexmodel, comprised of the following sub-models (Figure 2b): 1) A LSTM-Stack [13] responsiblefor memorizing Ct and producing a recurrent activation Ht, 2) A Visual attention and alignmentmodel responsible for selecting relevant regions of the encoded image for input to the LSTM-Stack, 6

3) A Deep Output Layer [20] that produces the output probabilities pt, 4) Init Model: A model thatgenerates the initial state C0 and 5) An embedding matrix E (learned by training) that transforms ytinto a dense representation ∈ Rm.

2.2.1 Inferencing

After the model is trained, the output sequence is generated by starting with the word ‘bos’ and thenrepeatedly sampling from pt until <eos> is produced. The sequence of words thus sampled is thepredicted sequence: y := (y1, . . . , yτ ) ; yt ∈ RK . For this procedure we use beam search decoding[8] with a beam width of 10. Figure 1 shows an example predicted sequence and Figures 5 and 6show examples of predictions rendered into images by a LATEX 2ε compiler.

2.2.2 Visual attention and alignment model

αt := (αt,1, . . . , αt,L)∣∣∣ 0≤αt,l≤1∑L

l αt,l=1(6)

αt = fatt (a; Ht−1) (7)zt = αta

> (8)

As previously alluded, the decoder soft selects/filtersrelevant (encoded) image regions at each step. Thisis implemented via. a ‘soft attention’ mechanism7

which computes a weighted sum zt of the pooledfeature vectors al. The visual attention model fatt,computes the weight distribution αt (Equations 6, 7 and 8). fatt is modeled by an MLP (detailsin the appendix). While it is a possible for αt to end up uniformly distributed over (a1 . . .aL), inpractice we see a unimodal shape with most of the weight concentrated on 1-4 neighborhood (seeFigure 3) around the mode. We call this neighborhood the focal-region - i.e. the focus of attention. Inother words we empirically observe that the attention model’s focus is ‘sharp’; converging towardsthe ‘hard attention’ formulation described by Xu et al. [31]. Also note that (Figure 3), the attentionmodel is able to utilize the extra granularity available to it in the I2L-NOPOOL case and consequentlygenerates much sharper focal-regions than I2L-STRIPS.

Furthermore, the model aligns the focal-region with the output word and thus scans text on the imageleft-to-right (I2L-STRIPS) or left-right and up-down (I2L-NOPOOL) just like a person would read it(Figure 3). We also observe that it doesn’t focus on empty margins of the image except at the firstand last (<eos>) steps which is quite intuitive for determining the beginning or end of text.

2.2.3 LSTM stack

LSTMq : xqt ;hqt−1; cqt−1 → h

qt ; c

qt

1 ≤ q ≤ Q ; hqt , cqt ∈ Rn

xqt = hq−1t ; q 6= 1 (9)

x1t = zt; Eyt−1

The core sequence generator of the DRNN is a mul-tilayer LSTM [9] (Figure 2b). Our LSTM cell imple-mentation follows Graves et al. [11]. The LSTM cellsare stacked in a multi-layer configuration [33, 20] asin Equation 9. LSTMq is the LSTM cell at positionq with xqt , h

qt and cqt being its input, hidden activa-

tion and cell state respectively. LSTM1 receives thestack’s input: soft attention context zt and previous output word Eyt−1. LSTMQ produces thestack’s output Ht = hQt , which is sent up to the Deep Output Layer. Accordingly, the stack’sactivation (Ht) and state (Ct) are defined as: Ht = hQt and Ct := (c1

t , . . . , cQt , h

1t , . . . ,h

Qt ). We

6The LSTM-Stack and Visual Attention and Alignment model jointly form a Conditioned Attentive LSTM(CALSTM); Ht and Ct being its activation and internal state respectively. Our source-code implements theCALSTM as a RNN cell which may be used as a drop-in replacement for a RNN cell.

7‘Soft’ attention as defined by Xu et al. [31] and originally proposed by Bahdanau et al. [1].

4

Page 5: Teaching Machines to Code: Neural Markup …Teaching Machines to Code: Neural Markup Generation with Interpretable Attention Sumeet S. Singh Independent Researcher Saratoga, CA 95070

Figure 3: Focal-regions learnt by the attention model: to the left by I2L-STRIPS and to the rightby I2L-NOPOOL. Image darkness is proportional to αt. Notice how αt concentrates on the imageregion corresponding to the output word (shown above the image). The \frac command starts afraction, \mathrm sets a font and \eos is the <eos> token.

do not use skip or residual connections between the cells. Both of our models have two LSTM layerswith n = 1500. Further discussion and details of this model can be found in the appendix.

2.2.4 Deep output layer

We use a Deep Output Layer [20] to produce the final output probabilities: pt =fout(Ht; zt; Eyt−1). fout is modeled by an MLP. Note that the output layer receives skip connec-tions from the LSTM-Stack input (Equation 9). Details of this model can be found in the appendix.

2.2.5 Init model

Hidden MLP

FC1 FC2Q

Figure 4: Init Model. FC= Fully Connected Layer.

The Init Model finit, produces the initial state C0 of the LSTM-Stack.finit is intended to ‘look’ at the entire image (a) and setup the decoderappropriately before it starts generating the output.

finit : a→ (c10, . . . , c

Q0 , h

10, . . . ,h

Q0 ) (10)

hq0, cq0 ∈ Rn

That said, since it only provides a very small improvement in performancein exchange for over 7 million parameters, its need could be questioned.finit is modeled as an MLP with common hidden layers and 2Q distinctoutput layers, one for each element of C0, connected as in Figure 4. Seethe appendix for more detail and discussion.

2.3 Training

J = −1

τlog (Pr (y|a)) + λRR (11)

R =1

2

∑θ

θ2 (11a)

The entire model was trained end-to-end by mini-mizing the objective function J (Equation 11) usingback propagation through time. The first term inEquation 11 is the average (per-word) log perplexityof the predicted sequence8 and is the main objective.R is the L2-regularization term, equal to L2-norm of the model’s parameters θ (weights and biases)

8i.e. Average cross-entropy, negative log-likelihood or negative log-probability.

5

Page 6: Teaching Machines to Code: Neural Markup …Teaching Machines to Code: Neural Markup Generation with Interpretable Attention Sumeet S. Singh Independent Researcher Saratoga, CA 95070

and λR is a hyperparameter requiring tuning. Following Xu et al. [31] at first, we had includeda penalty term intended to bias the distribution of the cumulative attention placed on an image-location αl :=

∑τt=1 αt,l. However we removed it for various reasons which are discussed in the

appendix along with other details and analyses.

We split the dataset into two fixed parts: 1) training dataset = 90-95% of the data and 2) test dataset5-10%. At the beginning of each run, 5% of the training dataset was randomly held out as thevalidation-set and the remainder was used for training. Therefore, each such run had a differenttraining/validation data-split, thus naturally cross-validating our learnings across the duration of theproject. We trained the model in minibatches of 56 using the ADAM optimizer [17]; periodicallyevaluating it over the validation set9. For efficiency we batched the data such that each minibatchhad similar length samples. For the final evaluation however, we fixed the training and validationdataset split and retrained our models for about 100 epochs (∼ 2 1

2 days). We then picked themodel-snapshots with the best validation BLEU score and evaluated the model over the test-datasetfor publication. Table 1 lists the training parameters and metrics of various configurations. Trainingsequence predictions (y) were obtained by CTC-decoding [10] pt. Training BLEU score was thencalculated over 100 consecutive mini-batches. We used two Nvidia GeForce 1080Ti graphics cards ina parallel towers configuration. Our implementation uses the Tensorflow toolkit and is distributedunder AGPL license.

Table 1: Training metrics. λR = 0.00005 and β2 = 0.9 for all runs. The number after @ sign is thetraining epoch of the selected model-snapshot. ∗ denotes that the row corresponds to Table 2.

Dataset Model Init β1 Train Train Validation Valid’nModel? Epochs BLEU BLEU ED

I2L-140K I2L-STRIPS Yes 0.5 104 0.9361 0.8900@72∗ 0.0677I2L-STRIPS No 0.5 75 0.9300 0.8874@62 0.0691I2L-NOPOOL Yes 0.5 104 0.9333 0.8909@72∗ 0.0684I2L-NOPOOL No 0.1 119 0.9348 0.8820@92 0.0738

Im2latex-90k I2L-STRIPS Yes 0.5 110 0.9366 0.8886@77∗ 0.0688I2L-STRIPS No 0.5 161 0.9386 0.8810@118 0.0750

3 Results

Given that there are multiple possible LATEXsequences that will render the same math image, ideallywe should perform a visual evaluation. However, since there is no widely accepted visual evaluationmetric, we report corpus BLEU (1,2,3 & 4 grams) and per-word Levenstein Edit Distance10 scores (seeTable 2). We also report a (non-standard) exact visual match score103 which reports the percentageof exact visual matches, discarding all partial matches. While the predicted and targeted imagesmatch in at least 70%103 of the cases, the model generates different but correct sequences (i.e. y 6= y)in about 40% of the cases (Figure 5). For the cases where the images do not exactly match, thedifferences in most cases are minor (Figure 6). Overall, our models produce syntactically correctsequences11 for at least 99.85% of the test samples (Table 2). Please visit our website to see hundredsof sample visualizations, analyses and discussions, data-set and source-code.

3.1 Model Interpretability via attention

Since the LSTM stack only sees a filtered view (i.e. focal-region) of the input, it can only baseits predictions on the focal-regions seen thus far and initial-state C0. Further since the init-modelhas a negligible impact on performance we can drop it from the model (Table 1) and thereby the

9Evaluation cycle was run once or twice per epoch and/or when a training BLEU score calculated onsequences decoded using CTC-Decoding[10] jumped significantly.

10i.e. Edit distance divided by number of words in the target sequence.11i.e. Those that were successfully rendered by LATEX 2ε.

103We use the ’match without whitespace’ algorithm provided by Deng et al. [6] wherein two images count asmatched if they match pixel-wise discarding white columns and allowing for upto 5 pixel image translation (apdflatex quirk). It outputs a binary match/no-match verdict for each sample - i.e. partial matches however close,are considered a non-match.

6

Page 7: Teaching Machines to Code: Neural Markup …Teaching Machines to Code: Neural Markup Generation with Interpretable Attention Sumeet S. Singh Independent Researcher Saratoga, CA 95070

Input Image / Rendered Sequence ylen ylen0 T+q

+2−2= 12γiqp(Ω+2i

+2 ψ1−−2p−Ω+2i

−2 ψ1−+2p), T

+q+p±2= 1

2γiqpΩ+2i

+p ψ1−±2p, 147 155

1 σij(x−,y−;x+)=

∫dP−4π

dp+

4πdk+

4π e−i2P−x+

e−i2

(p+x−−k+y−) σij(p+,k+;P−), 150 151

2 G(f)(n)β =Σnm=0Θ

(n−m)β ,f(m)(q)+Σ

(n−2)m=0 Θ

(n−m)β ,f(m+2)(φ)+Θ

(n+1)β ,f(1)(φ) 150 150

3 S0=∑l

1

2∆2l

Trφal φa−l+

∑l

1

2ε2l

Tr fal fa−l+

∑r

1gr

Tr ψarψar . 145 148

4 ds2=− t2

(t2+r2−)(t2−r2+

)dt2+t2(dφ+

r+r−t2

dr)2+(t2+r2−)(t2−r2+)

t2dr2. 147 147

5 H= 12EU

0 0 00 ∆m2

21 00 0 ∆m2

31

U†+ 12E

a ηb 0η∗b η′b 00 0 0

, 147 147

6 Dabµν(p,p3)= δabδa3

p2−p23+iε

[−gµν+pµpν

((1−δp3,0) 1

p23

+δp3,0(1−ξ) 1p2+iε

)]139 145

7 V (H1,H2)= 18 (g2

2+g21)(|H1|2−|H2|2)

2+m2

1|H1|2+m22|H2|2−m2

3(H1H2+h.c.) 144 145

8 A30(α′→0)=2gd ε

(1)λ ε(2)

µ ε(3)ν ηλµ(pν1−p

ν2 )+ηλν(pµ3−p

µ1 )+ηµν(pλ2−p

λ3 ). 146 145

9 U†LMlUlR=M∗l , U

†LMLU

∗L=M∗L , U

†LMDU

νR=M∗D , UνR

TMRUνR=M∗R . 149 145

10√−ggµ1ν1gµ2ν2 ···gµd−pνd−p Fν1ν2...νd−p= 1

p! εµ1µ2...µd−pν1ν2...νpFν1ν2...νp , 147 145

11 dEdz =

dEeldz +

dEraddz ≈C2αs

π µ2 ln 3ET2µ2

(ln 9Eπ3T

+ 3π2αs2µ2 T 2

). 130 144

12 L0=(2n+1)|h|2 +|h|d†0d0− |h|2 −|h|

∑∞k=1(d

†kdk−d

†kdk+a†kak−b

†kbk)+Lfree0 . 149 144

13 Q7γ= e8π2mbqασ

µν(1+γ5)bαFµν , Q8G= g

8π2mbqασµνtaαβbβG

aµν , (q=d or s). 141 143

14 ds2 = α′(u2h(u)

R2 eγAdx20+ u2

R2 eγCdx2i+

R2

u2h(u)eγBdu2+R2 eγDdΩ2

5

), 143 143

15 sin( p1·k2 ) sin( p2·k

2 ) sin( p3·k2 )=− 1

4 (sin p1·k+sin p2·k+sin p3·k) 133 143

16 [P0,X0]=i, [Pi,Xj ]=−i δij(

1− ~P 2

κ2

)eP0/κ, [P0,Xi]=− 2i

κ Pi eP0/κ 139 143

17 〈Ja1µ1

(P1)...Janµn (Pn)〉T≈(−i)n NT2

12 δΓa1...anµ1...µn

(P1,...,Pn)+O( 1f2π

) , 143 143

18 uπ(~k′1)†uπ(~k1)=N2π

[cos2χ(~k)− |~p|

2

4 (cosχ(~k)c2(~k)+ 13~k2b1(~k)2)

]. 142 142

19 τ0(y)=∑1σ1=0 ..

∑1σ4=0 Y

(0)σ1..σ4

(t1Z1)σ1 (t2Z2)σ2 (t5Z5)σ3 (t8Z8)σ4 143 142

20 sin(2β)

32π2 I(Ω)→ sin(2β)

32π2 (I(Ω)+c22c23δ1I(MG,MG1)+c22s

23δ2I(MG,MG2)) 142 142

21 W=YeLjEcHi1εij+YdQ

jaDcaHi1εij+YuQ

jaUcaHi2εij+µH

i1H

j2εij 141 141

22 −iκ(α)γµκ(β)∂µ=−iCαγΓs(T

I)γβΓAdj(u−1)aIea=−iCαγΓs(T

I)γβk(I) , 145 141

Figure 5: A sample of correct predictions by I2L-STRIPS. We’ve shown the long predictions hencelengths are touching 150. Note that at times the target length is greater than the predicted length andat times the reverse is true (though the original and predicted images were identical). All such caseswould evaluate to a less than perfect BLEU score or edit-distance. This happens in about 40% of thecases. For more examples visit our website.

y y0 Ψ:S2→M1,M2 Ψ:S2→M1,M2

1 lnE+√E2−m2

l−mπ

E−√E2−m2

l−mπ

. lnE+

√E2−m2

t−mπ

E−√E2−m2

l−mπ

.

2 ( r0`s )d∼g2−k

s . ( r0`s )d∼g2−k

s .

3 pa+εbapbωµxIµ=0 pa+εbapbωµx µµ=0

5 T=V+tGV , T=V+f GV ,

7 ΘA=(ϑα,ϑα), ∂A=(∂α,∂α), ∂A,ΘB=δBA ΘA=(ϑα,vα), ∂A=(∂α,∂α), ∂A,ΘB=δBA8 Yτ (MZ)|DR=

mpoleτ −<eΣτ (m

poleτ )|

DRv(MZ )|

DRcosβ(MZ )

Yτ (MZ)|DG=mvotr −kezτ (m

peler )|

DHδ(MZ )|DEcsβ(MZ )

9 3.4β[2|2](y)=−9y2[

1−22.21y+36.93y2

1−28.21y+143.2y2

]3.4β[2|2](y)=−9y2

[1−22.2y+36.93y2

1−28.2y+143.2y2

]10 Dα β=(Π2+2eBS3)

αβ Dα β=(Π2+2eBS3)

α

β

12∫dzdzdbdb

n εV (f)=0∫dzdzdbd

n εV (f)=0

13 L(p4)∆S=1=G8F

2∑37i=1 NiWi L(s0)

∆S2=1=G8F

2∑37i=1 NiWi

14 det(M(0)(N0))=αQ(0)∏r,s[α(h−hr,s)]P`(N0−rs/K) , det(M(0)(N0))=αQ(0)

∏r,s[α(h−hr,s)]Pf (N0−rs/K) ,

17 γ ≡ e2 N2T . γ ≡ e2 N

2T .

Figure 6: A random sample of mistakes made by I2L-STRIPS. Observe that usually the model getsmost of the formula right and the mistake is only in a small portion of the overall formula (e.g. sample# 1; generating one subscript t instead of an l). In some cases the mistake is in the font and in somecases the images are identical but were incorrectly flagged by the image-match evaluation software(e.g. sample # 0 & #17). In some cases the predicted formula appears more correct than the original!(sample # 10 where position of the subscript β has been ‘corrected’ by I2L-STRIPS).

7

Page 8: Teaching Machines to Code: Neural Markup …Teaching Machines to Code: Neural Markup Generation with Interpretable Attention Sumeet S. Singh Independent Researcher Saratoga, CA 95070

Table 2: Test results. Im2latex-100k results are from Deng et al. [6]. The last column is the percentageof successfully rendering predictions.

Dataset Model BLEU Edit Visual CompilingScore Distance Match103 Predictions

I2L-140K I2L-NOPOOL 89.0% 0.0676 70.37% 99.94%I2L-STRIPS 89.0% 0.0671 69.24% 99.85%

Im2latex-90k I2L-STRIPS 88.19% 0.0725 68.03% 99.81%Im2latex-100k IM2TEX 87.73% - 79.88% -

dependency on C0 (now randomly initialized). Therefore if It is the focal-region at step t definedby the predicate αt,l > 0, then pt (yt) = fL (It, It−1 . . . I0) where fL represents the LSTM-stackand Deep Output Layer. This fact aids considerably in interpreting the predictions of the model. Wefound heat-map type visuals of the focal-regions (Figure 3) very useful in interpreting the model evenas we were developing it.

Object detection via attention: Additionally, we observe that the model settles on a step-by-stepalignment of It with the output-word’s location on the image: i.e. pt (yt) ≈ fL (It). In other wordsIt marks the bounding-box of yt even though we trained without any bounding-box data. Thereforeour model -whose encoder has a narrow receptive field- can be applied to the object detection taskwithout requiring bounding box training data, bottom-up region proposals or pretrained classifiers.Note that this is not possible with encoder architectures having wide receptive fields, e.g. thosethat employ a RNN [6, 1] because their receptive fields encompass the entire input. A future workwill quantify the accuracy of object detection [18] using more granular receptive fields. Pedersoliet al. [21] have also used attention for object detection but their model is more complex in that itspecifically models bounding-boxes although it doesn’t require them for training.

3.2 Dataset

Datasets were created from single-line LATEX math formulas extracted from scientific papers andsubsequently processed as follows: 1) Normalize the formulas to minimize spurious ambiguity.12

2) Render the normalized formulas using pdflatex and discard ones that didn’t compile or rendersuccessfully. 3) Remove duplicates. 4) Remove formulas with low-frequencey words (frequen-cy-threshold = 24 for Im2latex-90k and 50 for I2l-140K). 5) Remove images bigger than 1086× 126and formulas longer than 150. Processing the Im2latex-100k dataset104 (103559 samples) as aboveresulted in the Im2latex-90k dataset which has 93741 samples. Of these, 4648 were set aside as thetest dataset and the remaining 89093 were split into training (95%) and validation (5%) sets beforeeach run (section 2.3). We found the Im2latex-90k dataset too small for good generalization andtherefore augmented it with additional samples from KDD Cup 2003. This resulted in the I2L-140Kdataset with 114406 (training), 14280 (validation) and 14280 (test) samples. Since the normalizedformulas are already space separated token sequences, no additional tokenization step was necessary.The vocabulary was therefore produced by simply identifying the set of unique space-separated wordsin the dataset.

Ancillary material All ancillary material: Both datasets, our model and data-processing sourcecode, visualizations, result samples etc. is available at our website. Appendix is provided alongsidethis paper.

References[1] Bahdanau, D., Cho, K., and Bengio, Y. (2014). Neural machine translation by jointly learning to

align and translate. CoRR, abs/1409.0473.

[2] Bluche, T. (2016). Joint line segmentation and transcription for end-to-end handwritten paragraphrecognition. In NIPS.

12Normalization was performed using the method and software used by [6] which parses the formulas into anAST and then converts them back to normalized sequences.

104Im2latex-100k dataset is provided by [6].

8

Page 9: Teaching Machines to Code: Neural Markup …Teaching Machines to Code: Neural Markup Generation with Interpretable Attention Sumeet S. Singh Independent Researcher Saratoga, CA 95070

[3] Bluche, T., Ney, H., and Kermorvant, C. (2014). A comparison of sequence-trained deep neuralnetworks and recurrent neural networks optical modeling for handwriting recognition. In SLSP.

[4] Chan, W., Jaitly, N., Le, Q. V., and Vinyals, O. (2015). Listen, attend and spell. CoRR,abs/1508.01211.

[5] Cho, K., van Merrienboer, B., Çaglar Gülçehre, Bahdanau, D., Bougares, F., Schwenk, H., andBengio, Y. (2014). Learning phrase representations using rnn encoder-decoder for statisticalmachine translation. In EMNLP.

[6] Deng, Y., Kanervisto, A., Ling, J., and Rush, A. M. (2017). Image-to-markup generation withcoarse-to-fine attention. In ICML.

[7] Donahue, J., Hendricks, L. A., Guadarrama, S., Rohrbach, M., Venugopalan, S., Saenko, K.,and Darrell, T. (2015). Long-term recurrent convolutional networks for visual recognition anddescription. CoRR, abs/1411.4389.

[8] Graves, A. (2008). Supervised sequence labelling with recurrent neural networks. In Studies inComputational Intelligence.

[9] Graves, A. (2013). Generating sequences with recurrent neural networks. CoRR, abs/1308.0850.

[10] Graves, A., Fernández, S., Gomez, F. J., and Schmidhuber, J. (2006). Connectionist temporalclassification: labelling unsegmented sequence data with recurrent neural networks. In ICML.

[11] Graves, A., Mohamed, A., and Hinton, G. E. (2013). Speech recognition with deep recurrentneural networks. CoRR, abs/1303.5778.

[12] Graves, A. and Schmidhuber, J. (2008). Offline handwriting recognition with multidimensionalrecurrent neural networks. In NIPS.

[13] Hochreiter, S. and Schmidhuber, J. (1997). Long short-term memory. Neural Comput.,9(8):1735–1780.

[14] Johnson, J., Karpathy, A., and Fei-Fei, L. (2016). Densecap: Fully convolutional localizationnetworks for dense captioning. CoRR, abs/1511.07571.

[15] Kalchbrenner, N., Espeholt, L., Simonyan, K., van den Oord, A., Graves, A., and Kavukcuoglu,K. (2016). Neural machine translation in linear time. CoRR, abs/1610.10099.

[16] Karpathy, A. and fei Li, F. (2015). Deep visual-semantic alignments for generating imagedescriptions. 2015 IEEE Conference on Computer Vision and Pattern Recognition (CVPR), pages3128–3137.

[17] Kingma, D. P. and Ba, J. (2014). Adam: A method for stochastic optimization. CoRR,abs/1412.6980.

[18] Liu, C., Mao, J., Sha, F., and Yuille, A. L. (2017). Attention correctness in neural imagecaptioning. In AAAI.

[19] Luong, M., Pham, H., and Manning, C. D. (2015). Effective approaches to attention-basedneural machine translation. CoRR, abs/1508.04025.

[20] Pascanu, R., Çaglar Gülçehre, Cho, K., and Bengio, Y. (2013). How to construct deep recurrentneural networks. CoRR, abs/1312.6026.

[21] Pedersoli, M., Lucas, T., Schmid, C., and Verbeek, J. (2016). Areas of attention for imagecaptioning. CoRR, abs/1612.01033.

[22] Salimans, T., Karpathy, A., Chen, X., and Kingma, D. P. (2017). Pixelcnn++: Improving the pix-elcnn with discretized logistic mixture likelihood and other modifications. CoRR, abs/1701.05517.

[23] Simonyan, K. and Zisserman, A. (2014). Very deep convolutional networks for large-scaleimage recognition. CoRR, abs/1409.1556.

9

Page 10: Teaching Machines to Code: Neural Markup …Teaching Machines to Code: Neural Markup Generation with Interpretable Attention Sumeet S. Singh Independent Researcher Saratoga, CA 95070

[24] Sutskever, I., Vinyals, O., and Le, Q. V. (2014). Sequence to sequence learning with neuralnetworks. CoRR, abs/1409.3215.

[25] Theis, L. and Bethge, M. (2015). Generative image modeling using spatial lstms. In NIPS.

[26] van den Oord, A., Dieleman, S., Zen, H., Simonyan, K., Vinyals, O., Graves, A., Kalchbrenner,N., Senior, A. W., and Kavukcuoglu, K. (2016a). Wavenet: A generative model for raw audio.CoRR, abs/1609.03499.

[27] van den Oord, A., Kalchbrenner, N., and Kavukcuoglu, K. (2016b). Pixel recurrent neuralnetworks. In ICML.

[28] van den Oord, A., Kalchbrenner, N., Vinyals, O., Espeholt, L., Graves, A., and Kavukcuoglu, K.(2016c). Conditional image generation with pixelcnn decoders. CoRR, abs/1606.05328.

[29] Vaswani, A., Shazeer, N., Parmar, N., Uszkoreit, J., Jones, L., Gomez, A. N., Kaiser, L., andPolosukhin, I. (2017). Attention is all you need. In NIPS.

[30] Vinyals, O., Toshev, A., Bengio, S., and Erhan, D. (2015). Show and tell: A neural imagecaption generator. In 2015 IEEE Conference on Computer Vision and Pattern Recognition (CVPR).

[31] Xu, K., Ba, J., Kiros, J. R., Cho, K., Courville, A. C., Salakhutdinov, R., Zemel, R. S., andBengio, Y. (2015). Show, attend and tell: Neural image caption generation with visual attention.In ICML.

[32] Yu, F. and Koltun, V. (2015). Multi-scale context aggregation by dilated convolutions. CoRR,abs/1511.07122.

[33] Zaremba, W., Sutskever, I., and Vinyals, O. (2014). Recurrent neural network regularization.CoRR, abs/1409.2329.

A Qualitative analyses and details

This section is an appendix to the paper. We present here further details, analyses and discussion ofour experiments and comparison with related work.

A.1 Encoder

Table 3 shows the configuration of the Encoder CNN. All convolution kernels have shape (3,3), stride(1,1) and tanh non-linearity, whereas all maxpooling windows have shape (2,2) and stride (2,2).We initially experimented with the output of the VGG16 model [23] - per Xu et al. [31]. However

Table 3: Specification of the Encoder CNN.Layer Output Shape ChannelsInput (Image) 128× 1088 1Convolution 128× 1088 64Maxpool 64× 544 64Convolution) 64× 544 128Maxpool 32× 272 128Convolution 32× 272 256Maxpool 16× 136 256Convolution 16× 136 512Maxpool 8× 68 512Convolution 8× 68 512Maxpool 4× 34 = (H × W ) 512 = (D)

(presumably since VGG16 was trained on a different dataset and a different problem) the BLEUscore didn’t improve beyond 40%. Then we started training VGG16 along with our model but theend-to-end model didn’t even start learning (the log-loss curve was flat) - possibly due to the largeoverall depth of the end-to-end model. Reducing the number of convolution layers to 6 and changing

10

Page 11: Teaching Machines to Code: Neural Markup …Teaching Machines to Code: Neural Markup Generation with Interpretable Attention Sumeet S. Singh Independent Researcher Saratoga, CA 95070

y y0

∂A0µ∂t =−i[A0µ,HF0],

∂A0µ∂t =−i[A0µ,HF0],

1 Φi(x),Φj(y)=εijδ2(x−y). Φi(x),Φj(y)=εijδ2(x−y).

2 Vtotal=∑i

∣∣∣ ∂W∂zi ∣∣∣2+VD+Vsoft Vtotal=∑i

∣∣∣ ∂W∂zi ∣∣∣2+VD+Vsoft

3 α†aλ (p)=∫d3x e−ip·x[eλ·(ωAa−iEa)+

∫Ω

(f1Πa+f2φa)] α†αλ (p)=

∫d3x e−ip·x[eλ·(ωAa−iEa)+

∫Ω

(f1Πa+f2φa)]

4 Hstat(k)=P+i

vk+iε , Hstat(k)=P+i

vk+iε ,

5 (φ∗Ps+φP∗s ) 2∆2

M2 , (φ∗Ps+φP∗s ) 2∆2

M2 ,

6 HG/H= 12 (πα− i~2 Γα)gαβ(πβ+ i~

2 Γβ)= 12παg

αβπβ+VG/H , HG/H= 12 (πα− i~2 Γα)gαβ(πβ+ i~

2 Γβ)= 12παg

αβπβ+VG/H ,

7 S[Φ]=S[φ]+S[ϕ]+Sint[φ,ϕ] S[Φ]=S[φ]+S[ϕ]+Sint[φ,ϕ]

8 γ1= κ4π γ2=λ

4 γ1= κ4π

9 Γ= 1−r28πMB

(|MS |2+|MP |2) . Γ= 1−r28πMB

(|MS |2+|MP |2) .

10 E(r)=−( 2GEνm2r ± q1q2r +

mνS1S2r ) E(r)=−( 2GEνm2

r ± q1q2r +mνS1S2

r )11 χ(x1,x2)=〈0|Tψ(x1)ψ(x2)|P 〉. χ(x1,x2)=〈0|Tψ(x1)ψ(x2)|P 〉.12 εL(2)θ=h

(2)1 v2φj(εγiθ)(θγijθ)+h

(2)1 vivjφk(εγiθ)(θγjkθ), εL(2)θ=h

(2)1 v2φj(εγiθ)(θγijθ)+h

(2)1 vivjφk(εγiθ)(θγjkθ),

13 sin2 2ϑ=sin2 2ϑsun=4 |Ue1|

2 |Ue2|2

(|Ue1|2+|Ue2|2)2. sin2 2ϑ=sin2 2ϑsun=

4|Ue1|2|Ue2|

2

(|Ue1|2+|Ue2|2)2.

14 F1 = g2

192π5/2MPl ' 1.5×1015 GeV. F1 = g2

192π5/2MPl ' 1.5×1015 GeV.

15 b<i>=∏

0≤p<q≤piXτp(i)τq(i)(z,z) . b<i>=

∑0≤p<q≤pXr(i) iτ(i)(z,z) .

16 FW+D

1 (x)=[dp(x)+up(x)+dn(x)+un(x)+2s(x)+2c(x)]/2. FW+D

1 (x)=[dp(x)+up(x)+dn(x)+un(x)+2s(x)+2c(x)]/2.

17 u=q2b2/2 or Q=Q0u, Q0= 1AmNb

2 u=q2b2/2 or Q=Q0u, Q0= 1AmNb

2

18 hv Γhv= 12 Tr(

ΓPv

)hv hv− 1

2 Tr(γµγ5 Pv ΓPv

)hv γ

µγ5 hv , hv Γhv= 12 Tr(ΓPv)hv hv− 1

2 Tr(γµγ5 Pv ΓPv)hv γµγ5 hv ,

19 Pg(z)=∆ns1z+ 1

1−z . Pg(z)=∆n1z+ 1

1−z .

20 S≤SH, T≥TH, Ec≤EBH, for HR≥1 S≤SH, T≥TH, Ec≤EBH, for HR≥1

21 Γ∼N−1/21023s−1 exp[− 8√

23·137 (−Eme )

3/2 B0NBA

1/2(mpme )1/2], Γ∼N−1/21023e−1 exp

[− 8√

23·137 (−Eme )

3/2 B0NBA

1/2(mpme )1/2],

22 2|J|2

m02∼=mb

2

md2∼2.5×105 .

2∫|2

m02∼=m2b

m2d

∼2.5×105 .

23 u= z`U−1/2 ∂

∂t , u= z`U−1/2 ∂

∂t ,

24 Ω= ρρc

Ω= ρρc

25 e(2r+1)πiL(0)Y1(v,x)e−(2r+1)πiL(0)=Y1((−1)L(0)v,−x), e(2r+1)πiL(0)Y1(v,x)e−(2r+1)πiL(0)=Y1((−1)L(0)v,−x),

26 A2=∫d2x A2(x)∗δα(x) , A2=

∫d2x A2(x)∗δα(x) ,

27 ds2=(k+f0R2

0R2 )−1dR2+R2dΩ2

k−(k+f0R2

0R2 )[dx5+AR(R)dR]2 ds2=(k+f0

R20

R2 )−1dR2+R2dΩ2k−(k+f0

R20

R2 )[dx5+AR(R)dR]2

28 [ρ0,ρ0]=0, [SA0 , [ρ0,ρ0]=0, [SA0 ,

29 L4=(F 1−∂5A2) dWdA1 +··· . L4=(F 1−∂5A

2) dWdA1 +··· .

30 ΨjΨi=δij−q−1RikjlΨlΨk ΨjΨi=δij−q−1RikljΨlΨk31 Πi=0 , Θk=∂kΠ0+cm2hk=0 , Πi=0 , Θk=∂kΠ0+cm2hk=0 ,

32 sin δ=s23c23s2c2

sin δ13 sin δ=s23c23s2c2

sin δ13

33 ∆A=−2H sAA , ∆A=−2H sAA ,

34 D−1µν=D−1

µαηαβ

(ηβν+

∑∞n=1 An(D−1)nβν

), D−1

µν=D−1µαη

αβ

(ηβν+

∑∞n=1 An(D−1)nβν

),

35 HG(x2)=− 18πGe2

([B(x2)]−2−1), HG(x2)=− 18πGe2

([B(x2)]−2−1),

36 Tµν=T+µν+

a2−a2+

T−µν . Tµν=T+µν+

a2−a2+

T−µν .

37 GHd+1= l1−d

Σd

∫+∞σl

dx

sinhd x. G

Hd+1= ll−dΣd

∫+∞q

dx

sinhd x.

38 Γ(n)

µ=δ(n)Γ(A′)

δA′µ1(x1)···δA′µj

(xj)···δA′µn (xn), Γ

(n)

µ=∂n(n)Γ(A′)

δA′µ1(x1)···δA′µµ (xj)···δAoµn (xn)

,

39 πab= πab=

40 γπ0a=0, γπia=fbacπ

ibηc2, γπ0i=0, γπij=0, γπ0

a=0, γπia=fbacπibηc2, γπ0i=0, γπij=0,

41 |Z1|2=|Z2|2= 1(4G)2

e−η0 [(QR1)2+(QR

2)2] , |Z1|2=|Z2|2= 1(4G)2

e−η0 [(QR1)2+(QR

2)2] ,

Figure 7: A random sample of predictions of I2L-STRIPS containing both good and bad predictions.Note that though this is a random sample, prediction mistakes are not obvious and it takes some effortto point them out! For more examples visit our website.105

11

Page 12: Teaching Machines to Code: Neural Markup …Teaching Machines to Code: Neural Markup Generation with Interpretable Attention Sumeet S. Singh Independent Researcher Saratoga, CA 95070

the non-linearity to tanh (to keep the activations in check) got us good results. Further reducingnumber of layers to 5 yielded the same performance, therefore we stuck with that configuration (Table3). In additon, we experimented with I2L-STRIPS because it reduces the rectangular image-map to alinear map, thereby presumably making the alignment model’s task easier because now it would onlyneed to scan in one-dimension. However, it performed around the same as I2L-NOPOOL and thereforethat hypothesis was debunked. In fact we prefer I2L-NOPOOL since it has fewer parameters and itsattention model has sharper focal-regions which helps with model interpretation.

A.2 Attention model

Table 4: Specification of the Visual AttentionModel MLP. L = 34 for I2L-STRIPS and and136 for I2L-NOPOOL.

Layer Num Units Activation3 (output) L softmax2 max(128, L) tanh1 max(256, L) tanh

Table 4 specifies the configuration of the attentionmodel MLP. Xu et al. 31’s formulation of attentionmodel (αt,l = MLP (al; Ht−1)) receives inputsfrom only a single image location. In comparison, ourformulation (αt = fatt (a; Ht−1)) receives the fullencoded image a in its input. This change was neededbecause the previous formulation did not progress be-yond a point, presumably because this problem war-ranted a wider receptive field. The new formulationworks equally well with different pooling strides (andcorrespondingly different values of L).

Also, Xu et al. 31’s formulation of zt = βt · αt · a includes a scalar βt = MLP (Ht−1) whichinforms the LSTM how much emphasis to place on the image v/s the language model. Experimentallywe found that it had no impact on end-to-end performance, therefore we dropped it from our model.

Xu et al. 31 also use a simpler formula for A =∑Ll=1 (

∑τt=1 αt,l − 1)

2 which they call ‘doublystochastic optimization’. Our formulation uses the true mean of αl, τ/L instead of 1, normalizes it toa fixed range so that it can be compared across models and more importantly, includes a target-ASEterm ASET . Without this term, i.e. with ASET = 0, A would bias the attention model towardsuniformly scanning all the L image locations. This is undesirable since there are many empty regionsof the images where it makes no sense for the attention model to spend much time. Conversely, thereare some densely populated regions (e.g. a symbol with complex superscript and subscripts) wherethe model would reasonably spend more time because it would have to produce a longer outputsequence. In other words, the optimal scanning pattern would have to be non-uniform - ASET 6= 0.Also, the scanning pattern would vary from sample to sample, but ASET is set to a single value (evenif zero) for all samples. Therefore we preferred to remove the attention-model bias altogether fromthe objective function by setting λA = 0 in all situations except when the attention model needed a’nudge’ in order to ‘get off the ground’. In such cases we set ASET based on observed values ofASEN (Table 8).

A.3 LSTM stack

Cell

Input Gate Output Gate

Forget Gate

Figure 8: LSTM Cell

it = σ (Wxixt +Whiht−1 +Wcict−1 + bi)

f t = σ (Wxfxt +Whfht−1 +Wcfct−1 + bf )

ct = f tct−1 + it tanh (Wxcxt +Whcht−1 + bc)

ot = σ (Wxoxt +Whoht−1 +Wcoct + bo)

ht = ot tanh(ct)

it,f t,ot, ct,ht ∈ Rn (12)

Our LSTM cell implementation (Figure. 8 and equation 12) follows Graves et al. [11], Zaremba et al.[33]. In equation 12 σ is the logistic sigmoid function and it, f t, ot, ct and ht are respectively theinput gate, forget gate, output gate, cell and hidden activation vectors of size n.

12

Page 13: Teaching Machines to Code: Neural Markup …Teaching Machines to Code: Neural Markup Generation with Interpretable Attention Sumeet S. Singh Independent Researcher Saratoga, CA 95070

During experimentation our penultimate LSTM-stack which had 3 LSTM layers with 1000 unitseach, gave us a validation score of 87.45%. At that point experimental observations suggested thatthe LSTM stack was the accuracy ’bottleneck’ because other sub-models were performing very well.Increasing the number of LSTM units to 1500 got us better validation score - but a worse overfit.Reducing the number of layers down to 2 got us the best overall validation score. In comparison, Xuet al. [31] have used a single LSTM layer with 1000 cells.

A.4 Deep output layer

Table 5: Configuration of the Deep Output LayerMLP.K = 339 and 358 for I2L-140K and Im2latex-90k datasets respectively.

Layer Num Units Activation3 (output) K softmax2 max(358, K) tanh1 max(358, K) tanh

Note that the output layer receives skip con-nections from the LSTM-Stack input (pt =fout(Ht; zt; Eyt−1)). We observed a 2% im-pact on the BLEU score with the addition ofinput-to-output skip-connections. This leads usto believe that adding skip-connections withinthe LSTM-stack may help further improvemodel accuracy. Overall accuracy also improvedby increasing the number of layers from 2 to 3.Lastly, observe that this sub-model is differentfrom Xu et al. [31] wherein the three inputs are affine-transformed into D dimensions, summed andthen passed through one fully-connected layer. After experimenting with their model we ultimatelychose to instead feed the inputs (concatenated) to a fully-connected layer thereby allowing the MLPto naturally learn the input-to-output function. We also increased the number of layers to 3, changedactivation function of hidden units from relu to tanh101 and ensured that each layer had at least asmany units as the softmax layer (K).

A.5 Init model

Table 6: Init Model layers.

Layer Num Units ActivationFunction

Output 2Q n tanhHidden 1 100 tanh

The init model MLP is specified in Table 6. Wequestioned the need for the Init Model and ex-perimented just using zero values for the initialstate. That caused a slight but consistent decline(< 1%) in the validation score, indicating thatthe initial state learnt by our Initial State Modeldid contribute in some way towards learningand generalization. Note however that our InitModel is different than 31, in that our version uses all L feature vectors of a while theirs takes theaverage. We also added a hidden layer and used tanh activation function instead of relu. We did startoff with their version but that did not provide an appreciable impact to the bottom line (validation).This made us hypothesize that perhaps taking an average of the feature vectors was causing a loss ofinformation; and we mitigated that by taking in all the L feature vectors without summing them. Aftermaking all these changes, the Init Model yields a consistent albiet small performance improvement(Table. 7). But given that it consumes ∼7.5 million parameters, its usefulness remains in question.

Table 7: Impact of the Init Model on overall performance. Since it comprises 10-12% of the totalparams, it may as well be omitted in exchange for a small performance hit.

Model Init Model Validation NumPresent? BLEU Params

I2L-NOPOOL Yes 89.09% 7,569,300I2L-NOPOOL No 88.20% 0I2L-STRIPS Yes 89.00% 7,569,300I2L-STRIPS No 88.74% 0

101We changed from relu to tanh partly in order to remedy ‘activation-explosions’ which were causingfloating-point overflow errors.

13

Page 14: Teaching Machines to Code: Neural Markup …Teaching Machines to Code: Neural Markup Generation with Interpretable Attention Sumeet S. Singh Independent Researcher Saratoga, CA 95070

A.6 Training and dataset

A.6.1 Alpha penalty

Please see equations 13 through 13e. The loss function equation stated in the paper is Equation13 but with λA set to 0. That was the case when training models who’s results we have published,however at other times we had included a penalty term λAA which we discuss next. Observethat while

∑Ll αt,l = 1, there is no constraint on how the attention is distributed across the L

locations of the image. The term λAA serves to steer the variance of αl by penalizing any deviationfrom a desired value. ASE (Alpha Squared Error) is the sum of squared-difference betweenαl and its mean τ/L; and ASEN is its normalized value 13 ∈ [0,100]14. Therefore ASEN ∝ASE ∝ σ2

αl. ASET which is the desired value of ASEN , is a hyperparameter that needs to be

discovered through experimentation15. Table 8 shows training results with alpha-penalty details.

Table 8: Training metrics. λR = 0.00005 and β2 = 0.9 for all runs.

Dataset Model Init λA β1 Training Training Validation ASENModel? Epochs BLEU ED

I2L-140K I2L-STRIPS Yes 0.0 0.5 104 0.9361 0.0677 5.3827I2L-STRIPS No 0.0 0.5 75 0.9300 0.0691 4.9899I2L-NOPOOL Yes 0.0 0.5 104 0.9333 0.0684 4.5801I2L-NOPOOL No 0.0 0.1 119 0.9348 0.0738 4.7099

Im2latex-90k I2L-STRIPS Yes 0.0 0.5 110 0.9366 0.0688 5.1237I2L-STRIPS No 0.0005 0.5 161 0.9386 0.0750 4.8291

J = −1

τlog (Pr (y|a)) + λRR+ λAA (13)

R =1

2

∑θ

θ2 (13a)

A = (ASEN −ASET ) (13b)

ASEN =100

τ2(L−1L

) ·ASE (13c)

ASE =

L∑l=1

(αl −

τ

L

)2

(13d)

αl :=

τ∑t=1

αt,l (13e)

Default values of β1andβ2 of the ADAM opti-mizer - 0.9 and 0.99 - yielded very choppy val-idation score curves with frequent down-spikeswhere the validation score would fall to verylow levels, ultimately resulting in lower peakscores. Reducing the first and second moments(i.e. β1andβ2) fixed the problem suggesting thatthe default momentum was too high for our ‘ter-rain’. We did not use dropout for regulariza-tion, however increasing the data-set size (I2L-140K) and raising the minimum-word-frequencythreshold from 24 (Im2latex-90k) to 50 ((I2L-140K)) did yield better generalization and over-all test scores (Table 8). Finally, normalizing thedata16 yielded about 25% more accuracy thanwithout.

13It can be shown that τ2(L−1L

)is the maximum possible value of ASE.

14We normalize ASE so that it may be compared across batches, runs and models.15Start with ASET = 0, observe where ASEN settles after training, then set ASET to that value and repeat

until approximate convergence.16Normalization was performed using the method and software used by [6] which parses the formulas into an

AST and then converts them back to normalized sequences.

14