Cheatsheet

Neural Networks & Deep Learning

All topics on one page

5modules
15articles
0definitions
7formulas

01

Fundamentals of Neural Networks

Perceptrons, multilayer networks, and the backpropagation algorithm

Perceptron and Multilayer Neural Networks

Formal neuron and perceptron → Activation functions → Multilayer network (MLP) → Weight initialization → Numerical example

Formulas

GELU: $G(z) = z \cdot \Phi(z)$ ($\Phi$ — normal CDF function). Used in BERT, GPT. Smoother than ReLU, combines "noisiness" with linearity.Glorot (Xavier) initialization: $\sigma^2 = 2/(n_l + n_{l-1})$ — keeps the variance of activations the same. For tanh/sigmoid.He initialization: $\sigma^2 = 2/n_{l-1}$ — for ReLU (accounts for zero half).

Neural networks are inspired by biology, but have long since become an independent mathematical field. Rosenblatt's perceptron (1957) is the simplest formal neuron that already reveals the main principle: linear transformation + nonlinear activation. Multilayer networks (MLP) made of such neurons...

Biological motivation: A neuron in the brain receives signals through dendrites, sums them in the cell body, and "fires" (spike) only when the threshold is exceeded. The perceptron models this: weighted sum of inputs + threshold activation function.

Perceptron: $y = \operatorname{sign}(w^\mathrm{T}x + b) = \operatorname{sign}(\sum_i w_ix_i + b)$, where $x \in \mathbb{R}^n$ is the input vector, $w \in \mathbb{R}^n$ are weights, $b$ is bias (threshold), sign is the sign function ($\pm1$). Geometrically: $w$ and $b$ define the hyperplane $w^\ma...

Perceptron convergence theorem (Rosenblatt, 1957): If classes are linearly separable, the update algorithm $w_{t+1} = w_t + y_ix_i$ (when there is an error on $(x_i, y_i)$) converges in a finite number of steps. The upper bound of iterations: $(R/\gamma)^2$, where $R = \max||x_i||$ is the radius,...

Convolutional Neural Networks (CNN)

Convolution Operation → Pooling and CNN Architecture → Evolution of Architectures → Specialized Architectures → Numerical Example

Formulas

VGGNet (2014): deep networks using only $3\times 3$ convolutions + max-pool. "Two 3×3 = one 5×5, but with fewer parameters." Top-5: 7.3%.
  • ·Weight sharing: one filter is applied to the entire image → shift invariance
  • ·Locality: each neuron “looks at” a local patch → local features
  • ·Hierarchy: early layers — edges, color; deep layers — objects, semantics

Image processing has become a breakthrough application of neural networks. Convolutional networks (CNN, LeCun et al., 1989) leverage the local structure of images: features (edges, textures) are local and shift-invariant. CNNs revolutionized computer vision, surpassing humans on ImageNet in 2012.

A fully connected network for a 224×224×3 image: the first layer would have 224×224×3 = 150,528 inputs. With 1,000 neurons: 150M parameters — inefficient, does not scale.

2D Convolution: A kernel (filter) $K \in \mathbb{R}^{f\times f\times C_{in}}$ “slides” over the input image $X \in \mathbb{R}^{H\times W\times C_{in}}$:

$(X * K)[i,j] = \sum_{m=0}^{f-1} \sum_{n=0}^{f-1} \sum_{c=0}^{C_{in}-1} X[i+m, j+n, c] \cdot K[m, n, c] + b$

Regularization and Prevention of Overfitting

Bias-Variance Tradeoff → L2 Regularization (Weight Decay) → L1 Regularization (LASSO) → Dropout (Srivastava et al., 2014) → Batch Normalization (Ioffe & Szegedy, 2015) → Data Augmentation → Early Stopping → Numerical Example

  • ·No regularization: train acc 99%, test acc 88% (overfitting 11%)
  • ·+ L2 (λ=1e-4): test acc 91%
  • ·+ Dropout (p=0.5 before FC): test acc 91.5%
  • ·+ Data augmentation (flip+crop): test acc 93.2%
  • ·+ Mixup (α=1.0): test acc 94.1%
  • ·All together: test acc 95.3%

