Refactor code formatting and improve readability in Jupyter notebooks for TP_4 and TP_5

- Adjusted indentation and line breaks for better clarity in function definitions and import statements.
- Standardized string quotes for consistency across the codebase.
- Enhanced readability of DataFrame creation and manipulation by breaking long lines into multiple lines.
- Cleaned up print statements and comments for improved understanding.
- Ensured consistent use of whitespace around operators and after commas.
This commit is contained in:
2025-11-25 10:46:16 +01:00
parent 21e376de79
commit 8400c722a5
17 changed files with 11975 additions and 11713 deletions

View File

@@ -24,20 +24,30 @@
"import matplotlib.pyplot as plt\n",
"import seaborn as sns\n",
"\n",
"sns.set(style='whitegrid')\n",
"sns.set(style=\"whitegrid\")\n",
"\n",
"import tensorflow as tf\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.preprocessing import StandardScaler\n",
"from tensorflow import keras\n",
"\n",
"(X_train_full, y_train_full), (X_test, y_test) = (keras.datasets.fashion_mnist.load_data())\n",
"X_train, X_valid, y_train, y_valid = train_test_split(X_train_full, y_train_full, train_size=0.8)\n",
"(X_train_full, y_train_full), (X_test, y_test) = (\n",
" keras.datasets.fashion_mnist.load_data()\n",
")\n",
"X_train, X_valid, y_train, y_valid = train_test_split(\n",
" X_train_full, y_train_full, train_size=0.8\n",
")\n",
"\n",
"scaler = StandardScaler()\n",
"X_train = scaler.fit_transform(X_train.astype(np.float32).reshape(-1, 28 * 28)).reshape(-1, 28, 28, 1)\n",
"X_valid = scaler.transform(X_valid.astype(np.float32).reshape(-1, 28 * 28)).reshape(-1, 28, 28, 1)\n",
"X_test = scaler.transform(X_test.astype(np.float32).reshape(-1, 28 * 28)).reshape(-1, 28, 28, 1)"
"X_train = scaler.fit_transform(X_train.astype(np.float32).reshape(-1, 28 * 28)).reshape(\n",
" -1, 28, 28, 1\n",
")\n",
"X_valid = scaler.transform(X_valid.astype(np.float32).reshape(-1, 28 * 28)).reshape(\n",
" -1, 28, 28, 1\n",
")\n",
"X_test = scaler.transform(X_test.astype(np.float32).reshape(-1, 28 * 28)).reshape(\n",
" -1, 28, 28, 1\n",
")"
]
},
{

View File

@@ -26,11 +26,13 @@
"import matplotlib.pyplot as plt\n",
"import seaborn as sns\n",
"\n",
"sns.set(style='whitegrid')\n",
"sns.set(style=\"whitegrid\")\n",
"\n",
"from tensorflow import keras\n",
"\n",
"(X_train_full, y_train_full), (X_test, y_test) = (keras.datasets.fashion_mnist.load_data())"
"(X_train_full, y_train_full), (X_test, y_test) = (\n",
" keras.datasets.fashion_mnist.load_data()\n",
")"
]
},
{
@@ -186,7 +188,7 @@
" keras.layers.Dense(units=64, activation=\"relu\"),\n",
" keras.layers.Dense(units=10, activation=\"softmax\"),\n",
" ]\n",
")\n"
")"
]
},
{
@@ -627,10 +629,7 @@
" batch_size=batch_size,\n",
" validation_data=(X_valid, y_valid),\n",
" )\n",
" training_curves.append({\n",
" 'history': history,\n",
" 'normalization': normalized\n",
" })"
" training_curves.append({\"history\": history, \"normalization\": normalized})"
]
},
{
@@ -653,7 +652,9 @@
"metadata": {},
"outputs": [],
"source": [
"def agregate_result(results: list, normalized: bool, metric_name: str = 'accuracy') -> pd.DataFrame:\n",
"def agregate_result(\n",
" results: list, normalized: bool, metric_name: str = \"accuracy\"\n",
") -> pd.DataFrame:\n",
" train_curves = []\n",
" val_curves = []\n",
"\n",
@@ -663,7 +664,7 @@
" train_curves.append(hist_obj.history[metric_name])\n",
" val_curves.append(hist_obj.history[f\"val_{metric_name}\"])\n",
"\n",
" return np.array(train_curves).flatten(), np.array(val_curves).flatten()\n"
" return np.array(train_curves).flatten(), np.array(val_curves).flatten()"
]
},
{
@@ -697,7 +698,9 @@
"for idx, metric in enumerate(metrics):\n",
" ax = axs[idx]\n",
" for normalized in [True, False]:\n",
" train, val = agregate_result(training_curves, normalized=normalized, metric_name=metric)\n",
" train, val = agregate_result(\n",
" training_curves, normalized=normalized, metric_name=metric\n",
" )\n",
" train_runs = train.reshape(-1, epochs)\n",
" val_runs = val.reshape(-1, epochs)\n",
"\n",
@@ -710,10 +713,22 @@
" label_prefix = \"With BN\" if normalized else \"Without BN\"\n",
"\n",
" ax.plot(mean_train, label=label_prefix, color=color, linestyle=\"-\")\n",
" ax.fill_between(range(epochs), mean_train - std_train, mean_train + std_train, color=color, alpha=0.2)\n",
" ax.fill_between(\n",
" range(epochs),\n",
" mean_train - std_train,\n",
" mean_train + std_train,\n",
" color=color,\n",
" alpha=0.2,\n",
" )\n",
"\n",
" ax.plot(mean_val, color=color, linestyle=\"--\")\n",
" ax.fill_between(range(epochs), mean_val - std_val, mean_val + std_val, color=color, alpha=0.2)\n",
" ax.fill_between(\n",
" range(epochs),\n",
" mean_val - std_val,\n",
" mean_val + std_val,\n",
" color=color,\n",
" alpha=0.2,\n",
" )\n",
"\n",
" ax.set_title(f\"Training and Validation {metric.capitalize()}\")\n",
" ax.set_xlabel(\"Epochs\")\n",
@@ -721,7 +736,7 @@
" ax.legend()\n",
"\n",
"plt.tight_layout()\n",
"plt.show()\n"
"plt.show()"
]
},
{