UML IX: Kernels and Boosting

https://www.lesswrong.com/posts/QExXmkRmsufYQLsoQ/uml-ix-kernels-and-boosting

Contents

Kernels

To motivate this chapter, consider some training sequence S = ((x_1,y_1), ..., (x_m,y_m)) with instances in some domain set \mathcal{X}. Suppose we wish to use an embedding \psi : \mathcal{X} \rightarrow \mathbb{R}^d of the kind discussed in the previous post (i.e., to make the representation of our points more expressive, so that they can be classified by a hyperplane). Most importantly, suppose that d is significantly larger than m. In such a case, we’re describing each point \psi(x_i) in terms of d coordinates, even though our space only has m points, which means that there can, in some sense, only be m "relevant" directions. In particular, let \hspace{30pt} U := \text{span}(\psi(S_\text{x})) = {\mathbf{p} \in \mathbb{R}^d \hspace{5pt} | \hspace{5pt} \exists \mathbf{a} \in \mathbb{R}^m : \mathbf{p} = \sum_{i = 1}^d \alpha_i \psi(x_i)} where S_\text{x} is the training sequence without labels, so that \psi(S_\text{x}) = (\psi(x_1), ..., \psi(x_m)). Then U is an (at most) m-dimensional subspace of \mathbb{R}^d, and we would like to prove that we can work in U rather than in \mathbb{R}^d.

I

As a first justification for this goal, observe that \psi(x_i) \in U for all i \in [m]. (The symbol [n] for any n \in \mathbb{N} denotes the set {1, ..., n}.) Recall that we wish to learn a hyperplane parametrized by some \mathbf{w} \in \mathbb{R}^d that can then be used to predict a new instance \psi(y) for some y \in \mathcal{X} by checking whether \langle \mathbf{w}, \psi(y) \rangle > 0. The bulk of the difficulty, however, lies in finding the vector \mathbf{w}; this is generally much harder than computing a single inner product \langle \mathbf{w}, \psi(y) \rangle. Thus, our primary goals are to show that (1) \mathbf{w} will lie in U (2) \mathbf{w} can somehow be computed by only working in U To demonstrate this, we need to look at how \mathbf{w} is chosen, which depends on the algorithm we use. In the case of Soft Support Vector Machines (previous post), we choose \hspace{50pt} \displaystyle \mathbf{w} \in \text{arg} \hspace{-3pt}\min_{\mathbf{w} \in \mathbb{R}^d} \left( \lambda ||\mathbf{w}||^2 + \frac{1}{m} \sum_{k = 1}^m \max[0, 1 - y_k\langle\mathbf{w}, \psi(x_k)\rangle]\right). This rule shows that we only care about the inner product between \mathbf{w} and our mapped training points, the \psi(x_i). Thus, if we could somehow prove (1), then (2) would seem to follow: if \mathbf{w} \in U, then, according to the rule above, we would only end up caring about inner products between points that are both in U. Therefore, we now turn to proving (1) formally. To have the result be a bit more general (so that it also applies to algorithms other than Soft Support Vector Machines), we will analyze a more general minimization problem. We assume that \hspace{50pt} \displaystyle \mathbf{w} \in \text{arg} \hspace{-3pt}\min_{\mathbf{w} \in \mathbb{R}^d} \big[ f_{(y_1, ..., y_m)}(\langle \mathbf{w}, \psi(x_1) \rangle, ..., \langle \mathbf{w}, \psi(x_m)\rangle) + R(||\mathbf{w}||^2)\big] where f is any function and R is any monotonically non-decreasing function. (You might verify that the original problem is an instance of this one.) Now let \mathbf{w}^* be a solution to the above problem. Then we can use extended orthogonal decomposition^1 to write \mathbf{w}^* = \pi(\mathbf{w}^) + \mathbf{q}, where \pi : \mathbb{R}^d \rightarrow U is the projection onto U that leaves vectors in U unchanged and \mathbf{q} is orthogonal to every vector in U. Then, for any \mathbf{u} \in U, we have \hspace{30pt} \langle \mathbf{w}^, \mathbf{u}\rangle = \langle \pi(\mathbf{w}^) + \mathbf{q}, \mathbf{u}\rangle = \langle \pi(\mathbf{w}^), \mathbf{u}\rangle + \langle \mathbf{q}, \mathbf{u}\rangle = \langle \pi(\mathbf{w}^), \mathbf{u}\rangle. In particular, this is true for all the \psi(x_i). Furthermore, since R is non-decreasing and the norm of \mathbf{w}^ is at least as large as the norm of \pi(\mathbf{w}^) (note that ||\mathbf{w}^||^2 = ||\pi(\mathbf{w}^)||^2 + ||\mathbf{u}||^2 due to the Pythagorean theorem), this shows that \pi(\mathbf{w}^) is a solution to the optimization problem. Moreover, if R is strictly monotonically increasing (as is the case for Soft Support Vector Machines), then if \mathbf{q} > 0, it would also be better than \mathbf{w}^, which is impossible since \mathbf{w}^ is by assumption optimal. Thus, \mathbf{q} must be 0, which implies that not only some but all solutions lie in U. [1] Regular orthogonal decomposition, as I’ve formulated in the previous post, only guarantees that \mathbf{u} is orthogonal to \psi(\mathbf{w}^*) rather than to every vector in U. But the extended version is no harder to prove. Choose some *orthonormal *basis B of U, extend it to an orthonormal basis B' of all of \mathbb{R}^d (amazingly, this is always possible), and define \pi by \pi(\sum_{i = 1}^{|B'|} \alpha_i b_i) = \sum_{i = 1}^{|B|} \alpha_i b_i; i.e., just discard all basis elements that belong to B' but not B. That does the job.