Neural networks with millions of parameters can "memorize" the training set completely — overfit. The gap between train and test error is the main enemy in practice. Regularization is a set of techniques that reduce overfitting without sacrificing model expressiveness.

Simple model: high bias, low variance. Complex model (DNN): low bias (can approximate everything), but high variance (sensitive to data).

Double descent: With modern overparameterized networks, the classical U-shaped tradeoff is broken — when the number of parameters ≫ n, error decreases again.

Penalty: L_reg = L + λ/2 · ||θ||² = L + λ/2 · Σᵢ θᵢ². SGD update: θ ← θ − α·∇L − αλθ = θ(1 − αλ) − α·∇L. "Weight decay": each step decreases weights by a factor of (1−αλ) — pulls towards zero.

02

Deep Learning: Theory and Practice

Optimization, normalization, and advanced architectures

Training Deep Networks: Problems and Solutions

The Vanishing/Exploding Gradient Problem → Residual Learning → Highway Networks and Gate Mechanisms → Normalization Within Layers → Transfer Learning and Fine-tuning → Numerical Example

Formulas

Group Normalization: divides channels into $G$ groups, normalizes within each. Compromise between BN and LN. Works at $\text{bs}=1–2$.

Training neural networks with dozens of layers is a non-trivial task. Vanishing gradient, exploding gradient, dead neurons, poor initialization—all these are practical problems that have required decades of research to solve.

