Machine Learning
Table of Contents
- 1. Paradigms
- 2. Perception Space and Concept Space.
- 3. World Model
- 4. Latent Space
- 5. Loss Function
- 6. Bias-Variance Tradeoff
- 7. Regularization
- 8. Regression
- 9. Variational Inference
- 10. Hopfield Network
- 11. Neural Network
- 12. Learning
- 13. Reinforcement Learning
- 14. Interpretation
- 15. History
- 16. Products
- 17. Softwares
- 18. Model Formats
- 19. Applications
- 20. Reference
1. Paradigms
1.1. Supervised Leaning
- Trained using labeled data
1.2. Unsupervised Leaning
- Unlabeled data
1.3. Reinforcement Leaning
- Use rewards
2. Perception Space and Concept Space.
A layer of neurons are like the classifying data with bunch of lines. Multiple layers correspond to cumulatively folding the paper.
3. World Model
An world model was given to the chess engine in 1970s. Then the AlphaGoZero build its own world model from its experience. LLM's world model is genral enough that it applies to individual cases.
The world model can be used to make the model dream. If the search space is too large, model uses intuition that is given by a neural network, and rollout random possible sequences of moves.
The recent GPT-4 uses the rollout to the thoughts within the tree of thoughts, to find the optimal response.
4. Latent Space
The embedding space.
4.1. Visualization
- Latent Space Visualisation: PCA, t-SNE, UMAP | Deep Learning Animated - YouTube
- Principal Component Analysis
- Fast but not extensible.
- t-SNE
- Maximize the probability that the low dimensional representation happens, according to a Gaussian distribution around each point.
- UMAP
- Find k nearest neighbor, and match the weights of the low dimensional adjacency graph with the high dimensional one.
5. Loss Function
5.1. Functions
5.1.1. Cross-Entropy Loss
- Calculate the cross entropy of the prediction with respect to the values that are expected.
5.1.1.1. Advantages
- Works well when the model outputs are probabilities
- Good for imbalanced classes
- Offers smoother gradients than other loss functions
6. Bias-Variance Tradeoff
Given an input variable \( x \sim p(x) \) and an unknown function \( f \) that generated the output variable \( y \) as \( y = f(x) + \varepsilon, \varepsilon \sim N(0,\sigma^2)\), we can train a model \( \hat{f} \) to predict the output of \( f \), on a set of training dataset \( \mathcal{D} := \{ (x_1, y_1), ..., (x_n, y_n) \}\) of size \( n \) sampled from the joint probability distribution \( p(x, y) \). with \( y_i = f(x_i) + \varepsilon, \varepsilon \sim N(0, \sigma^2)\).
For a given learning algorithm, different training dataset yields different model \( \hat{f} \) deterministically. We can evaluate the algorithm by taking the average over every possible training dataset \( \mathcal{D} \). In other world, we are taking average over all possible model \( \hat{f} \).
\begin{align*} \mathrm{Bias}[\hat{f}(x)] :=& \mathrm{E}_{\mathcal{D}}[\hat{f}(x)] - f(x) \\ = & \mathrm{E}_{\mathcal{D}}[\hat{f}(x)] - \mathrm{E}_{p(y|x)} [y], \\ \mathrm{Var}[\hat{f}(x)] := & \mathrm{E}_{\mathcal{D}}\left[ \left( \hat{f}(x) - \mathrm{E}_{\mathcal{D}}[\hat{f}(x)] \right)^2 \right]. \end{align*}There are also irreducible noise \( \sigma^2 \) from the noise term \( \varepsilon \): \[ \sigma^2 = \mathrm{E}_{p(y|x)}\left[ (y - f(x))^2 \right]. \]
The mean square error \( \mathrm{MSE} \) consists of these three properties determine the fitness of the algorithm:
\begin{align*} \mathrm{MSE}(x) :=& \mathrm{E}_{\mathcal{D}}\left[ (\hat{f}(x) - y)^2 \right] \\ =& \mathrm{Bias}[\hat{f}(x)]^2 + \mathrm{Var}[\hat{f}(x)] + \sigma^2. \end{align*}Bias, variance, irreducible noise, and mean square error are defined pointwise so far. We can also define global bias, global variance, MSE loss function specific to learning algorithm and the distribution \( p(x) \) of the input.
\begin{align*} \mathrm{Bias}^2 &:= \mathrm{E}_{p(x)}[\mathrm{Bias}[\hat{f}(x)]^2], \\ \mathrm{Variance} &:= \mathrm{E}_{p(x)}[\mathrm{Var}[\hat{f}(x)]], \\ \mathrm{MSE} &:= \mathrm{E}_{p(x)}\left[ \mathrm{Bias}[\hat{f}(x)]^2 + \mathrm{Var}[\hat{f}(x)] \right] + \sigma^2. \end{align*}In practice, these values are estimated from finite number of training datasets on test dataset of finite size. The test dataset is chosen differently from those in the training datasets, because it is likely to give unrealistic result due to overfitting.
7. Regularization
8. Regression
8.1. Linear Regression
9. Variational Inference
The goal is to approximate the probability distribution \( P(x) \). We postulate the existence of a latent factor \( z \) that affects the probability of \( x \) given as \( P(x\mid z) \). The latent factor also has a probability distribution \( P(z) \), factoring the distribution of \( x \): \[ P(x) = P(x\mid z)P(z). \]
10. Hopfield Network
- A Brain-Inspired Algorithm For Memory - YouTube
- Nodes with symmetric weights between each pair of them. The weight
signifies the correlation of two nodes.
- Model of the Hebbian theory.
- One a network learns the appropriate weight. Then it can be recovered
by sequential improvement.
- updating each note to be the most probable value at each sweep. It is guaranteed that it converges.
- Given that the interference is negligable.
- Multiple information can be stored at the same time by averaging.
- \(n_\text{patterns} \approx 0.14\cdot N_\text{neurons}\)
11. Neural Network
- Machine learning can occur on this.
11.1. Rectifiers
11.1.1. ReLU
- Rectified Linear Unit
\[ \mathrm{ReLU}(x) := \max (0,x). \]
11.1.2. Leaky ReLU
\[ \mathrm{LReLU}(x) := \max(x, \varepsilon x) \] where \( 0 < \varepsilon \ll 1. \)
11.1.3. ELU
- Exponential Linear Unit
\[ \mathrm{ELU}(x) := \begin{cases} x & x >0, \\ \alpha(e^x -1) & x\le 0 \] where \( \alpha \ge 0 \).
11.2. Softmax
The softmax function \( \sigma\colon \mathbb{R}^k \to (0,1)^k \) where \( k\ge 1 \), is defined by: \[ \sigma(z_i) = \frac{e^{z_i}}{\sum_{j=1}^k e^{z_j}}. \]
11.3. Architecture
11.3.1. Multilayer Perceptron
- Researched in 80s and 90s.
- But what is a neural network? | Deep learning chapter 1 - YouTube
- It is useful to have rough concrete meaning of each layer (beforehand), to assess the network.
- Layer \(n\) Neurons
\[a_{j}^{(n)}=\sigma\left( b_{j}^{(n)} + \sum_{i}w_{ij}^{(n)}a_{i}^{(n-1)}\right)\]
- where \(\sigma: \mathbb{R} \to [0,1]\) is usually the sigmoid
function, aka logistic curve: \[
\sigma(x) = \frac{1}{1+e^{-x}}
\]
- It could be any other functions, such as ReLU.
- with matrix notation: \[ \mathbf{a}^{(n)} = \sigma\left(\mathbf{b}^{(n)}+\mathbf{W}^{(n)}\mathbf{a}^{(n-1)}\right) \]
- where \(\sigma: \mathbb{R} \to [0,1]\) is usually the sigmoid
function, aka logistic curve: \[
\sigma(x) = \frac{1}{1+e^{-x}}
\]
- The entire neural network can be thought of as a function, as well as each neuron. \[ \mathbf{a}^{(-1)} = \mathbf{M}\left(\mathbf{\mathbf{a}}^{(0)}\right) = \dots\mathbf{M}^{(2)}\mathbf{M}^{(1)}\mathbf{a}^{(0)} \]
11.3.2. Recurrent Neural Network
- RNN
- Serial processing. Like for audio.
11.3.2.1. Long Short-Term Memory Network
- LSTMN
- Deep layers of small recurrences.
11.3.3. Convolutional Neural Network
- Used for image processing
- Perform multiple 2D convolution at each layer, to detect features.
11.3.4. Autoencoder
- Extract the core features and lift back to the original shape.
11.3.5. Transformer
- Self Attention Layer: Dynamic layer that adapt the weights based on the context. Measures the distance in the concept space.
- It is so named because it transforms the meaning of a sentence in the concept space.
- It is shallower but wider.
- Transformer Neural Networks Derived from Scratch - YouTube
- Calculate the attention of every pair of input vectors with an attention head neural network, and use it as a weight to reduce it down.
11.3.6. Auto-Regression
- Use the output, as the input for the next inference.
11.3.7. Diffusion
When generating an image, the diffusion model performs Langevin sampling, specifically using the Euler-Maruyama method: \[ \mathbf{x}_{t+1} = \mathbf{x}_t + \frac{\epsilon}{2} F(\mathbf{x}_t) + \sqrt{2\epsilon} \mathbf{z}_t, \] in order to sample from the space of all images, where the score function \( F \) is approximated by the trained model \( M_{\theta} \).
The random noise \( \mathbf{z}_t \) serves two main purpose.
- Escaping the local maxima,
- Providing high frequency information.
The model only learns to remove high frequency data, since they have to remove noise. Therefore constant supply of high frequency data is crucial for the generation of realistic image. Put it in other words, the noise alleviates the high correlation of contiguous pixels, generating a more realistic image.
11.4. Learning
11.4.1. Quantification
11.4.1.1. Loss Function
- The wrongness of the model with a single data
- \(\operatorname{square\,loss}(f(x_i|\theta),y_i) = (f(x_i|\theta)-y_i)^2\)
- For the case of vector output:
- \(\operatorname{square\,loss}(f(\mathbf{x}_{i}|\theta),\mathbf{y}_i) = |f(\mathbf{x}_i|\theta)-\mathbf{y}_i|^2\)
- See loss function.
11.4.1.2. Cost Function
- Error Function
- The wrongness of the model with the entire dataset.
- It can dramatically affect the resulting parameters of the model.
- Input: Weights and Biases, Parameters: Dataset, Output: A
Number.
- For a model, Input: Data point, Parameters: Weights&Biases, Output: Estimation.
- e.g. Mean Square Error: \[ MSE(\theta) = \operatorname*{avg}_i\left(\operatorname{square\,loss}(f(x_i|\theta),y_i)\right) \]
11.4.1.3. Objective Function
- A function that is to be optimized for a model.
- Cost function is one of them.
11.4.2. Gradient Descent
- Move in the direction of \(-\nabla C(\theta)\), repeatedly.
- In order to calculate the gradient, the activation must be continuous and differentiable.
11.4.2.1. Stochastic Gradient Descent
- Calculating the cost on the entire dataset every decent step is not practical, so we divide the data into batches to calculate on it each step.
- It is not the optimal decent, but it speeds up the computation.
11.4.3. Backpropagation
- Method of simplifying calculation by calculating layer by layer, starting from the last layer.
- \[ \frac{\partial C_i}{\partial a_{k}^{(L-1)}} = \sum_j \frac{\partial C_i}{\partial a_j^{(L)}}\frac{\partial a_j^{(L)}}{\partial a_k^{(L-1)}} \]
- Note that the gradient is readily calculated at each layer, since you already have all the values that the gradients is evaluated at.
- This is an application of ((65747a84-db71-4b38-aaf9-a357720b2aa9)), which enables multiple partial derivative at the same time.
- e.g. \[ \frac{\partial C_i}{\partial a_{k}^{(L-1)}} = \sum_j \left[2(a_j^{(L)}-y_j)\right]\left[w_{jk}^{(L)}\sigma'\!\!\left( z^{(L)}_j\right)\right] \]
- where \(z^{(L)}_j = b_{j}^{(L)} + \sum_k w_{jk}^{(L)}a_{k}^{(L-1)}\).
11.5. History
- The 35 Year History of LLMs - YouTube
- Ilya - OpenAI
- RNN trained on Amazon reviews.
- Sentiment Neuron was found
- GPT-1, GPT-2
- Transformer trained on various books and the data from the web.
- GPT-3
- Hundred times bigger than the previous version, with a hundred seventy five billion connections and ninety six layers, context window of around a thousand words.
- In-context learning
- 'prompt is the program'
- Through the reinforcement learning, InstructGPT later commercialized as ChatGPT in November, 2022.
- LLM became the kernel system for the other tasks.
- Language processing is related to conciousness?
- GPT-4, Gemini, Bard, Grok on 2023
11.6. See Also
12. Learning
Regression is producing a model for continuous variable, and classification is for discrete variable.
12.1. Gradient Descent/Ascent
12.1.1. Learning Rate
- The coefficient of the gradient.
12.2. Sparse Regression
12.2.1. Loss Function
The goal is to minimize the error of the approximation function and the complexity of the it. It is achieved with the loss function being an linear combination of two function: \[ L = L_{\mathrm{MSE}} + \alpha L_{\mathrm{count}}. \]
The \( L_{\mathrm{count}} \) can be in various form, something similar to \[ L_{\mathrm{count}} = \sum_i|w_i|^p \] when \( p \) is close to zero, this is an approximation of the maximum function. But often \( p=1 \) is used while producing reasonable result, and with simpler calculation using convex optimization.
\( w_{i} \) is the coefficient of the predetermined feature function, that linearly combines to produce the approximation function.
13. Reinforcement Learning
14. Interpretation
14.1. Mechanistic Interpretability
- The embedding of the input tokens are modified as it goes through attention layer and multilayer perceptron. The sequence of modified embeddings is called the residual stream.
- The final row that determines the next token can be unembedded at any point within the residual stream. Often the softmax is used that limits the models confidence in next token. When the unembedded tokens changes drastically in quality, it can be suspected that the layer in which the change occured controls certain quality in the next token.
- A single neuron exhibit polysemanticity, in which a concept is expressed with a combination of neurons and the neural network can learn more concept than they have neurons.
- The final row can be transformed into a sparse concept/feature vector with larger length, using a sparse autoencoder.
- rare features are harder to extract using the autoencoder.
- Cross-layer superposition is being under research.
15. History
- AlphaGo beats Lee Sedol in 2016.
15.1. Moore's Law
AI's Version of Moore's Law? - Computerphile - YouTube The power of the AIs are growing exponentially, in terms of the time for the experts to complete a task that AI gives 80% correct result.
16. Products
16.1. Image Generation
- Imagen - Google
- Flux.1 - Black Forest Labs
- Midjourney
16.2. Language Model
- GPT - OpenAI
- ChatGPT
- Grok - X
- Llama - Meta
- PaLM - Google
17. Softwares
17.1. Python
- TensorFlow
17.2. Rust
- Burn
17.3. Kernel Fusion Backend
Specialized execution layer or compiler module in deep learning framework that merges multiple sequential hardware operations into a single optimized GPU or CPU kernel.
Remove the unnecessary store and load between each operations.
17.4. Inference Engine
17.4.1. vLLM
17.4.2. ONNX Runtime
17.4.3. TensorRT
18. Model Formats
18.1. ONNX
- Open Neutral Network Exchange
18.2. gguf
18.3. ggml
19. Applications
- Nonlinear coordinate transformation using autoencoder
- Linear approximation of the nonlinear dynamics can be found using SINDy
- SINDy uses the loss function:
20. Reference
- Rectifier (neural networks) - Wikipedia
- Softmax function - Wikipedia
- Machine learning - Wikipedia
- Bias–variance tradeoff - Wikipedia
- The Diffusion Model's Unsung Sidekick: A Science of Solving (Almost) Any Prob…
- Why Does Diffusion Work Better than Auto-Regression? - YouTube
- Stochastic gradient descent - Wikipedia
- Learning rate - Wikipedia
- How AI Learns Concepts - YouTube
- How Computers Derive Equations - YouTube
- The Dark Matter of AI {Mechanistic Interpretability} - YouTube
- Deep Learning to Discover Coordinates for Dynamics: Autoencoders & Physics In…