Python Libraries for Explainable AI (XAI)

Python Libraries for Explainable AI (XAI)

Introduction

Explainable AI (XAI) tools help us interpret and understand machine learning and deep learning models. Python provides several open-source libraries for this purpose. Each library offers unique techniques for visualizing, explaining, and validating model decisions.

Moroccan Darija: كاينين بزاف ديال المكتبات فـ Python اللي كيساعدونا نفهمو كيفاش الموديل كيتصرف. كل مكتبة عندها طريقة خاصة باش تشرح وتفسر القرارات ديال الموديل.

Main Python Libraries for Explainable AI

1. LIME (Local Interpretable Model-agnostic Explanations)

LIME explains individual predictions by building a simple linear model around a specific instance. It works with any black-box model.

pip install lime
from lime import lime_tabular
explainer = lime_tabular.LimeTabularExplainer(training_data, feature_names=features)
explanation = explainer.explain_instance(sample, model.predict_proba)
explanation.show_in_notebook()

Darija: LIME كتفسر كل توقع بوحدو، وكاتخدم مع أي نوع ديال الموديل.

2. SHAP (SHapley Additive exPlanations)

SHAP uses game theory to assign importance values (Shapley values) to features. It supports deep learning, tree models, and tabular data.

pip install shap
import shap
explainer = shap.Explainer(model)
shap_values = explainer(X)
shap.summary_plot(shap_values, X)

Darija: SHAP كيعطي لكل ميزة رقم كيبين شحال ساهمات فالناتيج.

3. ELI5 (Explain Like I’m Five)

ELI5 helps visualize feature importance and weights in linear and tree-based models. It also integrates with scikit-learn.

pip install eli5
import eli5
eli5.show_weights(model, feature_names=feature_names)

Darija: ELI5 مكتبة بسيطة كتعرض شنو الميزات المهمة فالموديل بطريقة مفهومة.

4. InterpretML

Developed by Microsoft, InterpretML combines global and local explanation methods. It supports classical ML and deep learning models.

pip install interpret
from interpret.glassbox import ExplainableBoostingClassifier
model = ExplainableBoostingClassifier()
model.fit(X_train, y_train)
ebm_local = model.explain_local(X_test, y_test)
ebm_local.show()

Darija: InterpretML مكتبة من Microsoft كتسمح باش تشرح الموديلات الكلاسيكية والعميقة.

5. Captum

Captum is a PyTorch library for model interpretability. It provides methods like Integrated Gradients, DeepLIFT, and Saliency Maps for neural networks.

pip install captum
from captum.attr import IntegratedGradients
ig = IntegratedGradients(model)
attributions = ig.attribute(inputs, target=0)

Darija: Captum مكتبة ديال PyTorch كتعطي تفسيرات للموديلات العميقة بحال CNN وRNN.

6. Alibi

Alibi supports tabular, image, and text models. It provides counterfactual explanations, anchors, and saliency maps.

pip install alibi
from alibi.explainers import AnchorTabular
explainer = AnchorTabular(predict_fn=model.predict, feature_names=feature_names)
explainer.fit(X_train)
explanation = explainer.explain(X_test[0])

Darija: Alibi كتخدم مع النصوص، الصور، والجداول، وكتعطي شروحات بحال counterfactuals.

7. DALEX

DALEX (Descriptive mAchine Learning EXplanations) is used for model comparison and feature importance analysis. It supports Python and R.

pip install dalex
import dalex as dx
exp = dx.Explainer(model, X, y)
exp.model_parts().plot()

Darija: DALEX كتعاون باش نقارنو بين الموديلات ونشوفو الميزات اللي مؤثرة بزاف.

8. Skater

Skater provides model-agnostic interpretability for black-box models, supporting visualization and local explanations.

pip install skater
from skater.core.explanations import Interpretation
interpreter = Interpretation(X, feature_names=features)
interpreter.feature_importance(model.predict)

Darija: Skater كتخدم مع بزاف ديال أنواع الموديلات وكتعطي تفسيرات محلية وبصرية.

9. TensorFlow Explain

TensorFlow Explain integrates directly with Keras models to provide Grad-CAM, Occlusion Sensitivity, and SmoothGrad visualizations.

pip install tf-explain
from tf_explain.core.grad_cam import GradCAM
explainer = GradCAM()
grid = explainer.explain(validation_data, model, class_index=0)
explainer.save(grid, ".", "gradcam_result.png")

Darija: TensorFlow Explain كتعاون باش نفهمو الموديلات ديال الصور باستعمال Grad-CAM وطرق أخرى.

Comparison Table

Library Main Use Supports Deep Learning
LIMELocal explanationsYes
SHAPFeature importance (global/local)Yes
ELI5Linear/Tree modelsNo
InterpretMLHybrid explanationsYes
CaptumNeural network interpretationYes
AlibiCounterfactual and anchorsYes
DALEXModel comparisonYes
SkaterBlack-box interpretationNo
TensorFlow ExplainVisual CNN explanationsYes

10 Exercises for Practice

  1. Install and test the LIME library with a small scikit-learn model.
  2. Use SHAP to explain predictions of a deep learning model.
  3. Visualize feature importance using ELI5.
  4. Train a simple classifier and explain it using DALEX.
  5. Use Captum with a PyTorch CNN and interpret a specific layer.
  6. Compare explanations from LIME and SHAP for the same dataset.
  7. Generate counterfactual examples using Alibi.
  8. Visualize Grad-CAM on a Keras CNN model using TensorFlow Explain.
  9. Explain a regression model using InterpretML.
  10. Build a dashboard comparing XAI results from multiple libraries.

Internal Linking Suggestions

[internal link: Explainable AI in Deep Learning]

[internal link: Deep Learning Basics]

[internal link: AI Ethics and Transparency]

Conclusion

Python offers a rich ecosystem for Explainable AI. Libraries like LIME, SHAP, and Captum help visualize and understand complex model behavior. Choosing the right tool depends on your model type and data domain.

Darija Summary: فـ Python كاينين بزاف ديال المكتبات اللي كيساعدونا نفسرو الموديلات. بحال LIME وSHAP وCaptum. كل وحدة عندها استعمال خاص حسب نوع البيانات والموديل.

Share:

How Transformers Work in Detail

How Transformers Work in Detail

Introduction

Transformers are deep learning models that changed the way machines understand text, images, and even sound. They power systems like ChatGPT, BERT, and DALL·E. In this tutorial, we explain how transformers work step by step using clear logic and simple math.

Moroccan Darija: الموديلات ديال Transformers بدلو الطريقة اللي الماكينات كاتفهم بيها النصوص، الصور وحتى الصوت. فهاد الدرس غادي نشرحو كيفاش كيخدمو بالتفصيل وبطريقة بسيطة.

Core Concepts Explained

Transformers are based on a mechanism called Self-Attention. This allows the model to understand the relationship between all words in a sentence at once.

Example sentence: “The cat sat on the mat.” The model looks at how each word relates to the others. For example, “cat” relates more to “sat” and “mat” than to “the”.

Main Components:

  • Input Embeddings: Convert words into numerical vectors.
  • Positional Encoding: Add order information to embeddings.
  • Self-Attention Layers: Compute relationships between words.
  • Feed Forward Networks: Process the attention outputs.
  • Layer Normalization: Stabilize training.
  • Residual Connections: Help keep information flowing.

Moroccan Darija: المكونات الرئيسية هما:

  • Embeddings كتحول الكلمات لأرقام.
  • Positional Encoding كيعطي ترتيب للكلمات.
  • Self-Attention كيشوف العلاقة بين كل كلمة والتانية.
  • Feed Forward كيعالج النتائج.
  • Normalization كيساعد فالتدريب.
  • Residual Connections كتحافظ على المعلومة.

Syntax and Model Structure

A Transformer model is made of two parts: an Encoder and a Decoder.

  • Encoder: Reads the input sentence and creates hidden representations.
  • Decoder: Generates output based on encoder outputs and previous words.
# Simple transformer-like structure in PyTorch
import torch
from torch import nn

class SimpleTransformer(nn.Module):
    def __init__(self, vocab_size, embed_dim, num_heads, hidden_dim):
        super().__init__()
        self.embedding = nn.Embedding(vocab_size, embed_dim)
        self.attention = nn.MultiheadAttention(embed_dim, num_heads)
        self.feed_forward = nn.Sequential(
            nn.Linear(embed_dim, hidden_dim),
            nn.ReLU(),
            nn.Linear(hidden_dim, embed_dim)
        )
        self.norm = nn.LayerNorm(embed_dim)
    
    def forward(self, x):
        x = self.embedding(x)
        attn_output, _ = self.attention(x, x, x)
        x = self.norm(x + attn_output)
        x = self.feed_forward(x)
        return x

