Deep Learning Algorithms
Discover and understand AI algorithms at Programming Ocean Academy with clear explanations, equations, and illustrations for better learning.
AI Algorithms Explainer
Attention Mechanism
$$ \text{Attention}(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V $$
- Q (Query): Represents the current word or position we're focusing on.
- K (Key): Represents the words or positions we're comparing the query against.
- V (Value): Carries the actual information associated with each key.
- $QK^T$: Dot product indicating similarity between queries and keys.
- $\sqrt{d_k}$: Scaling factor to prevent large dot product values.
- softmax: Converts similarity scores into probabilities.
- Result: A weighted sum of values based on similarity scores.
Recurrent Neural Network (RNN)
$$ h_t = f(W_{hh}h_{t-1} + W_{xh}x_t + b_h) $$
- $h_t$: Hidden state at time step t, capturing sequence information.
- $h_{t-1}$: Hidden state from the previous time step.
- $x_t$: Input at the current time step.
- $W_{hh}$: Weight matrix for hidden-to-hidden connections.
- $W_{xh}$: Weight matrix for input-to-hidden connections.
- $b_h$: Bias term added to the hidden state computation.
- $f$: Activation function (usually tanh or ReLU).
Long Short-Term Memory (LSTM)
$$ \begin{aligned} f_t &= \sigma(W_f \cdot [h_{t-1}, x_t] + b_f) \\ i_t &= \sigma(W_i \cdot [h_{t-1}, x_t] + b_i) \\ \tilde{C}_t &= \tanh(W_C \cdot [h_{t-1}, x_t] + b_C) \\ C_t &= f_t * C_{t-1} + i_t * \tilde{C}_t \\ o_t &= \sigma(W_o \cdot [h_{t-1}, x_t] + b_o) \\ h_t &= o_t * \tanh(C_t) \end{aligned} $$
- $x_t$: Input at time step t.
- $h_t$: Hidden state at time step t.
- $C_t$: Cell state at time step t.
- $f_t$: Forget gate, decides what information to discard from the cell state.
- $i_t$: Input gate, determines what new information to store.
- $\tilde{C}_t$: Candidate cell state update.
- $o_t$: Output gate, controls the output from the cell state.
- $\sigma$: Sigmoid activation function; $\tanh$: Hyperbolic tangent activation.
Gated Recurrent Unit (GRU)
$$ \begin{aligned} z_t &= \sigma(W_z \cdot [h_{t-1}, x_t] + b_z) \\ r_t &= \sigma(W_r \cdot [h_{t-1}, x_t] + b_r) \\ \tilde{h}_t &= \tanh(W_h \cdot [r_t * h_{t-1}, x_t] + b_h) \\ h_t &= (1 - z_t) * h_{t-1} + z_t * \tilde{h}_t \end{aligned} $$
- $x_t$: Input at time step t.
- $h_t$: Hidden state at time step t.
- $z_t$: Update gate, controls the balance between the new and previous hidden state.
- $r_t$: Reset gate, determines how much of the past information to forget.
- $\tilde{h}_t$: Candidate hidden state, combines new input with past memory.
- $\sigma$: Sigmoid activation; $\tanh$: Hyperbolic tangent activation.
- Advantage: Fewer parameters than LSTM, faster training with similar performance.
Feedforward Neural Network (FNN)
$$ a^{(l)} = f(W^{(l)}a^{(l-1)} + b^{(l)}) $$
- $a^{(l)}$: Activation/output of layer l.
- $a^{(l-1)}$: Activation from the previous layer.
- $W^{(l)}$: Weight matrix for layer l.
- $b^{(l)}$: Bias vector for layer l.
- $f$: Activation function (e.g., ReLU, sigmoid, tanh).
- Structure: Data flows one direction from input to output without cycles.
Convolutional Neural Network (CNN)
$$ (X * K)(i, j) = \sum_m \sum_n X(i+m, j+n) \cdot K(m, n) $$
- $X$: Input image or feature map.
- $K$: Kernel or filter matrix.
- $(i, j)$: Position in the output feature map.
- $*$: Convolution operation.
- Result: Each output value is a weighted sum of neighboring input values, highlighting patterns like edges or textures.
- Layers: CNNs typically include convolutional layers, pooling layers, and fully connected layers.
Generative Adversarial Network (GAN)
$$ \min_G \max_D V(D, G) = \mathbb{E}_{x \sim p_{\text{data}}(x)}[\log D(x)] + \mathbb{E}_{z \sim p_z(z)}[\log(1 - D(G(z)))] $$
- $G$ (Generator): Tries to create data that mimics the real data distribution.
- $D$ (Discriminator): Attempts to distinguish real data from generated (fake) data.
- $x$: Real data sampled from the true data distribution $p_{\text{data}}(x)$.
- $z$: Random noise sampled from a prior distribution $p_z(z)$ (e.g., Gaussian).
- $G(z)$: Fake data generated by the generator from noise $z$.
- Objective: Generator minimizes, Discriminator maximizes the loss, forming a two-player minimax game.
Autoencoder
$$ \hat{x} = f_{\text{decoder}}(f_{\text{encoder}}(x)) $$
- $x$: Original input data.
- $f_{\text{encoder}}$: Maps input to a compressed latent representation.
- $f_{\text{decoder}}$: Reconstructs the input from the latent representation.
- $\hat{x}$: Reconstructed output, ideally close to the original input.
- Goal: Minimize reconstruction loss (e.g., MSE) between $x$ and $\hat{x}$.
- Use Cases: Dimensionality reduction, denoising, anomaly detection, representation learning.
Variational Autoencoder (VAE)
$$ \mathcal{L} = \mathbb{E}_{q_\phi(z|x)}[\log p_\theta(x|z)] - D_{KL}(q_\phi(z|x) \parallel p(z)) $$
- $x$: Input data.
- $z$: Latent variable sampled from an approximate posterior $q_\phi(z|x)$.
- $q_\phi(z|x)$: Encoder, approximating the true posterior distribution of $z$ given $x$.
- $p_\theta(x|z)$: Decoder, reconstructs $x$ from $z$.
- $D_{KL}$: Kullback–Leibler divergence, regularizes the learned latent distribution toward a prior (e.g., standard normal).
- Objective: Maximize reconstruction accuracy while keeping latent variables close to a known distribution.
Sparse Autoencoder
$$ \mathcal{L} = \|x - \hat{x}\|^2 + \beta \sum_j KL(\rho \| \hat{\rho}_j) $$
- $x$: Input data.
- $\hat{x}$: Reconstructed data output from the decoder.
- $\|x - \hat{x}\|^2$: Reconstruction loss (usually Mean Squared Error).
- $\beta$: Weight for the sparsity penalty term.
- $\rho$: Desired average activation (sparsity target, usually a small value like 0.05).
- $\hat{\rho}_j$: Actual average activation of hidden unit $j$ over the training set.
- $KL(\rho \| \hat{\rho}_j)$: Kullback–Leibler divergence penalizing deviation from desired sparsity.
- Goal: Encourage most hidden units to be inactive (near zero) for each input, enhancing feature selectivity.

