In previous posts, we discussed the Radial Basis Function (RBF) kernel and how to approximate it using Random Fourier Features. Today, we turn our attention to another fundamental kernel—the Polynomial Kernel—and a powerful algebraic technique to approximate it called Tensor Sketching.

The Polynomial Kernel

For $x,y\in \mathbb{R}^{d}$ and $c\ge 0$, the polynomial kernel of degree $p$ is defined as: $$K(x,y)=(\left\langle x,y \right\rangle+c)^{p}=\left(\sum_{i=1}^{d}x_{i}y_{i}+c\right)^{p}.$$ If $c=0$, the kernel is called Homogeneous.

By applying the binomial formula, we can expand the kernel as: $$K(x,y)=\sum_{j=0}^{p}\binom{p}{j}\cdot(\left\langle x,y \right\rangle)^{j}c^{p-j}.$$ Further applying the multinomial formula to the term $\left\langle x,y \right\rangle^{j}$: $$\left\langle x,y \right\rangle^{j}=\sum_{n_{1}+\ldots+n_{d}=j}\binom{j}{n_{1},\ldots,n_{d}} \cdot \prod_{i=1}^{d}(x_{i}y_{i})^{n_{i}}.$$ Note that for $n_{1}+\ldots+n_{d}=j$, we have the combinatorial identity: $$\binom{p}{j}\cdot \binom{j}{n_{1},\ldots,n_{d}}=\frac{p!}{(p-j)!n_{1}!\cdots n_{d}!}=\binom{p}{n_{1},\ldots,n_{d},p-j}.$$ Letting $n_{d+1}=p-j$, we have $n_{1}+\ldots+n_{d+1}=p$. Since we sum over all $j$, we can rewrite the kernel as a summation over all non-negative integer partitions of $p$ into $d+1$ parts: $$K(x,y)=\sum_{n_{1}+\ldots+n_{d+1}=p}\binom{p}{n_{1},\ldots,n_{d+1}}\prod_{i=1}^{d}(x_{i}y_{i})^{n_{i}}\cdot c^{n_{d+1}}.$$ The total number of summands is the number of solutions to $n_{1}+\ldots+n_{d+1}=p$, which is $\binom{p+d}{d}$.

We can explicitly construct a feature map $\varphi:\mathbb{R}^{d}\to \mathbb{R}^{\binom{p+d}{d}}$. Enumerate all solutions to the partition equation, and for the $k$-th solution $(n_{1},\ldots,n_{d+1})$, define the $k$-th feature: $$\psi_{k}(x)=\sqrt{\binom{p}{n_{1},\ldots,n_{d+1}}}\cdot x_{1}^{n_{1}}\cdots x_{d}^{n_{d}}\cdot c^{n_{d+1}/2}.$$ Defining $\varphi(x)=(\psi_{1}(x),\ldots,\psi_{\binom{p+d}{d}}(x))$, we satisfy: $$\left\langle \varphi(x),\varphi(y) \right\rangle = K(x,y).$$ While $\varphi$ maps to a finite-dimensional space, the dimension $\binom{p+d}{d} \approx p^d$ grows exponentially with $d$. For high-dimensional data, computing explicit features is intractable.

Note: The polynomial kernel is not stationary. For example, if $p=2$ and $c=0$, $K(x,x) = |x|^4$. If we take two vectors with different norms, $K(x,x) \neq K(y,y)$, even if $x-x=y-y=0$. Therefore, the Random Fourier Features method (which relies on Bochner’s theorem for shift-invariant kernels) is not applicable.

Note: we can always reduce the general case to the homogeneous case by appending a coordinate with value $\sqrt{c}$ to each vector. Thus, we will focus on the homogeneous case where $K(x,y) = \langle x, y \rangle^p$.

As Tensor Products

Proof. We proceed by induction on $p$.

This suggests that if we can approximate $x^{(p)}$ and $y^{(p)}$ with low-dimensional vectors, we can approximate the polynomial kernel efficiently.

Sketching

Linear sketches are random linear dimension-reducing maps that preserve the norm of vectors with high probability. The most famous examples include the Johnson-Lindenstrauss Transform and the Count-Sketch. We will focus on the Count-Sketch because of its unique interaction with tensor products.

Hash Functions

Recall the definition of $k$-wise independent hash families.

Note that if $\mathcal{H}$ is $k$-wise independent it is also $k’$-wise independent for $k’<k$.

Count Sketch

