# Some ML-Related Math I Now Understand Better

Here are some simple Math facts rarely taught in ML & Math lectures:

*   SVD is decomposing a matrix into a sum of simple read-and-write operations
*   There is exponentially much room for close vectors in high dimensional space
*   Layer Normalization is a projection

SVD Is Decomposing a Matrix Into a Sum of Simple Read and Write Operations
--------------------------------------------------------------------------

*Thanks to zfurman, on EleutherAI, which told me about the core idea.*

Let's say I want to understand what a weight matrix does. A large table of numbers isn't really helpful, I want something better.

Here is a linear transformation which is much easier to understand than a table of numbers: $f(x) = \lambda \langle v,x \rangle u$ (where $u$ and $v$ are unit vectors, $\lambda$ is a non-negative scalar, and $\langle v,x \rangle$ is the dot product between $v$ and $x$). $f$** is reading in the **$v$** direction, and writing in the **$u$** direction with a magnitude scaled by** $\lambda$. I can understand this much more clearly and do nice interpretability with it. Therefore, if I know things about my embedding space (for example, using the logit lens), I can tell what $f$ is doing.

Sadly, not all linear transformations can be expressed in this way: it's just a rank-1 transformation, so there is no hope of capturing something as complex as a usual linear transformation.

But what if I allow myself to sum many simple transformations? Then I could just look at each operation independently. More precisely, **here is a wishlist to understand the big matrix**$M$** of the transformation **$g$ :