Practical Examples

Example 1: Understanding Word Relationships

from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
model = AutoModel.from_pretrained("bert-base-uncased")

inputs = tokenizer("The cat sat on the mat", return_tensors="pt")
outputs = model(**inputs)
print(outputs.last_hidden_state.shape)

This shows how BERT encodes each token into a vector that contains contextual information.

Example 2: Machine Translation

from transformers import MarianMTModel, MarianTokenizer

model_name = "Helsinki-NLP/opus-mt-en-fr"
tokenizer = MarianTokenizer.from_pretrained(model_name)
model = MarianMTModel.from_pretrained(model_name)

text = "I love learning AI"
inputs = tokenizer(text, return_tensors="pt")
translated = model.generate(**inputs)
print(tokenizer.decode(translated[0], skip_special_tokens=True))

Darija Explanation: هاد المثال كيبين كيفاش Transformer كيدير الترجمة الآلية، بحال من الإنجليزية للفرنسية.

Example 3: Text Generation

from transformers import GPT2LMHeadModel, GPT2Tokenizer

tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
model = GPT2LMHeadModel.from_pretrained("gpt2")

input_ids = tokenizer.encode("Artificial Intelligence is", return_tensors="pt")
output = model.generate(input_ids, max_length=20)
print(tokenizer.decode(output[0], skip_special_tokens=True))

This example shows how GPT models use the decoder part to generate coherent sentences.

Explanation of Each Example

  • Example 1: Shows attention in action — every word depends on context.
  • Example 2: Uses encoder-decoder structure for translation.
  • Example 3: Uses decoder-only architecture to predict next words.

10 Exercises for Practice

  1. Explain what self-attention means in your own words.
  2. Write a Python function to normalize attention scores using softmax.
  3. Modify the SimpleTransformer class to add dropout.
  4. Compare encoder-only (BERT) and decoder-only (GPT) architectures.
  5. Try using a different tokenizer and see how tokenization changes.
  6. Visualize attention scores for a simple sentence using any library.
  7. Train a small transformer on a custom text dataset.
  8. Explain why positional encoding is needed.
  9. Implement a small feed-forward block in PyTorch.
  10. Experiment with different numbers of heads in MultiheadAttention.

Internal Linking Suggestions

[internal link: Machine Learning Basics]

[internal link: Neural Networks Guide]

[internal link: Attention Mechanism Explained]

Conclusion

Transformers changed AI by allowing parallel training and better context understanding. They are now the foundation for models in NLP, vision, and multimodal AI.

Darija Summary: Transformers دارو ثورة فالعالم ديال الذكاء الاصطناعي، وخلاو الموديلات تفهم النصوص والصور بطريقة قوية وسريعة.

Share:

Automated Machine Learning (AutoML)

Automated Machine Learning (AutoML)

Introduction

Automated Machine Learning (AutoML) is the process of automating the steps involved in building a machine learning model. It helps users automatically preprocess data, select algorithms, tune hyperparameters, and evaluate models without deep manual intervention.

بالعربية المغربية (الدارجة): التعلم الآلي الآلي (AutoML) هو طريقة كتمكن النظام يدير مراحل بناء النموذج بوحدو، بحال تنظيف البيانات، اختيار الخوارزميات، وضبط الإعدادات، بلا تدخل كبير من الإنسان.

Why AutoML is Important

  • Saves time and effort for data scientists and engineers.
  • Finds the best model automatically.
  • Improves model performance through optimized hyperparameters.
  • Makes machine learning accessible to non-experts.

بالعربية المغربية: AutoML كيوفر الوقت والمجهود، كيعطي نتائج قوية، وكيسهّل استعمال الذكاء الاصطناعي حتى للناس اللي ما عندهمش خبرة كبيرة.

Core Concepts Explained

  • Data Preprocessing: Cleaning and transforming raw data automatically.
  • Feature Engineering: Creating new features or selecting the most useful ones.
  • Model Selection: Choosing the best algorithm (e.g., Random Forest, XGBoost, etc.).
  • Hyperparameter Optimization: Automatically tuning parameters for better performance.
  • Ensembling: Combining multiple models to achieve higher accuracy.

Popular AutoML Frameworks

FrameworkMain Features
Auto-sklearnOpen-source library for automatic model selection and tuning
TPOTUses genetic programming to find the best pipelines
H2O AutoMLScalable, fast AutoML tool supporting multiple algorithms
Google Cloud AutoMLCloud-based AutoML for vision, text, and tabular data
PyCaretLow-code AutoML library for Python

بالعربية المغربية: كاينين بزاف ديال الأدوات بحال Auto-sklearn، TPOT، H2O، وPyCaret اللي كيساعدونا نخدمو AutoML بسهولة وبطرق مختلفة.

Python Example: AutoML with PyCaret


# Install PyCaret
# pip install pycaret

from pycaret.datasets import get_data
from pycaret.classification import *

# Load dataset
data = get_data('iris')

# Initialize AutoML environment
s = setup(data=data, target='species', session_id=123)

# Compare all models automatically
best_model = compare_models()

# Print best model
print(best_model)

Explanation of the Example

  • The PyCaret library is used for AutoML in Python.
  • The setup() function initializes preprocessing and data handling automatically.
  • compare_models() tests multiple algorithms and returns the best-performing one.

بالعربية المغربية: PyCaret كيدير كلشي بوحدو، من تنظيف البيانات حتى مقارنة النماذج. فالنهاية كيعطينا أحسن نموذج يخدم فهاد الحالة.

Example: AutoML with Auto-sklearn


# Install Auto-sklearn
# pip install auto-sklearn

import autosklearn.classification
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
from sklearn.metrics import accuracy_score

# Load data
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Run AutoML
automl = autosklearn.classification.AutoSklearnClassifier(time_left_for_this_task=60)
automl.fit(X_train, y_train)

# Evaluate
y_pred = automl.predict(X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))

Explanation of the Auto-sklearn Example

  • The system automatically tests several models and hyperparameters.
  • It returns the best-performing combination based on evaluation metrics.
  • The result shows model accuracy on test data.

بالعربية المغربية: Auto-sklearn كيجرب بزاف ديال النماذج والإعدادات، ومن بعد كيعطينا أفضل نتيجة ممكنة على البيانات.

Benefits of AutoML

  • Reduces manual experimentation and trial-and-error.
  • Enables faster deployment of models.
  • Improves productivity in AI workflows.
  • Ensures consistent evaluation across models.

Limitations of AutoML

  • Less control over algorithm internals.
  • Requires computational resources.
  • May overfit if not configured properly.
  • Not always interpretable for complex models.

بالعربية المغربية: رغم الفوائد ديالو، AutoML كيعاني من بعض العيوب بحال قلة التحكم، الاستهلاك الكبير ديال الحواسيب، وصعوبة الفهم فبعض المرات.

Best Practices

  • Use AutoML for baseline experiments before fine-tuning manually.
  • Always interpret model results with explainability tools (e.g., SHAP, LIME).
  • Limit computation time using time budgets.
  • Validate AutoML results with independent test data.

10 Exercises for Practice

  1. Define AutoML and its main purpose.
  2. List three frameworks used for automated machine learning.
  3. Install and test PyCaret with the Iris dataset.
  4. Use Auto-sklearn to build a model and report its accuracy.
  5. Change the time limit in Auto-sklearn and observe performance differences.
  6. Use PyCaret to compare models for a regression task.
  7. Explain the role of feature engineering in AutoML.
  8. Identify the limitations of AutoML in real-world use cases.
  9. Apply SHAP to explain predictions from an AutoML model.
  10. Discuss how AutoML helps accelerate MLOps pipelines.
Share:

Bias and Fairness in Artificial Intelligence

Bias and Fairness in AI

Introduction

Bias and fairness are critical topics in artificial intelligence and machine learning. A biased AI model can lead to unfair, discriminatory, or unethical outcomes. Fairness ensures that AI systems make decisions that are consistent, transparent, and equal for all individuals or groups.

بالعربية المغربية (الدارجة): الانحياز والإنصاف فـالذكاء الاصطناعي مواضيع مهمة بزاف. النموذج المنحاز يقدر يعطي قرارات ظالمة أو تمييزية. الإنصاف كيعني النموذج يخدم بعدالة ويعامل الناس كاملين بنفس الطريقة.

What is Bias in AI?

Bias happens when an AI system systematically favors or disadvantages certain groups. It can come from data, algorithms, or human choices during model design.

  • Data Bias: When the dataset does not represent all groups fairly.
  • Label Bias: When labels reflect human prejudice or errors.
  • Algorithmic Bias: When the model amplifies existing unfair patterns.

