add: TP2 (NN)

This commit is contained in:
2025-03-24 14:11:13 +01:00
parent bc64c7ddcc
commit 4853ad1d64

View File

@@ -0,0 +1,93 @@
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": [
"# Automatic Differentiation\n",
"\n",
"### Neural Network\n",
"\n",
"Loss function: softmax layer in $\\mathbb{R}^3$\n",
"\n",
"Architecture: FC/ReLU 4-5-7-3"
],
"id": "c897654e0a140cbd"
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-03-24T13:10:01.794956Z",
"start_time": "2025-03-24T13:09:58.777694Z"
}
},
"cell_type": "code",
"source": [
"\n",
"import numpy as np\n",
"from sklearn.neural_network import MLPClassifier\n",
"from sklearn.datasets import make_classification\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.metrics import accuracy_score\n",
"\n",
"accuracies = []\n",
"\n",
"for _ in range(10):\n",
" X, y = make_classification(n_samples=1000, n_features=4, n_classes=3, n_clusters_per_class=1)\n",
"\n",
" X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\n",
" model = MLPClassifier(hidden_layer_sizes=(5, 7), activation='relu', max_iter=10000, solver='adam')\n",
" model.fit(X_train, y_train)\n",
"\n",
" y_pred = model.predict(X_test)\n",
" accuracies.append(accuracy_score(y_test, y_pred))\n",
"\n",
"print(f'Accuracy: {np.mean(accuracies) * 100:.0f}%')\n",
"print(f\"Max accuracy: {np.max(accuracies) * 100:.0f}%\")\n",
"print(f\"Min accuracy: {np.min(accuracies) * 100:.0f}%\")"
],
"id": "70a4eb1d928b10d0",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Accuracy: 93%\n",
"Max accuracy: 100%\n",
"Min accuracy: 80%\n"
]
}
],
"execution_count": 21
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": "",
"id": "96b6d46883ed5570"
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}