-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
style: update black rules to allow lines up to 100 characters
- Loading branch information
Showing
6 changed files
with
347 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,339 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"ename": "ModuleNotFoundError", | ||
"evalue": "No module named 'matplotlib'", | ||
"output_type": "error", | ||
"traceback": [ | ||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | ||
"\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", | ||
"Cell \u001b[0;32mIn[1], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mnumpy\u001b[39;00m \u001b[39mas\u001b[39;00m \u001b[39mnp\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mmatplotlib\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mpyplot\u001b[39;00m \u001b[39mas\u001b[39;00m \u001b[39mplt\u001b[39;00m\n\u001b[1;32m 3\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39msklearn\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mdatasets\u001b[39;00m \u001b[39mimport\u001b[39;00m make_blobs, make_circles\n\u001b[1;32m 4\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39msklearn\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mmetrics\u001b[39;00m \u001b[39mimport\u001b[39;00m accuracy_score, log_loss\n", | ||
"\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'matplotlib'" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"import numpy as np\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"from sklearn.datasets import make_blobs, make_circles\n", | ||
"from sklearn.metrics import accuracy_score, log_loss\n", | ||
"from tqdm import tqdm" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Fonctions" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"ename": "", | ||
"evalue": "", | ||
"output_type": "error", | ||
"traceback": [ | ||
"\u001b[1;31mRunning cells with 'cmnemoi-learn-sm69zs_S-py3.11' requires the ipykernel package.\n", | ||
"\u001b[1;31mRun the following command to install 'ipykernel' into the Python environment. \n", | ||
"\u001b[1;31mCommand: '/Users/charles/Library/Caches/pypoetry/virtualenvs/cmnemoi-learn-sm69zs_S-py3.11/bin/python -m pip install ipykernel -U --force-reinstall'" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"def initialisation(dimensions):\n", | ||
" \n", | ||
" parametres = {}\n", | ||
" C = len(dimensions)\n", | ||
"\n", | ||
" np.random.seed(1)\n", | ||
"\n", | ||
" for c in range(1, C):\n", | ||
" parametres['W' + str(c)] = np.random.randn(dimensions[c], dimensions[c - 1])\n", | ||
" parametres['b' + str(c)] = np.random.randn(dimensions[c], 1)\n", | ||
"\n", | ||
" return parametres" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"ename": "", | ||
"evalue": "", | ||
"output_type": "error", | ||
"traceback": [ | ||
"\u001b[1;31mRunning cells with 'cmnemoi-learn-sm69zs_S-py3.11' requires the ipykernel package.\n", | ||
"\u001b[1;31mRun the following command to install 'ipykernel' into the Python environment. \n", | ||
"\u001b[1;31mCommand: '/Users/charles/Library/Caches/pypoetry/virtualenvs/cmnemoi-learn-sm69zs_S-py3.11/bin/python -m pip install ipykernel -U --force-reinstall'" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"def forward_propagation(X, parametres):\n", | ||
" \n", | ||
" activations = {'A0': X}\n", | ||
"\n", | ||
" C = len(parametres) // 2\n", | ||
"\n", | ||
" for c in range(1, C + 1):\n", | ||
"\n", | ||
" Z = parametres['W' + str(c)].dot(activations['A' + str(c - 1)]) + parametres['b' + str(c)]\n", | ||
" activations['A' + str(c)] = 1 / (1 + np.exp(-Z))\n", | ||
"\n", | ||
" return activations" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"ename": "", | ||
"evalue": "", | ||
"output_type": "error", | ||
"traceback": [ | ||
"\u001b[1;31mRunning cells with 'cmnemoi-learn-sm69zs_S-py3.11' requires the ipykernel package.\n", | ||
"\u001b[1;31mRun the following command to install 'ipykernel' into the Python environment. \n", | ||
"\u001b[1;31mCommand: '/Users/charles/Library/Caches/pypoetry/virtualenvs/cmnemoi-learn-sm69zs_S-py3.11/bin/python -m pip install ipykernel -U --force-reinstall'" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"def back_propagation(y, parametres, activations):\n", | ||
"\n", | ||
" m = y.shape[1]\n", | ||
" C = len(parametres) // 2\n", | ||
"\n", | ||
" dZ = activations['A' + str(C)] - y\n", | ||
" gradients = {}\n", | ||
"\n", | ||
" for c in reversed(range(1, C + 1)):\n", | ||
" gradients['dW' + str(c)] = 1/m * np.dot(dZ, activations['A' + str(c - 1)].T)\n", | ||
" gradients['db' + str(c)] = 1/m * np.sum(dZ, axis=1, keepdims=True)\n", | ||
" if c > 1:\n", | ||
" dZ = np.dot(parametres['W' + str(c)].T, dZ) * activations['A' + str(c - 1)] * (1 - activations['A' + str(c - 1)])\n", | ||
"\n", | ||
" return gradients" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"ename": "", | ||
"evalue": "", | ||
"output_type": "error", | ||
"traceback": [ | ||
"\u001b[1;31mRunning cells with 'cmnemoi-learn-sm69zs_S-py3.11' requires the ipykernel package.\n", | ||
"\u001b[1;31mRun the following command to install 'ipykernel' into the Python environment. \n", | ||
"\u001b[1;31mCommand: '/Users/charles/Library/Caches/pypoetry/virtualenvs/cmnemoi-learn-sm69zs_S-py3.11/bin/python -m pip install ipykernel -U --force-reinstall'" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"def update(gradients, parametres, learning_rate):\n", | ||
"\n", | ||
" C = len(parametres) // 2\n", | ||
"\n", | ||
" for c in range(1, C + 1):\n", | ||
" parametres['W' + str(c)] = parametres['W' + str(c)] - learning_rate * gradients['dW' + str(c)]\n", | ||
" parametres['b' + str(c)] = parametres['b' + str(c)] - learning_rate * gradients['db' + str(c)]\n", | ||
"\n", | ||
" return parametres" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"ename": "", | ||
"evalue": "", | ||
"output_type": "error", | ||
"traceback": [ | ||
"\u001b[1;31mRunning cells with 'cmnemoi-learn-sm69zs_S-py3.11' requires the ipykernel package.\n", | ||
"\u001b[1;31mRun the following command to install 'ipykernel' into the Python environment. \n", | ||
"\u001b[1;31mCommand: '/Users/charles/Library/Caches/pypoetry/virtualenvs/cmnemoi-learn-sm69zs_S-py3.11/bin/python -m pip install ipykernel -U --force-reinstall'" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"def predict(X, parametres):\n", | ||
" activations = forward_propagation(X, parametres)\n", | ||
" C = len(parametres) // 2\n", | ||
" Af = activations['A' + str(C)]\n", | ||
" return Af >= 0.5" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"ename": "", | ||
"evalue": "", | ||
"output_type": "error", | ||
"traceback": [ | ||
"\u001b[1;31mRunning cells with 'cmnemoi-learn-sm69zs_S-py3.11' requires the ipykernel package.\n", | ||
"\u001b[1;31mRun the following command to install 'ipykernel' into the Python environment. \n", | ||
"\u001b[1;31mCommand: '/Users/charles/Library/Caches/pypoetry/virtualenvs/cmnemoi-learn-sm69zs_S-py3.11/bin/python -m pip install ipykernel -U --force-reinstall'" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"def deep_neural_network(X, y, hidden_layers = (16, 16, 16), learning_rate = 0.001, n_iter = 3000):\n", | ||
" \n", | ||
" # initialisation parametres\n", | ||
" dimensions = list(hidden_layers)\n", | ||
" dimensions.insert(0, X.shape[0])\n", | ||
" dimensions.append(y.shape[0])\n", | ||
" np.random.seed(1)\n", | ||
" parametres = initialisation(dimensions)\n", | ||
"\n", | ||
" # tableau numpy contenant les futures accuracy et log_loss\n", | ||
" training_history = np.zeros((int(n_iter), 2))\n", | ||
"\n", | ||
" C = len(parametres) // 2\n", | ||
"\n", | ||
" # gradient descent\n", | ||
" for i in tqdm(range(n_iter)):\n", | ||
"\n", | ||
" activations = forward_propagation(X, parametres)\n", | ||
" gradients = back_propagation(y, parametres, activations)\n", | ||
" parametres = update(gradients, parametres, learning_rate)\n", | ||
" Af = activations['A' + str(C)]\n", | ||
"\n", | ||
" # calcul du log_loss et de l'accuracy\n", | ||
" training_history[i, 0] = (log_loss(y.flatten(), Af.flatten()))\n", | ||
" y_pred = predict(X, parametres)\n", | ||
" training_history[i, 1] = (accuracy_score(y.flatten(), y_pred.flatten()))\n", | ||
"\n", | ||
" # Plot courbe d'apprentissage\n", | ||
" plt.figure(figsize=(12, 4))\n", | ||
" plt.subplot(1, 2, 1)\n", | ||
" plt.plot(training_history[:, 0], label='train loss')\n", | ||
" plt.legend()\n", | ||
" plt.subplot(1, 2, 2)\n", | ||
" plt.plot(training_history[:, 1], label='train acc')\n", | ||
" plt.legend()\n", | ||
" plt.show()\n", | ||
"\n", | ||
" return training_history" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Dataset" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"ename": "", | ||
"evalue": "", | ||
"output_type": "error", | ||
"traceback": [ | ||
"\u001b[1;31mRunning cells with 'cmnemoi-learn-sm69zs_S-py3.11' requires the ipykernel package.\n", | ||
"\u001b[1;31mRun the following command to install 'ipykernel' into the Python environment. \n", | ||
"\u001b[1;31mCommand: '/Users/charles/Library/Caches/pypoetry/virtualenvs/cmnemoi-learn-sm69zs_S-py3.11/bin/python -m pip install ipykernel -U --force-reinstall'" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"X, y = make_circles(n_samples=100, noise=0.1, factor=0.3, random_state=0)\n", | ||
"X = X.T\n", | ||
"y = y.reshape((1, y.shape[0]))\n", | ||
"\n", | ||
"print('dimensions de X:', X.shape)\n", | ||
"print('dimensions de y:', y.shape)\n", | ||
"\n", | ||
"plt.scatter(X[0, :], X[1, :], c=y, cmap='summer')\n", | ||
"plt.show()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"ename": "", | ||
"evalue": "", | ||
"output_type": "error", | ||
"traceback": [ | ||
"\u001b[1;31mRunning cells with 'cmnemoi-learn-sm69zs_S-py3.11' requires the ipykernel package.\n", | ||
"\u001b[1;31mRun the following command to install 'ipykernel' into the Python environment. \n", | ||
"\u001b[1;31mCommand: '/Users/charles/Library/Caches/pypoetry/virtualenvs/cmnemoi-learn-sm69zs_S-py3.11/bin/python -m pip install ipykernel -U --force-reinstall'" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"deep_neural_network(X, y, hidden_layers = (16, 16, 16), learning_rate = 0.1, n_iter = 3000)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"ename": "", | ||
"evalue": "", | ||
"output_type": "error", | ||
"traceback": [ | ||
"\u001b[1;31mRunning cells with 'cmnemoi-learn-sm69zs_S-py3.11' requires the ipykernel package.\n", | ||
"\u001b[1;31mRun the following command to install 'ipykernel' into the Python environment. \n", | ||
"\u001b[1;31mCommand: '/Users/charles/Library/Caches/pypoetry/virtualenvs/cmnemoi-learn-sm69zs_S-py3.11/bin/python -m pip install ipykernel -U --force-reinstall'" | ||
] | ||
} | ||
], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"interpreter": { | ||
"hash": "038c04557dfd72b4d6039cb7951b93ffe7520921b6515cb88d8784deedfaf89f" | ||
}, | ||
"kernelspec": { | ||
"display_name": "Python 3.7.9 ('base')", | ||
"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.11.4" | ||
}, | ||
"orig_nbformat": 4 | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.