بالعربية المغربية: الانحياز كيتوقع ملي البيانات ما كتمثلش الجميع، ولا ملي التصنيفات فيها أخطاء بشرية، ولا ملي الخوارزمية كتزيد فالتمييز اللي كان من قبل.

Fairness in Machine Learning

Fairness means that predictions are not dependent on sensitive attributes like gender, race, or age. There are different definitions of fairness, including:

  • Demographic Parity: Each group gets positive predictions at similar rates.
  • Equal Opportunity: True positive rates are equal across groups.
  • Equalized Odds: Both true positive and false positive rates are equal across groups.

بالعربية المغربية: الإنصاف كيعني القرار ما يكونش مرتبط بالعمر ولا بالجنس ولا بالأصل. كاين بزاف ديال طرق باش نقيسو الإنصاف، بحال المساواة فالنسب أو ففرص النجاح.

Python Example: Detecting Bias in a Model


import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score

# Sample dataset
data = pd.DataFrame({
    "age": [22, 45, 35, 26, 50, 29],
    "gender": ["male", "female", "female", "male", "female", "male"],
    "income": [30000, 60000, 55000, 35000, 62000, 40000],
    "approved": [0, 1, 1, 0, 1, 0]
})

# Encode gender as numeric
data["gender_num"] = data["gender"].map({"male": 0, "female": 1})

X = data[["age", "gender_num", "income"]]
y = data["approved"]

model = LogisticRegression()
model.fit(X, y)
predictions = model.predict(X)

data["predicted"] = predictions
accuracy = accuracy_score(y, predictions)

print("Model Accuracy:", accuracy)
print("\nPredictions by gender:")
print(data.groupby("gender")[["predicted"]].mean())

Explanation of the Example

  • The dataset includes features like age, gender, and income.
  • The model predicts loan approval.
  • By comparing average predictions by gender, we can check if the model favors one group.

بالعربية المغربية: هاد الكود كيدرب نموذج بسيط باش يتوقع القبول ديال القرض. من بعد كنشوفو واش النتيجة كتميل لجنس معين، باش نعرفو واش النموذج فيه انحياز.

Mitigating Bias in AI Models

  • Preprocessing: Balance datasets before training (e.g., re-sampling, re-weighting).
  • In-processing: Modify learning algorithms to reduce bias (e.g., fairness constraints).
  • Post-processing: Adjust predictions after training to improve fairness.

بالعربية المغربية: باش نصلحو الانحياز نقدر نعدلو البيانات قبل التدريب، ولا نبدلو الطريقة اللي كيتعلم بها النموذج، أو نصححو النتائج من بعد التدريب.

Example: Balancing Data Before Training


from sklearn.utils import resample

# Suppose 'female' group is smaller
female_data = data[data.gender == "female"]
male_data = data[data.gender == "male"]

# Resample females to balance
female_upsampled = resample(female_data, replace=True, n_samples=len(male_data), random_state=42)
balanced_data = pd.concat([male_data, female_upsampled])

print("Data balanced successfully:")
print(balanced_data.gender.value_counts())

Best Practices for Fair AI

  • Use diverse and representative datasets.
  • Continuously monitor models after deployment.
  • Collaborate with ethicists and domain experts.
  • Report model fairness metrics in documentation.
  • Test for bias before releasing an AI product.

بالعربية المغربية: باش نضمنو الإنصاف، خاص البيانات تكون متنوعة، ونراقبو النموذج حتى بعد الإطلاق، ونتأكدو دايمًا من التوازن فالنتائج.

Tools for Bias and Fairness Analysis

ToolPurpose
AI Fairness 360 (IBM)Detect and mitigate bias in datasets and models.
Fairlearn (Microsoft)Measure and reduce unfairness in machine learning.
What-If Tool (Google)Visualize model predictions and fairness metrics.
SHAP / LIMEExplain model decisions and feature influence.

بالعربية المغربية: كاينين بزاف ديال الأدوات اللي كتعاون فالكشف والتصحيح ديال الانحياز، بحال AI Fairness 360 وFairlearn وWhat-If Tool.

10 Exercises for Practice

  1. Define bias and fairness in AI using your own words.
  2. List three types of bias and provide an example for each.
  3. Use the sample Python code to check if your model is biased by gender.
  4. Add a new sensitive attribute (like age) and measure fairness again.
  5. Balance your dataset using re-sampling and compare results.
  6. Install and use AI Fairness 360 to detect bias in your model.
  7. Explain the difference between preprocessing and post-processing techniques.
  8. Apply SHAP or LIME to explain model decisions on sensitive features.
  9. Discuss how bias affects AI systems in healthcare or finance.
  10. Write a short report suggesting how to design fair AI systems.
Share:

Experiment Tracking with MLflow

Experiment Tracking with MLflow

Introduction

Experiment tracking is the process of recording and managing all information about machine learning experiments, such as model parameters, metrics, and results. MLflow is a powerful open-source platform that simplifies this process by providing easy tracking, reproducibility, and deployment.

بالعربية المغربية (الدارجة): تتبع التجارب فالتعلم الآلي هو عملية تسجيل كل المعلومات ديال التجارب بحال المعاملات، النتائج، والمقاييس. MLflow هو منصة مفتوحة كيسهّل هاد الخدمة وكيخلي التجارب منظمة وسهلة الإعادة.

Why Experiment Tracking Matters

  • Ensures reproducibility of experiments.
  • Helps compare different models easily.
  • Keeps track of hyperparameters and results.
  • Facilitates collaboration in data teams.
  • Supports model versioning and deployment.

بالعربية المغربية: التتبع ديال التجارب كيساعد باش نرجعو لأي تجربة قديمة، نقارنو النماذج، ونعرفو شنو أحسن إعدادات بدون ما نضيعو الوقت.

Core Concepts of MLflow

  • Run: A single experiment execution where metrics and parameters are logged.
  • Experiment: A collection of related runs under one project.
  • Artifact: Files like models, plots, or logs stored for each run.
  • Tracking Server: A centralized service to manage experiments and results.

Setting Up MLflow


# Install MLflow
pip install mlflow

# Start MLflow tracking server locally
mlflow ui

This command opens the MLflow user interface at http://localhost:5000 where you can view all your experiments.

بالعربية المغربية: من بعد التثبيت، نقدر نفتحو واجهة MLflow على الرابط http://localhost:5000 باش نشوفو كل التجارب ديالنا بشكل منظم.

Python Example: Logging Experiments with MLflow


import mlflow
import mlflow.sklearn
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

# Load data
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Define experiment
mlflow.set_experiment("iris-rf-experiment")

# Start run
with mlflow.start_run():
    n_estimators = 100
    max_depth = 5
    model = RandomForestClassifier(n_estimators=n_estimators, max_depth=max_depth, random_state=42)
    model.fit(X_train, y_train)
    accuracy = model.score(X_test, y_test)
    
    # Log parameters and metrics
    mlflow.log_param("n_estimators", n_estimators)
    mlflow.log_param("max_depth", max_depth)
    mlflow.log_metric("accuracy", accuracy)
    
    # Save model
    mlflow.sklearn.log_model(model, "model")

print("Experiment logged successfully.")

Explanation of the Example

  • The experiment is named iris-rf-experiment.
  • Model parameters and accuracy are recorded using mlflow.log_param() and mlflow.log_metric().
  • The model is saved automatically as an artifact.
  • All results can be viewed on the MLflow UI.

بالعربية المغربية: هاد الكود كيدير تجربة جديدة بالإسم iris-rf-experiment، كيسجّل المعاملات بحال عدد الأشجار والعمق، وكيحسب الدقة، ومن بعد كيسجّل النموذج فالواجهة ديال MLflow.

Comparing Multiple Experiments

MLflow lets you compare different runs visually. You can see parameter values, metrics, and charts side by side to pick the best model.

بالعربية المغربية: فـMLflow نقدر نقارنو بين التجارب باش نعرفو شنو النموذج اللي خدم مزيان، وكل تجربة كتكون فيها النتائج والمقاييس بشكل واضح.

Advanced MLflow Features

  • MLflow Projects: Package code and environment for reproducibility.
  • MLflow Models: Manage model deployment formats.
  • MLflow Registry: Store and version trained models.
  • MLflow UI: Visualize all experiments in one dashboard.

Best Practices for Experiment Tracking

  • Always log both parameters and metrics.
  • Tag each experiment with dataset and model version.
  • Use consistent experiment naming conventions.
  • Regularly clean unused runs to save storage.
  • Automate experiment logging in your training pipeline.

Example: Registering Models in MLflow


# After logging a model, you can register it
result = mlflow.register_model(
    "runs:/<run_id>/model",
    "IrisModelRegistry"
)

print("Model registered with version:", result.version)

بالعربية المغربية: من بعد التجربة نقدر نسجلو النموذج فالـModel Registry باش نستعملوه فالتطبيق أو التحديث القادم.

