Count sketch

From HandWiki
Short description: Method of a dimension reduction

Count sketch is a type of dimensionality reduction that is particularly efficient in statistics, machine learning and algorithms.[1][2] It was invented by Moses Charikar, Kevin Chen and Martin Farach-Colton[3] in an effort to speed up the AMS Sketch by Alon, Matias and Szegedy for approximating the frequency moments of streams[4] (these calculations require counting of the number of occurrences for the distinct elements of the stream).

The sketch is nearly identical[citation needed] to the Feature hashing algorithm by John Moody,[5] but differs in its use of hash functions with low dependence, which makes it more practical. In order to still have a high probability of success, the median trick is used to aggregate multiple count sketches, rather than the mean.

These properties allow use for explicit kernel methods, bilinear pooling in neural networks and is a cornerstone in many numerical linear algebra algorithms.[6]

Intuitive explanation

The inventors of this data structure offer the following iterative explanation of its operation:[3]

  • at the simplest level, the output of a single hash function s mapping stream elements q into {+1, -1} is feeding a single up/down counter C. After a single pass over the data, the frequency [math]\displaystyle{ n(q) }[/math] of a stream element q can be approximated, although extremely poorly, by the expected value [math]\displaystyle{ \bold E[C \cdot s(q)] }[/math];
  • a straightforward way to improve the variance of the previous estimate is to use an array of different hash functions [math]\displaystyle{ s_i }[/math], each connected to its own counter [math]\displaystyle{ C_i }[/math]. For each element q, the [math]\displaystyle{ \bold E[C_i \cdot s_i(q)]= n(q) }[/math] still holds, so averaging across the i range will tighten the approximation;
  • the previous construct still has a major deficiency: if a lower-frequency-but-still-important output element a exhibits a hash collision with a high-frequency element, [math]\displaystyle{ n(a) }[/math] estimate can be significantly affected. Avoiding this requires reducing the frequency of collision counter updates between any two distinct elements. This is achieved by replacing each [math]\displaystyle{ C_i }[/math] in the previous construct with an array of m counters (making the counter set into a two-dimensional matrix [math]\displaystyle{ C_{i,j} }[/math]), with index j of a particular counter to be incremented/decremented selected via another set of hash functions [math]\displaystyle{ h_i }[/math] that map element q into the range {1..m}. Since [math]\displaystyle{ \bold E[C_{i, h_i(q)} \cdot s_i(q)] = n(q) }[/math], averaging across all values of i will work.

Mathematical definition

1. For constants [math]\displaystyle{ w }[/math] and [math]\displaystyle{ t }[/math] (to be defined later) independently choose [math]\displaystyle{ d=2t+1 }[/math] random hash functions [math]\displaystyle{ h_1, \dots, h_d }[/math] and [math]\displaystyle{ s_1,\dots,s_d }[/math] such that [math]\displaystyle{ h_i : [n] \to [w] }[/math] and [math]\displaystyle{ s_i : [n] \to \{\pm 1\} }[/math]. It is necessary that the hash families from which [math]\displaystyle{ h_i }[/math] and [math]\displaystyle{ s_i }[/math] are chosen be pairwise independent.

2. For each item [math]\displaystyle{ q_i }[/math] in the stream, add [math]\displaystyle{ s_j(q_i) }[/math] to the [math]\displaystyle{ h_j(q_i) }[/math]th bucket of the [math]\displaystyle{ j }[/math]th hash.

At the end of this process, one has [math]\displaystyle{ wd }[/math] sums [math]\displaystyle{ (C_{ij}) }[/math] where

[math]\displaystyle{ C_{i,j} = \sum_{h_i(k)=j}s_i(k). }[/math]

To estimate the count of [math]\displaystyle{ q }[/math]s one computes the following value:

[math]\displaystyle{ r_q = \text{median}_{i=1}^d\, s_i(q)\cdot C_{i, h_i(q)}. }[/math]

The values [math]\displaystyle{ s_i(q)\cdot C_{i, h_i(q)} }[/math] are unbiased estimates of how many times [math]\displaystyle{ q }[/math] has appeared in the stream.

The estimate [math]\displaystyle{ r_q }[/math] has variance [math]\displaystyle{ O(\mathrm{min}\{m_1^2/w^2, m_2^2/w\}) }[/math], where [math]\displaystyle{ m_1 }[/math] is the length of the stream and [math]\displaystyle{ m_2^2 }[/math] is [math]\displaystyle{ \sum_q (\sum_i [q_i=q])^2 }[/math].[7]

