Neural Architecture Hub
AI & Deep Learning

Deep Learning Reference

Quick-reference formulas, tensor dimensions, activation functions, and optimizer update rules.

Deep Learning Cheatsheet

A concise, high-utility reference for formulas, tensor dimensions, activation functions, and optimizer update rules. Bookmark this page for fast lookup during research and implementation.


Spatial & Convolution Dimensions

Conv2D Output Spatial Dimension

Given an input feature map of spatial dimension WW, kernel size KK, padding PP, and stride SS:

O=WK+2PS+1O = \left\lfloor \frac{W - K + 2P}{S} \right\rfloor + 1
# PyTorch calculation
out_dim = ((W - K + 2 * P) // S) + 1

Transposed Conv2D (Deconvolution / Upsampling)

O=(W1)×S2P+K+PoutO = (W - 1) \times S - 2P + K + P_{\text{out}}
# PyTorch calculation
out_dim = (W - 1) * S - 2 * P + K + P_out

Dilated Convolution Effective Kernel Size

Applying a dilation factor DD expands the kernel's spatial span without adding parameters:

Keff=K+(K1)(D1)K_{\text{eff}} = K + (K - 1)(D - 1)

Receptive Field Recurrence Relation

The cumulative receptive field at layer ll is given by:

RFl=RFl1+(Kl1)×ScumRF_l = RF_{l-1} + (K_l - 1) \times S_{\text{cum}}

where Scum=i=1l1SiS_{\text{cum}} = \prod_{i=1}^{l-1} S_i is the cumulative stride of all preceding layers.


Transformers & Attention

Scaled Dot-Product Attention

Attention(Q,K,V)=softmax(QKTdk)V\text{Attention}(Q, K, V) = \text{softmax}\left( \frac{QK^T}{\sqrt{d_k}} \right) V
import torch
import math

def scaled_dot_product(q, k, v, mask=None):
    d_k = q.size(-1)
    scores = torch.matmul(q, k.transpose(-2, -1)) / math.sqrt(d_k)
    if mask is not None:
        scores = scores.masked_fill(mask == 0, -1e9)
    weights = torch.softmax(scores, dim=-1)
    return torch.matmul(weights, v), weights

Tensor Dimensions in Multi-Head Attention (MHA)

TensorShapeDescription
Input XX[B,L,dmodel][B, L, d_{\text{model}}]Batch size BB, Sequence length LL, Model dimension dmodeld_{\text{model}}
Query QQ, Key KK[B,H,L,dk][B, H, L, d_k]Heads HH, Head dimension dk=dmodel/Hd_k = d_{\text{model}} / H
Value VV[B,H,L,dv][B, H, L, d_v]Value dimension dv=dmodel/Hd_v = d_{\text{model}} / H
Attention Scores[B,H,L,L][B, H, L, L]Pairwise token correlation matrix
Output[B,L,dmodel][B, L, d_{\text{model}}]Linearly projected concatenated heads

Rotary Position Embedding (RoPE)

Rotates consecutive pairs of query and key coordinates by frequency angles:

θi=100002(i1)/d\theta_i = 10000^{-2(i-1)/d} RΘ,mdxm=(xm(1)cos(mθ1)xm(2)sin(mθ1)xm(1)sin(mθ1)+xm(2)cos(mθ1))R_{\Theta, m}^d x_m = \begin{pmatrix} x_m^{(1)} \cos(m\theta_1) - x_m^{(2)} \sin(m\theta_1) \\ x_m^{(1)} \sin(m\theta_1) + x_m^{(2)} \cos(m\theta_1) \\ \vdots \end{pmatrix}

RoPE preserves relative distance: RΘ,mq,RΘ,nk\langle R_{\Theta, m} q, R_{\Theta, n} k \rangle depends solely on the relative offset (mn)(m - n).


Activation Functions

FunctionFormulaOutput RangeDerivative / Behavior
ReLUmax(0,x)\max(0, x)[0,)[0, \infty)11 if x>0x > 0 else 00; sparse activations
GELUxΦ(x)0.5x[1+tanh(2/π(x+0.044715x3))]x \Phi(x) \approx 0.5x[1 + \tanh(\sqrt{2/\pi}(x + 0.044715x^3))][0.17,)[-0.17, \infty)Smooth probabilistic stochastic regularizer
SiLU / Swishxσ(x)=x1+exx \cdot \sigma(x) = \frac{x}{1 + e^{-x}}[0.28,)[-0.28, \infty)Self-gated non-monotonic activation
SwiGLUSwish(xWgate)(xWup)\text{Swish}(x W_{\text{gate}}) \odot (x W_{\text{up}})(,)(-\infty, \infty)Modern transformer MLP standard (LLaMA)
Softmaxσ(z)i=ezi/τjezj/τ\sigma(z)_i = \frac{e^{z_i / \tau}}{\sum_j e^{z_j / \tau}}(0,1)(0, 1)Normalizes logits to sum to 1; temperature τ\tau controls entropy

Optimizers & Dynamics

AdamW Parameter Update (Decoupled Weight Decay)

mt=β1mt1+(1β1)gtm_t = \beta_1 m_{t-1} + (1 - \beta_1) g_t vt=β2vt1+(1β2)gt2v_t = \beta_2 v_{t-1} + (1 - \beta_2) g_t^2 m^t=mt1β1t,v^t=vt1β2t\hat{m}_t = \frac{m_t}{1 - \beta_1^t}, \quad \hat{v}_t = \frac{v_t}{1 - \beta_2^t} θt=θt1ηλθt1ηv^t+ϵm^t\theta_t = \theta_{t-1} - \eta \lambda \theta_{t-1} - \frac{\eta}{\sqrt{\hat{v}_t} + \epsilon} \hat{m}_t

In standard L2 regularization (Adam), weight decay is coupled to gradient scaling: gt+λθvt\frac{g_t + \lambda \theta}{\sqrt{v_t}}. AdamW decouples weight decay, applying it directly: θηλθ\theta - \eta \lambda \theta.

Cosine Annealing Learning Rate Schedule

ηt=ηmin+12(ηmaxηmin)(1+cos(tTmaxπ))\eta_t = \eta_{\min} + \frac{1}{2}(\eta_{\max} - \eta_{\min})\left(1 + \cos\left(\frac{t}{T_{\max}}\pi\right)\right)

Generative Modeling

DDPM Forward Diffusion Step (Closed-Form)

Given variance schedule β1,,βT\beta_1, \dots, \beta_T, define αt=1βt\alpha_t = 1 - \beta_t and αˉt=s=1tαs\bar{\alpha}_t = \prod_{s=1}^t \alpha_s:

q(xtx0)=N(xt;αˉtx0,(1αˉt)I)q(x_t \mid x_0) = \mathcal{N}\left(x_t; \sqrt{\bar{\alpha}_t} x_0, (1 - \bar{\alpha}_t) \mathbf{I}\right)

Sampling at arbitrary timestep tt:

xt=αˉtx0+1αˉtϵ,ϵN(0,I)x_t = \sqrt{\bar{\alpha}_t} x_0 + \sqrt{1 - \bar{\alpha}_t} \epsilon, \quad \epsilon \sim \mathcal{N}(0, \mathbf{I})

Variational Autoencoder (VAE) ELBO

logp(x)Eqϕ(zx)[logpθ(xz)]DKL(qϕ(zx)p(z))\log p(x) \ge \mathbb{E}_{q_\phi(z \mid x)}\left[\log p_\theta(x \mid z)\right] - D_{\text{KL}}\left(q_\phi(z \mid x) \parallel p(z)\right)

Closed-Form Gaussian KL Divergence

For encoder output qϕ(zx)=N(μ,diag(σ2))q_\phi(z \mid x) = \mathcal{N}(\mu, \text{diag}(\sigma^2)) and standard prior p(z)=N(0,I)p(z) = \mathcal{N}(0, \mathbf{I}):

DKL=12j=1J(1+log(σj2)μj2σj2)D_{\text{KL}} = -\frac{1}{2} \sum_{j=1}^J \left(1 + \log(\sigma_j^2) - \mu_j^2 - \sigma_j^2\right)
kl_loss = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())

On this page