10 Exercises for Practice

  1. Define what experiment tracking is and why it’s important.
  2. Install MLflow and start the local tracking server.
  3. Run the provided example and view results in the MLflow UI.
  4. Add more hyperparameters (like min_samples_split) to your logged model.
  5. Log confusion matrix as an artifact in MLflow.
  6. Compare two models using different hyperparameters.
  7. Export experiment results as a CSV file from the MLflow UI.
  8. Register your best model using MLflow Model Registry.
  9. Integrate MLflow tracking with your existing ML pipeline.
  10. Explain the difference between MLflow Tracking, Projects, and Models.
Share:

Explainable AI (XAI)

Explainable AI (XAI) Tutorial

Introduction

Explainable AI (XAI) refers to techniques and methods that help humans understand and trust the decisions made by artificial intelligence systems. It focuses on making models transparent, interpretable, and accountable. As AI systems become more complex, XAI helps ensure fairness, safety, and compliance.

بالعربية المغربية (الدارجة): الذكاء الاصطناعي القابل للتفسير (XAI) هو مجموعة ديال الطرق اللي كتخلي الناس يفهمو كيفاش النموذج كيدير القرارات ديالو. الهدف هو الشفافية والعدالة باش نقدر نثقو فالنظام الذكي.

Why Explainable AI is Important

  • Builds trust between humans and AI systems.
  • Helps detect bias and unfair decisions.
  • Improves debugging and model performance.
  • Supports legal and ethical compliance.
  • Enhances decision-making in sensitive domains (healthcare, finance).

بالعربية المغربية: XAI مهم حيت كيعطينا الثقة فالنموذج، كيساعدنا نكتاشفو الأخطاء، وكيفيد فالمجالات الحساسة بحال الطب والبنوك.

Core Concepts Explained

  • Interpretability: How easily a human can understand the model’s logic.
  • Transparency: How clearly the inner workings of the model are visible.
  • Post-hoc Explanation: Explaining decisions after the model has made predictions.
  • Feature Importance: Identifying which input variables most affect predictions.

Types of Explainability

TypeDescriptionExample Methods
GlobalExplains the entire model behaviorFeature Importance, Partial Dependence Plots
LocalExplains a single predictionLIME, SHAP

بالعربية المغربية: كاين تفسير شامل (Global) اللي كيبين كيفاش النموذج كيتصرف فالمجمل، وتفسير محلي (Local) اللي كيركّز على تفسير قرار واحد فقط.

Python Example: Explainable AI with LIME


# Install LIME if not installed
# pip install lime scikit-learn

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from lime.lime_tabular import LimeTabularExplainer

# Load dataset
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train model
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)

# Create LIME explainer
explainer = LimeTabularExplainer(X_train, feature_names=['sepal_length','sepal_width','petal_length','petal_width'], class_names=['setosa','versicolor','virginica'], discretize_continuous=True)

# Explain one prediction
i = 3
exp = explainer.explain_instance(X_test[i], model.predict_proba)
exp.show_in_notebook(show_table=True)

Explanation of the Example

  • The Random Forest model predicts flower species.
  • LIME explains why the model made a specific prediction.
  • It shows which features contributed most to that decision.

بالعربية المغربية: هاد المثال كيدير تدريب على بيانات الزهور. من بعد LIME كيعطينا تفسير مفصل لقرار النموذج، وكيورينا شنو هما الميزات اللي كانو مؤثرين بزاف فالتصنيف.

Another Example: Using SHAP for Feature Importance


# Install SHAP if not installed
# pip install shap

import shap

# Create SHAP explainer
explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X_test)

# Plot feature importance
shap.summary_plot(shap_values, X_test, feature_names=['sepal_length','sepal_width','petal_length','petal_width'])

Explanation of the SHAP Example

  • SHAP uses game theory to explain model predictions.
  • It shows how each feature pushes the prediction higher or lower.
  • The summary plot visualizes global feature importance.

بالعربية المغربية: SHAP كيعتمد على نظرية الألعاب باش يشرح القرارات. كيعطينا فكرة على شنو المميزات اللي رفعو القيمة ولا نقصوها، وكيعطي رسم بياني واضح للميزات المهمة.

Best Practices for Explainable AI

  • Choose interpretable models for critical systems (e.g., linear models, decision trees).
  • Use post-hoc explainers like LIME or SHAP for complex models.
  • Communicate explanations in simple terms to end-users.
  • Validate explanations with domain experts.
  • Regularly test for bias and fairness.

10 Exercises for Practice

  1. Define Explainable AI and its importance in real-world systems.
  2. Differentiate between interpretability and transparency.
  3. Implement a simple interpretable model (e.g., Decision Tree) and visualize feature importance.
  4. Install and use LIME to explain one prediction.
  5. Install and use SHAP to analyze feature contributions globally.
  6. Compare explanations from LIME and SHAP for the same model.
  7. Create a visualization that shows bias in model predictions.
  8. Explain one AI decision to a non-technical user in simple language.
  9. Evaluate how explainability affects model trust and fairness.
  10. Document your explainability process for model governance.
Share:

Model Versioning in Machine Learning

Model Versioning in Machine Learning

Introduction

Model versioning is the process of tracking and managing different versions of machine learning models over time. It helps data scientists and engineers keep control of changes, reproduce results, and deploy the right model confidently.

بالعربية المغربية (الدارجة): تتبع نسخ النماذج (Model Versioning) هو الطريقة اللي كتنظم بيها الإصدارات المختلفة ديال النموذج. بهاد الطريقة نقدر نعرف شنو تبدّل، ونعيد التجارب، ونخدم بالإصدار الصحيح بسهولة.

Why Model Versioning Matters

  • Ensures reproducibility of experiments.
  • Keeps a record of model improvements.
  • Prevents confusion during deployment.
  • Allows rollback to a previous stable version.
  • Facilitates collaboration between data teams.

بالعربية المغربية: التتبع ديال النسخ كيعطينا إمكانية نرجعو لأي نسخة قديمة، نعرفو شنو تحسّن، ونخدمو كفريق بلا مشاكل.

Core Concepts Explained

  • Model Metadata: Information about the model (parameters, dataset version, metrics).
  • Version Identifier: A unique tag or hash assigned to each model version.
  • Artifact Storage: A repository where model files are saved (like S3, DVC remote, MLflow server).
  • Version Control Integration: Tools like Git or DVC that track experiments, data, and models.

Example: Using DVC for Model Versioning

DVC (Data Version Control) is a tool that helps track datasets and models like Git tracks code.


# Initialize DVC in your project
dvc init

# Add a trained model file to version control
dvc add model.pkl

# Save the change in Git
git add model.pkl.dvc .gitignore
git commit -m "Add model version 1.0"

# Push model to remote storage
dvc remote add -d myremote s3://my-bucket/models
dvc push

This process saves your model with its metadata and links it to the specific experiment that produced it.

بالعربية المغربية: بـ DVC نقدر نسجّل النموذج ديالنا بحال الكود. كنضيفو، كنحطّو فالـGit، ومن بعد كنرفعو للسيرفر. كل نسخة كتكون مربوطة بتجربة محددة.

Python Example: Tracking Model Versions with MLflow

MLflow is another tool used to manage model versions, experiments, and deployments.


import mlflow
import mlflow.sklearn
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

# Load data
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# Train model
model = RandomForestClassifier(n_estimators=50, random_state=42)
model.fit(X_train, y_train)
accuracy = model.score(X_test, y_test)

# Track model with MLflow
mlflow.set_experiment("iris-classification")
with mlflow.start_run():
    mlflow.log_param("n_estimators", 50)
    mlflow.log_metric("accuracy", accuracy)
    mlflow.sklearn.log_model(model, "model")

print("Model version logged successfully.")

Explanation of the Example

  • The model and parameters are saved automatically by MLflow.
  • Each experiment run creates a unique version ID.
  • All metrics and models can be viewed in the MLflow UI.

بالعربية المغربية: MLflow كيسجّل النموذج، المعاملات، والنتائج فكل تجربة. كل مرة كتدير تجربة جديدة كتولد نسخة جديدة بآيدي خاص بيها.

Best Practices for Model Versioning

  • Tag models with clear version numbers (v1.0, v1.1).
  • Always log dataset version and preprocessing steps.
  • Store evaluation metrics with each model.
  • Use automation to register models after training.
  • Link each model to its source code commit.

Common Tools for Model Versioning

ToolMain Purpose
GitCode versioning
DVCData and model tracking
MLflowExperiment and model version management
Weights & Biases (W&B)Visualization and experiment tracking
TensorBoardModel metrics visualization