One way to think of count-sketch is as a random binning operator. We send each element in the vector to a different bin, with a total of $R$ bins, while multiplying it by a random sign. Note that since $C$ has at $d$ non-zero values, computing $Cx$ can be done in $O(d)$ operations.

An important property of Count-Sketch is that it preserves inner products in expectation, and the variance is controllable:

Proof. The inner product of the sketches is: $$\left\langle Cx,Cy \right\rangle=\sum_{k=1}^{R}(Cx)_{k}(Cy)_{k}=\sum_{k=1}^{R}\sum_{i,j:h(i)=h(j)=k} s(i)s(j)x_{i}y_{j}.$$ Taking the expectation over the sign functions $s$:

Thus, the only surviving terms are where $i=j$: $$\mathbb{E}[\left\langle Cx,Cy \right\rangle] = \sum_{k=1}^R \sum_{i:h(i)=k} x_i y_i = \sum_{i=1}^d x_i y_i = \langle x, y \rangle.$$ Note that since $\sum_k \mathbb{1}_{h(i)=k} = 1$ always, the randomness of $h$ does not affect the mean.

For the variance, let $\delta_{i,j}$ be the indicator that $h(i)=h(j)$. Note $$\begin{aligned} \left\langle Cx,Cy \right\rangle&=\sum_{i,j}\delta_{i,j}s(i)s(j)\cdot x_{i}y_{j} \\ &=\sum_{i}\delta_{i,i}s(i)^{2} x_{i}y_{i}+\sum_{i\not=j}\delta_{i,j}s(i)s(j)x_{i}y_{j} \\ &=\left\langle x,y \right\rangle+\sum_{i\not=j}\delta_{i,j}s(i)s(j)x_{i}y_{j} \end{aligned}$$ Thus: $$\left\langle Cx,Cy \right\rangle^2 = \left( \langle x,y \rangle + \sum_{i \neq j} \delta_{i,j} s(i)s(j) x_i y_j \right)^2.$$ The cross-terms ($\langle x,y\rangle\cdot \sum_{i\not=j}\delta_{i,j}s(i)s(j)x_i y_j$) vanish in expectation due to $\mathbb{E}[s(i)]=0$. The expectation of the square of the sum involves terms like $\delta_{i,j}\delta_{i’,j’}s(i)s(j)s(i’)s(j’)$. Due to 4-wise independence of $s$, the expectation is non-zero only if the indices ${i,j,i’,j’}$ match in pairs. Specifically, either $(i,j)=(i’,j’)$ or $(i,j)=(j’,i’)$. Using $\mathbb{E}[\delta_{i,j}] = 1/R$ (for $i \neq j$), we obtain $$\mathbb{E}\left[\left(\sum_{i\not=j}\delta_{i,j}s(i)s(j)x_{i}y_{j}\right)^{2}\right]= \frac{1}{R}\sum_{i\not=j}(x_{i}^{2}y_{j}^{2}+x_{i}y_{i}x_{j}y_{j})$$ Note that $$\sum_{i\not=j}x_{i}^{2}y_{j}^{2}\le \sum_{i,j}x_{i}^{2}y_{j}^{2}=\left(\sum_{i}x_{i}^{2}\right)\left(\sum_{j}y_{j}^{2}\right)= \left\lVert x \right\rVert^{2}\cdot \left\lVert y \right\rVert^{2},$$and by Cauchy-Schwarz $$\sum_{i\not=j}x_{i}y_{i}x_{j}y_{j}\le \sum_{i,j}x_{i}y_{i}x_{j}y_{j}=\left(\sum_{i}x_{i}y_{i}\right)^{2}=\left\langle x,y \right\rangle^{2}\le \left\lVert x \right\rVert^{2}\cdot \left\lVert y \right\rVert^{2},$$ and so we conclude that $$\mathbf{Var}(\left\langle Cx,Cy \right\rangle) = \mathbb{E}[\langle Cx, Cy\rangle^2]- \mathbb{E}[\langle Cx,Cy\rangle]^2= \frac{1}{R}\sum_{i\not=j}(x_i ^2 y_j^2 + x_iy_i x_j y_j)\le \frac{2}{R}\left\lVert x \right\rVert^{2}\left\lVert y \right\rVert^{2}$$ $\blacksquare$

Tensor Sketch

Suppose we have a $2$-wise independent family $[d]\to [R]$. How can we construct a $2$-wise family $[d^{2}]\to [R]$?

