Skip to content

Commit

Permalink
Créé avec Colaboratory
Browse files Browse the repository at this point in the history
  • Loading branch information
roxin committed Sep 8, 2022
1 parent 1e231d3 commit 709c4b5
Showing 1 changed file with 64 additions and 63 deletions.
127 changes: 64 additions & 63 deletions _Exo1_Descente_de_gradient.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@
{
"cell_type": "markdown",
"metadata": {
"id": "jpoYHDHo6Ya0",
"colab_type": "text"
"id": "jpoYHDHo6Ya0"
},
"source": [
"# Algorithme de la descente de gradient\n",
Expand All @@ -81,8 +80,7 @@
{
"cell_type": "markdown",
"metadata": {
"id": "oFq5HwUE6Ya1",
"colab_type": "text"
"id": "oFq5HwUE6Ya1"
},
"source": [
"### Descente du gradient (descente graduelle)"
Expand All @@ -91,8 +89,7 @@
{
"cell_type": "markdown",
"metadata": {
"id": "HCoo_tO26Ya2",
"colab_type": "text"
"id": "HCoo_tO26Ya2"
},
"source": [
"### Fonction coût"
Expand All @@ -101,8 +98,7 @@
{
"cell_type": "markdown",
"metadata": {
"id": "OIno64Rp6ihR",
"colab_type": "text"
"id": "OIno64Rp6ihR"
},
"source": [
"\\begin{equation*}\n",
Expand All @@ -113,8 +109,7 @@
{
"cell_type": "markdown",
"metadata": {
"id": "0IEYRPXs79S7",
"colab_type": "text"
"id": "0IEYRPXs79S7"
},
"source": [
"Algorithme de la descente du gradient :\n",
Expand All @@ -128,31 +123,37 @@
{
"cell_type": "markdown",
"metadata": {
"id": "pPhugYuc-G1-",
"colab_type": "text"
"id": "pPhugYuc-G1-"
},
"source": [
"\\begin{equation*}\n",
"\\theta_1 := \\theta_1 - \\alpha \\frac{1}{m}\\sum_{i=1}^m ( h_\\theta(x_i)- y_i)x_i\n",
"\\end{equation*}"
]
},
{
"cell_type": "code",
"source": [
"# importation des bibliothéques\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt"
],
"metadata": {
"id": "P0Bv_xNS4oc_"
},
"execution_count": 1,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"ExecuteTime": {
"end_time": "2020-09-13T12:52:05.213359Z",
"start_time": "2020-09-13T12:52:05.207791Z"
},
"id": "RTREml0f6Ya4",
"colab_type": "code",
"colab": {}
"id": "RTREml0f6Ya4"
},
"source": [
"# importation des bibliothéques\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"# déclaration d'une fonction pour la descente de gradient\n",
"def descente_de_gradient(alpha, X, y, iterations):\n",
" m = X.shape[0] # number d'echantillons\n",
Expand All @@ -173,7 +174,7 @@
" return theta\n",
"#"
],
"execution_count": 1,
"execution_count": null,
"outputs": []
},
{
Expand All @@ -184,27 +185,28 @@
"start_time": "2020-09-13T12:52:09.969544Z"
},
"id": "YORc1oVy6Ya7",
"colab_type": "code",
"colab": {},
"outputId": "9e0b6d59-eb65-41c0-a620-b002525403ab"
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "dd0dc3e8-e361-4e36-a523-d4b368c4cba7"
},
"source": [
"# Longue serie de données\n",
"#X = 3 * np.random.rand(100, 1) # genère un vecteur colonne avec 100 éléments (valeurs de 0 à 1) \n",
"#y = 2 + 5 * X + np.random.randn(100, 1)\n",
"#print(np.shape(y))\n",
"#y = y.reshape(100,)\n",
"#print(np.shape(y))"
"# Serie longue de données\n",
"X = 3 * np.random.rand(100, 1) # genère un vecteur colonne avec 100 éléments (valeurs de 0 à 1) \n",
"y = 2 + 5 * X + np.random.randn(100, 1)\n",
"print(np.shape(y))\n",
"y = y.reshape(100,)\n",
"print(np.shape(y))"
],
"execution_count": null,
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"(100, 1)\n",
"(100,)\n"
],
"name": "stdout"
]
}
]
},
Expand All @@ -216,9 +218,10 @@
"start_time": "2020-09-13T16:33:28.080982Z"
},
"id": "bzg1KcRS6Ya_",
"colab_type": "code",
"colab": {},
"outputId": "77521165-3b5c-4025-b017-2dd19276f3a7"
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "005134b2-75e2-4282-ae94-17a0b93dffe1"
},
"source": [
"# Série courte de données\n",
Expand All @@ -228,18 +231,18 @@
"print('y = ', y)\n",
" "
],
"execution_count": null,
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"X = [[ 3]\n",
" [ 4]\n",
" [ 6]\n",
" [10]]\n",
"y = [ 7 9 10 17]\n"
],
"name": "stdout"
]
}
]
},
Expand All @@ -251,23 +254,24 @@
"start_time": "2020-09-13T16:28:31.505493Z"
},
"id": "hCFWrqtn6YbB",
"colab_type": "code",
"colab": {},
"outputId": "f63762f5-2b92-4ad1-f119-8a42284724c8"
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "f09573fe-f8e9-4484-86fa-74e018497127"
},
"source": [
"#print(X, \"\\n\", y)\n",
"m = np.shape(X)\n",
"print(m)"
],
"execution_count": null,
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"(6, 1)\n"
],
"name": "stdout"
"(4, 1)\n"
]
}
]
},
Expand All @@ -279,9 +283,11 @@
"start_time": "2020-09-13T16:29:03.320492Z"
},
"id": "i8R5OB4M6YbE",
"colab_type": "code",
"colab": {},
"outputId": "94ef9fd6-a423-4d83-fc7f-262420285a1b"
"colab": {
"base_uri": "https://localhost:8080/",
"height": 222
},
"outputId": "98c27d84-c8a8-4e85-af95-d4fbc557961d"
},
"source": [
"# concatenation de deux matrices\n",
Expand All @@ -295,15 +301,18 @@
"theta = descente_de_gradient(alpha, X_b, y, nombreIterations)\n",
"print(X_b.transpose())"
],
"execution_count": null,
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"text": [
"[[ 1. 1. 1. 1. 1. 1.]\n",
" [ 3. 4. 6. 10. 14. 16.]]\n"
],
"name": "stdout"
"output_type": "error",
"ename": "NameError",
"evalue": "ignored",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-5-13d41055588f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mnombreIterations\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m1000\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;31m#print(valTheta)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mtheta\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdescente_de_gradient\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0malpha\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mX_b\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnombreIterations\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 10\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mX_b\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtranspose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mNameError\u001b[0m: name 'descente_de_gradient' is not defined"
]
}
]
},
Expand All @@ -315,8 +324,6 @@
"start_time": "2020-09-13T12:54:25.750447Z"
},
"id": "LUKJRaZw6YbG",
"colab_type": "code",
"colab": {},
"outputId": "8c1c0dc4-64f6-42d5-a197-e0e3750a6d52"
},
"source": [
Expand Down Expand Up @@ -356,8 +363,6 @@
"start_time": "2020-09-13T12:54:33.584014Z"
},
"id": "t_JXLMFz6YbJ",
"colab_type": "code",
"colab": {},
"outputId": "fb3a7041-6ac3-4bb5-c40e-bc5f81f805bd"
},
"source": [
Expand Down Expand Up @@ -391,8 +396,6 @@
"start_time": "2020-09-13T12:54:39.526014Z"
},
"id": "NhyMFbaY6YbM",
"colab_type": "code",
"colab": {},
"outputId": "906fe8ad-c3a4-437e-995c-975d3145650c"
},
"source": [
Expand Down Expand Up @@ -430,8 +433,6 @@
"start_time": "2020-09-13T03:53:42.750358Z"
},
"id": "Z5RDxCcT6YbP",
"colab_type": "code",
"colab": {},
"outputId": "5b7303ac-0de2-4b84-c46a-49475e15c580"
},
"source": [
Expand Down

0 comments on commit 709c4b5

Please sign in to comment.