Fast Sketching of Polynomial Kernels of Polynomial Degree
Zhao Song, David P. Woodruff, Zheng Yu, Lichen Zhang
Introduction
Kernel methods are a powerful tool for solving non-parametric learning problems, such as kernel regression, support vector machines (SVM), principal component analysis (PCA), and many others. A typical burden for kernel methods is that they suffer from scalability, since computing a kernel matrix requires computing a quadratic (in the number of input points) number of entries in the matrix. A direction that has received less attention but still of particular interest is the regime where the dimension of the data points is large. Typically, applying the kernel function to each pair of data points takes time. This is especially undesirable in applications for natural language processing [DL20] and computational biology [TPK02], where can be as large as , with being the number of data points. To compute the kernel matrix, the algorithm does have to read the input matrix. Therefore, algorithms that have a nearly linear dependence on are of particular interest.
To accelerate the computation of kernel matrices from the naïve time algorithm, a lot of work has focused on finding a good approximation to a kernel matrix efficiently [RR07, AM15, MM17, AKK+20, WZ20]. All of these methods make use of randomized algorithmic primitives such as sampling or sketching. Roughly speaking, the idea is to randomly generate a “sketching matrix" with a small number of rows, multiply the sketching matrix with the input matrix, and show that the resulting matrix approximately preserves the length of vectors in the row or column space of the original matrix.
Does there exist a sketch for polynomial kernels of degree , such that the runtime is nearly linear in , and with an improved dependence on ?
Notice this is especially desirable for kernels such as the neural tangent kernel () [JGH18] and the arc-cosine kernel [CS09], whose Taylor series have a much slower decay rate ( for some ) compared to the Gaussian kernel (which is ).
We develop an efficient algorithm that computes a sketch of the polynomial kernel of degree in time linear in and nearly linear in .
Our algorithm only uses two distinct sketches compared to the independent sketches of [AKK+20]. This enables us to use repeated powering to compute our sketch very efficiently.
We characterize kernel matrices by considering their Taylor series, and provide different algorithmic schemes to solve them. Our characterization includes a family of interesting and popular kernels. We also use this sketch as a preconditioner for solving linear systems involving a kernel matrix, and we extend our sketch to solve kernel ridge regression, by composing it with another sketch that depends on the statistical dimension.
Sketching techniques for tensor-related problems
Sketching techniques have been used extensively in tensor-related problems, e.g., for linear-algebraic problems involving polynomial kernels [ANW14, AKK+20, WZ20], for tensor low-rank approximation [SWZ19], and for tensor regression [HLW17, DSSW18, DJS+19].
Subspace embeddings
An (oblivious) subspace embedding is a useful concept in randomized numerical linear algebra introduced by Sárlos [Sar06]. Many applications rely on subspace embeddings or their variants, such as linear regression, low-rank approximation [CW13, NN13, MM13, BW14, BWZ16, SWZ17, ALS+18], tensor decomposition [SWZ19], cutting plane methods [JLSW20], and linear programming [LSZ19, JSWZ21, SY21]
Roadmap
In Section 2, we introduce definitions, notations and some basic facts. In Section 3, we present a technical overview of our results. In Section 4, we propose an efficient algorithm to generate a sketch and apply it to a polynomial kernel of arbitrary positive integer degree . In Section 5, we analyze our algorithm with a specific sketching matrix. In Section 6, we present applications to the Gaussian kernel and a more general class of kernels, which can be characterized through the coefficients of their Taylor expansion. We also discuss how to use our sketch as a preconditioner for solving kernel linear systems, and solve sketched kernel ridge regression.
Preliminaries
We define an oblivious subspace embedding ([Sar06]) as follows:
We also introduce tensor products of vectors and Kronecker products of matrices.
The Kronecker product of matrices is a natural extension of the tensor product of vectors:
An important property of the Kronecker product is the so-called mixed product property:
For matrices with appropriate sizes, the following holds:
We will extensively use the following notation:
2 Sketching Matrices
We recall the Subsampled Randomized Hadamard Transform (), which is a Fast Johnson-Lindenstrauss transform [AC06].
Using the Fast Fourier Transform (FFT) [CT65], can be applied to a vector in time .
We also introduce a sketching matrix for degree- tensors, which is a generalization of the .
By leveraging the FFT algorithm in the sketch space, can be computed in time .
We will use the following properties of the and .
Let be an matrix defined in Definition 2.7. If , then is an -.
Let be a matrix defined in Definition 2.9. If , then is an - for degree- tensors.
3 Kernels
We introduce several kernels that are widely-used in practice, e.g., see [GE08, CHC+10] for the polynomial kernel, and see [NJW01] for the Gaussian kernel.
Technical Overview
We first consider one way to compute the polynomial kernel via the identity . Our algorithm will try to compute quickly.
This allows for a much faster way to compute : “square” a vector by computing the tensor product with itself, then apply a sketch, and repeat this process. By doing so, we reduce the dependence on in the first level of the tree from linear to logarithmic. However, this will incur a factor in the dimension of the sketch, and so we will pay more for in levels other than the first level. Fortunately, levels other than the first level apply sketches to lower dimensional vectors. By carefully balancing the complexity of applying and , we achieve an improved running time, which is useful when the degree is large.
Fast Sketching Algorithm for the Polynomial Kernel
We introduce our algorithm that sketches a single vector and extend it to each column of a matrix. In Section 4.1 we give some definitions. In Section 4.2 we prove several technical tools related to tensors. In Section 4.3 we show our sketch preserves the column space of the polynomial kernel. In Section 4.4 we prove our main result for this section.
We define the sketching matrix formed by Algorithm 1 as follows:
where T^{q}=\underbrace{T\times T\times\ldots\times T}_{\text{qtimes}} and , and S^{l}=\underbrace{S\times S\times\ldots\times S}_{\text{ltimes}}.
We will design an algorithm that achieves the following goal:
Case 1 If is a power of , then it computes efficiently.
Case 2 If is not a power of , then let be its binary representation and let
We will iterate through all indices in and continue tensoring two vectors where , and apply to them.
2 Equivalence Results for Tensors
We provide two technical tools for handling tensors.
If is a power of , then Algorithm 1 will output on line 8. We will exploit the fact that although Algorithm 1 only computes one vector at a time, we can view it as computing identical vectors in the iteration. On line 3, we can treat it as computing copies of , and therefore, by Claim 2.5, we have
We can apply the same line of reasoning to line 4 of the algorithm. In the iteration, we can treat it as
a total of times. Again, using Claim 2.5, we have
Recursively applying this identity, we will end up with
Next, we wish to show that if is a power of , then preserves the subspace spanned by the columns of within a factor of . Notice this is weaker than being an , but sufficient for our application to polynomial kernels. We will show that
The following lemma outlines the main technique in our proof of this property. It establishes a one-to-one mapping between a vector in the column span of and a matrix. We then use this equivalence to inductively prove that preserves the target subspace.
On the other hand, we can write in its column form:
Recall that is a diagonal matrix and therefore, the product can be expressed as
Using the outer product definition of matrix product, we have
This is a matrix of size . Therefore, we can write its Frobenius norm as
3 Preserving the Polynomial Kernel Subspace
We prove the sketch generated by Algorithm 1 preserves the column space of the polynomial kernel.
We proceed by induction on . For , by Definition 2.1, we have
For the inductive step, we will prove this for a general positive integer . We break into and . By Lemma 4.4, we have
Recall that is an for . This means right multiplying by preserves the length of all columns of , and therefore we have
Applying this to each column of , we have
As we motivated in Section 3, if we view Algorithm 1 as a binary tree, Lemma 4.5 effectively proves that the bottom layer of the tree preserves the column space of . We then pick to be an for degree- tensors, and inductively establish our embedding.
We will prove this by induction on the number of iterations of Algorithm 1. Let ; we will induct on the parameter from to . Let denote the sketching matrix at level : and . We will prove the following statement: , we have
Note that when , we have , and therefore it gives us the desired result.
For the base case, note that Lemma 4.5 automatically gives our desired result. For the inductive step, we assume it holds for some , so
Notice that . Let be defined as the matrix
where we use to denote the column of after the iteration. From Algorithm 1, we have that , and so the product can be written as .
If , then similar to Lemma 4.5,
The third step uses the same reasoning as Lemma 4.5, i.e., we can pull out by paying an extra factor. The last line uses the inductive hypothesis.
If , then we will end up with , and can simply use the fact that is an to argue that preserves the length of . We then use the inductive hypothesis on to conclude the proof. ∎
Below, we state and prove a theorem that establishes the correctness of Algorithm 1 without instantiating the sketching matrix and . This enables us to use different sketches with various trade-offs.
4 Main Result
We prove the main result of this section, which establishes the correctness of Algorithm 1.
Let be the binary representation of , and let . If is a power of , by Lemma 4.7, we are done. So suppose is not a power of . Let . Algorithm 1 computes and combines intermediate results with indices in to form the final result. We will again prove this by induction on the indices in , from smallest to largest. For the base case, let be an index in and let . Since is a power of , Lemma 4.7 establishes this case.
For the inductive step, suppose this holds for , and let . We will prove this holds for . Let denote the matrix after the application of this recursive process. We will show that
We first use the fact is an to obtain
Combining Eq. (2) and Eq. (4.4), we obtain Eq. (1), which is our desired result. ∎
In this section, we analyze the runtime of Algorithm 1 with being an sketch (Definition 2.7) and being a sketch (Definition 2.9).
The goal of this section is to give a runtime analysis of Algorithm 1 using as and as .
Moreover, using Algorithm 1, can be computed in time .
We will use an for and a for . We pick both of these sketches to be -s where . Let be the matrix generated by Algorithm 1 with these parameters. By Theorem 4.8, we have
By Taylor expanding around , we have
Thus, by picking , we have
For both and to be s, we need .
We now analyze the runtime of Algorithm 1 under and . On line 2, we compute in time since is an . We then enter a loop with iterations, where in each iteration we apply to the tensor product of a column with itself resulting from the previous iteration. Since is a , this takes time per column, and there are columns, so time in total for this step. We also compute each bit in the binary representation, which incurs an factor in the final runtime. So it takes Algorithm 1 time to compute . This completes the proof. ∎
2 Discussion
We compare our result with the results obtained in [AKK+20, WZ20]. The setting we are considering is 1) matrix is dense, i.e., , and 2) . In such a scenario, [AKK+20] obtains a sketching dimension and the runtime of applying the sketch to is , so our result improves the dependence on the term and pays only instead of on the second term. Another result from [WZ20] has but the time to apply sketching is , which is much worse in the leading term, compared to our result. However, we also point out the results obtained in these two works are more general than ours in the sense that their sketches have the property, while our sketch only preserves the column space of . Nevertheless, the latter suffices for our applications. We use Table 1 to summarize and compare the different results.
Also, the prior results mentioned are stated in terms of the statistical dimension, while we do not directly obtain bounds in terms of the statistical dimension, though our sketches can be composed with sketches that do. Therefore, we consider the case when there is no regularization and is of full rank. In this case, the statistical dimension reduces to .
Applications
In this section, we introduce various applications using our sketch. In Section 6.1, we study approximating the Gaussian kernel using our algorithm. In Section 6.2 we extend the analysis to a class of slow-decaying kernels. In Section 6.3 we illustrate an efficient algorithm to solve kernel linear systems. In Section 6.4 we show how to solve kernel ridge regression using our sketch.
We provide the fastest algorithm to preserve the column space of a Gaussian kernel when is large.
where and .
We provide a sketch of the proof here, and further details can be found in the appendix. The Taylor expansion of the Gaussian kernel can be written as
where is a diagonal matrix with . Let
If we set and just use the first terms of :
2 General p𝑝p-convergent Kernels
A key advantage of Algorithm 1 is its moderate dependence on the degree , which gives it more leverage when is large. We introduce a characterization of kernels, based on the series of the coefficients in the Taylor expansion of the kernel. As we will later see in the proof of Theorem B.2, the decay rate of coefficients has a direct relation with the degree we need for approximating a kernel.
We say the kernel matrix for data matrix is -convergent if its corresponding Taylor expansion series can be written as follows: , where the coefficients .
For the sake of illustration, suppose . Then the first term in the running time becomes . When is large, Theorem 6.3 gives a fast algorithm for approximating the kernel, but the runtime becomes much slower when . Therefore, we propose a novel sampling scheme to deal with small values of . Roughly speaking, we exactly compute the first terms in the Taylor expansion, while for the remaining terms we sample only of them proportional to their coefficient. Using a matrix Bernstein bound (Theorem B.4), we obtain an even faster algorithm. We apply our result to the neural tangent kernel (), which is a -convergent kernel.
We remark that our definition of -convergent kernels captures a wide range of kernels that have slow decay rate in their coefficients in their Taylor expansion, such as and arc-cosine kernels. Typically, the coefficients are of the form for some . In contrast, Gaussian kernels enjoy a much faster decay rate, and therefore, designing algorithm for the Gaussian kernel is considerably simpler, since the number of terms we need to approximate it with in its Taylor expansion is small, and no sampling is necessary.
3 Kernel Linear Systems
Another interesting application of our sketching scheme is to constructing a preconditioner for solving PSD systems involving a kernel matrix [COCF16]. In order to apply algorithms such as Conjugate Gradient [She94], one has to obtain a good preconditioner for a potentially ill-conditioned kernel system.
in time, where is the exponent of matrix multiplication (currently [Wil12, LG14]).
In certain NLP [DL20] and biological tasks [TPK02] where for a positive integer , Theorem 6.6 provides a fast algorithm for which the running time depends nearly linearly on . We also remark that the algorithm we use for Theorem 6.6 is inspired by the idea of [BPSW21] (their situation involves ). It is also interesting that in their applications, regularization is not needed since solving a kernel system is equivalent to training an over-parametrized ReLU network without regularization.
4 Kernel Ridge Regression
for . A relevant notion is the statistical dimension:
One drawback of our sketch is that we cannot obtain a dimension depending on instead of , since it does not have the approximate matrix product property. To mitigate this effect, we propose the following composition of sketches.
Acknowledgments: D. Woodruff would like to thank partial support from NSF grant No. CCF-1815840, Office of Naval Research grant N00014-18-1-256, and a Simons Investigator Award.
References
Appendix
In Section A, we give an algorithm to compute a subspace embedding for the Gaussian kernel using Theorem 4.8. In Section B, we characterize a large class of kernels based on the coefficients in their Taylor expansion, and develop fast algorithms for different scenarios. In Section C, we apply our results in Section B to the Neural Tangent kernel. In Section D, we use our sketch in conjunction with another sketch to compute a good preconditioner for the Gaussian kernel. In Section E, we compose our sketch with our sketching matrices to solve Kernel Ridge Regression.
Notation
We use to denote and use to denote .
Appendix A Gaussian Kernel
We remark that our construction of a sketch for the Gaussian kernel and its corresponding analysis is inspired by [AKK+20], and is thus similar to the proof of Theorem 5 in their paper. For completeness, we include a proof here.
where and .
Let for a sufficiently large constant , and let be the first terms of . By the triangle inequality we have:
with probability at least . Moreover, can be computed in time
Our algorithm will simply compute from to , normalize each by , and then multiply by . More precisely, the approximation will be
By combining terms in (4) and using a union bound over all , we obtain that with probability at least , we have the following:
Also, by Theorem 5.1, the time to compute is
Notice we will have to pay an additive due to line 2 of Algorithm 1, when applying the SRHT to . However, we only need to perform this operation once for the term with the highest degree, or the terms with lower degree that can be formed by combining nodes computed with the highest degree. Thus, the final runtime is
Appendix B General p𝑝p-Convergent Sequences
We consider general -convergent kernels defined below in Definition B.1. We apply our proposed Algorithm 1 to compute a subspace embedding with a fast running time.
In this section, we state a general theorem for . The proof is similar to the proof for Theorem A.1. We start by restating the definition of a -convergent kernel.
where the positive coefficients are a function of , and satisfies
Similar to the Gaussian kernel, here we use the first terms to approximate the kernel matrix .
Let for a sufficiently large constant , and let be the first terms of . By the triangle inequality, we have
The proof is identical to the proof of Theorem A.1, with the target dimension of being .
Similar to Theorem A.1, we have to pay an extra term to apply the to , so the final running time is
Recall our setting is when , so if , Theorem B.2 gives a running time of , which is better than the classical result of as long as . However, if , Theorem B.2 gives a worse dependence on , which can be further optimized.
B.2 Sampling Scheme for 1<p<31𝑝31<p<3
We next describe a novel sampling scheme if , with a better dependence on compared to Theorem B.2. We first state some probability tools.
Let be independent, zero-mean random matrices with common size , and assume each one is uniformly bounded:
Let be the degree used in Theorem B.2 where , and let be some positive integer smaller than . We will consider the following scheme:
For the first terms in the Taylor expansion, we approximate each term directly using Theorem B.2.
For each of the next terms, we sample proportional to their coefficient , taking only samples in total.
We now consider the operator norm of the expectation of :
Let . Since each sample is sampled independently, we have
Picking , and then averaging over samples, we get that
where we use the fact that the operator norm of is at least , by our choice of . We now compute the expected running time of this algorithm.
Runtime part 1: Computing the first s𝑠s terms
For the first terms, we can apply the same reasoning as in Theorem B.2 to get a running time of .
Runtime part 2: Sampling the next s𝑠s terms
For the sampling part, we consider the expected degree of the sample we will be working with:
Now we are ready to compute the expected running time of the sampling phase:
Additionally, we need to apply the to at most twice, once for the initial phase, and once for the sampling phase, so the final running time is
∎ When , we use the largest degree as an upper bound for analyzing our running time.
In addition, using that , the first part of our running time can be upper bounded by
The proof is almost identical to the proof of Theorem B.5. The only difference is when considering the expected degree , we use as an upper bound. The number of terms we approximate in the initial phase will be
Simplifying the Exponent For the exponent of , we have
Appendix C Properties of the Neural Tangent Kernel
We discuss an application of our sampling algorithm for (Corollary B.6) to the Neural Tangent Kernel (). We will first formally define the , then consider its Taylor expansion, and then use a -convergent kernel to bound it.
In this section, we give the Taylor expansion of the , by first examining its corresponding function in a single variable, and then extend it to the matrix case.
Consider a simple two-layer (an alternative name is one-hidden-layer) ReLU network with input layer initialized to standard Gaussians, activation function ReLU, and output layer initialized to uniform and independent Rademacher () random variables. Suppose we fix the output layer. Then the neural network can be characterized by a function
The above formulation is standard for convergence analysis of neural networks [LL18, DZPS19, AZLS19b, AZLS19a, SY19, BPSW21, HLSY21, MOSW21].
For the sake of simplicity, assume all , , and consider an individual summand, which gives rise to
The Taylor expansion of the is
C.2 Approximating the 𝖭𝖳𝖪𝖭𝖳𝖪\mathsf{NTK}
In this section, we will use a -convergent kernel to bound the , then apply Corollary B.6 to approximate it.
Let denote the coefficient of the term in the Taylor expansion of the :
The term is the central binomial coefficient. We will use the following bound on it:
This gives upper and lower bounds on :
Thus, , and we can use a -convergent kernel for our approximation. Using Corollary B.6 with , we obtain an -approximation in time
Appendix D Preconditioning to Solve a Kernel Linear System
In our result, we follow a similar approach as in [BPSW21] and the proof is similar to the proof of Lemma 4.2 in their paper. The main novelty of our framework is that we use a spectral approximation to the kernel matrix and analyze the error and runtime under our approximation. For completeness, we include a proof in this setting.
Moreover, can be computed in time
where is the matrix multiplication exponent.
Before the proof, we define some notation and corresponding facts specifically about a PSD matrix.
Let be conforming square matrices. Then the following inequality holds:
where is the condition number of .
We will make use of Lemma B.2 in [BPSW21].
Suppose is a PSD matrix for which holds for all . Using gradient descent for iterations, we obtain
where is our initial guess, is the optimal solution, and .
Throughout the proof, we will set . By Theorem A.1, we can compute an -approximation to and in time
with solution , then we have
Suppose is the matrix computed via a QR decomposition, so that has orthonormal columns. Then for any , we have
Now, pick and solve the following regression problem:
Notice that Algorithm 2 implements gradient descent. Using Lemma D.3, after iterations, we have
where is the optimal solution to Equation (7). We will show the following for :
Recalling that , plugging into Eq. (8) we get
The second inequality uses that is a square matrix, the third inequality uses Fact D.2, and the second-to-last inequality uses that we have a -subspace embedding.
This means by setting the number of iterations to , we obtain
Computing , by Theorem A.1, takes time
Applying to , using the FFT algorithm, takes time
A QR decomposition algorithm, due to [DDH07], can be computed in time .
The cost of each iteration is bounded by the cost of taking a matrix-vector product, which is at most , and there are iterations in total. Thus, we obtain a final runtime of
Appendix E Kernel Ridge Regression
In this section, we show how to compose our sketch with other sketches whose dimensions depend on the statistical dimension of instead of . Before proceeding, we introduce the notion of the statistical dimension.
Solving ridge regression with runtime depending on thhe statistical dimension is done in a number of works, for example [RR07, AM15, AKM+17, ACW17a, MM17].
We state and prove our main result in this section below.
Moreover, there exists a matrix with rows such that if is the optimal solution to , then
Finally, The time to solve above is .
Before starting the proof, we introduce a key lemma regarding using the to approximate the solution of .
Throughout the proof, we assume has full rank and set to be a matrix with rows. We also use to denote .
Part 1: Provide a construction of matrix ;
Part 2: Provide a sketching matrix with the solution guarantee;
Part 3: Provide a runtime analysis for solving .
Note that part 1 can be solved using Theorem 5.1. As a side note, since is a approximation to , with high probability it also has full rank. Consequently, has full rank as well.
To show part 2, we will show the following:
The optimal solution to is a approximation to the optimum of ;
The optimum of is a approximation to the optimum of .
which can be solved using Lemma E.3. The only thing we need to justify is that the statistical dimension of gives a good approximation to the statistical dimension of . Note that
Thus, the dimension , which means we can invoke Lemma E.3.
For the final part, note that applying the sketch takes time. To solve the regression problem, we instead solve:
Since has full rank, we know the argument realizing the minimum is the we are looking for. To output an , we can simply solve the linear system , which takes time. Finally, solving the above regression problem takes time (see [SGV98]). This concludes our runtime analysis. ∎