II

We’ve demonstrated that only the inner products between mapped training points matter for the training process. Another way to phrase this statement is that, if we have access to the function \hspace{50pt} K_\psi : \mathcal{X} \times \mathcal{X} \rightarrow \mathbb{R} \hspace{50pt}K_\psi : (x,y) \mapsto \langle \psi(x),\psi(y) \rangle we no longer have any need to represent the points \psi(x_k) explicitly. The function K is what is called the kernel function, that gives the chapter its name. Note that K takes two arbitrary points in \mathcal{X}; it is not restricted to elements in the training sequence. This is important because, to actually apply the predictor, we will have to compute \langle \mathbf{w}, \psi(y) \rangle for some y \in \mathcal{X}, as mentioned above. But to train the predictor, we only need inner products between mapped training points, as we’ve shown. Thus, if we set \hspace{50pt} g_{k, \ell} := K(x_k, x_\ell) = \langle \psi(x_k), \psi(x_\ell) \rangle \hspace{20pt} \forall k,\ell \in [m] then we can do our training based solely on the g_{k, \ell} (which will lead to a predictor that uses K to classify domain points.) Now let’s reformulate all our relevant terms to that end. Recall that we have just proved that \mathbf{w}^* \in U. This implies that \mathbf{w}^* = \sum_{i = 1}^m \alpha_i \psi(x_i) for the right α_i. Also recall that our objective is to find \mathbf{w}^* in the set \hspace{40pt} \text{arg}\hspace{-2pt}\max_{\mathbf{w} \in U} f(\langle \mathbf{w}, \psi(x_1) \rangle, ..., \langle \mathbf{w}, \psi(x_m)\rangle) + R(||\mathbf{w}||^2)
Now we can reformulate \hspace{10pt}\langle \mathbf{w}, \psi(x_k) \rangle = \langle \sum_{i = 1}^m \alpha_i \psi(x_i), \psi(x_k) \rangle = \sum_{i = 1}^m \alpha_i \langle \psi(x_i), \psi(x_k) \rangle = \sum_{i = 1}^m \alpha_i g_{i,k} for all k \in [m], and \hspace{10pt}||\mathbf{w}||^2 = \langle \mathbf{w}, \mathbf{w} \rangle = \langle \sum_{i = 1}^m \alpha_i \psi(x_i), \sum_{i = 1}^m \alpha_i \psi(x_i) \rangle = \sum_{k,\ell = 1}^m \alpha_k \alpha_\ell g_{k,\ell}. Plugging both of those into the term behind the \text{argmax}, we obtain \hspace{40pt} f(\sum_{i = 1}^m \alpha_i g_{i,1}, ..., \sum_{i = 1}^m \alpha_i g_{i,m}) + R(\sum_{k,\ell = 1}^m \alpha_k \alpha_\ell g_{k,\ell}) This is enough to establish that one can learn purely based on the g_{k, \ell}. Unfortunately, the Machine Learning literature has the annoying habit of writing everything that can possibly be written in terms of matrices and vectors in terms of matrices and vectors, so we won’t quite leave it there. By setting \mathbf{\alpha} := (\alpha_1, ..., \alpha_m) (a row vector), we can further write the above as \hspace{40pt} f([\mathbf{\alpha G}]1, ..., [\mathbf{\alpha G}]m) + R(\alpha \mathbf{G} \alpha^T) \hspace{20pt} \text{where} \hspace{20pt}\mathbf{G} = (g{k,\ell}){\substack{1 \le k \le m \ 1 \le \ell \le m}} or even as f((\mathbf{\alpha G})) + R(\alpha \mathbf{G} \alpha^T), at which point we’ve successfully traded any conceivable intuition for compactness. Nonetheless, the point that \mathbf{G} is sufficient for learning still stands.\mathbf{G} is also called the Gram matrix. And for predicting a new point \psi(y), we have \hspace{10pt} \langle \mathbf{w}, \psi(y) \rangle = \langle \sum_{i = 1}^m \alpha_i \psi(x_i), \psi(y) \rangle = \sum_{i = 1}^m \alpha_i \langle \psi(x_i), \psi(y) \rangle = \sum_{i = 1}^m \alpha_i K(x_i, \psi(y)). At this point, you might notice that we never represented U explicitly, but just reformulated everything in terms of inner products. Indeed, one could introduce kernels without mentioning U, but I find that thinking in terms of U is quite helpful for understanding why all of this stuff works. Note that the above equation (where we predict the label of a new instance) is not an exception to the idea that we’re working in U. Even though it might not be immediately apparent from looking at it, it is indeed the case that we could first project \psi(y) into U without changing anything about its prediction. In other words, it is indeed the case that \langle \mathbf{w}, \psi(y) \rangle = \langle \mathbf{w}, \pi(\psi(y)) \rangle for all y \in \mathcal{X}. This follows from the definition of \pi and the fact that all basis vectors outside of U are orthogonal to everything in U.

