KHEIREDDINE RIAHI

Deep Learning: ANN and LLM

1. Neural network models

Overview of Deep Learning, from ANN neural networks and Transformers to LLMs, with architectures, functions, use cases and metrics.
Overview of neural network architectures, Transformers and LLM.

1.1 CNN

Convolutional network for images: convolution, activation, pooling, classification.

Y(i,j) = ΣₘΣₙ X(i−m,j−n)K(m,n)

MaxPooling: Y(i,j) = max₍ₘ,ₙ₎∈Rᵢⱼ

1.2 RNN

Recurrent network for sequences and time series. Limits: vanishing and exploding gradients.

hₜ = f(Wₓxₜ + Wₕhₜ₋₁ + b); yₜ = g(Wᵧhₜ+bᵧ)

1.3 LSTM

Internal memory with Forget Gate, Input Gate and Output Gate.

fₜ = σ(Wf[hₜ₋₁,xₜ]+bf)

iₜ = σ(Wi[hₜ₋₁,xₜ]+bi)

C̃ₜ = tanh(Wc[hₜ₋₁,xₜ]+bc)

Cₜ = fₜ ⊙ Cₜ₋₁ + iₜ ⊙ C̃ₜ

oₜ = σ(Wo[hₜ₋₁,xₜ]+bo); hₜ = oₜ ⊙ tanh(Cₜ)

1.4 GRU

Recurrent network with two gates: Update and Reset; generally simpler than LSTM.

1.5 Autoencoder

Encode → latent representation → Decoder. Used for dimension reduction, compression and anomaly detection.

z = fθ(x); x̂ = gφ(z); L = ||x−x̂||²

1.6 GAN

Two adversarial networks: Generator and Discriminator.

minG maxD V(D,G) = Eₓ[log D(x)] + E𝓏[log(1−D(G(z)))].

2. Transformers

Attention-based architecture, presented in 2017. Embeddings, positional information, attention, FFN.

2.1 Embeddings

Embedding(token) = [e₁,e₂,…,e𝑑]

2.2 Positional Encoding

PE(pos,2i) = sin(pos/10000^(2i/d))

PE(pos,2i+1) = cos(pos/10000^(2i/d))

Other models use learned or relative positional encodings (RoPE).

2.3 Self-Attention

Q=XWQ ; K=XWK; V=XWV

Attention(Q,K,V) = Softmax(QKᵀ/√dₖ)V

Q: Query; K:Key; V: Value. Scores → normalization → Softmax → weighted sum of Values.

2.4 Multi-Head Caution

MultiHead(Q,K,V) = Concat(head₁,…,headₕ)WO

headᵢ = Attention(QWᵢQ, KWᵢK, VWᵢV)

2.5 Feed Forward Network

FFN(x) = W₂σ(W₁x+b₁)+b₂

2.6 Encode and Decode

Architecture

Role

Examples

Encoder-only

Represent and understand

BERT

Decoder-only

Generate

GPT, Llama

Encoder-Decoder

Transform sequences

T5

3. Large Language Models (LLM)

Processing chain: Tokenization → Embedding → Position → Transform → Logits → Softmax → Generation.

3.1 Next Token Prediction

P(tokenᵢ) = eᶻⁱ / Σⱼeᶻʲ

L = −Σᵢ log P(xᵢ | x₁,…,xᵢ₋₁)

3.2 Temperature

Pᵢ = Softmax(zᵢ/T)

Low temperature: more concentrated predictions; high temperature: more diversified distribution.

3.3 Fine-Tuning and LoRA

Full Fine-Tuning modifies all weights. LoRA learns low-rank correction. QLoRA combines LoRA and quantification.

W′ = W + ΔW; ΔW = BA

3.4 RAG

Documents → chunks → embeddings → vector base → question → search → context → LLM → answer.

CosineSimilarity(A,B) = (A·B)/(||A|| ||B||)

RAG

Fine-Tuning

Retrieves external information

Adapt the settings

Updatable documents

Requires training

Potentially cited sources

Less traceable knowledge

4. GNN and GraphRAG

Graph Neural Networks learn on graphs (nodes, edges, attributes); GraphRAG combines search and knowledge graph.

hᵥ⁽ˡ⁺¹⁾ = σ(W⁽ˡ⁾ AGG{hᵤ⁽ˡ⁾ : u∈N(v)})

Architectures: GCN, GAT, GraphSAGE. GraphRAG example: Customer repository → Customer API → CRM → Billing → Customer portal.

5. Reinforcement Learning

An agent interacts with an environment: state, action, reward and policy.

5.1 Q-Learning

Q(s,a) ← Q(s,a) + α[r + γ maxₐ′ Q(s′,a′) − Q(s,a)]

Other algorithms: DQN, Policy Gradient, Actor-Critic, PPO.

6. Summary table of mathematical functions

Function

Algorithms

Role

ax+b

Regression

Linear relationship

Sigmoid

Logistics, ANN

Binary probability

Softmax

ANN, LLM

Multiclass distribution

MSE / MAE

Regression

Error

Cross-Entropy

Classification, LLM

Probabilistic error

Gradient Descent

ML, DL

Optimization

ReLU / GELU

ANN, Transformers

Nonlinearity

Gini / Entropy

Trees

Impurity

Euclidean distance

KNN, K-Means, DBSCAN

Proximity

Cosine Similarity

RAG

Similarity

Covariance / eigenvalues

PCA

Main axes

Convolution

CNN

Pattern extraction

Chain Rule

ANN

Backpropagation

Attention

Transformers

Token weighting

Bayes

Naive Bayes

Probabilities

Q-Learning

R.L.

Decisions

7. Which algorithm to use?

Need

Algorithms

Predict a price

Linear Regression

Churn

Logistic Regression, XGBoost

Fraud

Random Forest, XGBoost, GNN

Segmentation

K-Means, DBSCAN

Anomalies

DBSCAN, Forest Isolation, Autoencoder

Pictures

CNN, Vision Transformers

Time series

XGBoost, LSTM, Transformers

Text

Naive Bayes, BERT

Generation

LLM

Documents

RAG + LLM

Relationships

GNN, GraphRAG

Decisions

Reinforcement Learning

8. Stages of a Machine Learning project

Stage

Description

1

Business Understanding

2

Data Collection

3

Data Preparation

4

Feature Engineering

5

Model Selection

6

Training

7

Assessment

8

deployment

9

Monitoring

8.1 Evaluation Metrics

Accuracy = (TP+TN)/(TP+TN+FP+FN)

Precision = TP/(TP+FP)

Recall = TP/(TP+FN)

F1 = 2×Precision×Recall/(Precision+Recall)

Classification: Accuracy, Precision, Recall, F1, AUC-ROC. Regression: MAE, MSE, RMSE, R². Clustering: Silhouette, Davies-Bouldin, Calinski-Harabasz.