بالعربية المغربية: كاينين بزاف ديال الأدوات اللي كتعاون فالتتبع: Git للكود، DVC للبيانات والنماذج، وMLflow للتجارب. كل وحدة كتخدم جزء مهم فالسيرورة ديال التعلم الآلي.

10 Exercises for Practice

  1. Define what model versioning means in machine learning.
  2. List three benefits of using model versioning.
  3. Explain the difference between Git and DVC.
  4. Set up DVC in a local project and add a model file.
  5. Use MLflow to log model metrics and parameters.
  6. Create a versioning system using model version tags (v1, v2).
  7. Track both data and model versions for one experiment.
  8. Connect DVC or MLflow with remote storage (like S3 or Google Drive).
  9. Build a Python script to automatically register new model versions.
  10. Discuss how versioning improves collaboration in AI projects.
Share:

Policy Gradient Methods

Policy Gradient Tutorial

Introduction

Policy Gradient methods are a family of Reinforcement Learning algorithms that directly optimize the policy instead of estimating value functions like Q-Learning or DQN. These methods use gradient ascent to adjust the parameters of a policy network to maximize expected rewards.

بالعربية المغربية (الدارجة): طرق Policy Gradient كيتعلمو السياسة (policy) مباشرة بلا ما يحسبو القيم Q. كيتستعملو الشبكات العصبية باش يتبدلو المعاملات ديال السياسة تدريجياً باش تزيد الأرباح المتوقعة.

Core Concepts Explained

  • Policy (π): A function that maps states to probabilities of actions.
  • Objective Function (J(θ)): The goal is to maximize the expected cumulative reward under the policy parameters θ.
  • Gradient Ascent: Instead of minimizing loss, we maximize performance by moving parameters in the direction of the gradient.
  • Monte Carlo Estimation: Used to estimate returns from sampled episodes.

بالعربية المغربية: السياسة (policy) كتحدد شنو الفعل اللي يدير الوكيل فكل حالة. الهدف هو نحسنو السياسة باش نربحو بزاف فالمتوسط. كنستعملو الانحدار Gradient باش نزيدو فالقيمة ديال الأرباح.

Mathematical Formula

The main idea of Policy Gradient is to adjust the parameters θ in the direction that increases performance:


∇θ J(θ) = E[ ∇θ log πθ(a | s) * Gt ]

Here, Gt is the total discounted reward after time step t, and log πθ(a | s) is the logarithm of the policy probability for the chosen action.

Python Example: Simple REINFORCE Algorithm (Policy Gradient)


import gym
import torch
import torch.nn as nn
import torch.optim as optim

# Define Policy Network
class PolicyNetwork(nn.Module):
    def __init__(self, state_size, action_size):
        super(PolicyNetwork, self).__init__()
        self.fc1 = nn.Linear(state_size, 128)
        self.fc2 = nn.Linear(128, action_size)

    def forward(self, x):
        x = torch.relu(self.fc1(x))
        return torch.softmax(self.fc2(x), dim=-1)

# Initialize environment and policy
env = gym.make("CartPole-v1")
policy = PolicyNetwork(env.observation_space.shape[0], env.action_space.n)
optimizer = optim.Adam(policy.parameters(), lr=0.01)
gamma = 0.99

def select_action(state):
    state = torch.from_numpy(state).float()
    probs = policy(state)
    action = torch.multinomial(probs, 1).item()
    return action, torch.log(probs[action])

# Training loop
for episode in range(500):
    log_probs = []
    rewards = []
    state = env.reset()[0]
    done = False
    while not done:
        action, log_prob = select_action(state)
        next_state, reward, done, _, _ = env.step(action)
        log_probs.append(log_prob)
        rewards.append(reward)
        state = next_state
    # Compute returns
    returns = []
    G = 0
    for r in reversed(rewards):
        G = r + gamma * G
        returns.insert(0, G)
    returns = torch.tensor(returns)
    returns = (returns - returns.mean()) / (returns.std() + 1e-9)
    # Policy update
    loss = []
    for log_prob, Gt in zip(log_probs, returns):
        loss.append(-log_prob * Gt)
    loss = torch.stack(loss).sum()
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    if episode % 50 == 0:
        print(f"Episode {episode}, Total Reward: {sum(rewards)}")

Explanation of the Example

  • The policy network outputs probabilities for each action.
  • The agent samples actions according to the policy.
  • Rewards are collected during the episode.
  • The total discounted return is computed for each step.
  • The network parameters are updated using gradient ascent on the log probabilities weighted by returns.

بالعربية المغربية: الشبكة العصبية كتنتج احتمال كل فعل. الوكيل كيجرب الأفعال وكيجمع المكافآت. من بعد كيتحسب الربح الكلي (discounted return) وكتتصلح الشبكة باش الأفعال المزيانة تولي أكثر احتمال.

Advantages of Policy Gradient

  • Works well in continuous action spaces.
  • Can learn stochastic policies.
  • More stable for high-dimensional problems.

Limitations

  • High variance in gradient estimates.
  • Requires many episodes to converge.
  • Performance depends on good hyperparameter tuning.

بالعربية المغربية: من ميزات Policy Gradient أنه خدام مزيان فالأفعال المتواصلة وكيقدر يتعلم سياسات عشوائية. لكن كيعاني من التباين الكبير وكيحتاج تجارب بزاف باش يوصل لحل جيد.

Advanced Extensions

  • Actor-Critic: Combines Policy Gradient with a value estimator for lower variance.
  • Proximal Policy Optimization (PPO): Improves training stability using clipping.
  • Trust Region Policy Optimization (TRPO): Restricts large policy updates for safety.

بالعربية المغربية: كاينين نسخ مطوّرة بحال Actor-Critic اللي كتدمج بين السياسة والقيمة، وPPO وTRPO اللي كيعطيو استقرار أكبر فالتعلم.

10 Exercises for Practice

  1. Define the goal of Policy Gradient methods.
  2. Write down the formula for the policy gradient update.
  3. Explain the difference between Q-Learning and Policy Gradient.
  4. Implement REINFORCE in any simple Gym environment.
  5. Experiment with different learning rates and observe performance.
  6. Normalize returns and compare results with unnormalized ones.
  7. Add a baseline to reduce variance in gradient estimates.
  8. Implement Actor-Critic and compare with pure Policy Gradient.
  9. Plot total rewards across episodes to visualize learning.
  10. Explain why Policy Gradient is effective in continuous action spaces.
Share:

Deep Q-Networks (DQN)

Deep Q-Networks (DQN) Tutorial

Introduction

Deep Q-Networks (DQN) combine Q-Learning with deep neural networks. Instead of storing a Q-table, the algorithm uses a neural network to approximate the Q-function. This approach allows Reinforcement Learning to handle large or continuous state spaces that traditional Q-Learning cannot manage.

بالعربية المغربية (الدارجة): الشبكات العصبية العميقة ديال Q (DQN) كدمج التعلم بالتعزيز مع الشبكات العصبية. بلا ما نستعمل جدول Q كبير، كندرب شبكة عصبية باش تتعلم شنو هي القيمة ديال كل فعل فكل حالة. بهاد الطريقة نقدر نخدم فبيئات كبيرة بزاف.

Core Concepts Explained

  • Q-Network: A neural network that takes a state as input and outputs Q-values for each action.
  • Target Network: A copy of the Q-network used to stabilize learning.
  • Experience Replay: A memory buffer that stores past experiences to break correlation between samples.
  • Loss Function: Mean squared error between predicted Q-values and target Q-values.

بالعربية المغربية: عندنا شبكة عصبية كتسمى Q-Network كتخرج القيم Q لكل فعل. وعندنا نسخة منها (Target Network) كتعاون باش التعلم يكون مستقر. كذلك كنسجلو التجارب القديمة فـMemory باش مانبقوش نتعلمو من نفس البيانات بزاف.

DQN Update Rule

The DQN loss function is based on the Bellman equation:


Loss = (R + γ * max(Q_target(s', a')) - Q(s, a))²

We minimize this loss to update the weights of the Q-network.

Python Example: Basic DQN with PyTorch


import torch
import torch.nn as nn
import torch.optim as optim
import random
import numpy as np
from collections import deque

# Neural Network for Q-function
class DQN(nn.Module):
    def __init__(self, state_size, action_size):
        super(DQN, self).__init__()
        self.fc1 = nn.Linear(state_size, 24)
        self.fc2 = nn.Linear(24, 24)
        self.fc3 = nn.Linear(24, action_size)

    def forward(self, x):
        x = torch.relu(self.fc1(x))
        x = torch.relu(self.fc2(x))
        return self.fc3(x)

# Environment (example: simple 1D world)
state_size = 4
action_size = 2
policy_net = DQN(state_size, action_size)
target_net = DQN(state_size, action_size)
target_net.load_state_dict(policy_net.state_dict())
optimizer = optim.Adam(policy_net.parameters(), lr=0.001)