III

Kernels allow us to deal with arbitrarily high-dimensional data (even infinitely dimensional) by computing m^2 distances, and later do some additional computations to apply the output predictor – under the essential condition that we are able to evaluate the kernel function K. Thus, we are interested in embeddings \psi such that K_\psi is easy to evaluate. For an important example, consider an embedding for multi-variable polynomials. Suppose we have such a polynomial of the form p : \mathbb{R}^n \rightarrow \mathbb{R}, i.e. something like \hspace{80pt} p(x,y,z) = x^2yz^2 + 3xyz^2 - 2x^3z^2 + 12y^2 where the above would be a 3-variable polynomial of degree 5. Now recall that, to learn one-dimensional polynomials with linear methods, we chose the embedding \psi : x \mapsto (1, x, x^2, ..., x^k). That way, a linear combination of the image coordinates can do everything a polynomial predictor can do. To do the same for an arbitrary n-dimensional polynomial of degree k, we need the far more complex embedding \hspace{30pt} \psi : \mathbb{R}^n \rightarrow \mathbb{R}^{(n+1)^k} \hspace{30pt} \psi : (x_1, ..., x_n) \mapsto(\prod_{i = 1}^k x_{w(i)} ){\mathbf{w} \in {0, ..., n}^{k}} An n-dimensional polynomial of degree k may have one value for each possible combination of its n variables such that at most k variables appear in each term. Each \mathbf{w} \in {0, ..., n}^k defines such a combination. Note that this is a sequence, so repetitions are allowed: for example, the sequence (1,2, ..., 2) \in {0, ..., n}^k corresponds to the term x_1 x_2^{k-1}. We set x_0 = 1 so that we also catch all terms with degree less than k: for example, the sequence (0,0,0,3, ..., 3) corresponds to the term x_3^{k-3} and the sequence (0, ..., 0) to the absolute value of the polynomial. For large n and k this target space is extremely high-dimensional, but we’re studying kernels here, so the whole point will be that we won’t have to represent it explicitly. Now suppose we have two such instances \psi(\mathbf{x}) and \psi(\mathbf{x'}). Then,\hspace{30pt} \langle \psi(\mathbf{x}), \psi(\mathbf{x'})\rangle = \Big\langle(\prod{i = 1}^k x_{w(i)} ){\mathbf{w} \in {0, ..., n}^{k}}, (\prod{i = 1}^k x'{w(i)} ){\mathbf{w} \in {0, ..., n}^{k}} \Big\rangle \ \hspace{30pt} \phantom{\langle \psi(\mathbf{x}), \psi(\mathbf{x'})\rangle} = \sum_{\mathbf{w} \in {0, ..., n}^{k}} \Big\langle\prod_{i = 1}^k x_{w(i)}, \prod_{i = 1}^k x'{w(i)} \Big\rangle \ \hspace{30pt} \phantom{\langle \psi(\mathbf{x}), \psi(\mathbf{x'})\rangle} = \sum{\mathbf{w} \in {0, ..., n}^{k}} \prod_{i = 1}^k x_{w(i)} x'{w(i)} And for the crucial step, the last term can be rewritten as \big( \sum{i = 0}^n x_ix'_i \big)^k – both terms include all sequences x_i x'_i of length k where i \in {0, ..., n}. Now (recall that x_0 = x'0 = 1) this means that the above sum simply equals (1 + \langle \mathbf{x}, \mathbf{x}' \rangle)^k. In summary, this calculation shows that \hspace{30pt} K(\mathbf{x}, \mathbf{x}') := (1 + \langle \mathbf{x}, \mathbf{x}' \rangle)^k = \langle \psi(\mathbf{x}), \psi(\mathbf{x}')\rangle \hspace{20pt} \forall \mathbf{x}, \mathbf{x}' \in \mathcal{X} Thus, even though \psi maps points into the very high-dimensional space \mathbb{R}^{(n+1)^k}, it is nonetheless feasible to learn a multi-polynomial predictor through linear methods, namely by embedding the values via \psi and then ignoring \psi and using K instead. The gram matrix G will consist of m^2 entries, where for each, a term of the form (1 + \langle \mathbf{x}, \mathbf{x}' \rangle)^k = (1 + \sum{i = 1}^n x_i x'_i)^k has to be computed. This doesn’t look that scary! Even for relatively large values of d, k, and m, it should be possible to compute on a reasonable machine. If we do approach learning a multi-dimensional polynomial in this way, then (I think) there are strong reasons to question in what sense the embedding \psi actually happens – this question is what I was trying to wrap my head around at the end of the previous post. It seemed questionable to me that \psi is fundamental even if the problem is learned without kernels, but even more so if it is learned with them. And that is all I have to say about kernels. For the second half of this post, we’ll turn to a largely independent topic.

Boosting

***Boosting ***is another item under the "widening the applicability of classes" category, much like the \psi from earlier.

I

This time, the approach is not to expand the representation of data and then apply a linear classifier on that representation. Instead, we wish to construct a complex classifier as a linear combination of simple classifiers. When hyperplanes are visualized, it is usually understood that one primarily cares about hyperplanes in higher-dimensional spaces where they are much more expressive, despite the illustration depicting an instance in 2-d or 3-d. But this time, think of the problem instance below in literal 2-d space: No hyperplane can classify this instance correctly, but consider a combination of these three hyperplanes: By letting h(\mathbf{p}) = \sigma_{\text{sign}}(h_1(\mathbf{p}) + h_2(\mathbf{p}) + h_3(\mathbf{p}) - 2.5) where h_i is the predictor corresponding to the i-th hyperplane and \sigma_\text{sign} is the sign function, we have constructed a predictor h which has zero empirical error on this training instance. Perhaps more surprisingly, this trick can also learn non-convex areas. The instance below, will be classified correctly by letting h(\mathbf{p}) = \sigma_{\text{sign}}(h_1(\mathbf{p}) +2 h_2(\mathbf{p}) + 2 h_3(\mathbf{p})), with the h_i (ordered left to right) defined like so: These two examples illustrate that the resulting class is quite expressive. The question is, how to learn such a linear combination?

II

First, note that hyperplanes are just an example; the framework is formulated in terms of a learning algorithm that has access to a weak learner, where An algorithm > A is called a \gamma-weak learner for a hypothesis class \mathcal{H} iff there is a function w^* : (0,1) \rightarrow \mathbb{N} such that, for any probability distribution \mathcal{D} over \mathcal{X} \times \mathcal{Y} and any failure probability \delta \in (0,1), if S consists of at least w^*(\delta) i.i.d. points sampled via \mathcal{D}, then with probability at least 1-\delta over the choice of S, it holds that \ell(A(S)) \le \frac{1}{2} - \gamma.If you recall the definition of PAC learnability back from chapter 1, you’ll notice that this is very similar. The only difference is in the error: PAC learning demands that it be arbitrarily close to the best possible error, while a weak learner merely has to bound it away from \frac{1}{2} by some fixed amount \gamma, which can be quite small. Thus, a weak learner is simply an algorithm that puts out a predictor that performs a little bit better than random. In the first example, the upper hyperplane could be the output of a weak learner. The term "boosting" refers to the process of upgrading this one weak learner into a better one, precisely by applying it over and over again under the supervision of a smartly designed algorithm – – which brings us back to the question of how to define such an algorithm. The second example (the non-convex one) illustrates a key insight here: repeatedly querying the weak learner on the unaltered training instance is unlikely to be fruitful, because the third hyperplane by itself performs worse than random, and will thus not be output by a \gamma-weak learner (not for any \gamma \in \mathbb{R}_+). To remedy this, we somehow need to prioritize the points we’re currently getting wrong. Suppose we begin with the first two hyperplanes. At this point, we have classified the left and middle cluster correctly. If we then weigh the right cluster sufficiently more strongly than the other two, eventually, h_3 will perform better than random. Alas, we wish to adapt our weighting of training points dynamically, and we can do this in terms of a probability distribution over the training sequence. Now the roadmap for defining the algorithm which learns a predictor on a binary classification problem via boosting is as follows:

III

Let’s first look into how to define our probability distribution, which will be the most complicated part of the algorithm. Suppose we have our current distribution D^{(t)} based on past predictors h_1, ..., h_{t-1} output by A_\gamma, and suppose further that we have computed weights w_1, ..., w_{t-1} such that w_i measures the quality of h_i (higher is better). Now we receive a new predictor h_t with quality w_t. Then we can define a new probability distribution D^{(t+1)} by letting \hspace{50pt} D^{(t+1)}((x_i, y_i)) \propto D^{(t)}((x_i, y_i)) \cdot e^{-w_t y_i h_t(x_i)} \hspace{10pt} \forall i \in [m] where we write \propto rather than = because the term isn’t normalized; it will equal the above scaled such that all probabilities sum to 1. The term y_i h_t(x_i) is 1 iff predictor h_t classified x_i correctly. Thus, the right component of the product equals e^{-w_t} iff the point was classified correctly, and e^{w_t} if it wasn’t. If h_t is a bad predictor and w_t is small, say 10^{-3}, the two terms are both close to 1, and we don’t end up changing our weight on (x_i, y_i) very much. But if h_t is good and w_t is large, the old weight D^{(t)}((x_i, y_i)) will be scaled significantly upward (if it got the point wrong) or downward (if it got the point right). In our second example, the middle hyperplane performs quite well on the uniform distribution, so w_2 should be reasonably high, which will cause the probability mass on the right cluster to increase and on the two other clusters to decrease. If this is enough to make the right cluster dominate the computation, then the weak learner might output the right hyperplane next. If not, it might output the second hyperplane again. Eventually, the weights will have shifted enough for the third hyperplane to become feasible.

IV

Now let’s look at the weights. Let \epsilon_t = \ell^{0-1}_S(h_t) be the usual empirical error of h_t, i.e., \epsilon_t = \frac{1}{m}|{(x,y) \in \mathcal{S} \hspace{5pt} | \hspace{5pt} h_t(x) \neq y}|. We would like w_i to be a real number, which starts close to 0 for \epsilon_t close to \frac{1}{2} and grow indefinitely for \epsilon_t close to 0. One possible choice is w_t := \frac{1}{2}\ln(\frac{1}{\epsilon_t} - 1). You can verify that it has these properties – in particular, recall that h_t is output by a weak learner so that its error is bounded away from \frac{1}{2} by at least \gamma. Because of this, \frac{1}{\epsilon_t} is larger than 2 so that \frac{1}{\epsilon_t} - 1 is larger than 1 and w_t is larger than 0.

V

To summarize, **AdaBoost (A_\gamma : weak learner, S : training sequence, T : \mathbb{N}+) \hspace{0pt}\hspace{10pt}D^{(0)} \leftarrow (\frac{1}{m} \cdots \frac{1}{m}) \hspace{0pt}\hspace{10pt}for (t \leftarrow 1 \text{ to } T) do \hspace{0pt}\hspace{10pt}\hspace{10pt} h_t \leftarrow A\gamma(S,D^{(t-1)}) \hspace{20pt}\epsilon_t \leftarrow \frac{1}{m}|{(x,y) \in S \hspace{5pt} | \hspace{5pt} h_t(x) \neq y}| \hspace{20pt}w_t \leftarrow \frac{1}{2}\ln(\frac{1}{\epsilon_t} - 1) \hspace{20pt}D^{(t)} \leftarrow \text{normalize}\big((D_i \cdot e^{-{w_ty_ih_t(x_i)}})_{i \in [m]}\big) ** \hspace{10pt}endfor \hspace{10pt}**return **f : x \mapsto \sigma_{\text{sign}}(\sum_{t = 1}^T w_t h_t(x)) end

VI

If one assumes that A_\gamma always returns a predictor with error at most \frac{1}{2} - \gamma (recall that it may fail with probability \delta), one can derive a bound on the error of the output predictor. Fortunately, the dependence of the sample complexity on \delta is only logarithmic, so \delta can probably be pushed low enough that A_\gamma is unlikely to fail even if it is called T times. Now the error bound one can derive is e^{-2\gamma^2T}. Looking at this, it has exactly the properties one would expect: a higher \gamma pushes the error down, and so do more rounds of the algorithm. On the other hand, doing more rounds increases the chance of overfitting to random quirks in the training data. Thus, the parameter T allows one to balance the overfitting vs. underfitting tradeoff, which is another nice thing about AdaBoost.The book mentions that Boosting has been successfully applied to the task of classifying gray-scale images into ‘contains a human face’ and ‘doesn’t contain a human face’. This implies that human faces can be recognized using a set of quantitative rules – but, importantly, rules which have been generated by an algorithm rather than constructed by hand. (In that case, the weak learner did not return hyperplanes, but simple predictors of another form.) In this case, the result fits with my intuition (that face recognition is the kind of task where a set-of-rules approach will work). It would be interesting to know how well boosting performs on other problems.

Comment

https://www.lesswrong.com/posts/QExXmkRmsufYQLsoQ/uml-ix-kernels-and-boosting?commentId=isYQJMvKxAu8XTCGQ

Just wanted to thank you for writing up this series. I’ve been slowly going through the book on my own. Just finished Chapter 2 and it’s awesome to have these notes to review.