Furthermore, [math]\displaystyle{ r_q }[/math] is guaranteed to never be more than [math]\displaystyle{ 2m_2/\sqrt{w} }[/math] off from the true value, with probability [math]\displaystyle{ 1-e^{-O(t)} }[/math].

Vector formulation

Alternatively Count-Sketch can be seen as a linear mapping with a non-linear reconstruction function. Let [math]\displaystyle{ M^{(i\in[d])}\in\{-1,0,1\}^{w \times n} }[/math], be a collection of [math]\displaystyle{ d=2t+1 }[/math] matrices, defined by

[math]\displaystyle{ M^{(i)}_{h_i(j),j} = s_i(j) }[/math]

for [math]\displaystyle{ j\in[w] }[/math] and 0 everywhere else.

Then a vector [math]\displaystyle{ v\in\mathbb{R}^n }[/math] is sketched by [math]\displaystyle{ C^{(i)} = M^{(i)} v \in \mathbb{R}^w }[/math]. To reconstruct [math]\displaystyle{ v }[/math] we take [math]\displaystyle{ v^*_j = \text{median}_i C^{(i)}_j s_i(j) }[/math]. This gives the same guarantees as stated above, if we take [math]\displaystyle{ m_1=\|v\|_1 }[/math] and [math]\displaystyle{ m_2=\|v\|_2 }[/math].

Relation to Tensor sketch

The count sketch projection of the outer product of two vectors is equivalent to the convolution of two component count sketches.

The count sketch computes a vector convolution

[math]\displaystyle{ C^{(1)}x \ast C^{(2)}x^T }[/math], where [math]\displaystyle{ C^{(1)} }[/math] and [math]\displaystyle{ C^{(2)} }[/math] are independent count sketch matrices.

Pham and Pagh[8] show that this equals [math]\displaystyle{ C(x \otimes x^T) }[/math] – a count sketch [math]\displaystyle{ C }[/math] of the outer product of vectors, where [math]\displaystyle{ \otimes }[/math] denotes Kronecker product.

The fast Fourier transform can be used to do fast convolution of count sketches. By using the face-splitting product[9][10][11] such structures can be computed much faster than normal matrices.

See also

References

  1. Faisal M. Algashaam; Kien Nguyen; Mohamed Alkanhal; Vinod Chandran; Wageeh Boles. "Multispectral Periocular Classification WithMultimodal Compact Multi-Linear Pooling" [1]. IEEE Access, Vol. 5. 2017.
  2. Ahle, Thomas; Knudsen, Jakob (2019-09-03). "Almost Optimal Tensor Sketch". https://www.researchgate.net/publication/335617805. 
  3. 3.0 3.1 Charikar, Chen & Farach-Colton 2004.
  4. Alon, Noga, Yossi Matias, and Mario Szegedy. "The space complexity of approximating the frequency moments." Journal of Computer and system sciences 58.1 (1999): 137-147.
  5. Moody, John. "Fast learning in multi-resolution hierarchies." Advances in neural information processing systems. 1989.
  6. Woodruff, David P. "Sketching as a Tool for Numerical Linear Algebra." Theoretical Computer Science 10.1-2 (2014): 1–157.
  7. Larsen, Kasper Green, Rasmus Pagh, and Jakub Tětek. "CountSketches, Feature Hashing and the Median of Three." International Conference on Machine Learning. PMLR, 2021.
  8. Ninh, Pham (2013). "Fast and scalable polynomial kernels via explicit feature maps". SIGKDD international conference on Knowledge discovery and data mining. Association for Computing Machinery. doi:10.1145/2487575.2487591. 
  9. Slyusar, V. I. (1998). "End products in matrices in radar applications". Radioelectronics and Communications Systems 41 (3): 50–53. http://slyusar.kiev.ua/en/IZV_1998_3.pdf. 
  10. Slyusar, V. I. (1997-05-20). "Analytical model of the digital antenna array on a basis of face-splitting matrix products.". Proc. ICATT-97, Kyiv: 108–109. http://slyusar.kiev.ua/ICATT97.pdf. 
  11. Slyusar, V. I. (March 13, 1998). "A Family of Face Products of Matrices and its Properties". Cybernetics and Systems Analysis C/C of Kibernetika I Sistemnyi Analiz.- 1999. 35 (3): 379–384. doi:10.1007/BF02733426. http://slyusar.kiev.ua/FACE.pdf. 

Further reading