# Replay memory
memory = deque(maxlen=2000)
gamma = 0.95
batch_size = 32

def replay():
    if len(memory) < batch_size:
        return
    minibatch = random.sample(memory, batch_size)
    states, actions, rewards, next_states, dones = zip(*minibatch)
    states = torch.tensor(states, dtype=torch.float32)
    next_states = torch.tensor(next_states, dtype=torch.float32)
    actions = torch.tensor(actions)
    rewards = torch.tensor(rewards)
    dones = torch.tensor(dones, dtype=torch.float32)

    q_values = policy_net(states).gather(1, actions.unsqueeze(1)).squeeze()
    max_next_q_values = target_net(next_states).max(1)[0]
    targets = rewards + (1 - dones) * gamma * max_next_q_values
    loss = nn.MSELoss()(q_values, targets.detach())
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

# Example of adding experience
for i in range(100):
    s = np.random.rand(state_size)
    a = random.randint(0, action_size - 1)
    r = random.random()
    s_next = np.random.rand(state_size)
    done = random.choice([0, 1])
    memory.append((s, a, r, s_next, done))
    replay()

print("Training step completed.")

Explanation of the Example

  • The Q-network predicts Q-values for all actions given a state.
  • The target network is updated periodically to keep learning stable.
  • The replay buffer stores experiences to train with random samples.
  • The network learns by minimizing the Bellman loss.

بالعربية المغربية: الشبكة العصبية كتتعلم تدريجياً من تجارب مخزونة فـMemory. كل مرة كتحسب الخطأ بين القيمة المتوقعة والقيمة الحقيقية (Bellman Loss) وكتصلح راسها. الشبكة الهدف كتتبدل من وقت لآخر باش التعلم ما يكونش غير مستقر.

Advanced Concepts

  • Double DQN: Reduces overestimation by separating action selection and evaluation.
  • Dueling DQN: Splits the network into value and advantage streams for better learning.
  • Prioritized Experience Replay: Samples more important experiences more often.

بالعربية المغربية: كاينين نسخ مطوّرة بحال Double DQN اللي كتقلل الأخطاء فالتقدير، وDueling DQN اللي كتفصل بين القيمة العامة والميزة ديال الأفعال. كذلك كاين Prioritized Replay باش يتعلم الوكيل من التجارب المهمة بزاف.

10 Exercises for Practice

  1. Explain the difference between Q-Learning and DQN.
  2. Define the role of the target network in DQN.
  3. Implement a DQN for the CartPole environment using Gym.
  4. Add Experience Replay to your Q-Learning agent.
  5. Experiment with different batch sizes and learning rates.
  6. Implement Double DQN and compare the results.
  7. Add visualization of episode rewards during training.
  8. Explain why DQN is more stable than vanilla Q-Learning.
  9. Test your trained agent in a new environment.
  10. Discuss how neural networks help approximate Q-values in complex environments.
Share:

Q-Learning Tutorial

Q-Learning Tutorial

Introduction

Q-Learning is a model-free Reinforcement Learning algorithm. It allows an agent to learn the best actions to take in an environment without knowing the transition probabilities. The goal is to learn the optimal action-value function, called the Q-function.

بالعربية المغربية (الدارجة): خوارزمية Q-Learning هي وحدة من أشهر الطرق فالتعلم بالتعزيز. كتعلم الوكيل ياخد قرارات صحيحة بلا ما يعرف شنو القوانين ديال البيئة. الهدف ديالها هو يتعلم قيمة كل فعل فكل حالة باش يختار الأفضل.

Core Concepts Explained

  • Q-Value (Q(s, a)): The expected total reward when taking action a in state s and following the optimal policy after that.
  • Learning Rate (α): Controls how fast the algorithm updates knowledge.
  • Discount Factor (γ): Measures how much the agent values future rewards.
  • Exploration vs Exploitation: The agent must balance between exploring new actions and exploiting the best-known actions.

بالعربية المغربية: القيمة ديال Q (Q(s,a)) كتورينا شحال كيتوقع الوكيل يربح إلا دار الفعل a فالحالة s. وعندنا معاملات بحال α (سرعة التعلم) و γ (أهمية المستقبل). الوكيل خاصو يوازن بين يجرب الجديد ولا يستعمل شنو تعلم.

Q-Learning Equation

The core update rule of Q-Learning is:


Q(s, a) = Q(s, a) + α * [R + γ * max(Q(s', a')) - Q(s, a)]

This formula updates the Q-value based on the immediate reward and the estimated future reward.

Python Example: Simple Q-Learning


import numpy as np
import random

# Define environment
states = [0, 1, 2, 3, 4, 5]
actions = [0, 1]  # 0 = left, 1 = right
rewards = [0, 0, 0, 0, 1, 10]  # goal at state 5

# Initialize Q-table
Q = np.zeros((len(states), len(actions)))

# Parameters
alpha = 0.1
gamma = 0.9
epsilon = 0.1  # exploration rate
episodes = 200

for episode in range(episodes):
    state = 0
    while state != 5:
        if random.uniform(0, 1) < epsilon:
            action = random.choice(actions)
        else:
            action = np.argmax(Q[state, :])
        next_state = state + 1 if action == 1 else max(state - 1, 0)
        reward = rewards[next_state]
        Q[state, action] = Q[state, action] + alpha * (reward + gamma * np.max(Q[next_state, :]) - Q[state, action])
        state = next_state

print("Learned Q-table:")
print(Q)

Explanation of the Example

  • The environment is a 1D path of 6 states.
  • The agent can move left or right.
  • Rewards are given only at the last state.
  • Q-table is updated with the Q-Learning equation.
  • Over episodes, the agent learns which actions lead to the goal faster.

بالعربية المغربية: الوكيل كيتعلم يمشي من الحالة 0 حتى الحالة 5. فالأول كيجرب بزاف، ومن بعد كيبدا يعرف شنو الأفعال اللي كتوصلو بسرعة للجائزة. الماتريصة Q كتتعمر بالقيم مع الوقت.

Visualization of Learning Process (Optional)

After enough episodes, the Q-table should show higher values for actions that move the agent toward the goal.

10 Exercises for Practice

  1. Define what Q-Learning is and its main goal.
  2. Explain the difference between Q-Learning and Value Iteration.
  3. Write the Q-Learning update rule in your own words.
  4. Implement Q-Learning for a grid environment (3x3 states).
  5. Change the learning rate α and observe its effect.
  6. Modify the exploration rate ε and describe what happens.
  7. Plot the total reward per episode using matplotlib.
  8. Implement a greedy policy from a learned Q-table.
  9. Explain how Q-Learning can work in stochastic environments.
  10. Extend Q-Learning to continuous states using function approximation.

Advanced Topic: ε-Greedy Policy

In Q-Learning, the ε-greedy policy is used to balance exploration and exploitation. With probability ε, the agent explores (chooses random actions). With probability (1 - ε), it exploits the best known action.

بالعربية المغربية: سياسة ε-greedy كتخلي الوكيل يجرب أفعال جديدة بنسبة صغيرة (ε) ويدير الأفعال المضمونة بنسبة كبيرة (1-ε). بهاد الطريقة كيقدر يتعلم مزيان بلا ما يبقى حابس فحلول ناقصة.

Share:

Markov Decision Processes (MDPs)

Markov Decision Processes (MDPs) Tutorial

Introduction

Markov Decision Processes, or MDPs, are the foundation of Reinforcement Learning. They describe environments where an agent makes decisions, receives rewards, and transitions between states. MDPs help machines learn how to act optimally in uncertain situations.

بالعربية المغربية (الدارجة): عمليات ماركوف ديال اتخاذ القرار (Markov Decision Processes) هي الأساس ديال التعلم بالتعزيز. كتوصف البيئة اللي فيها الوكيل (agent) كيدير قرارات، كيتلقى مكافآت، وكيبدل الحالات. الهدف هو يتعلم كيفاش يتصرف بطريقة مثالية فظروف فيها عدم اليقين.

Core Concepts Explained

An MDP is defined by four main components:

  • States (S): All possible situations where the agent can be.
  • Actions (A): All possible moves the agent can take.
  • Transition Function (P): Probability of moving from one state to another after taking an action.
  • Reward Function (R): The immediate reward received after an action.

Formally, an MDP is a tuple (S, A, P, R, γ) where γ (gamma) is the discount factor, controlling how much the agent values future rewards.

بالعربية المغربية: الـ MDP فيها أربع عناصر رئيسيين: الحالات، الأفعال، احتمال الانتقال، والمكافآت. كل عنصر عندو دور فتعليم الوكيل شنو يدير باش يوصل لأفضل نتيجة.

Mathematical Representation

The goal in an MDP is to find a policy π(s) that maximizes the expected return:

