SlowGuess/NAACL_2021_OCR huggingface dataset:
Jennifer C. White $^{\check{\mathcal{O}}}$  Tiago Pimentel $^{\check{\mathcal{O}}}$  Naomi Saphra $^{\check{\mathcal{O}}}$  Ryan Cotterell $^{\check{\mathcal{O}},\check{\mathcal{I}}}$ $^{\check{\mathcal{O}}}$ University of Cambridge,  $^{\check{\mathcal{O}}}$ University of Edinburgh, ETH Zürich
jw2088@cam.ac.uk, tp472@cam.ac.uk  
n.saphra@ed.ac.uk, ryan.cotterell@inf.ethz.ch
Probes are models devised to investigate the encoding of knowledge—e.g. syntactic structure—in contextual representations. Probes are often designed for simplicity, which has led to restrictions on probe design that may not allow for the full exploitation of the structure of encoded information; one such restriction is linearity. We examine the case of a structural probe (Hewitt and Manning, 2019), which aims to investigate the encoding of syntactic structure in contextual representations through learning only linear transformations. By observing that the structural probe learns a metric, we are able to kernelize it and develop a novel non-linear variant with an identical number of parameters. We test on 6 languages and find that the radial-basis function (RBF) kernel, in conjunction with regularization, achieves a statistically significant improvement over the baseline in all languages—implying that at least part of the syntactic knowledge is encoded non-linearly. We conclude by discussing how the RBF kernel resembles BERT's self-attention layers and speculate that this resemblance leads to the RBF-based probe's stronger performance.
Probing has been widely used in an effort to better understand what linguistic knowledge may be encoded in contextual word representations such as BERT (Devlin et al., 2019) and ELMo (Peters et al., 2018). These probes tend to be designed with simplicity in mind and with the intent of revealing what linguistic structure is encoded in an embedding, rather than simply learning to perform an NLP task (Hewitt and Liang, 2019; Zhang and Bowman, 2018; Voita and Titov, 2020) This preference for simplicity has often led researchers to place restrictions on probe designs that may not allow them to fully exploit the structure in which information is encoded (Saphra and Lopez, 2019; Pimentel et al.,
2020b,a). This preference has led many researchers to advocate the use of linear probes over non-linear ones (Alain and Bengio, 2017).
This paper treats and expands upon the structural probe of Hewitt and Manning (2019), who crafted a custom probe with the aim of investigating the encoding of sentence syntax in contextual representations. They treat probing for syntax as a distance learning problem: they learn a linear transformation that warps the space such that two words that are syntactically close to one another (in terms of distance in a dependency tree) should have contextual representations whose Euclidean distance is small. This linear approach performs well, but the restriction to learning only linear transformations seems arbitrary. Why should it be the case that this information would be encoded linearly within the representations?
In this paper, we recast Hewitt and Manning (2019)'s structural probing framework as a general metric learning problem. This reduction allows us to take advantage of a wide variety of non-linear extensions—based on kernelization—proposed in the metric learning literature (Kulis, 2013). These extensions lead to probes with the same number of parameters, but with an increased expressivity.
By exploiting a kernelized extension, we are able to directly test whether a structural probe that is capable of learning non-linear transformations improves performance. Empirically, we do find that non-linearity helps—a structural probe based on a radial-basis function (RBF) kernel improves performance significantly in all 6 languages tested over a linear structural probe. We then perform an analysis of BERT's attention, asserting it is a rough approximation to an RBF kernel. As such, it is not surprising that the syntactic information in BERT representations is more accessible with this specific non-linear transformation. We conclude that kernelization is a useful tool for analyzing contextual representations—enabling us to run controlled
Proceedings of the 2021 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies, pages 132-138 June 6-11, 2021. ©2021 Association for Computational Linguistics
experiments and investigate the structure in which information is encoded.
2 The Structural Probe
Hewitt and Manning (2019) introduce the structural probe, a novel model designed to probe for syntax in contextual word representations. We review their formulation here and build upon it in §4. A sentence  $\mathbf{w}$  lives in a space  $V^{*}$ , defined here as the Kleene closure of a (potentially open) vocabulary  $V$ . The syntactic distance  $\Delta_{ij}$  between any two words in a sentence  $\mathbf{w}$  is the number of steps needed to go from one word to the other while walking in the sentence's syntactic tree. More formally, if we have a dependency tree  $\mathbf{t}$  (a tree on  $n + 1$  nodes) of a sentence  $\mathbf{w}$  of length  $n$ , we define  $\Delta_{ij}$  as the length of the shortest path in  $\mathbf{t}$  between  $w_{i}$  and  $w_{j}$ ; this may be computed, for example, by Floyd-Warshall. Contextual representations of a sentence  $\mathbf{w}$  are a sequence of vectors  $\mathbf{h}_{i} \in \mathbb{R}^{d_{1}}$  that encode some linguistic knowledge about a sequence. In the case of BERT, we have
$$
\mathbf {h} _ {i} = \operatorname {B E R T} (\mathbf {w}) _ {i} \in \mathbb {R} ^ {d _ {1}} \tag {1}
$$
Here, the goal of probing is to evaluate whether the contextual representations capture the syntax in a sentence. In the case of the structural probe, the goal is to see whether the syntactic distance between any two words can be approximated by a learned, linear distance function:
$$
d _ {\mathbf {B}} \left(\mathbf {h} _ {i}, \mathbf {h} _ {j}\right) = \left\| \mathbf {B h} _ {i} - \mathbf {B h} _ {j} \right\| _ {2} \tag {2}
$$
where  $\mathbf{B} \in \mathbb{R}^{d_2 \times d_1}$  is a linear projection matrix. That is to say, they seek a linear transformation such that the transformed contextual representations relate to one another roughly as their corresponding words do in the dependency tree. To learn this probe, Hewitt and Manning minimize the following per-sentence objective with respect to  $\mathbf{B}$  through stochastic gradient descent
$$
\frac {1}{| \mathbf {w} | ^ {2}} \sum_ {i = 1} ^ {| \mathbf {w} |} \sum_ {j = i + 1} ^ {| \mathbf {w} |} \left| \Delta_ {i j} - d _ {\mathbf {B}} \left(\mathbf {h} _ {i}, \mathbf {h} _ {j}\right) \right| \tag {3}
$$
This is simply minimizing the difference between the syntactic distances obtained from the dependency tree and the distance between the two vectors under our learned transformation. From the pairwise distances predicted by the probe, Prim's (1957) algorithm can be used to recover the one-best undirected dependency tree.
3 Kernelized Metric Learning
The restriction to a linear transformation may hinder us from uncovering some of the syntactic structure encoded in the contextual representations. Indeed, there is no reason  $a$ -priori to expect that BERT encodes its knowledge in a fashion that is specifically accessible to a linear model. However, if we were to introduce non-linearity by using a neural probe, for example, we would have to pit a model with very few parameters (the linear model) against one with very many (the neural network); this comparison is not fair and also goes against the spirit of designing simple probes. To preclude the need for a neural probe, we instead turn to a kernelized probe.
The key insight is that the structural probe reduces the problem of probing for linguistic structure to that of metric learning (Kulis, 2013). This can be clearly seen in eq. (3), where the probe learns a distance metric between two representations in such a way that it matches the syntactic one. Recognizing this relationship allows us to take advantage of established techniques from the metric learning literature to improve the performance of the probe without increasing its complexity, e.g. through kernelization.
3.1 The "Kernel Trick" for Distances
Many algorithms in machine learning, e.g. support vector machines and  $k$ -means, can be kernelized (Schölkopf and Smola, 2002), thus allowing for linear models to be adapted into non-linear ones. Expanding on a classic result (Schoenberg, 1938), Schölkopf (2001) show that any positive semi-definite (PSD) kernel can be used to construct a distance in a Hilbert space  $\mathcal{H}$ . Formally, their result states that for any PSD kernel  $\kappa : \mathcal{X} \times \mathcal{X} \to \mathbb{R}_{\geq 0}$ , there exists a feature map  $\phi : \mathcal{X} \to \mathcal{H}$  such that
$$
\begin{array}{l} \left| \left| \phi (\mathbf {x}) - \phi (\mathbf {y}) \right| \right| _ {2} = \tag {4} \\ \sqrt {\kappa (\mathbf {x} , \mathbf {x}) - 2 \kappa (\mathbf {x} , \mathbf {y}) + \kappa (\mathbf {y} , \mathbf {y})} \\ \end{array}
$$
This generalizes eq. (2) to yield a new, non-linear distance metric. This means that we can achieve the effects of using some non-linear feature mapping  $\phi$  without having to specify it: we need only specify a kernel function and perform calculations using this kernelized distance metric. Importantly, as opposed to deep neural probes, this learnable metric has an identical number of parameters to the original.
<sup>1</sup>We note that we do not use selectivity (Hewitt and Liang, 2019) to control for probe complexity since it does not apply to
3.2 Common Kernels
In this section we introduce the kernels to be used. These kernels were chosen as they represent a comprehensive selection of commonly-used kernels in the metric learning literature (Kulis, 2013). The original work of Hewitt and Manning (2019) makes use of the linear kernel:
$$
\kappa_ {\text {l i n e a r}} \left(\mathbf {h} _ {i}, \mathbf {h} _ {j}\right) = \left(\mathbf {B h} _ {i}\right) ^ {\top} \left(\mathbf {B h} _ {j}\right) \tag {5}
$$
The first non-linear kernel we consider is the polynomial kernel, defined as
$$
\kappa_ {\mathrm {p o l y}} (\mathbf {h} _ {i}, \mathbf {h} _ {j}) = \left((\mathbf {B h} _ {i}) ^ {\top} (\mathbf {B h} _ {j}) + c\right) ^ {d} \quad (6)
$$
where  $d \in \mathbb{Z}_+$  and  $c \in \mathbb{R}_{\geq 0}$ . A polynomial kernel of degree  $d$  allows for  $d$ -order interactions between the terms. When working with BERT, this means that we may construct  $d$ -order conjunctions of the dimensions of the contextual representations input into the probe. Next, we consider the radial-basis function kernel (RBF). This kernel is also called the Gaussian kernel and is defined as
$$
\kappa_ {\mathrm {r b f}} \left(\mathbf {h} _ {i}, \mathbf {h} _ {j}\right) = \exp \left(- \frac {\left| \left| \mathbf {B h} _ {i} - \mathbf {B h} _ {j} \right| \right| ^ {2}}{2 \sigma^ {2}}\right) \tag {7}
$$
This kernel has an alternative interpretation as a similarity measure between both vectors, being at its maximum value of 1 when  $\mathbf{h}_i = \mathbf{h}_j$ . In contrast to the polynomial kernel, the Gaussian kernel implies a feature map in an infinite dimensional Hilbert space. When the RBF kernel is used in our probe, we may rewrite eq. (2) as follows:
$$
\begin{array}{l} d _ {\kappa_ {\mathrm {r b f}}} \left(\mathbf {h} _ {i}, \mathbf {h} _ {j}\right) ^ {2} \tag {8} \\ = \kappa_ {\mathrm {r b f}} (\mathbf {h} _ {i}, \mathbf {h} _ {i}) - 2 \kappa_ {\mathrm {r b f}} (\mathbf {h} _ {i}, \mathbf {h} _ {j}) + \kappa_ {\mathrm {r b f}} (\mathbf {h} _ {j}, \mathbf {h} _ {j}) \\ = 2 - 2 \kappa_ {\mathrm {r b f}} \left(\mathbf {h} _ {i}, \mathbf {h} _ {j}\right) \\ = 2 - 2 \exp \left(- \frac {\left| \left| \mathbf {B h} _ {i} - \mathbf {B h} _ {j} \right| \right| ^ {2}}{2 \sigma^ {2}}\right) \\ \end{array}
$$
Which is similar to the original linear case in eq. (2), but with a scaling term  $-\frac{1}{2\sigma^2}$  and a non-linearity  $\exp (\cdot)$ . Finally, we consider, the sigmoid kernel, which is defined as
$$
\kappa_ {\mathrm {s i g}} (\mathbf {h} _ {i}, \mathbf {h} _ {j}) = \tanh \left(a (\mathbf {B h} _ {i}) ^ {\top} (\mathbf {B h} _ {j}) + b\right) (9)
$$
this syntax tree reconstruction task—selectivity control tasks work at the word type level, as opposed to the sentence one.
$^{2}$ Lin and Lin (2003) observe that it is difficult to effectively tune  $a$  and  $b$  in the sigmoid kernel. They also note that although this kernel is not in fact PSD, it is PSD when  $a$  and  $b$  are both positive, which we enforce in this work.
4 Regularized Metric Learning
We also take advantage of two common regularization techniques employed in the metric learning literature to further improve the transformations learned; both act on the matrix  $\mathbf{A} = \mathbf{B}^{\top}\mathbf{B}$  and are added to the objective specified in eq. (3). The Frobenius norm regularizer takes the form
$$
r (\mathbf {A}) = \left\| \mathbf {A} \right\| _ {\mathrm {F}} ^ {2} = \operatorname {t r} \left(\mathbf {A} ^ {\top} \mathbf {A}\right) \tag {10}
$$
This is the matrix analogue of the  $L_{2}$  squared regularizer. Minimizing the Frobenius norm of the learned matrix has the effect of keeping the values in the matrix small. It has been a popular choice for regularization in metric learning with adaptations to a variety of problems (Schultz and Joachims, 2004; Kwok and Tsang, 2003). We also consider the trace norm regularizer, which is of the form
$$
r (\mathbf {A}) = \operatorname {t r} (\mathbf {A}) \tag {11}
$$
The trace norm regularizer is the matrix analogue of the  $L_{1}$  regularizer and it encourages the matrix  $\mathbf{A}$  to be low rank. As Jain et al. (2010) point out, using a low-rank transformation in conjunction with a kernel corresponds to a supervised kernel dimensionality reduction method.
We experiment with Hewitt and Manning's (2019) probe on 6 typologically diverse languages, following the experimental design of Hall Maudslay et al. (2020). Our data comes from the Universal Dependency 2.4 Treebank (Nivre et al., 2019), providing sentences and their dependency trees, annotated using the Universal Dependencies annotation scheme. For each sentence we calculate contextual representations using multilingual BERT. For all languages, we took the first 12,000 sentences (or the maximum number thereof) in the train portion of the treebank and created new 80-10-10 train-test-dev splits.
<sup>3</sup It was recently demonstrated by Kuznetsov and Gurevych (2020) that choice of linguistic formalism may have an impact on probing results. In this work, we investigate using only one formalism, so we cannot be sure that our results would not differ if an alternative formalism were used. Nonetheless, we believe that the results that we find most interesting, which are discussed in §6, should be robust to a change in formalism, since their explanation lies in the way attention is calculated in the transformer architecture.
4We cap the maximum number of sentences analyzed as a naive control for our multilingual analysis.
Kernel	Basque	English	Finnish	Korean	Tamil	Turkish
UUAS	DSpr	UUAS	DSpr	UUAS	DSpr	UUAS	DSpr	UUAS	DSpr	UUAS	DSpr
None	58.39	0.6737	57.96	0.7382	59.90	0.7560	68.63	0.7026	48.52	0.5116	58.87	0.6784
Polynomial	50.10	0.5751	59.67	0.7635	57.12	0.7401	67.58	0.6966	54.43	0.5776	55.29	0.6421
Sigmoid	43.14	0.4500	41.62	0.6152	53.14	0.6201	44.48	0.3734	44.30	0.3836	51.77	0.5557
RBF	60.99	0.6937	62.77	0.7213	63.08	0.7382	71.87	0.6918	56.96	0.5379	61.67	0.6841
Table 1: Results of probes using various kernels, in terms of UUAS and DSpr
We present the results from our comparison of a re-implementation of Hewitt and Manning's (2019) linear structural probe and the non-linear kernelized probes in Table 1. The two evaluation metrics shown are unlabeled undirected attachment score (UUAS) and the Spearman rank-order correlation (DSpr) between predicted distances and gold standard pairwise distances. UUAS is a standard parsing metric expressing the percentage of correct attachments in the dependency tree, while DSpr is a measure of how accurately the probe predicts the overall ordering of distances between words. We can see that the use of an RBF kernel results in a statistically significant improvement in performance, as measured by UUAS, in all 6 of the languages tested. For some languages this improvement is quite substantial, with Tamil seeing an improvement of 8.44 UUAS from the baseline probe to the RBF kernel probe.
6 The RBF Kernel and Self-Attention
The RBF kernel produces improvements across all analyzed languages. This suggests that it is indeed the case that syntactic structure is encoded nonlinearly in BERT. As such, analyzing this specific kernel may yield insights into what this structure is. Indeed, none of the other kernels systematically improve over the linear baseline, implying this is not just an effect of the non-linearity introduced through use of a kernel—the specific structure of the RBF kernel must be responsible. In this section, we argue that the reason that the RBF kernel serves as such a boon to probing is that it resembles BERT's attention mechanism; recall that BERT's attention mechanism is defined as
$$
\operatorname {a t t} \left(\mathbf {h} _ {i}, \mathbf {h} _ {j}\right) \propto \exp \left(\frac {\left(\mathbf {K h} _ {i}\right) ^ {\top} \left(\mathbf {Q h} _ {j}\right)}{\sqrt {d _ {2}}}\right) \tag {12}
$$
where  $\mathbf{K}$  and  $\mathbf{Q}$  are linear transformations and  $d_{2}$  is the dimension vectors are projected into.
$\mathbf{K}$  projects vector  $\mathbf{h}_i$  into a key vector, while  $\mathbf{Q}$  projects  $\mathbf{h}_j$  into a query one. When the key and query vectors are similar (i.e. have a high dot product), the value of this equation is large and word  $j$  attends to word  $i$ .
This bears a striking resemblance to the Gaussian kernel. Indeed, if we assume the linearly transformed representations have unit norm, i.e.
$$
\left| \left| \mathbf {B h} _ {i} \right| \right| ^ {2} = \left| \left| \mathbf {B h} _ {j} \right| \right| ^ {2} = 1 \tag {13}
$$
$$
\begin{array}{l} \exp \left(\frac {- 1}{2 \sqrt {d _ {2}}} \left| \left| \mathbf {B h} _ {i} - \mathbf {B h} _ {j} \right| \right| ^ {2}\right) \tag {14} \\ = \exp \left(\frac {- 1}{\sqrt {d _ {2}}} + \frac {\left(\mathbf {B h} _ {i}\right) ^ {\top} \left(\mathbf {B h} _ {j}\right)}{\sqrt {d _ {2}}}\right) \\ \propto \exp \left(\frac {(\mathbf {B h} _ {i}) ^ {\top} (\mathbf {B h} _ {j})}{\sqrt {d _ {2}}}\right) \\ \end{array}
$$
where we take  $\sigma^2 = \sqrt{d_2}$ . The similarity between eqs. (12) and (14) suggests the attention mechanism in BERT is, up to a multiplicative factor, roughly equivalent to an RBF kernel—as such, it is not surprising that the RBF kernel produces the strongest results.
The resemblance between these equations, taken together with the significant improvements in capturing syntactic distance, suggest that this encoded information indeed lives in an RBF-like space in BERT. Such information can then be used in its self-attention mechanism; allowing BERT to pay attention to syntactically close words when solving the cloze language modeling task. Being attentive to syntactically close words would also be supported by recent linguistic research, since words sharing syntactic dependencies have higher mutual information on average (Futrell et al., 2019).
The representations we analyze, though, are taken from BERT's final layer; as such, they are not trained to be used in any self-attention layer—so why should such a resemblance be relevant? BERT's architecture is based on the Transformer
${}^{5}$  Significance was established using paired permutation tests with 10,000 samples,to the level of  $p < {0.05}$  .
(Vaswani et al., 2017), and uses skip connections between each self-attention layer. Such skip connections create an incentive for residual learning, i.e. only learning residual differences in each layer, while propagating the bulk of the information (He et al., 2016). As such, BERT's final hidden representations should roughly live in the same manifold as its internal ones.
It is interesting to note that the RBF kernel achieves the best performance in terms of UUAS in all languages, but it only twice achieves the best performance in terms of DSpr. This may be due to the fact that, as we can see by examination of eq. (8), the distance returned by the RBF kernel will not exceed 2, whereas syntactic distances in the tree will. Further, the gradient of the RBF kernel contains an exponential term which will cause it to go to zero as distance increases (while an examination of the unkernelized loss function reveals the opposite behavior). This means that it will be less sensitive to the distances between syntactically distant words and focus more on words with small distances. This may partially explain its better performance on UUAS, and comparably worse performance as measured by correlation (which counts pairwise differences between all words, not just those which are directly attached in the tree). Furthermore, our probe's focus on nearby words resembles the general attentional bias towards syntactically close words (Voita et al., 2019).
The direct resemblance between self-attention mechanisms and our proposed probe metric poses a new way of understanding results from more complex probes. While Reif et al. (2019) understood the Euclidean-squared distance of Hewitt and Manning as an isometric tree embedding, their geometric interpretation did not factor in the rest of BERT's architecture. Such simplified contextless probes cannot tell us how linguistic properties are processed by a sequence of learned modules (Saphra and Lopez, 2019). However, we consider representations in the context of the model which is expected to employ them. From this perspective, simpler metrics may be rough approximations to our RBF kernel space, which is actually capable of measuring linguistic properties that can be easily extracted by an attention-based architecture.
We find that the linear structural probe (Hewitt and Manning, 2019) used to investigate the encoding
of syntactic structure in contextual representations can be improved through kernelization, yielding a non-linear model. This kernelization does not introduce additional parameters and thus does not increase the complexity of the probe—at least if one treats the number of parameters as a good proxy for model complexity. At the same time, the RBF kernel improves probe performance in all languages under consideration. This suggests that syntactic information may be encoded non-linearly in the representations produced by BERT. We hypothesize that this is true due to the similarity of the RBF kernel and BERT's self-attention layers.
The authors foresee no ethical concerns with the research presented in this paper.