*   $\boldsymbol{g(x) = \sum_{i=1}^r \lambda_i \langle v_i,x \rangle u_i}$ where $u$ and $v$ are unit vectors, and $\lambda$ is a positive scalar - it's a sum of simple transformations;
*   $\langle v_i,v_j \rangle = 0$ for all $i \neq j$ \- **no double read**, always read in orthogonal directions;
*   $\langle u_i,u_j \rangle = 0$ for all $i \neq j$ \- **no double write**, always write in orthogonal directions;
*   $r=\text{rank}(g)$ - the number $r$ of simple transformation summed should be as small as possible, because that would mean less individual transformation to inspect (and I can't hope for fewer than $\text{rank}(g)$ operations, since the rank of the sum of $r$ rank-1 linear transformations is at most $r$).

In terms of matrices, this is $M = U \text{diag}(\lambda) V^{\text T}$ where $U$ and $V$ are semi-unitary matrices (orthogonal matrices when they are square matrices). This is exactly [SVD](https://en.wikipedia.org/wiki/Singular_value_decomposition).

Understanding matrices & SVD this way helped me find a geometric intuition behind some basic properties of matrices. The questions below can also be quickly answered by "doing the math", but I think it's interesting to have a geometric understanding of why these are true. 

*   What is a geometric interpretation of a symmetric matrix? Why is the inverse of an invertible symmetric matrix always symmetric?
*   What is the geometric interpretation of transposing a linear transformation ?
*   Why are $\lambda_i^2$ the eigenvalues of $MM^{\text T}$ and why are its eigenvectors $u_i$? Why are $\lambda_i^2$ the eigenvalues of $M^{\text T}M$ and why are its eigenvectors $v_i$?

Feel free to share yours in the comments!

There Is Exponentially Much Room for Almost Orthogonal Vectors in High Dimensional Space
----------------------------------------------------------------------------------------

According to [this paper](https://ieeexplore.ieee.org/document/6771362?arnumber=6771362&tag=1), the number of almost orthogonal vectors you can put in $n$ dimensional space is at least $\exp(n \log (\frac 1 {\sin \theta}) + o(n))$ where all pairs of vectors have an angle of at least $\theta$ between them (and $o(n) \xrightarrow[n \rightarrow +\infty]{} 0$).[^rr2ktjyeh9]

Ignoring the $o(n)$, and using $n=1600$ (the number of dimensions of the embedding space of [GPT-2 XL](https://d4mucfpksywv.cloudfront.net/better-language-models/language-models.pdf)) it means you can fit at least 3000 vectors with pairwise cosine similarities smaller than 0.1 and $10^{14}$ vectors with cosine similarities smaller than 0.2. Using $n=12288$ (the number of dimensions of the embedding space of [GPT-3](https://arxiv.org/abs/2005.14165)), you can fit at least $10^6$ vectors with pairwise cosine similarities smaller than 0.05 and $\pmb{10^{26}}$** vectors with cosine similarities smaller than 0.1**.

This means you can get a lot of vectors in your embedding space if you allow for relatively small cosine similarities. But this does not hold for tiny cosine similarities (e.g. 0.01 for $n=12288$, which gives a lower bound of 2 using the formula above). I'm not aware of a lower bound better than $n$ for tiny angles. Therefore, **for a neural network to use this space to fit independent concepts, it needs to be resilient to many** ***small-but-not-that-small*** **conflicts**.

Layer Normalization Is a Projection
-----------------------------------

*Thanks to Lawrence Chan for telling me about this.*

[Layer Normalization](https://arxiv.org/abs/1607.06450) is a normalization layer often found in LLMs. [People](https://pytorch.org/docs/stable/generated/torch.nn.LayerNorm.html) usually write it as $y = \frac {x - \mathrm{E}[x]}{\sqrt {\mathrm{Var}[x]}}$ (followed by a scaling and a bias term[^fw4b3gcw0m]), which is confusing because we are taking the expected value and the variance *of a vector*, which is not a common geometric operation.

But it can easily be expressed in terms of two successive projection: $x - \mathrm{E}[x]$ is simply $p_\vec 1(x)$, the projection on the hyperplane orthogonal to the vector $\vec 1 =(1 \ldots 1)$, $p_\vec 1(x) = x - \langle x, \vec 1\rangle\vec 1/||\vec 1||^2 = x - (\frac 1 d \sum_i x_i) \vec 1 = (x_j - \frac 1 d \sum_i x_i)_j$.

Then$\frac {x - \mathrm{E}[x]}{\sqrt {\mathrm{Var}[x]}} = \frac {x - \mathrm{E}[x]}{\sqrt {\mathrm{E}[(x - \mathrm{E}[x])^2]}}  = \frac {p_1(x)} {\sqrt{\frac 1 d \sum_i p_1(x)_i^2}} = \sqrt d \frac {p_1(x)} {||p_1(x)||} = \sqrt d \space p_S(p_1(x))$, where $p_S$ is the projection on the unit sphere.

This gives us $\frac {x - \mathrm{E}[x]}{\sqrt {\mathrm{Var}[x]}} = \sqrt d \space p_S(p_1(x))$.

Therefore, **LayerNorm is just projecting on the hyperplane orthogonal to **$\vec 1$**, and then projecting again on the unit sphere** (of the hyperplane orthogonal to $\vec 1$), times $\sqrt d$. These two projections are **the same as projecting a point to its closest point on the unit sphere of the hyperplane orthogonal** to $\vec 1$[^66pv8wxnhyg].

For example, in dimension 3, LayerNorm is the same as projecting a point to the point of the black ring below closest to it.

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/ca369856241e1116f72d75be802427570f6795d25d7a9073.png)

This helps me see geometrically what $\text{LayerNorm}(\mathbb{R}^d)$ is: it's the unit sphere of the hyperplane orthogonal to $1$.

This also gives me a geometrical intuition of why it might help (though the usual formula works just as well), though it is quite speculative. First, projecting on the hypersphere clearly solves the problem of exploding or vanishing activations. Second, the projection on the $\vec 1$ vector pushes you away from the quadrants where ReLU is boring. Indeed, the $\vec 1$ vector points towards the quadrant where all coordinates are positive, where ReLU = id, and points away from the quadrant where all coordinates are negative, where ReLU = 0. In all other quadrants, ReLU does something a bit more interesting: it zeros out some coordinates and leaves others intact.

**What is missing from this geometrical point of view**: It's hard to understand high dimensions. In particular, *it's not easy to see that the projection on the hyperplane does almost nothing to high dimensional vectors*: it only changes one coordinate among thousands. Therefore, the cosine similarity between a vector $$$x$ and $\text{LayerNorm}(x)$ is almost always very close to 1. Indeed, if $\hat x_i$ are the coordinates of $x$ in a basis which has the $1$ vector (normalized) as first vector, $\frac{\langle x, \text{LayerNorm}(x) \rangle} {||x|| \, || \text{LayerNorm}(x)||} = \frac{\sum_{i=2}^d \hat x_i^2 }{ \sqrt {(\sum_{i=2}^d \hat x_i^2)(\sum_{i=1}^d \hat x_i^2)}} = \sqrt {1 - \frac{\hat x_1^2}{\sum_{i=1}^d \hat x_i^2}} \approx 1$

Appendix: Solutions to the Simple Questions Using the Geometric Interpretation of SVD
-------------------------------------------------------------------------------------

*   What is a geometric interpretation of a symmetric matrix? Why is the inverse of an invertible symmetric matrix always symmetric?

A symmetric matrix is, according to the spectral theorem, a matrix of the form $U\text{diag}(\lambda)U^T$ where U is an orthogonal matrix. Therefore, a symmetric matrix is a matrix which can be decomposed as a sum of transformation of the form $x \mapsto \lambda \langle x, u \rangle u$. **A symmetric matrix is the matrix of a linear transformation, which writes where it reads**! Therefore, inverting a symmetric matrix is easy: if the $\lambda_i$ are all non-zero, just read where you just wrote (in the direction of $u_i$), scale by $1/\lambda_i$, and then write back along $u_i.$ The transformation I just described to invert the action of a symmetric matrix also writes where it reads, so the inverse of a symmetric matrix is also symmetric. (You can check, that because there is no double read nor double write, what I described is legit, though the formal proof with usual linear algebra is much faster than the formal proof using geometry.)

*   What is the geometric interpretation of transposing a linear transformation ?

$(U\text{diag}(\lambda)V^T)^T = V\text{diag}(\lambda)U^T$, therefore M transposed is the same as M, except **it reads where M writes, and writes where M reads** (the simple rank-1 operations are "swapped").

*   Why are $\lambda_i^2$ the eigenvalues of $MM^{\text T}$ and why are its eigenvectors $u_i$? Why are $\lambda_i^2$ the eigenvalues of $M^{\text T}M$ and why are its eigenvectors $v_i$?

$MM^T$ is the composition of two linear transformations. A first one (the transposed of M, see above) which reads along $u_i$ scales by $\lambda_i$ and writes along $v_i$, and a second one which reads along $v_i$, scales by $\lambda_i$, and writes along $u_i$. Therefore, $MM^T$ reads along $u_i$, scales by $\lambda_i^2$ and writes along $u_i$. Therefore, it is symmetric (it reads where it writes), and the $u_i$ are eigenvectors with eigenvalues $\lambda_i^2$ because if it read $u_i$, it will write $\lambda_i^2 u_i$ (there is no double read nor double writes). The same reasoning works for $M^TM$.

[^rr2ktjyeh9]: Toy Models of Superposition states something similar: "It's possible to have \(\exp(n)\) many "almost orthogonal" (\(<\epsilon\) cosine similarity) vectors in high-dimensional spaces. See the Johnson–Lindenstrauss lemma." But this lemma does not directly state this property, and rather tells us that there is a linear map to a space with \(\log n\) dimensions which almost preserves L2-distances between points. I prefer the theorem I present in this post which is directly about how many almost orthogonal vectors can fit in high-dimensional spaces.  

[^fw4b3gcw0m]: For numerical stability reasons, people also usually add a small \(\epsilon\) to the denominator. 

[^66pv8wxnhyg]: Using the L2 distance, for a given point \(x\), if y is the closest point to \(x\) on the unit sphere of the hyperplane \(H\) orthogonal to the \(1\) vector, and if \(p_H(x)\) is the orthogonal projection of \(x\) on \(H\), then by the Pythagorean theorem, \(d(x, y)^2 = d(x, p_H(x))^2 + d(p_H(x), y)^2\), which is minimal when y is the projection of \(p_H(x)\) on the unit sphere. This proves that projecting on the unit sphere of \(H\) is the same as first projecting on \(H\) and then projecting on the unit sphere.