Cherry pick pretty plots

This commit is contained in:
2025-09-11 20:02:41 +02:00
parent 977059e8c2
commit 1eaa75a9cf
17 changed files with 37 additions and 29 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

Before

Width:  |  Height:  |  Size: 581 KiB

After

Width:  |  Height:  |  Size: 111 KiB

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+36 -24
View File
@@ -53,6 +53,7 @@
"x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)\n",
"\n",
"fig, ax = plotting.scatter_dataset(x_train, x_test, y_train, y_test)\n",
"fig.set_layout_engine(\"compressed\")\n",
"fig.savefig(os.path.join(FIG_DIR, \"data_scatter.png\"), dpi=300)"
]
},
@@ -101,6 +102,7 @@
"fig, ax = plotting.mse_r2_plot(\n",
" polynomial_degrees, train_mse_list, mse_list, train_r2_list, r2_list\n",
")\n",
"fig.set_layout_engine(\"compressed\")\n",
"fig.savefig(os.path.join(FIG_DIR, \"ols_mse_r2.pdf\"))"
]
},
@@ -112,6 +114,7 @@
"outputs": [],
"source": [
"fig, ax = plotting.parameter_plot(polynomial_degrees, beta_OLS_list)\n",
"# fig.set_layout_engine(\"compressed\")\n",
"fig.savefig(os.path.join(FIG_DIR, \"ols_parameter_plot.pdf\"))"
]
},
@@ -165,6 +168,7 @@
" train_r2_ridge_list,\n",
" r2_ridge_list,\n",
")\n",
"fig.set_layout_engine(\"compressed\")\n",
"fig.savefig(os.path.join(FIG_DIR, \"ridge_mse_r2.pdf\"))"
]
},
@@ -222,6 +226,7 @@
")\n",
"for ax in axs:\n",
" ax.set_xscale(\"log\")\n",
"fig.set_layout_engine(\"compressed\")\n",
"fig.savefig(os.path.join(FIG_DIR, \"ridge_mse_r2_lambda.pdf\"))"
]
},
@@ -249,6 +254,7 @@
"ax.set_xticklabels(\n",
" [f\"${format_number(tick)}$\" for tick in lambda_values[::3]], rotation=45\n",
")\n",
"\n",
"fig.savefig(os.path.join(FIG_DIR, \"ridge_parameter_plot.pdf\"))"
]
},
@@ -260,7 +266,7 @@
"outputs": [],
"source": [
"lambda_values = np.logspace(-5, 3, 60)\n",
"polynomial_degrees = np.arange(1, 31, dtype=int)\n",
"polynomial_degrees = np.arange(3, 20, dtype=int)\n",
"\n",
"test_mse_ridge_list = np.zeros((len(polynomial_degrees), len(lambda_values)))\n",
"\n",
@@ -312,7 +318,7 @@
"X_tr, X_te = datamanip.scale_data(X_train, X_test)\n",
"y_tr, y_te = datamanip.scale_data(y_train, y_test)\n",
"\n",
"fig, (ax1, ax2) = plt.subplots(1, 2, figsize=plotting.get_figsize(0.5))\n",
"fig, (ax1, ax2) = plt.subplots(2, 1, figsize=plotting.get_figsize(1.0))\n",
"num_iters = 1_000\n",
"\n",
"learning_rates = np.logspace(-4, 0, 5)\n",
@@ -339,7 +345,7 @@
"plotting.plot_optimizers(ax2, ridge_optimizers, X_tr, y_tr, \"Ridge Cost\", labels=labels)\n",
"ax1.set_ylim(bottom=0.465, top=0.505)\n",
"ax2.set_ylim(bottom=0.4775, top=0.505)\n",
"fig.tight_layout()\n",
"fig.set_layout_engine(\"compressed\")\n",
"fig.savefig(os.path.join(FIG_DIR, \"gradient_descent_convergence.pdf\"))"
]
},
@@ -350,7 +356,7 @@
"metadata": {},
"outputs": [],
"source": [
"fig, (ax1, ax2) = plt.subplots(2, 1, figsize=plotting.get_figsize(0.8))\n",
"fig, (ax1, ax2) = plt.subplots(2, 1, figsize=plotting.get_figsize(1.0))\n",
"\n",
"X_train = datamanip.polynomial_features(x_train, 10, False)\n",
"X_test = datamanip.polynomial_features(x_test, 10, False)\n",
@@ -393,7 +399,7 @@
"\n",
"ax1.set_ylim(bottom=0.465, top=0.505)\n",
"ax2.set_ylim(bottom=0.4775, top=0.505)\n",
"fig.tight_layout()\n",
"fig.set_layout_engine(\"compressed\")\n",
"fig.savefig(os.path.join(FIG_DIR, \"optimizer_comparison.pdf\"))"
]
},
@@ -404,7 +410,7 @@
"metadata": {},
"outputs": [],
"source": [
"fig, ax = plt.subplots(figsize=plotting.get_figsize(0.5))\n",
"fig, ax = plt.subplots(figsize=plotting.get_figsize(0.6))\n",
"\n",
"ols_adam = optimizers.OLSAdam(learning_rate=0.01, beta1=0.9, beta2=0.999)\n",
"ridge_adam = optimizers.RidgeAdam(learning_rate=0.01, beta1=0.9, beta2=0.999, lam=0.1)\n",
@@ -435,7 +441,7 @@
"]\n",
"labels += [\"Adam Opt.\", \"Grad. Desc.\"]\n",
"ax.legend(handles, labels)\n",
"fig.tight_layout()\n",
"fig.set_layout_engine(\"compressed\")\n",
"fig.savefig(os.path.join(FIG_DIR, \"cost_function_comparison.pdf\"))"
]
},
@@ -488,7 +494,7 @@
"fig, ax = plotting.optimization_performance_evaluation(\n",
" optimizer_list, labels, X_tr, y_tr\n",
")\n",
"fig.tight_layout()\n",
"fig.set_layout_engine(\"compressed\")\n",
"fig.savefig(os.path.join(FIG_DIR, \"optimization_performance.pdf\"))"
]
},
@@ -500,7 +506,7 @@
"outputs": [],
"source": [
"X_size = 1_000_000\n",
"num_epochs = 1000\n",
"num_epochs = 500\n",
"batches_per_epoch = 100\n",
"batch_size = 128\n",
"\n",
@@ -566,23 +572,27 @@
"\n",
"labels = [\"SGD\", \"Mom. SGD\", \"AdaGrad\", \"RMSProp\", \"Adam\"]\n",
"\n",
"fig, axs = plt.subplots(1, 3, figsize=plotting.get_figsize(0.5), sharey=True)\n",
"plotting.plot_optimizers(\n",
" axs[0], optimizers_ols, X_tr, y_tr, ylabel=\"Average Cost per Epoch\", labels=labels\n",
"fig, axs = plt.subplots(\n",
" 3, 1, figsize=plotting.get_figsize(1.0, full_width=False), sharex=True\n",
")\n",
"plotting.plot_optimizers(\n",
" axs[1], optimizers_ridge, X_tr, y_tr, ylabel=None, labels=labels\n",
" axs[0], optimizers_ols, X_tr, y_tr, ylabel=\"OLS Cost\", labels=labels\n",
")\n",
"plotting.plot_optimizers(\n",
" axs[2], optimizers_lasso, X_tr, y_tr, ylabel=None, labels=labels\n",
" axs[1], optimizers_ridge, X_tr, y_tr, ylabel=\"Ridge Cost\", labels=labels\n",
")\n",
"plotting.plot_optimizers(\n",
" axs[2], optimizers_lasso, X_tr, y_tr, ylabel=\"LASSO Cost\", labels=labels\n",
")\n",
"for ax in axs:\n",
" ax.set_xlabel(\"Epoch\")\n",
" ax.set_xlabel(None)\n",
"\n",
"axs[0].set_title(\"OLS\")\n",
"axs[1].set_title(\"Ridge\")\n",
"axs[2].set_title(\"LASSO\")\n",
"fig.tight_layout()\n",
"axs[2].set_xlabel(\"Epoch\")\n",
"\n",
"# axs[0].set_title(\"OLS\")\n",
"# axs[1].set_title(\"Ridge\")\n",
"# axs[2].set_title(\"LASSO\")\n",
"fig.set_layout_engine(\"compressed\")\n",
"fig.savefig(os.path.join(FIG_DIR, \"stochastic_gradient_descent_convergence.pdf\"))"
]
},
@@ -674,7 +684,7 @@
"ax.set_ylabel(\"Error\")\n",
"ax.legend()\n",
"\n",
"fig.tight_layout()\n",
"fig.set_layout_engine(\"compressed\")\n",
"fig.savefig(os.path.join(FIG_DIR, \"bias_variance_tradeoff.pdf\"))"
]
},
@@ -709,7 +719,9 @@
"metadata": {},
"outputs": [],
"source": [
"fig, (ax, ax2) = plt.subplots(1, 2, figsize=plotting.get_figsize(0.5), sharey=True)\n",
"fig, (ax, ax2) = plt.subplots(\n",
" 1, 2, figsize=plotting.get_figsize(0.35, True), sharey=True\n",
")\n",
"mse_mean = np.mean(mses, axis=1)\n",
"bias_mean = np.mean(biases, axis=1)\n",
"var_mean = np.mean(variances, axis=1)\n",
@@ -768,7 +780,7 @@
"ax2.set_xlabel(\"Polynomial Degree\")\n",
"# ax2.set_ylabel(\"Mean Squared Error\")\n",
"ax2.legend()\n",
"fig.tight_layout()\n",
"fig.set_layout_engine(\"compressed\")\n",
"fig.savefig(os.path.join(FIG_DIR, \"bias_variance_tradeoff_combined_plot.pdf\"))"
]
},
@@ -838,9 +850,9 @@
" )\n",
"\n",
"ax.set_xlabel(\"Polynomial Degree\")\n",
"ax.set_ylabel(f\"MSE ({k_folds}-fold validation)\")\n",
"ax.set_ylabel(f\"MSE ({k_folds}-fold)\")\n",
"ax.legend()\n",
"fig.tight_layout()\n",
"fig.set_layout_engine(\"compressed\")\n",
"fig.savefig(os.path.join(FIG_DIR, \"kfold_mse_comparison_per_cost_function.pdf\"))"
]
},
+1 -5
View File
@@ -95,7 +95,6 @@ def mse_r2_plot(
ax2.set_xlabel(labels["xlabel"])
ax2.set_ylabel(labels["ylabel2"])
fig.tight_layout()
return fig, (ax1, ax2)
@@ -136,7 +135,6 @@ def parameter_plot(
ax.set_xticklabels(polynomial_degrees[::3])
ax.set_yticks(np.arange(0, beta_OLS.shape[0], 5))
ax.set_yticklabels(np.arange(1, beta_OLS.shape[0] + 1, step=5))
fig.tight_layout()
return fig, ax
@@ -149,7 +147,6 @@ def scatter_dataset(
ax.set_xlabel("$x$")
ax.set_ylabel("$y$")
ax.legend()
fig.tight_layout()
return fig, ax
@@ -188,7 +185,7 @@ def optimization_performance_evaluation(
X_tr: np.ndarray,
y_tr: np.ndarray,
):
fig, ax = plt.subplots(figsize=get_figsize(0.5))
fig, ax = plt.subplots(figsize=get_figsize(0.6))
for optimizer, label in zip(optimizers, labels):
start_time = time.time()
@@ -215,5 +212,4 @@ def optimization_performance_evaluation(
ax.set_xlabel("Epoch")
ax.set_ylabel("Cost")
ax.legend()
fig.tight_layout()
return fig, ax