Files
DEV5.2/TP04/Untitled.ipynb
2025-01-15 16:34:18 +01:00

206 lines
5.8 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"cell_type": "markdown",
"id": "c49056b3-e2c2-471d-b998-14eb1cd1a082",
"metadata": {},
"source": [
"# Exercice 1 : \n",
"Autre fonctions d'erreurs : \n",
"- Erreur résiduelle : ei = f(xi)-yi\n",
"- Erreur logarithmique : ei = log(1+|f(xi)-yi|)\n",
"- Erreur relative : ei = (f(xi)-yi)/yi\n",
"\n",
"# Exercice 2 : \n",
"Autre fonctions de couts : \n",
"- Erreur Log-Cosh, Log-Cosh est similaire à MAE pour de petites erreurs\n",
"- Erreur Huber (𝛿=1), Huber réduit limpact des grandes erreurs\n",
"- Erreur de Poisson, Poisson est adaptée aux distributions asymétriques\n",
" "
]
},
{
"cell_type": "markdown",
"id": "3aa6d565-ac05-48ca-9f10-ece048a7eff2",
"metadata": {},
"source": [
"# Exercice 3 : \n"
]
},
{
"cell_type": "code",
"execution_count": 84,
"id": "d8b00644-a239-44ba-9b4c-4982e624227f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"iteration = 1\n",
"a= 50 b= 75\n",
"iteration = 2\n",
"a= -25.75309997451889 b= 74.9568945\n",
"iteration = 3\n",
"a= 13.539805289078245 b= 74.97929653630847\n",
"iteration = 4\n",
"a= -6.841306604859323 b= 74.9677200103882\n",
"iteration = 5\n",
"a= 3.730314856415273 b= 74.97376806255475\n",
"iteration = 6\n",
"a= -1.7531534482568194 b= 74.9706742990146\n",
"iteration = 7\n",
"a= 1.0911050566712879 b= 74.97232236778468\n",
"iteration = 8\n",
"a= -0.3842033063747208 b= 74.97151086236455\n",
"iteration = 9\n",
"a= 0.3810346829632343 b= 74.97197513066288\n",
"iteration = 10\n",
"a= -0.015891986938224745 b= 74.97177765894565\n",
"iteration = 11\n",
"a= 0.1899926591360049 b= 74.9719234297637\n",
"iteration = 12\n",
"a= 0.08320088730389696 b= 74.97189116168829\n",
"iteration = 13\n",
"a= 0.13859343372153637 b= 74.97195124182983\n",
"iteration = 14\n",
"a= 0.10986146169209325 b= 74.97196342120678\n",
"iteration = 15\n",
"a= 0.12476462455318163 b= 74.97200044654436\n",
"iteration = 16\n",
"a= 0.1170343740876787 b= 74.97202458433483\n",
"iteration = 17\n",
"a= 0.12104400709218902 b= 74.97205540683525\n",
"iteration = 18\n",
"a= 0.11896419782388343 b= 74.97208276197472\n",
"iteration = 19\n",
"a= 0.12004296396127893 b= 74.97211191560189\n",
"Paramètre a : 0.11948338652853815\n",
"Paramètre b : 74.97214013633689\n"
]
}
],
"source": [
"import numpy as np\n",
"\n",
"# Données d'exemple (à remplacer par vos propres données)\n",
"# xi : les valeurs d'entrée\n",
"# yi : les valeurs de sortie cibles\n",
"xi = np.array([313, 2384, 342, 420]) # Exemple\n",
"yi = np.array([124, 339, 179, 186]) # Exemple\n",
"\n",
"# Hyperparamètres\n",
"alpha = 0.000001 # Taux d'apprentissage\n",
"m = len(xi) # Nombre d'exemples\n",
"\n",
"# Initialisation des paramètres\n",
"a = 50 # Paramètre a initial\n",
"b = 75 # Paramètre b initial\n",
"\n",
"# Seuils d'arrêt\n",
"epsilon_threshold = 1e-3\n",
"\n",
"# Initialisation des erreurs\n",
"epsilon_1 = float('inf')\n",
"epsilon_2 = float('inf')\n",
"tmp = 0\n",
"\n",
"# Boucle principale\n",
"while epsilon_1 > epsilon_threshold or epsilon_2 > epsilon_threshold:\n",
" a_prev = a\n",
" b_prev = b\n",
" tmp += 1\n",
" print(\"iteration = \",tmp)\n",
" print(\"a=\",a,\" b=\",b)\n",
" \n",
"\n",
"\n",
" # Calcul des gradients et mise à jour des paramètres\n",
" b = b - alpha * (1 / m) * np.sum(a * xi + b - yi)\n",
" a = a - alpha * (1 / m) * np.sum((a * xi + b - yi) * xi)\n",
"\n",
" # Calcul des erreurs\n",
" epsilon_1 = abs(a_prev - a)\n",
" epsilon_2 = abs(b_prev - b)\n",
"\n",
"# Résultats finaux\n",
"print(f\"Paramètre a : {a}\")\n",
"print(f\"Paramètre b : {b}\")"
]
},
{
"cell_type": "markdown",
"id": "10a19dd5-878c-4b92-b0e1-9b3324bb3407",
"metadata": {},
"source": [
"| Itération | A | B |\n",
"| :--------------- |:---------------:| -----:|\n",
"| 1 | 50 | 75 |\n",
"| 9 | 0.38103 | 74.97197 |\n",
"| 19 | 0.12004 | 74.97214 |"
]
},
{
"cell_type": "markdown",
"id": "c9fed1c7-6c60-4589-8086-3f8fefec8fca",
"metadata": {},
"source": [
"# Exercice 4 :\n",
"## 1)\n",
"![image](Illustration_sans_titre.png)"
]
},
{
"cell_type": "markdown",
"id": "04ea9f05-9b2d-43a9-86bf-afa9f17f0336",
"metadata": {},
"source": [
"## 2)\n",
"\n",
"data : 01 11 10 00\n",
"\n",
"| _ | 01 | 11 | 10 | 00 |\n",
"| :--------------- |:---------------:| -----:| -----:| -----:|\n",
"| W0 | 2 | 1 | 1 | 0 |\n",
"| W1 | 0 | 0 | 0 | 1 |\n",
"| W2 | 0 | 1 | 1 | 1 |\n",
"| RF |-2 | 0 | -1 | 0 |\n",
"| RO | 0 | 0 | 0 | 0 |\n",
"| RA | 1 | 0 | 1 | 0 |\n",
"| W0' | 2+(1-0)*-1 = 1 | 1 | 1+(1-0)*-1 = 0 | 0 |\n",
"| W1' | 0+(1-0)*0 = 0 | 0 | 0+(1-0)*1 = 1 | 1 |\n",
"| W2' | 0+(1-0)*1 = 1 | 1 | 1+(1-0)*0 = 1 | 1 |"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d205f2d3-a9cb-4c8b-964d-a25f18f793d3",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}