V*(s) = max_a [ R(s, a) + γ * Σ P(s' | s, a) * V*(s') ]

This is called the Bellman Optimality Equation.

Python Example: Value Iteration


import numpy as np

# States: 0, 1, 2
# Actions: 0=Left, 1=Right
P = {
    0: {0: [(1.0, 0, 0, False)], 1: [(1.0, 1, 0, False)]},
    1: {0: [(1.0, 0, 0, False)], 1: [(1.0, 2, 1, True)]},
    2: {0: [(1.0, 2, 0, True)], 1: [(1.0, 2, 0, True)]}
}

def value_iteration(P, gamma=0.9, theta=0.001):
    V = np.zeros(3)
    while True:
        delta = 0
        for s in range(3):
            v = V[s]
            Q = []
            for a in P[s]:
                Q.append(sum([p * (r + gamma * V[s_]) for p, s_, r, done in P[s][a]]))
            V[s] = max(Q)
            delta = max(delta, abs(v - V[s]))
        if delta < theta:
            break
    return V

V = value_iteration(P)
print("Optimal Values:", V)

Explanation of the Example

  • The states represent positions in a simple environment.
  • The actions are Left and Right.
  • The transition dictionary P defines how actions change the state.
  • The algorithm updates the value of each state until convergence using the Bellman equation.

بالعربية المغربية: فالكود، عندنا 3 حالات و جوج ديال الأفعال. كل مرة الوكيل كيجرب يشوف شنو القيمة الأفضل لكل حالة حتى كيتوقف التغيير (يعني وصل للحل الأمثل).

Policy Iteration (Alternative Method)

Another way to solve MDPs is Policy Iteration, which alternates between evaluating a policy and improving it.


def policy_iteration(P, gamma=0.9, theta=0.001):
    policy = np.zeros(3, dtype=int)
    V = np.zeros(3)
    while True:
        # Policy Evaluation
        while True:
            delta = 0
            for s in range(3):
                v = V[s]
                a = policy[s]
                V[s] = sum([p * (r + gamma * V[s_]) for p, s_, r, done in P[s][a]])
                delta = max(delta, abs(v - V[s]))
            if delta < theta:
                break
        # Policy Improvement
        stable = True
        for s in range(3):
            old_action = policy[s]
            Q = [sum([p * (r + gamma * V[s_]) for p, s_, r, done in P[s][a]]) for a in P[s]]
            policy[s] = np.argmax(Q)
            if old_action != policy[s]:
                stable = False
        if stable:
            break
    return policy, V

policy, V = policy_iteration(P)
print("Optimal Policy:", policy)


10 Exercises for Practice

  1. Define what an MDP is in your own words.
  2. List the main components of an MDP.
  3. Write the Bellman Equation and explain each term.
  4. Implement Value Iteration for a 4-state environment.
  5. Change the discount factor γ and see how it affects the results.
  6. Implement Policy Iteration in Python.
  7. Compare the outputs of Value Iteration and Policy Iteration.
  8. Create an MDP where rewards are negative and observe the effect.
  9. Write a function that extracts the optimal policy from a value function.
  10. Explain the difference between deterministic and stochastic policies.
Share:

How to Build an Expert System with Prolog | AI Tutorial

How to build an Expert system with Prolog

1 Introduction

An expert system is a rule based program that tries to act like a human expert in a narrow domain. With Prolog you can express rules and facts in a natural logic style, so Prolog fits expert systems well.

Darija: النظام الخبير هو واحد النوع ديال البرامج اللي كيتصرف بحال خبير حقيقي فمجال محدد وصغير. مع Prolog نقدرو نكتبو القواعد والحقائق بطريقة منطقية وبسيطة، وهاد الشي كيجعل Prolog مناسب بزاف للنظم الخبيرة.

In this tutorial you learn how to build a simple expert system with Prolog. You see core ideas, then code examples, then practice exercises.

2 Core Concepts Explained

2.1 What is an expert system

An expert system tries to answer questions or give advice in a domain like medicine, troubleshooting, or recommendation. Main parts:

  • Knowledge base contains facts and rules about the domain
  • Inference engine uses logic to answer questions from the user
  • User interface asks questions and prints results

Darija: النظام الخبير كيحاول يجاوب على الأسئلة ولا يعطي نصائح فمجال معين بحال الطب، إصلاح الأجهزة، ولا الاقتراحات. عندو ثلاثة ديال المكونات الرئيسية

  • قاعدة المعارف فيها الحقائق والقواعد على المجال
  • محرك الاستدلال كييستعمل المنطق باش يخرج أجوبة من القواعد
  • واجهة المستخدم كتطرح الأسئلة على المستخدم وكتوريه النتائج

2.2 Why Prolog for expert systems

  • Prolog is logic based
  • You write facts and rules in a direct way
  • The inference engine of Prolog uses backward chaining
  • You focus on knowledge, not on low level control flow

Darija: Prolog مبني على المنطق، وكيخليك تركز على المعارف والقواعد بلا ما تغرق فالتفاصيل ديال التحكم فالكود. محرك Prolog كيدير الاستنتاج بالرجوع للوراء، يعني كيبدا من الهدف وكيقلب على القواعد اللي توصّلو ليه.

2.3 Key Prolog concepts for expert systems

  • Fact simple statement about the world
  • Rule conclusion that depends on conditions
  • Query question that you ask to the system
  • Variables start with uppercase and stand for unknown values

Darija: الحقائق هي جُمل بسيطة كتوصف العالم القواعد هي استنتاجات مرتبطة بشروط والاستعلام هو السؤال اللي كنسولو للنظام المتغيّرات فـ Prolog كيبداو بحرف كبير وكيعبرو على قيم مجهولة.

3 Prolog Syntax and Structure for Expert Systems

3.1 Facts

Example facts for diseases and symptoms

% disease(Name)
disease(flu).
disease(cold).

% symptom(Disease, Symptom)
symptom(flu, fever).
symptom(flu, cough).
symptom(cold, cough).
symptom(cold, sneezing).

English: Each line is one fact. Darija: كل سطر هنا واحد الحقيقة بسيطة.

3.2 Rules

Rules describe when something holds. Example rule for a disease if a person has some symptoms.

% has_disease(Person, Disease)
has_disease(Person, flu) :-
    has_symptom(Person, fever),
    has_symptom(Person, cough).

has_disease(Person, cold) :-
    has_symptom(Person, cough),
    has_symptom(Person, sneezing).

Here :- reads as "if". On the right side you see conditions separated by commas. Prolog treats comma as logical AND.

Darija: الرمز :- كنقراوه "إلا". الشرطين مفصولين بـ , وهاد الشي كيعبّر على "و" منطقية.

3.3 Queries

You ask the system using queries in the Prolog console.

?- has_disease(ali, flu).
?- has_disease(Who, flu).

First query checks if Ali has flu. Second query asks for all people with flu.

Darija: فأول استعلام كنشوفو واش علي عندو la grippe فالثاني كنطلبو جميع الناس اللي عندهم la grippe.

4 Example 1 – Simple Medical Expert System in Prolog

4.1 Knowledge base

% facts about symptoms of diseases
disease(flu).
disease(cold).

symptom(flu, fever).
symptom(flu, cough).
symptom(flu, body_ache).

symptom(cold, cough).
symptom(cold, sneezing).
symptom(cold, runny_nose).

% user symptom facts
has_symptom(ali, fever).
has_symptom(ali, cough).
has_symptom(sara, cough).
has_symptom(sara, sneezing).

4.2 Reasoning rules

% a person has a disease if person has all symptoms linked to that disease
possible_disease(Person, Disease) :-
    disease(Disease),
    symptom(Disease, Symptom),
    has_symptom(Person, Symptom).

This rule says Person has a possible disease if disease exists, symptom belongs to disease, and person has same symptom. You can ask

?- possible_disease(ali, D).
?- possible_disease(sara, D).

Darija: القاعِدة كتعني إلا كان مرض معيّن وعندو واحد العَرَض وهاد العَرَض موجود عند الشخص النظام كيعتبر هاد المرض احتمال عند هاد الشخص ومن بعد نقدر نسول Prolog على الأمراض المحتملة عند علي ولا سارة.

4.3 Simple interactive question system

Now build a small expert system that asks the user about symptoms.

% ask user about a symptom
ask(Symptom) :-
    write('Do you have '),
    write(Symptom),
    write('? (yes or no) '),
    read(Reply),
    Reply == yes.

% dynamic facts to store user answers
:- dynamic has_symptom_user/1.

% gather symptoms from user
collect_symptoms :-
    symptom(_, Symptom),
    \+ has_symptom_user(Symptom),
    ask(Symptom),
    assertz(has_symptom_user(Symptom)),
    fail.
