Functions and operators

cos⁡(u,v)=u⊤v∥u∥ ∥v∥\cos(\mathbf{u}, \mathbf{v}) = \frac{\mathbf{u}^\top \mathbf{v}}{\|\mathbf{u}\| \, \|\mathbf{v}\|} Cosine

The cosine function; in ML papers usually cosine similarity, a cosine schedule, or positional encodings.

In ML papers

cos⁡x\cos x is the cosine of an angle xx in radians, between −1-1 and 11. Three uses dominate ML papers:

Cosine similarity: cos⁡(u,v)=u⊤v∥u∥∥v∥\cos(\mathbf{u}, \mathbf{v}) = \frac{\mathbf{u}^\top \mathbf{v}}{\|\mathbf{u}\|\|\mathbf{v}\|}, the cosine of the angle between two vectors: 1 if they point the same way, 0 if orthogonal, −1-1 if opposite. Contrastive losses and embedding search use it.

Cosine schedules: the learning rate decays along half a cosine wave, as in SGDR (Loshchilov & Hutter 2017); diffusion models use a cosine noise schedule (Nichol & Dhariwal 2021). And with sin⁡\sin, it builds positional encodings and rotary embeddings.

How to type it

LaTeXShows
\coscos⁡\cos
\arccos inverse cosinearccos⁡\arccos

A worked example

u=(1,0),  v=(1,1):cos⁡=11×2≈0.707\mathbf{u} = (1, 0), \; \mathbf{v} = (1, 1): \quad \cos = \frac{1}{1 \times \sqrt{2}} \approx 0.707

The two vectors are 45 degrees apart, and arccos⁡(0.707)=π4\arccos(0.707) = \frac{\pi}{4}.

Common confusions

Equations that use it

Related

PE(pos,2i)=sin⁡(pos100002i/d)PE_{(pos, 2i)} = \sin\left(\frac{pos}{10000^{2i/d}}\right)Sine⟨x,y⟩\langle \mathbf{x}, \mathbf{y} \rangleInner product (angle brackets)∥x∥2\|\mathbf{x}\|_2Norm

Decode this example →