Mechanism: During backpropagation through $L$ layers: $\dfrac{\partial L}{\partial x_0} = \left(\prod_l \dfrac{\partial a^l}{\partial a^{l-1}}\right) \cdot \dfrac{\partial L}{\partial a^L}$. Each multiplier $\dfrac{\partial a^l}{\partial a^{l-1}} = W^l \cdot \operatorname{diag}(\sigma'(z^l))$. If...

Sigmoid and tanh: $\sigma'(z) \leq 0.25$ (maximum at $z=0$). For $L$ layers: $||\dfrac{\partial L}{\partial x_0}|| \leq (0.25 \cdot \max||W^l||)^L \rightarrow 0$ if $\max||W^l|| < 4$. ReLU eliminates saturation problem: $\sigma'(z) = 1$ for $z>0$ — gradient passes without attenuation.

Gradient clipping: for explosion: $\nabla \leftarrow \nabla \cdot \min(1, \tau/||\nabla||)$. Limit the gradient's norm. Standard in RNN/LSTM. PyTorch: `torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)`.

Explainable AI and Representation Analysis

Visualization of CNN Features → Interpretability via Feature Perturbations → Probing → Mechanistic Interpretability → Numerical Example

Neural networks are "black boxes": they output predictions but do not explain their decisions. In high-stakes domains (medicine, finance, law), explainability is a legal requirement (GDPR Art. 22). Interpretability also helps debug models and detect systematic errors.

What has the CNN learned? One can directly study the filters $W^{(1)}$ of the first layer (dimensions 3×3×3 or 7×7×3): they often resemble Gabor edges. But deeper layers are high-dimensional and not directly interpretable.

Activation maps: for an image $x$, we visualize the feature map $a^l[k]$ (the $k$-th channel of layer $l$). Bright pixels = strong response to the $k$-th pattern.

$ w_k = \frac{1}{Z}\sum_{i,j} \frac{\partial y^c}{\partial A_{ij}^k} \quad \text{(global average pooling of the gradients)} $

Graph Neural Networks (GNN)

The Problem of Learning on Graphs → Message Passing Framework → Specific GNN Architectures → Graph Transformer → GNN Applications → Numerical Example

Many real-world data have a graph structure: social networks (users + connections), molecules (atoms + bonds), road networks (intersections + roads), knowledge (entities + relations). Standard CNN and MLP ignore the structure — GNN preserve it.

Graph G = (V, E): n nodes, m edges. Each node v has features x_v ∈ ℝᵈ. Adjacency matrix A ∈ {0,1}^{n×n}. Task: prediction of the label for each node (node classification), edge (link prediction), or the entire graph (graph classification).

The main difference from ordinary data: there is no fixed order of nodes (graph is permutation-equivariant). The size of the graph may vary. It is necessary to account for the neighborhood structure.

The general principle of GNN is "message passing" between neighbors. Each node aggregates information from neighbors, updates its representation:

03

Recurrent Neural Networks

LSTM, GRU, sequence modeling, and NLP

RNN, LSTM, and GRU: Architectures for Sequences

Basic RNN → LSTM (Long Short-Term Memory, Hochreiter-Schmidhuber, 1997) → GRU (Gated Recurrent Unit, Cho et al., 2014) → Bidirectional RNN and Deep RNN → Numerical Example: Language Model

Recurrent neural networks are designed for processing sequences—text, time series, audio. They maintain “memory” through a hidden state passed through time. LSTM is a key invention from 1997 that enabled learning of long-term dependencies.

Breakdown: hₜ is the hidden state (memory of the past), xₜ is the input at time t, Wₕₕ is the “memory” matrix (links hₜ and hₜ₋₁), Wₓₕ is the input matrix, Wₕᵧ is the output matrix. The same weights W are used at every step (weight sharing over time).

Backpropagation Through Time (BPTT): To train an RNN, we unroll it over time and apply the chain rule: ∂L/∂Wₕₕ = Σₜ ∂L/∂hₜ · ∂hₜ/∂Wₕₕ. Gradient: ∂hₜ/∂hₛ = Π_{k=s}^{t-1} ∂hₖ₊₁/∂hₖ = Π_{k=s}^{t-1} Wₕₕᵀ diag(tanh'(zₖ)).

The product of (t−s) matrices → if ||Wₕₕᵀ diag(tanh'(zₖ))|| < 1: vanishing; if > 1: exploding. This is a problem for long sequences (T > 20–30).

Word Embeddings and Pretrained Language Models

The Problem of Word Representation → Word2Vec (Mikolov et al., 2013) → GloVe (Pennington et al., 2014) → Contextual Embeddings: ELMo and BERT → Numerical Example: Word2Vec Analogies

Before neural networks can process text, words must be transformed into numerical representations. The evolution from one-hot vectors to Word2Vec, FastText, and BERT demonstrates how neural networks have "learned" to "understand" language. This is one of the most revolutionary trajectories in NLP.

One-hot: word i → a vector of zeros with a one at the i-th position. Dimension = |V| (vocabulary, 50K–200K). Problems: (1) All pairwise cosine distances = 0 — there is no semantic similarity. (2) Huge dimensionality. (3) Sparsity.

Distributional Hypothesis (Harris, 1954): words with similar context have similar meanings. "Bank" and "finance" often occur in the same context → are close. "Cat" and "dog" are close. This is the basis for Word2Vec.

Skip-gram: Given a word $w_t$ — predict the surrounding words $w_{t-2}, \ldots, w_{t-1}, w_{t+1}, \ldots, w_{t+2}$ (window $c=2$).

Architectures for Time Series and Forecasting

Specifics of Time Series → Seq2Seq for Forecasting → N-BEATS (Oreshkin et al., 2020) → PatchTST and Transformers for Time Series → Comparison with Classical Methods → Numerical Example

Formulas

Train/test split: You cannot randomly shuffle! Only chronological splitting: train = first 80%, test = last 20%.

Time series—financial data, IoT sensors, meteorological observations, energy consumption—require specialized architectures. Neural networks compete with classical methods (ARIMA, ES) and often outperform them when sufficient data volume is available.

Key properties: Autocorrelation (values depend on the past). Trend (long-term direction). Seasonality (periodic patterns: daily, weekly, yearly). Non-stationarity (statistics change over time: mean, variance).

Preprocessing: Normalization: $\hat{x}_t = (x_t − \mu)/\sigma$. Differencing (removes non-stationarity): $\Delta x_t = x_t − x_{t-1}$. Seasonal differencing: $x_t − x_{t-s}$. Log transformation: $\ln(x_t)$—stabilizes variance under multiplicative seasonality.

Train/test split: You cannot randomly shuffle! Only chronological splitting: train = first 80%, test = last 20%.

04

Generative Models

GANs, VAEs, diffusion models, and data generation

Generative Adversarial Networks (GAN)

Principle of GAN: Game Setting → GAN Training Problems → Improvements: DCGAN, WGAN, StyleGAN → Numerical Example

GAN (Goodfellow et al., 2014) is one of the most creative architectural patterns in the history of machine learning. Yann LeCun called them “the most interesting idea in machine learning in the past 20 years.” They spawned an entire era of synthetic media: realistic faces, deepfakes, AI artists.

Two agents: Generator G: z → x̂ (generates synthetic data from noise z). Discriminator D: x → [0,1] (distinguishes real data from synthetic).

Decoding: D maximizes the probability of correct classification of real (D(x)→1) and fake (D(G(z))→0) data. G minimizes the probability that D will detect the fake (D(G(z))→1).

Optimal D with fixed G: D*(x) = p_data(x)/(p_data(x) + p_G(x)). Substituting: max_D V = −log(4) + 2·JSD(p_data || p_G). JSD is the Jensen-Shannon divergence. At equilibrium: p_G = p_data → D* = 1/2 → V = −log(4).

Variational Autoencoders and Diffusion Models

Variational Autoencoder (VAE, Kingma & Welling, 2013) → Diffusion Models (Ho et al., DDPM, 2020) → Latent Diffusion and Stable Diffusion → Numerical Example

Formulas

DDPM problem: Works in pixel space — slow ($512 \times 512 \times 3 = 786$K iterations per step).

VAE and diffusion models are alternatives to GANs for data generation, with fundamentally different mathematical foundations. VAE is based on variational Bayesian inference; diffusion models are based on iterative denoising and connections with diffusion equations.

Problem: We want to train a generative model $p_\theta(x) = \int p_\theta(x|z) p(z) dz$, where $z$ is a latent code. The integral over all $z$ is intractable (high-dimensional integration).

$ \log p_\theta(x) \geq \mathbb{E}_{q_\phi(z|x)}[\log p_\theta(x|z)] - KL(q_\phi(z|x) \| p(z)) = \mathrm{ELBO}(\theta, \phi; x) $

Decoding the two terms: (1) $\mathbb{E}[\log p_\theta(x|z)]$ — reconstruction loss: how well the decoder reconstructs $x$ from $z$ — reconstruction quality. (2) $-KL(q_\phi(z|x)\|p(z))$ — KL-divergence between the posterior $q_\phi(z|x)$ and prior $p(z)=N(0,I)$ — regularization, pulls the posteri...

Applications of Generative Models in Science and Industry

Molecule Generation and Drug Discovery → Data Synthesis for Privacy-Preserving ML → Text-to-Image and the AIGC Revolution → Generative Models in Climate Science → Numerical Example: GAN for ECG Synthesis

Generative models have gone far beyond academic tasks. They create real value in pharmaceuticals, materials science, data synthesis, media production, and climate science.

AlphaFold 2 (DeepMind, 2021): Revolutionary prediction of 3D protein structure from amino acid sequence. Architecture: Evoformer (transformer with “pair representation” of residue interactions) + Structure Module (explicit 3D positioning). Accuracy: error ≈ 0.96 Å — comparable to crystallographic...

Generative molecule discovery: VAE or normalizing flows in the space of SMILES strings. Candidate molecules: maximize binding to target, minimize toxicity, maximize solubility. Navigation in the latent space of molecules. Company Insilico Medicine: GAN-designed molecule against IPF (lung fibrosis...

Protein design (RFDiffusion, David Baker lab, 2023): Diffusion model for de novo protein design. Specifies desired function → generates structure → no amino acids yet → inverse folding (ProteinMPNN) → synthesis and test. 70% of designs function in the laboratory (vs 5% for predecessors).

05

Transformers and Large Language Models

The attention mechanism, BERT, GPT, and the LLM era

Attention Mechanism and Transformer Architecture

Attention Mechanism (Attention) → Multi-Head Attention → Transformer Architecture → Efficient Alternatives → Numerical Example

Transformer (Vaswani et al., “Attention Is All You Need”, 2017) is a revolutionary architecture that eliminated recurrence in sequence processing. Self-attention allows each element to interact directly with any other, without recurrent bottlenecks. Transformers now dominate in NLP, vision, prote...

Intuition: When translating “The animal didn't cross the street because it was too tired” → it is necessary to realize that “it” = “animal”. An RNN “forgets” long-range dependencies. Attention allows you to explicitly “look” at needed positions when generating each token.

Q (queries) ∈ ℝ^{n×d_k}: “what we’re searching for”. K (keys) ∈ ℝ^{m×d_k}: “what is offered” at all positions. V (values) ∈ ℝ^{m×d_v}: “what we take” from the selected positions.

Steps: (1) QKᵀ ∈ ℝ^{n×m}: dot products of every query-key pair. Element [i,j]: how much position j “responds” to query at position i. (2) /√d_k: scaling prevents overly large values with high d_k (otherwise softmax saturates). (3) softmax: normalization into a distribution (rows sum to 1). (4) ·V...

Large Language Models: Capabilities and Limitations

Scaling Laws → Emergent Abilities → RLHF: Reinforcement Learning from Human Feedback → Limitations of LLMs → Numerical Example

GPT-4, Claude, Gemini, LLaMA — large language models (LLM) have become a transformative technology of the 2020s. Understanding their capabilities, training mechanisms, and limitations is a necessity for every AI specialist.

Interpretation: N — number of parameters, D — dataset size in tokens, C — compute budget in FLOPs. Loss decreases according to a power law as each factor increases — improvement is predictable and continuous. A practical conclusion: more data + larger model + more computation → better results.

Hoffmann-Chinchilla Law (DeepMind, 2022): Optimal ratio at fixed C: N∝D — parameters ≈ tokens (multiplied by a constant ≈ 20). GPT-3 (175B) was trained on 300B tokens → “undertrained”. Chinchilla (70B, 1.4T tokens) outperforms Gopher (280B, 300B tokens). Strategy: fewer parameters, more data → gr...

Inference efficiency: LLaMA-3-8B (8B parameters, 15T tokens): 90% of GPT-3 performance with 22× fewer parameters and open access.

Fine-tuning and Application of LLMs in Specialized Fields

Methods for Adapting LLMs → Retrieval-Augmented Generation (RAG) → Specialized LLMs → Evaluation of LLMs → Numerical Example

  • ·Rank r=16, alpha=32, target modules=q_proj, v_proj
  • ·Trainable params: 16.7M (0.21% of 8B)
  • ·Training: 3 epochs × 50K samples, A100 40GB, ~6 hours
  • ·Val loss: 1.82 → 1.31 (28% reduction)
  • ·NER in contracts (extraction of parties, dates, sums): F1 = 0.91 vs 0.74 for base LLaMA-3-8B (zero-shot)
  • ·RAG over contract database: F1 = 0.86 (worse than fine-tune, but no retraining needed)

Universal LLMs are strong across a broad range of tasks, but in specialized fields—medicine, law, finance, code—adaptation is required. Fine-tuning and RAG allow for the creation of domain experts without training from scratch.

Full Fine-tuning: we update all model parameters on domain data. GPT-3 (175B): fine-tuning cost ~$100K. Risk of "catastrophic forgetting": the model loses general knowledge. Justified only for highly specialized tasks with a large dataset.

LoRA (Low-Rank Adaptation, Hu et al., 2021): We add to each weight matrix W a low-rank update: W' = W + BA, where B ∈ ℝ^{d×r}, A ∈ ℝ^{r×k}, r ≪ min(d,k). We train only B and A (0.01–1% parameters). Initialization: B=0, A~N — initially, there are no changes. During inference: W' = W + BA — just ad...

QLoRA (Dettmers et al., 2023): LoRA + 4-bit NormalFloat quantization of the base model. Allows fine-tuning LLaMA-65B on a single A100 GPU (80GB). "Killer feature" for academic labs.