SlowGuess/ACL_2020_OCR huggingface dataset:
Yu Zhang, Zhenghua Li\*, Min Zhang
Institute of Artificial Intelligence, School of Computer Science and Technology, Soochow University, Suzhou, China
yzhang.cs@outlook.com, {zhli13,minzhang} @suda.edu.cn
In the deep learning (DL) era, parsing models are extremely simplified with little hurt on performance, thanks to the remarkable capability of multi-layer BiLSTMs in context representation. As the most popular graph-based dependency parser due to its high efficiency and performance, the bioffine parser directly scores single dependencies under the arc-factorization assumption, and adopts a very simple local token-wise cross-entropy training loss. This paper for the first time presents a second-order TreeCRF extension to the bioffine parser. For a long time, the complexity and inefficiency of the inside-outside algorithm hinder the popularity of TreeCRF. To address this issue, we propose an effective way to batchify the inside and Viterbi algorithms for direct large matrix operation on GPUs, and to avoid the complex outside algorithm via efficient back-propagation. Experiments and analysis on 27 datasets from 13 languages clearly show that techniques developed before the DL era, such as structural learning (global TreeCRF loss) and high-order modeling are still useful, and can further boost parsing performance over the state-of-the-art bioaffine parser, especially for partially annotated training data. We release our code at https://github.com/yzhangcs/crfpar.
As a fundamental task in NLP, dependency parsing has attracted a lot of research interest due to its simplicity and multilingual applicability in capturing both syntactic and semantic information (Nivre et al., 2016). Given an input sentence  $\pmb{x} = w_0w_1\ldots w_n$ , a dependency tree, as depicted in Figure 1, is defined as  $\pmb{y} = \{(i,j,l), 0 \leq i \leq n, 1 \leq j \leq n, l \in \mathcal{L}\}$ , where  $(i,j,l)$  is a dependency from the head word  $w_i$  to the modifier word
Figure 1: An example full dependency tree. In the case of partial annotation, only some (not all) dependencies are annotated, for example, the two thick (blue) arcs.
$w_{j}$  with the relation label  $l\in \mathcal{L}$ . Between two mainstream approaches, this work focuses on the graph-based paradigm (vs. transition-based).
Before the deep learning (DL) era, graph-based parsing relies on many hand-crafted features and differs from its neural counterpart in two major aspects. First, structural learning, i.e., explicit awareness of tree structure constraints during training, is indispensable. Most non-neural graph-based parsers adopt the max-margin training algorithm, which first predicts a highest-scoring tree with the current model, and then updates feature weights so that the correct tree has a higher score than the predicted tree.
Second, high-order modeling brings significant accuracy gains. The basic first-order model factors the score of a tree into independent scores of single dependencies (McDonald et al., 2005a). Second-order models were soon propose to incorporate scores of dependency pairs, such as adjacent-siblings (McDonald and Pereira, 2006) and grand-parent-child (Carreras, 2007; Koo and Collins, 2010), showing significant accuracy improvement yet with the cost of lower efficiency and more complex decoding algorithms.
In contrast, neural graph-based dependency parsing exhibits an opposite development trend. Pei et al. (2015) propose to use feed-forward neural
Third-order and fourth-order models show little accuracy improvement probably due to the feature sparseness problem (Koo and Collins, 2010; Ma and Zhao, 2012).
Proceedings of the 58th Annual Meeting of the Association for Computational Linguistics, pages 3295-3305 July 5 - 10, 2020. ©2020 Association for Computational Linguistics
networks for automatically learning combinations of dozens of atomic features similar to Chen and Manning (2014), and for computing subtree scores. They show that incorporating second-order scores of adjacent-sibling subtrees significantly improved performance. Then, both Wang and Chang (2016) and Kiperwasser and Goldberg (2016) propose to utilize BiLSTM as an encoder and use minimal feature sets for scoring single dependencies in a first-order parser. These three representative works all employ global max-margin training. Dozat and Manning (2017) propose a strong and efficient bi-affine parser and obtain state-of-the-art accuracy on a variety of datasets and languages. The biaffine parser is also first-order and employs simpler and more efficient non-structural training via local head selection for each token (Zhang et al., 2017).
Observing such contrasting development, we try to make a connection between pre-DL and DL techniques for graph-based parsing. Specifically, the first question to be addressed in this work is: can previously useful techniques such as structural learning and high-order modeling further improve the state-of-the-art $^2$  biaffine parser, and if so, in which aspects are they helpful?
For structural learning, we focus on the more complex and less popular TreeCRF instead of max-margin training. The reason is two-fold. First, estimating probability distribution is the core issue in modern data-driven NLP methods (Le and Zuidema, 2014). The probability of a tree, i.e.,  $p(\boldsymbol{y} \mid \boldsymbol{x})$ , is potentially more useful than an unbounded score  $s(\boldsymbol{x}, \boldsymbol{y})$  for high-level NLP tasks when utilizing parsing outputs. Second, as a theoretically sound way to measure model confidence of subtrees, marginal probabilities can support Minimum Bayes Risk (MBR) decoding (Smith and Smith, 2007), and are also proven to be crucial for the important research line of token-level active learning based on partial trees (Li et al., 2016).
One probable reason for the less popularity of TreeCRF, despite its usefulness, is due to the complexity and inefficiency of the inside-outside algorithm, especially the outside algorithm. As far as we know, all existing works compute the inside and outside algorithms on CPUs. The inefficiency issue becomes more severe in the DL era, due to
the unmatched speed of CPU and GPU computation. This leads to the second question: can we batchify the inside-outside algorithm and perform computation directly on GPUs? In that case, we can employ efficient TreeCRF as a built-in component in DL toolkits such as PyTorch for wider applications (Cai et al., 2017; Le and Zuidema, 2014).
Overall, targeted at the above two questions, this work makes the following contributions.
- We for the first time propose second-order TreeCRF for neural dependency parsing. We also propose an efficient and effective triaffine operation for scoring second-order subtrees.
- We propose to batchify the inside algorithm via direct large tensor computation on GPUs, leading to very efficient TreeCRF loss computation. We show that the complex outside algorithm is no longer needed for the computation of gradients and marginal probabilities, and can be replaced by the equally efficient back-propagation process.
- We conduct experiments on 27 datasets from 13 languages. The results and analysis show that both structural learning and high-order modeling are still beneficial to the state-of-the-art biaffine parser in many ways in the DL era.
2 The Basic Biaffine Parser
We re-implement the state-of-the-art biaffine parser (Dozat and Manning, 2017) with two modifications, i.e., using CharLSTM word representation vectors instead of POS tag embeddings, and the first-order Eisner algorithm (Eisner, 2000) for projective decoding instead of the non-projective MST algorithm.
Scoring architecture. Figure 2 shows the scoring architecture, consisting of four components.
Input vectors. The  $i$ th input vector is composed of two parts: the word embedding and the CharLSTM word representation vector of  $w_{i}$ .
$$
\mathbf {e} _ {i} = \operatorname {e m b} \left(w _ {i}\right) \oplus \operatorname {C h a r L S T M} \left(w _ {i}\right) \tag {1}
$$
where  $\mathrm{CharLSTM}(w_i)$  is obtained by feeding  $w_{i}$  into a BiLSTM and then concatenating the two last hidden vectors (Lample et al., 2016). We find that replacing POS tag embeddings with
2Though many recent works report higher performance with extra resources, for example contextualized word representations learned from large-scale unlabeled texts under language model loss, they either adopt the same architecture or achieve similar performance under fair comparison.
Figure 2: Scoring architecture with second-order extension.
$\operatorname{CharLSTM}(w_i)$  leads to consistent improvement, and also simplifies the multilingual experiments by avoiding POS tag generation (especially n-fold jackknifing on training data).
BiLSTM encoder. To encode the sentential contexts, the parser applies three BiLSTM layers over  $\mathbf{e}_0\ldots \mathbf{e}_n$ . The output vector of the top-layer BiLSTM for the  $i$ th word is denoted as  $\mathbf{h}_i$ .
MLP feature extraction. Two shared MLPs are applied to  $\mathbf{h}_i$ , obtaining two lower-dimensional vectors that detain only syntax-related features:
$$
\mathbf {r} _ {i} ^ {h}; \mathbf {r} _ {i} ^ {m} = \mathrm {M L P} ^ {h / m} \left(\mathbf {h} _ {i}\right) \tag {2}
$$
where  $\mathbf{r}_i^h$  and  $\mathbf{r}_i^m$  are the representation vector of  $w_{i}$  as a head word and a modifier word respectively.
Biaffine scorer. Dozat and Manning (2017) for the first time propose to compute the score of a dependency  $i \to j$  via biaffine attention:
$$
s (i, j) = \left[ \begin{array}{c} \mathbf {r} _ {j} ^ {m} \\ 1 \end{array} \right] ^ {\mathrm {T}} \mathbf {W} ^ {b i a f f i n e} \mathbf {r} _ {i} ^ {h} \tag {3}
$$
where  $\mathbf{W}^{biaffine} \in \mathbb{R}^{d \times d}$ . The computation is extremely efficient on GPUs.
Local token-wise training loss. The biaffine parser adopts a simple non-structural training loss, trying to independently maximize the local probability of the correct head word for each word. For a gold-standard head-modifier pair  $(w_{i},w_{j})$  in a training instance, the cross-entropy loss is
$$
L (i, j) = - \log \frac {e ^ {s (i , j)}}{\sum_ {0 \leq k \leq n} e ^ {s (k , j)}} \tag {4}
$$
In other words, the model is trained based on simple head selection, without considering the tree structure at all, and losses of all words in a minibatch are accumulated.
Decoding. Having scores of all dependencies, we adopt the first-order Eisner algorithm with time complexity of  $O(n^{3})$  to find the optimal tree.
$$
\boldsymbol {y} ^ {*} = \arg \max  _ {\boldsymbol {y}} \left[ s (\boldsymbol {x}, \boldsymbol {y}) \equiv \sum_ {i \rightarrow j \in \boldsymbol {y}} s (i, j) \right] \tag {5}
$$
Handling dependency labels. The bioffine parser treats skeletal tree searching and labeling as two independent (training phase) and cascaded (Parsing phase) tasks. This work follows the same strategy for simplicity. Please refer to Dozat and Manning (2017) for details.
3 Second-order TreeCRF
This work substantially extends the biaffine parser in two closely related aspects: using probabilistic TreeCRF for structural training and explicitly incorporating high-order subtree scores. Specifically, we further incorporate adjacent-sibling subtree scores into the basic first-order model: $^3$
$$
s (\boldsymbol {x}, \boldsymbol {y}) = \sum_ {i \rightarrow j \in \boldsymbol {y}} s (i, j) + \sum_ {i \rightarrow \{k, j \} \in \boldsymbol {y}} s (i, k, j) \tag {6}
$$
where  $k$  and  $j$  are two adjacent modifiers of  $i$  and satisfy either  $i < k < j$  or  $j < k < i$ .
As a probabilistic model, TreeCRF computes the conditional probability of a tree as
$$
p (\boldsymbol {y} \mid \boldsymbol {x}) = \frac {e ^ {s (\boldsymbol {x} , \boldsymbol {y})}}{Z (\boldsymbol {x}) \equiv \sum_ {\boldsymbol {y} ^ {\prime} \in \mathcal {Y} (\boldsymbol {x})} e ^ {s (\boldsymbol {x} , \boldsymbol {y} ^ {\prime})}} \tag {7}
$$
where  $\mathcal{V}(\pmb{x})$  is the set of all legal (projective) trees for  $\pmb{x}$ , and  $Z(\pmb{x})$  is commonly referred to as the normalization (or partition) term.
During training, TreeCRF employs the following structural training loss to maximize the conditional probability of the gold-standard tree  $\mathbf{y}$  given  $\mathbf{x}$ .
$$
\begin{array}{l} L (\boldsymbol {x}, \boldsymbol {y}) = - \log p (\boldsymbol {y} \mid \boldsymbol {x}) \tag {8} \\ = - s (\boldsymbol {x}, \boldsymbol {y}) + \log Z (\boldsymbol {x}) \\ \end{array}
$$
This work can be further extended to incorporate grandparent-modifier subtree scores based on the viterbi algorithm of  $O(n^{4})$  time complexity proposed by Koo and Collins (2010), which we leave for future work.
Figure 3: Diagrams of the second-order inside algorithm based on bottom-up dynamic programming.
3.1 Scoring Second-order Subtrees
To avoid major modification to the original scoring architecture, we take a straightforward extension to obtain scores of adjacent-sibling subtrees. First, we employ three extra MLPs to perform similar feature extraction.
$$
\mathbf {r} _ {i} ^ {h ^ {\prime}}; \mathbf {r} _ {i} ^ {s}; \mathbf {r} _ {i} ^ {m ^ {\prime}} = \mathrm {M L P} ^ {h ^ {\prime} / s / m ^ {\prime}} \left(\mathbf {h} _ {i}\right) \tag {9}
$$
where  $\mathbf{r}_i^{h'}; \mathbf{r}_i^s; \mathbf{r}_i^{m'}$  are the representation vectors of  $w_i$  as head, sibling, and modifier respectively.
Then, we propose a natural extension to the bi-affine equation, and employ triaffine for score computation over three vectors.
$$
s (i, k, j) = \left[ \begin{array}{c} \mathbf {r} _ {k} ^ {s} \\ 1 \end{array} \right] ^ {\mathrm {T}} \mathbf {r} _ {i} ^ {h ^ {\prime} \mathrm {T}} \mathbf {W} ^ {\text {t r i a f f i n e}} \left[ \begin{array}{c} \mathbf {r} _ {j} ^ {m ^ {\prime}} \\ 1 \end{array} \right] \tag {10}
$$
where  $\mathbf{W}^{tri\text{affine}} \in \mathbb{R}^{d' \times d' \times d'}$  is a three-way tensor. The triaffine computation can be quite efficiently performed with the einsum function on PyTorch.
3.2 Computing TreeCRF Loss Efficiently
The key to TreeCRF loss is how to efficiently compute  $\log Z(x)$ , as shown in Equation 8. This problem has been well solved long before the DL era for non-neural dependency parsing. Straightforwardly, we can directly extend the viterbi decoding algorithm by replacing max product with sum
Algorithm 1 Second-order Inside Algorithm.
1: define:  $I, S, C \in \mathbb{R}^{n \times n \times B}$  ▷  $B$  is #sents in a batch
2: initialize:  $C_{i,i} = \log e^0 = 0, 0 \leq i \leq n$
3: for  $w = 1$  to  $n$  do
4: Batchify:  $0 \leq i$ ;  $j = i + w \leq n$
5:  $I_{i,j} = \log \left(\sum_{i <   r <   j}e^{I_{i,r} + S_{r,j} + s(i,r,j)}\right) + \mathrm{s(i,j)}$
6:  $S_{i,j} = \log \sum_{i\leq r <   j}e^{C_{i,r} + C_{j,r + 1}}$
7:  $C_{i,j} = \log \sum_{i < r \leq j} e^{I_{i,r} + C_{r,j}}$
8: end for  $\triangleright$  refer to Figure 3
9: return  $C_{0,n} \equiv \log Z$
product, and naturally obtain  $\log Z(x)$  in the same polynomial time complexity. However, it is not enough to solely perform the inside algorithm for non-neural parsing, due to the inapplicability of the automatic differentiation mechanism. In order to obtain marginal probabilities and then feature weight gradients, we have to realize the more sophisticated outside algorithm, which is usually at least twice slower than the inside algorithm. This may be the major reason for the less popularity of TreeCRF (vs. max-margin training) before the DL era.
As far as we know, all previous works on neural TreeCRF parsing explicitly implement the inside-outside algorithm for gradient computation (Zhang et al., 2019; Jiang et al., 2018). To improve efficiency, computation is transferred from GPUs to CPUs with Cython programming.
This work shows that the inside algorithm can be effectively batchified to fully utilize the power of GPUs. Figure 3 and Algorithm 1 together illustrate the batchified version of the second-order inside algorithm, which is a direct extension of the second-order Eisner algorithm in McDonald and Pereira (2006) by replacing max product with sum product. We omit the generations of incomplete, complete, and sibling spans in the opposite direction from  $j$  to  $i$  for brevity.
Basically, we first pack the scores of same-width spans at different positions  $(i,j)$  for all  $B$  sentences in the data batch into large tensors. Then we can do computation and aggregation simultaneously on GPUs via efficient large tensor operation.
Similarly, we also batchify the decoding algorithm. Due to space limitation, we omit the details.
It is noteworthy that the techniques described here are also applicable to other grammar formulations such as CKY-style constituency parsing (Finkel et al., 2008; Drozdov et al., 2019).
Another way is to use one extra MLP for sibling representation, and re-use head and modifier representation from the basic first-order components, which however leads to inferior performance in our preliminary experiments.
We have also tried the approximate method of Wang et al. (2019), which uses three biaffine operations to simulate the interactions of three input vectors, but observed inferior performance. We omit the results due to the space limitation.
3.3 Outside via Back-propagation
Eisner (2016) proposes a theoretical proof on the equivalence between the back-propagation mechanism and the outside algorithm in the case of constituency (phrase-structure) parsing. This work empirically verifies this equivalence for dependency parsing.
Moreover, we also find that marginal probabilities  $p(i \to j \mid x)$  directly correspond to gradients after back-propagation with  $\log Z(x)$  as the loss:
$$
\frac {\partial \log Z}{\partial \mathrm {s} (i , j)} = \sum_ {\boldsymbol {y}: (i, j) \in \boldsymbol {y}} p (\boldsymbol {y} \mid \boldsymbol {x}) = p (i \rightarrow j \mid \boldsymbol {x}) \tag {11}
$$
which can be easily proved. For TreeCRF parsers, we perform MBR decoding (Smith and Smith, 2007) by replacing scores with marginal probabilities in the decoding algorithm, leading to a slight but consistent accuracy increase.
3.4 Handling Partial Annotation
As an attractive research direction, studies show that it is more effective to construct or even collect partially labeled data (Nivre et al., 2014; Hwa, 1999; Pereira and Schabes, 1992), where a sentence may correspond to a partial tree  $|y^p| < n$  in the case of dependency parsing. Partial annotation can be very powerful when combined with active learning, because annotation cost can be greatly reduced if annotators only need to annotate sub-structures that are difficult for models. Li et al. (2016) present a detailed survey on this topic. Moreover, Peng et al. (2019) recently released a partially labeled multi-domain Chinese dependency treebank based on this idea.
Then, the question is how to train models on partially labeled data. Li et al. (2016) propose to extend TreeCRF for this purpose and obtain promising results in the case of non-neural dependency parsing. This work applies their approach to the neural biaffine parser. We are particularly concerned at the influence of structural learning and high-order modeling on the utilization of partially labeled training data.
For the basic biaffine parser based on first-order local training, it seems the only choice is omitting losses of unannotated words. In contrast, tree constraints allow annotated dependencies to influence the probability distributions of unannotated words, and high-order modeling further helps by promoting inter-token interaction. Therefore, both
structural learning and high-order modeling are intuitively very beneficial.
Under partial annotation, we follow Li et al. (2016) and define the training loss as:
$$
\begin{array}{l} L (\boldsymbol {x}, \boldsymbol {y} ^ {p}) = - \log \sum_ {\boldsymbol {y} \in \mathcal {Y} (\boldsymbol {x}); \boldsymbol {y} \supseteq \boldsymbol {y} ^ {p}} p (\boldsymbol {y} \mid \boldsymbol {x}) \\ Z (\boldsymbol {x}, \boldsymbol {y} ^ {p}) \equiv \sum e ^ {s (\boldsymbol {x}, \boldsymbol {y})} \\ = - \log \frac {\mathbf {y} \in \mathcal {Y} (\mathbf {x}) ; \mathbf {y} \supseteq \mathbf {y} ^ {p}}{Z (\mathbf {x})} \tag {12} \\ \end{array}
$$
where  $Z(\pmb{x}, \pmb{y}^p)$  only considers all legal trees that are compatible with the given partial tree and can also be efficiently computed like  $Z(\pmb{x})$ .
Data. We conduct experiments and analysis on 27 datasets from 13 languages, including two widely used datasets: the English Penn Treebank (PTB) data with Stanford dependencies (Chen and Manning, 2014), and the Chinese data at the CoNLL09 shared task (Hajic et al., 2009).
We also adopt the Chinese dataset released at the NLPCC19 cross-domain dependency parsing shared task (Peng et al., 2019), containing one source domain and three target domains. For simplicity, we directly merge the train/dev/test data of the four domains into larger ones respectively. One characteristic of the data is that most sentences are partially annotated based on active learning.
Finally, we conduct experiments on Universal Dependencies (UD) v2.2 and v2.3 following Ji et al. (2019) and Zhang et al. (2019) respectively. We adopt the 300d multilingual pretrained word embeddings used in Zeman et al. (2018) and take the CharLSTM representations as input. For UD2.2, to compare with Ji et al. (2019), we follow the raw text setting of the CoNLL18 shared task (Zeman et al., 2018), and directly use their sentence segmentation and tokenization results. For UD2.3, we also report the results of using gold-standard POS tags to compare with Zhang et al. (2019).
Evaluation metrics. We use unlabeled and labeled attachment score (UAS/LAS) as the main metrics. Punctuations are omitted for PTB. For the partially labeled NLPCC19 data, we adopt the official evaluation script, which simply omits the words without gold-standard heads to accommodate partial annotation. We adopt Dan Bikel's randomized parsing evaluation comparator for significance test.
Figure 4: Parsing speed comparison on PTB-test.
Parameter settings. We directly adopt most parameter settings of Dozat and Manning (2017), including dropout and initialization strategies. For CharLSTM, the dimension of input char embeddings is 50, and the dimension of output vector is 100, following Lample et al. (2016). For the second-order model, we set the dimensions of  $\mathbf{r}_i^{h' / s / m'}$  to 100, and find little accuracy improvement when increasing to 300. We trained each model for at most 1,000 iterations, and stop training if the peak performance on the dev data does not increase in 100 consecutive epochs.
Models. LOC uses local cross-entropy training loss and employs the Eisner algorithm for finding the optimal projective tree. CRF and CRF20 denote the first-order and second-order TreeCRF model respectively.  $\mathrm{LOC}_{\mathrm{MST}}$  denotes the basic local model that directly produces non-projective tree based on the MST decoding algorithm of Dozat and Manning (2017).
4.1 Efficiency Comparison
Figure 4 compares the parsing speed of different models on PTB-test. For a fair comparison, we run all models on the same machine with Intel Xeon CPU (E5-2650v4, 2.20GHz) and GeForce GTX 1080 Ti GPU. "CRF (CPU)" refers to the model that explicitly performs the inside-outside algorithm using Cython on CPUs. Multi-threading is employed since sentences are mutually independent. However, we find that using more than 4 threads does not further improve the speed.
We can see that the efficiency of TreeCRF is greatly improved by batchifying the inside algorithm and implicitly realizing the outside algorithm by back-propagation on GPUs. For the first-order CRF model, our implementation can parse about 500 sentences per second, over 10 times faster than the multi-thread "CRF (CPU)". For the second-order CRF2O, our parser achieves the speed of 400