diff --git a/figures/bias_variance_tradeoff.pdf b/figures/bias_variance_tradeoff.pdf index 0cd3e4c..d0b5b3f 100644 Binary files a/figures/bias_variance_tradeoff.pdf and b/figures/bias_variance_tradeoff.pdf differ diff --git a/figures/bias_variance_tradeoff_combined_plot.pdf b/figures/bias_variance_tradeoff_combined_plot.pdf index 39569e4..da33eeb 100644 Binary files a/figures/bias_variance_tradeoff_combined_plot.pdf and b/figures/bias_variance_tradeoff_combined_plot.pdf differ diff --git a/figures/cost_function_comparison.pdf b/figures/cost_function_comparison.pdf index e92df9c..a3961b8 100644 Binary files a/figures/cost_function_comparison.pdf and b/figures/cost_function_comparison.pdf differ diff --git a/figures/data_scatter.png b/figures/data_scatter.png index 28a6677..1ab7c52 100644 Binary files a/figures/data_scatter.png and b/figures/data_scatter.png differ diff --git a/figures/gradient_descent_convergence.pdf b/figures/gradient_descent_convergence.pdf index 6913623..00a78f5 100644 Binary files a/figures/gradient_descent_convergence.pdf and b/figures/gradient_descent_convergence.pdf differ diff --git a/figures/kfold_mse_comparison_per_cost_function.pdf b/figures/kfold_mse_comparison_per_cost_function.pdf index 0f69870..d4d741b 100644 Binary files a/figures/kfold_mse_comparison_per_cost_function.pdf and b/figures/kfold_mse_comparison_per_cost_function.pdf differ diff --git a/figures/ols_mse_r2.pdf b/figures/ols_mse_r2.pdf index ec853fd..f86bf9f 100644 Binary files a/figures/ols_mse_r2.pdf and b/figures/ols_mse_r2.pdf differ diff --git a/figures/ols_parameter_plot.pdf b/figures/ols_parameter_plot.pdf index a42d3c7..31b4571 100644 Binary files a/figures/ols_parameter_plot.pdf and b/figures/ols_parameter_plot.pdf differ diff --git a/figures/optimization_performance.pdf b/figures/optimization_performance.pdf index d8bd6e8..f5a7b8c 100644 Binary files a/figures/optimization_performance.pdf and b/figures/optimization_performance.pdf differ diff --git a/figures/optimizer_comparison.pdf b/figures/optimizer_comparison.pdf index 1fad5b5..67a89a9 100644 Binary files a/figures/optimizer_comparison.pdf and b/figures/optimizer_comparison.pdf differ diff --git a/figures/ridge_mse_heatmap.pdf b/figures/ridge_mse_heatmap.pdf index 735ebd0..4586791 100644 Binary files a/figures/ridge_mse_heatmap.pdf and b/figures/ridge_mse_heatmap.pdf differ diff --git a/figures/ridge_mse_r2.pdf b/figures/ridge_mse_r2.pdf index bfeec55..014c161 100644 Binary files a/figures/ridge_mse_r2.pdf and b/figures/ridge_mse_r2.pdf differ diff --git a/figures/ridge_mse_r2_lambda.pdf b/figures/ridge_mse_r2_lambda.pdf index 0d5bcbb..981f7b3 100644 Binary files a/figures/ridge_mse_r2_lambda.pdf and b/figures/ridge_mse_r2_lambda.pdf differ diff --git a/figures/ridge_parameter_plot.pdf b/figures/ridge_parameter_plot.pdf index 3a2b22a..570baaf 100644 Binary files a/figures/ridge_parameter_plot.pdf and b/figures/ridge_parameter_plot.pdf differ diff --git a/figures/stochastic_gradient_descent_convergence.pdf b/figures/stochastic_gradient_descent_convergence.pdf index b577b8a..4610363 100644 Binary files a/figures/stochastic_gradient_descent_convergence.pdf and b/figures/stochastic_gradient_descent_convergence.pdf differ diff --git a/src/main.ipynb b/src/main.ipynb index 37dd488..edff716 100644 --- a/src/main.ipynb +++ b/src/main.ipynb @@ -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\"))" ] }, diff --git a/src/pyoptim/plotting.py b/src/pyoptim/plotting.py index 8aedc1d..592dd6a 100644 --- a/src/pyoptim/plotting.py +++ b/src/pyoptim/plotting.py @@ -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