One way is to take $h_{1},h_{2}:[d]\to [R]$ (sampled randomly) and define $$H(i_{1},i_{2})=h_{1}(i_{1})+ h_{2}(i_{2})\pmod{R}.$$ The function $H$ sampled this way is $2$-wise independent too. For sign functions, we can take $$S(i_{1},i_{2})=s_{1}(i_{1})\cdot s_{2}(i_{2}),$$ giving a new $2$-wise independent sign function on $[d^{2}]$. Of course we can take $h_{1},h_{2}$ to be from different families and universe sizes, say $h_1:[d_1]\to [R],h_2:[d_2]\to [R]$ and obtain $H:[d_1 d_2]\to [R]$.

The main observation is the following lemma.

Let us define cyclic convolution:

Proof. Let $\equiv$ denote congruence modulo $R$. For $k\in [R]$ we have $$\begin{aligned}(C(x \otimes y))_{k} &= \sum_{i_{1},i_{2}:H(i_{1},i_{2})=k}S(i_{1},i_{2})\cdot (x \otimes y)_{i_{1}i_{2}} \\ &=\sum_{i_{1},i_{2}:h_{1}(i_{1})+h_{2}(i_{2})\equiv k}s_{1}(i_{1})s_{2}(i_{2})x_{i_{1}}y_{i_{2}} \\ &= \sum_{j_{1},j_{2}\in [R]: j_{1}+j_{2}\equiv k}\sum_{i_{1},i_{2}:h_{1}(i_{1})=j_{1},h_{2}(i_{2})=j_{2}}(s_{1}(i_{1})x_{i_{1}})\cdot (s_{2}(i_{2})y_{i_{2}}) \\ &= \sum_{j_{1},j_{2}\in [R]: j_{1}+j_{2}\equiv k}\left(\sum_{i:h_{1}(i)=j_{1}} s_{1}(i)x_{i} \right)\cdot \left(\sum_{i: h_{2}(i)=j_{2}}s_{2}(i)y_{i}\right) \\ &= \sum_{j_{1},j_{2}\in[R]:j_{1}+j_{2}\equiv k}(C^{1}x)_{j_{1}}\cdot (C^{2}y)_{j_{2}} =(C^{1}x * C^{2}y)_{k}.\end{aligned}$$ $\blacksquare$

We thus obtain the corollary:

Computing Convolutions

The nice thing about cyclic convolution is that it can be computed fast via the fast Fourier transform.

By induction, we see that (note convolution is associative) $$u*v*w=(u*v)*w=\mathrm{DFT}^{-1}(\mathrm{DFT}(u*v)\odot \mathrm{DFT}(w))=\mathrm{DFT}^{-1}(\mathrm{DFT}(u)\odot \mathrm{DFT}(v)\odot \mathrm{DFT}(w))$$ and in general to compute the convolution of $p$ vectors, we need to compute the DFT of each vector, take the element-wise products, and compute a single inverse DFT.

Therefore computing the convolution of $p$ vectors requires time $O(pm\log m)$.

Thus, to compute $Cx^{(p)}$, one needs to:

Recap

To conclude, the tensor sketch produces a random feature vector for an input $x\in\mathbb{R}^{d}$, in $\mathbb{R}^{R}$, which is given as the count-sketch of $x^{(p)}$. Computing this random feature vector requires $$O(p(d+R\log R)),$$ since we have to perform $p$ sketches and compute the convolution of $p$ vectors of size $R$. Using the statistical properties of count-sketch, we have $$\mathbb{E}\left[\left\langle Cx^{(p)},Cy^{(p)} \right\rangle\right]=\left\langle x^{(p)},y^{(p)} \right\rangle=\left\langle x,y \right\rangle^{p}=K(x,y),$$ with considerably good variance (which by Chebyshev’s inequality implies decent concentration around the mean).

This is much better than computing the inner product of $x^{(p)}$ and $y^{(p)}$ in time $O(d^{p})$. The ideas behind tensor sketch and count-sketch are relevant to many other fields, especially numerical linear algebra, dimensionality reduction and even neural network acceleration.

References

  1. Pagh, R. (2013), Compressed Matrix Multiplication .
  2. Pham, N., & Pagh, R. (2013), Fast and Scalable Polynomial Kernels via Explicit Feature Maps .
  3. Ahle, T. D., et al. (2020), Oblivious Sketching of High-Degree Polynomial Kernels .
  4. Charikar, M., et al. (2002), Finding Frequent Items in Data Streams .
  5. Woodruff, D. P. (2014), Sketching as a Tool for Numerical Linear Algebra .

Start searching

Enter keywords to search articles

↑↓
↵
ESC
⌘K Shortcut