collect_symptoms.

% reason using user symptoms
user_possible_disease(Disease) :-
    disease(Disease),
    symptom(Disease, Symptom),
    has_symptom_user(Symptom).

run_expert_system :-
    retractall(has_symptom_user(_)),
    collect_symptoms,
    write('Possible diseases '), nl,
    user_possible_disease(D),
    write(D), nl,
    fail.
run_expert_system.

Now in the Prolog console run

?- run_expert_system.

System asks questions then prints possible diseases.

Darija: منين تشغّل run_expert_system النظام كيسوّل المستخدم على الأعراض ومن بعد كيعطيه لائحة ديال الأمراض المحتملة.

5 Example 2 – Animal Classification Expert System

5.1 Facts and rules

% animal facts
animal(dog).
animal(cat).
animal(bird).

has_feature(dog, mammal).
has_feature(dog, four_legs).
has_feature(dog, barks).

has_feature(cat, mammal).
has_feature(cat, four_legs).
has_feature(cat, meows).

has_feature(bird, two_legs).
has_feature(bird, flies).

% rule for classification
classify(Animal, mammal) :-
    has_feature(Animal, mammal).

classify(Animal, quadruped) :-
    has_feature(Animal, four_legs).

classify(Animal, flying_animal) :-
    has_feature(Animal, flies).

You can ask

?- classify(dog, Class).
?- classify(bird, Class).

Darija: هنا عندنا نظام خبير بسيط كيصنّف الحيوانات حسب المميزات ديالهم بحال واش هو mammal ولا كيطرّ فسماء.

6 Example 3 – Simple Course Recommendation Expert System

6.1 Knowledge base

% user preferences
interest(user1, ai).
interest(user1, programming).

course('Intro to AI', ai, beginner).
course('Machine Learning with Python', ai, intermediate).
course('Prolog for AI', ai, intermediate).
course('Python Basics', programming, beginner).
course('Data Structures', programming, intermediate).

% rule to recommend
recommend(Person, Course) :-
    interest(Person, Topic),
    course(Course, Topic, Level),
    Level == beginner.

recommend(Person, Course) :-
    interest(Person, Topic),
    course(Course, Topic, Level),
    Level == intermediate.

Query examples

?- recommend(user1, Course).

Darija: فهاد المثال النظام كيقترح دورات حسب الاهتمامات ديال المستخدم بحال ai ولا programming.

7 Explanation of Key Parts

7.1 Backward chaining in Prolog

Prolog tries to prove a goal by looking for rules where goal appears in the head. Then it tries to satisfy conditions in body step by step. You do not write the inference engine yourself. Prolog handles search and unification.

Darija: منين كتعطي هدف لـ Prolog هو كيقلب على القواعد اللي الرأس ديالها هو نفس الهدف من بعد كيبدا يحقّق الشروط واحد بواحد إنت غير كتركز على الحقائق والقواعد و Prolog كيدير الخدمة ديال الاستنتاج.

7.2 Knowledge base design

You design a good expert system when you:

  • Pick a narrow domain like small medical subproblem or toy diagnostic task
  • List important concepts as predicates
  • Write clear facts with simple arguments
  • Write rules with small bodies and clear logic

Darija: باش تصايب نظام خبير مزيان اختار مجال صغير وواضح حدّد المفاهيم المهمة فشكل predicates ومن بعد كتب الحقائق والقواعد بطريقة بسيطة.

7.3 User interaction part

For real systems you:

  • Ask user questions
  • Store answers as dynamic facts
  • Run inference using those dynamic facts
  • Show results or explanations

Prolog gives assertz and retract for dynamic facts. You saw this in the medical example with has_symptom_user.

Darija: فالنظام الحقيقي كتسول المستخدم كتسجّل الأجوبة فحقائق ديناميكية ومن بعد كتحسب النتائج وكتوري المستخدم التفسير والنتيجة.

8 Step by Step – Build Your Own Expert System in Prolog

Step 1 Choose a domain

Examples:

  • Plant disease hints
  • Computer network troubleshooting
  • Car starting problems
  • Study path advice for AI

Darija: أول خطوة هي تختار مجال مثلا أمراض النباتات أو مشاكل الحاسوب أو اقتراحات الدراسة فمجال الذكاء الاصطناعي.

Step 2 Define predicates

Decide names for:

  • Facts about entities, for example problem/1, symptom/2, has_feature/2
  • Derived knowledge, for example diagnose/2, recommend/2, classify/2

Darija: اختار الأسماء ديال الـ predicates بحال symptom للأعراض ولا recommend للاقتراحات.

Step 3 Write facts

Enter fixed knowledge as facts.

problem(bad_battery).
problem(no_fuel).

symptom_problem(bad_battery, engine_clicks).
symptom_problem(no_fuel, engine_silent).

Step 4 Write rules

diagnose(Problem) :-
    symptom_problem(Problem, Symptom),
    has_symptom_user(Symptom).

Then later you add rules for recommendation or explanations.

Step 5 Add simple question interface

Reuse pattern from previous ask and collect_symptoms predicates. Adapt names for your domain, for example question_feature instead of symptom.

Step 6 Test and extend

Run many queries. Check if answers match expert expectations. Adjust rules and add more facts.

Darija: جرّب النظام مع بزاف ديال الأمثلة شوف واش النتائج قريبة من رأي خبير حقيقي إلا لقيتي خطأ بدّل القواعد وزيد حقائق جديدة.

9 Practice Exercises – Prolog Expert Systems

Use these exercises to strengthen your understanding of expert systems in Prolog. Try to write full Prolog files and test in a Prolog interpreter like SWI Prolog.

  1. Build an expert system for recommending a drink Inputs temperature (hot or cold), time of day, and caffeine preference Output drink like tea, coffee, juice Use facts for properties and rules for recommendation.
  2. Create an animal expert system Ask about features like has_fur, lays_eggs, lives_in_water Infer animal group like mammal, bird, fish.
  3. Extend the medical example Add new disease and symptoms Add rule for high risk case when user has certain combination of symptoms.
  4. Design a laptop selection expert system Facts about laptops and properties like ram_16gb, gpu_midrange Rules that recommend laptops for gaming or for study.
  5. Implement explanation feature When system suggests a disease or recommendation Add predicate explain/1 that prints reasons using rules.
  6. Add certainty levels Store facts symptom_weight(Disease, Symptom, Weight) Compute a score per disease based on user symptoms Print diseases sorted by score or at least print scores.
  7. Build a course advisor for AI learning Inputs background (math, programming) and goals (research, industry) Output learning path or next course suggestion.
  8. Make a simple troubleshooting expert system for Wi Fi Ask user questions about lights, distance, other devices Output advice step to try next.
  9. Add persistence Use file predicates like tell and told or modern equivalents Save user answers or logs to a file then read them back in a new session.
  10. Combine two expert systems First part diagnoses car battery issue Second part recommends next action such as call mechanic or charge battery Connect them with shared facts.
  11. Bonus in Darija صايب نظام خبير صغير كيعطي نصيحة على شنو تقرا فمجال الذكاء الاصطناعي دخّل مستوى المستخدم مبتدئ ولا متوسط واهتمام ديالو vision ولا NLP ولا reinforcement learning وخليه يقترح ليه واحد الكورس ولا كتاب.

Darija: حاول تحل هاد التمارين بوحدك كتب الكود فملف Prolog وجرّبو فـ SWI Prolog ولا أي مفسّر آخر كل مرة شوف النتيجة وعدّل القواعد حتى توصل لنظام خبير معقول.

10 SEO Keywords and Internal Linking Ideas

Include these phrases in titles or text when you write about this topic:

  • Prolog expert system tutorial
  • How to build an expert system with Prolog
  • AI expert system in Prolog
  • Rule based system in artificial intelligence
  • Knowledge base and inference engine in Prolog

Suggested internal links for a learning site:

[internal link: Machine Learning Basics]

[internal link: Neural Networks Guide]

[internal link: Intro to Prolog for AI]

[internal link: Rule Based Systems in AI]

[internal link: Knowledge Representation in AI]

Darija: من بعد هاد الدرس تقدّر تنتاقل لدروس أخرى بحال Machine Learning Neural Networks ولا Intro to Prolog for AI باش تكمل رحلة التعلم فمجال الذكاء الاصطناعي.

Share:

Ai With Darija

Discover expert tutorials, guides, and projects in machine learning, deep learning, AI, and large language models . start learning to boot your carrer growth in IT تعرّف على دروس وتوتوريالات ، ومشاريع فـ الماشين ليرنين، الديب ليرنين، الذكاء الاصطناعي، والنماذج اللغوية الكبيرة. بّدا التعلّم باش تزيد تقدم فـ المسار ديالك فـ مجال المعلومات.