Regression Algorithms
Discover and understand AI algorithms at Programming Ocean Academy with clear explanations, equations, and illustrations for better learning.
AI Algorithms Explainer
Linear Regression
$$ y = f\left(\sum_{i=1}^{n}w_ix_i + b\right) $$
- y: Predicted value or output of the model.
- f: Activation function (e.g., identity function for regression).
- wi: Weights assigned to each input feature.
- xi: Input features or variables.
- b: Bias term, allowing flexibility in the prediction.
Polynomial Regression
$$ y = \beta_0 + \beta_1x + \beta_2x^2 + \dots + \beta_nx^n + \epsilon $$
- $y$: Target or predicted value.
- $x$: Input feature or variable.
- $\beta_0, \beta_1, \dots, \beta_n$: Coefficients of the polynomial terms.
- $n$: Degree of the polynomial.
- $\epsilon$: Error term capturing the noise in predictions.
- Goal: Capture non-linear relationships between the input and output variables using higher-order terms.
Ridge Regression (L2 Regularization)
$$ \mathcal{L} = \sum_{i=1}^{n}(y_i - \hat{y}_i)^2 + \lambda \sum_{j=1}^{p} \beta_j^2 $$
- $y_i$: Actual value for the ith data point.
- $\hat{y}_i$: Predicted value from the model.
- $\beta_j$: Coefficient for the jth feature.
- $\lambda$: Regularization strength (hyperparameter).
- First term: Mean Squared Error (MSE) loss for fitting.
- Second term: L2 penalty discouraging large weights to reduce overfitting.
- Effect: Shrinks coefficients but keeps all features; balances bias and variance.
Lasso Regression (L1 Regularization)
$$ \mathcal{L} = \sum_{i=1}^{n}(y_i - \hat{y}_i)^2 + \lambda \sum_{j=1}^{p} |\beta_j| $$
- $y_i$: Actual value for the ith observation.
- $\hat{y}_i$: Predicted value by the model.
- $\beta_j$: Coefficients for each feature.
- $\lambda$: Regularization strength controlling the L1 penalty.
- First term: Mean Squared Error (MSE) for accuracy of predictions.
- Second term: L1 penalty encourages sparsity in the model coefficients.
- Effect: Drives some coefficients to zero, effectively performing feature selection.
Elastic Net Regression (L1 + L2 Regularization)
$$ \mathcal{L} = \sum_{i=1}^{n}(y_i - \hat{y}_i)^2 + \lambda_1 \sum_{j=1}^{p} |\beta_j| + \lambda_2 \sum_{j=1}^{p} \beta_j^2 $$
- $y_i$: Actual output for the ith observation.
- $\hat{y}_i$: Predicted output from the model.
- $\beta_j$: Coefficient for the jth feature.
- $\lambda_1$: Weight of the L1 penalty (sparsity).
- $\lambda_2$: Weight of the L2 penalty (shrinkage).
- Combines: Benefits of both Lasso (feature selection) and Ridge (stability).
- Use Case: Works well when there are multiple correlated features or when feature selection is needed.
Logistic Regression
$$ P(y=1|x) = \frac{1}{1 + e^{-(\beta_0 + \beta_1x_1 + \dots + \beta_nx_n)}} $$
- $P(y=1|x)$: Probability that the output $y$ is 1 given the input features $x$.
- $\beta_0$: Intercept term.
- $\beta_1, \dots, \beta_n$: Coefficients for each feature $x_1, \dots, x_n$.
- $e$: Base of the natural logarithm (used in the sigmoid function).
- Sigmoid function: Maps input to a value between 0 and 1.
- Use Case: Binary classification problems (e.g., spam detection, medical diagnosis).
Poisson Regression
$$ \log(\lambda) = \beta_0 + \beta_1x_1 + \dots + \beta_nx_n $$
- $\lambda$: Expected count or rate (mean of the Poisson distribution).
- $\log(\lambda)$: Link function connecting predictors to the Poisson mean.
- $\beta_0$: Intercept term.
- $\beta_1, \dots, \beta_n$: Coefficients for predictor variables $x_1, \dots, x_n$.
- Distribution: Assumes target variable follows a Poisson distribution (non-negative integers).
- Use Case: Modeling event counts, such as number of customer arrivals or defects per unit.
Probit Regression
$$ P(y=1|x) = \Phi(\beta_0 + \beta_1x_1 + \dots + \beta_nx_n) $$
- $P(y=1|x)$: Probability that the outcome $y$ is 1 given inputs $x$.
- $\Phi$: Cumulative distribution function (CDF) of the standard normal distribution.
- $\beta_0$: Intercept term.
- $\beta_1, \dots, \beta_n$: Coefficients for each predictor variable.
- Difference from Logistic Regression: Uses the normal CDF instead of the logistic (sigmoid) function for mapping inputs to probabilities.
- Use Case: Binary classification when the underlying latent variable assumption follows a normal distribution.
Decision Tree Regression
$$ \hat{y} = \frac{1}{|R_m|} \sum_{x_i \in R_m} y_i $$
- $R_m$: Region (leaf node) $m$ in the feature space partitioned by the tree.
- $|R_m|$: Number of samples in region $R_m$.
- $y_i$: Actual target value for sample $i$.
- $\hat{y}$: Predicted value, calculated as the average of $y_i$ in the region.
- Splitting: Based on minimizing Mean Squared Error (MSE) within nodes.
- Interpretability: Provides a flowchart-like model that's easy to understand.
- Use Case: Non-linear regression problems with interpretable rules and handling of heterogeneous data.
Random Forest Regression
$$ \hat{y} = \frac{1}{T} \sum_{t=1}^{T} \hat{y}^{(t)} $$
- $\hat{y}^{(t)}$: Prediction from the tth decision tree.
- $T$: Total number of trees in the forest.
- $\hat{y}$: Final prediction, computed as the average of predictions from all trees.
- Ensemble: Combines multiple decision trees to improve generalization.
- Bootstrapping: Each tree is trained on a random sample with replacement (bagging).
- Random Feature Selection: A random subset of features is considered for splitting at each node.
- Use Case: Complex regression tasks with high variance or many features; robust to overfitting.
Gradient Boosting Regression
$$ F_m(x) = F_{m-1}(x) + \gamma_m h_m(x) $$
- $F_m(x)$: The boosted model after $m$ iterations.
- $F_{m-1}(x)$: Previous ensemble prediction.
- $h_m(x)$: New weak learner (usually a shallow decision tree).
- $\gamma_m$: Learning rate or step size controlling contribution of $h_m$.
- Boosting: Sequentially adds models to correct errors made by previous models.
- Loss Function: Optimized using gradient descent (e.g., MSE for regression).
- Use Case: High-accuracy regression tasks with complex patterns in data.
XGBoost Regression
$$ \mathcal{L}^{(t)} = \sum_{i=1}^{n} l(y_i, \hat{y}_i^{(t-1)} + f_t(x_i)) + \Omega(f_t) $$
- $l$: Loss function (e.g., squared error) measuring model prediction error.
- $f_t(x_i)$: New tree added at iteration $t$.
- $\hat{y}_i^{(t-1)}$: Prediction from the previous model.
- $\Omega(f_t)$: Regularization term penalizing model complexity (e.g., number of leaves).
- Objective: Minimize a regularized loss for better generalization and performance.
- Efficiency: Uses histogram-based splits, parallelization, and regularization for speed and accuracy.
- Use Case: Structured/tabular regression problems with high accuracy needs.
LightGBM Regression
$$ \mathcal{L} = \sum_{i=1}^{n} l(y_i, \hat{y}_i) + \sum_{t=1}^{T} \Omega(f_t) $$
- $y_i$: Actual target value for the ith sample.
- $\hat{y}_i$: Predicted output from the ensemble of trees.
- $l$: Loss function used for training (e.g., squared error).
- $f_t$: Prediction function (tree) at iteration t.
- $\Omega(f_t)$: Regularization term for controlling model complexity (e.g., number of leaves, tree depth).
- Leaf-wise tree growth: Grows trees leaf-wise (best-first) reducing more loss but potentially deeper trees.
- Optimized for speed: Utilizes histogram-based splitting, exclusive feature bundling, and gradient-based one-side sampling.
- Use Case: Large-scale tabular regression with high performance and efficiency.
CatBoost Regression
$$ \mathcal{L} = \sum_{i=1}^{n} l(y_i, \hat{y}_i) + \sum_{t=1}^{T} \Omega(f_t) $$
- $y_i$: Actual target value for the ith data point.
- $\hat{y}_i$: Predicted value from the model.
- $l$: Loss function (e.g., squared error) measuring prediction error.
- $f_t$: Prediction function (tree) added at iteration t.
- $\Omega(f_t)$: Regularization term penalizing model complexity.
- Ordered Boosting: CatBoost handles categorical features natively and uses ordered boosting to reduce prediction shift.
- Efficient Handling: Automatically processes categorical variables without extensive preprocessing.
- Use Case: Tabular regression with categorical features; reliable performance out-of-the-box.
Support Vector Regression (SVR)
$$ \min_{\mathbf{w}, b, \xi, \xi^*} \frac{1}{2} \|\mathbf{w}\|^2 + C \sum_{i=1}^{n} (\xi_i + \xi_i^*) $$
subject to:
$$ y_i - (\mathbf{w} \cdot \mathbf{x}_i + b) \leq \epsilon + \xi_i $$
$$ (\mathbf{w} \cdot \mathbf{x}_i + b) - y_i \leq \epsilon + \xi_i^* $$
$$ \xi_i, \xi_i^* \geq 0 $$
- $\mathbf{x}_i$: Input vector for the ith sample.
- $y_i$: Actual output.
- $\mathbf{w}$, $b$: Weight vector and bias term.
- $\epsilon$: Margin of tolerance where no penalty is given for errors.
- $\xi_i$, $\xi_i^*$: Slack variables for points outside the $\epsilon$-tube.
- $C$: Regularization parameter controlling the trade-off between flatness and tolerance to deviations.
- Kernel Trick: SVR can model non-linear relationships using kernel functions (e.g., RBF).
- Use Case: Robust to outliers and effective for high-dimensional and non-linear regression tasks.
Bayesian Linear Regression
$$ p(\mathbf{w}|\mathcal{D}) = \frac{p(\mathcal{D}|\mathbf{w}) \, p(\mathbf{w})}{p(\mathcal{D})} $$
$$ \hat{y} = \mathbf{x}^\top \mathbb{E}[\mathbf{w}] $$
- $p(\mathbf{w}|\mathcal{D})$: Posterior distribution of weights given data.
- $p(\mathcal{D}|\mathbf{w})$: Likelihood of data under model parameters.
- $p(\mathbf{w})$: Prior belief about weight distribution.
- $p(\mathcal{D})$: Marginal likelihood (evidence).
- $\hat{y}$: Prediction is the expected value of the output using the posterior mean of weights.
- Uncertainty: Provides confidence intervals and probabilistic predictions.
- Use Case: Useful in small datasets and applications where uncertainty estimation is important.
Multilayer Perceptron (MLP) Regression
$$ y = f_L(\dots f_2(f_1(\mathbf{x}W_1 + b_1)W_2 + b_2) \dots W_L + b_L) $$
- $\mathbf{x}$: Input feature vector.
- $W_l$, $b_l$: Weights and biases of the lth layer.
- $f_l$: Activation function at layer l (e.g., ReLU, tanh).
- $y$: Continuous output prediction.
- Structure: Fully connected layers, forming a feedforward neural network.
- Flexibility: Capable of modeling complex nonlinear relationships.
- Use Case: Regression tasks involving high-dimensional or complex data patterns.
Deep Neural Network (DNN) Regression
$$ y = f^{(L)}(f^{(L-1)}(\dots f^{(2)}(f^{(1)}(\mathbf{x}W^{(1)} + b^{(1)})) \dots )) $$
- $\mathbf{x}$: Input feature vector.
- $W^{(l)}, b^{(l)}$: Weights and biases of layer l.
- $f^{(l)}$: Activation function at layer l (e.g., ReLU, Leaky ReLU, tanh).
- $y$: Final output prediction, typically continuous for regression.
- Depth: Multiple hidden layers capture hierarchical feature representations.
- Capacity: Capable of learning complex nonlinear patterns with high expressiveness.
- Use Case: High-dimensional, structured/unstructured data where simple models underperform.
Quantile Regression
$$ \mathcal{L}_\tau(y, \hat{y}) = \begin{cases} \tau (y - \hat{y}) & \text{if } y \geq \hat{y} \\ (1 - \tau)(\hat{y} - y) & \text{if } y < \hat{y} \end{cases} $$
- $y$: Actual observed value.
- $\hat{y}$: Predicted quantile value.
- $\tau$: Quantile level (e.g., 0.5 for median, 0.25 for lower quartile).
- Loss Function: Asymmetric, penalizes over- and under-predictions differently based on $\tau$.
- Flexibility: Models conditional quantiles instead of just the mean.
- Use Case: Forecasting range estimates, capturing distribution spread (e.g., in risk modeling).
Robust Regression
$$ \min_{\beta} \sum_{i=1}^{n} \rho(y_i - \mathbf{x}_i^\top \beta) $$
- $y_i$: Actual response for the ith observation.
- $\mathbf{x}_i$: Input features for the ith observation.
- $\beta$: Regression coefficients.
- $\rho$: Robust loss function (e.g., Huber loss) less sensitive to outliers than squared error.
- Goal: Minimize the impact of outliers on model estimation.
- Methods: M-estimators, RANSAC, Huber regression, Theil–Sen estimator.
- Use Case: Data with noisy outliers or when standard least squares assumptions don’t hold.
Stepwise Regression
$$ \text{Model Selection: Add or remove predictors based on a criterion (e.g., AIC, BIC)} $$
- Approach: Iteratively adds or removes predictors from a multiple regression model.
- Forward Selection: Starts with no predictors, adds one at a time based on performance improvement.
- Backward Elimination: Starts with all predictors, removes the least significant iteratively.
- Bidirectional Elimination: Combination of forward selection and backward elimination.
- Criteria: AIC (Akaike Information Criterion), BIC (Bayesian Information Criterion), or adjusted R².
- Use Case: Automated feature selection when interpretability and simplicity are desired.
Principal Component Regression (PCR)
$$ \hat{y} = Z \beta + \epsilon \quad \text{where} \quad Z = XW $$
- $X$: Original feature matrix.
- $W$: Matrix of principal component loadings (from PCA).
- $Z$: Principal components (transformed features).
- $\beta$: Coefficients fitted on the principal components.
- $\hat{y}$: Predicted values; $\epsilon$: error term.
- Dimensionality Reduction: Retains only top components that explain most variance in data.
- Use Case: Regression when predictors are highly collinear or numerous.
Partial Least Squares Regression (PLSR)
$$ X = T P^\top + E, \quad Y = U Q^\top + F $$
$$ \text{Find latent vectors } T, U \text{ such that } T \text{ explains both } X \text{ and } Y $$
- $X$: Predictor matrix.
- $Y$: Response matrix.
- $T$, $U$: Latent scores capturing directions that explain variation in both $X$ and $Y$.
- $P$, $Q$: Loadings relating $T$ and $U$ to $X$ and $Y$, respectively.
- $E$, $F$: Residuals for $X$ and $Y$.
- Compared to PCR: PLS finds directions that explain both predictors and responses, not just variance in $X$.
- Use Case: High-dimensional data with multicollinearity or when response is also multivariate.
Autoregressive Models (AR, ARIMA)
$$ \text{AR}(p): \quad y_t = \phi_1 y_{t-1} + \dots + \phi_p y_{t-p} + \epsilon_t $$
$$ \text{ARIMA}(p,d,q): \quad \Delta^d y_t = \phi_1 y_{t-1} + \dots + \theta_q \epsilon_{t-q} + \epsilon_t $$
- $y_t$: Value at time step $t$.
- $\phi_i$: Coefficients of lagged observations (AR terms).
- $\theta_j$: Coefficients of past error terms (MA terms).
- $\Delta^d$: Differencing operator of order $d$ to make data stationary.
- $\epsilon_t$: White noise error term.
- AR: Uses past values only.
- MA: Uses past forecast errors.
- ARIMA: Combines AR, MA, and differencing to model non-stationary time series.
- Use Case: Forecasting stock prices, sales, or weather where historical patterns matter.
Kernel Ridge Regression
$$ \hat{y}(x) = \sum_{i=1}^{n} \alpha_i K(x, x_i) $$
$$ \alpha = (K + \lambda I)^{-1} y $$
- $K(x, x_i)$: Kernel function measuring similarity between input $x$ and training sample $x_i$ (e.g., RBF, polynomial).
- $\alpha_i$: Dual coefficients learned from the data.
- $\lambda$: Regularization parameter controlling overfitting.
- $K$: Kernel matrix with $K_{ij} = K(x_i, x_j)$.
- Nonlinearity: Enables modeling of complex relationships via the kernel trick.
- Use Case: Regression with non-linear patterns and small-to-medium datasets where interpretability isn’t the priority.
Adam Optimizer Algorithm
$$ \theta_t = \theta_{t-1} - \frac{\eta}{\sqrt{\hat{v_t}} + \epsilon} \hat{m_t} $$
- \(\theta_t\): Updated parameter values at time step \(t\).
- \(\theta_{t-1}\): Previous parameter values.
- \(\eta\): Learning rate, which controls the step size for updates.
- \(\hat{m_t}\): Bias-corrected first moment estimate (mean of gradients).
- \(\hat{v_t}\): Bias-corrected second moment estimate (variance of gradients).
- \(\epsilon\): A small constant to prevent division by zero (e.g., \(10^{-8}\)).
Stochastic Gradient Descent (SGD)
$$ \theta_t = \theta_{t-1} - \eta \nabla_\theta J(\theta) $$
- \(\theta_t\): Updated parameter values at time step \(t\).
- \(\theta_{t-1}\): Previous parameter values.
- \(\eta\): Learning rate, which determines the step size for updates.
- \(\nabla_\theta J(\theta)\): Gradient of the loss function \(J(\theta)\) with respect to the parameters.
- Purpose: Minimizes the loss function by iteratively updating the parameters in the direction of the negative gradient.
- Illustration: The image represents how the algorithm iteratively reduces the cost by adjusting weights using gradients.
Binary Classification Algorithm
$$ P(y|x) = \sigma(w^Tx + b) = \frac{1}{1 + e^{-(w^Tx + b)}} $$
- \(P(y|x):\) Probability of class \(y = 1\) given input features \(x\).
- \(\sigma:\) Sigmoid activation function, maps output to a range between 0 and 1.
- \(w^T x:\) Weighted sum of the input features.
- \(b:\) Bias term, allowing flexibility in the decision boundary.
- Output: If \(P(y|x) > 0.5\), classify as class 1; otherwise, classify as class 0.
- Purpose: To separate data into two distinct classes by fitting a decision boundary.
K-Nearest Neighbors (KNN) Algorithm
$$ d(x, x_i) = \sqrt{\sum_{j=1}^n (x_j - x_{ij})^2} $$
- \(d(x, x_i):\) Distance between the input \(x\) and the \(i\)-th training data point \(x_i\).
- \(\sqrt{\sum_{j=1}^n (x_j - x_{ij})^2}:\) Euclidean distance formula to calculate the similarity between two points.
- K: Number of neighbors to consider for classification.
- Majority Voting: The class label is determined by the majority of the K nearest neighbors.
- Purpose: Classify new data points by considering the closest points in the training set.
Support Vector Classifier (SVC)
$$ f(x) = \text{sign}(w^T \phi(x) + b) $$
- \(f(x):\) Decision function that determines the class of \(x\).
- \(w^T:\) Weight vector that defines the orientation of the decision boundary.
- \(\phi(x):\) Feature mapping function to project input into higher dimensions for linear separability.
- \(b:\) Bias term that shifts the decision boundary.
- Support Vectors: Data points closest to the decision boundary, crucial for determining the margin.
- Purpose: Finds the hyperplane that maximizes the margin between two classes for classification.
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.

