Run ruff
This commit is contained in:
@@ -586,8 +586,10 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"data = pd.DataFrame(load_breast_cancer().data, columns=load_breast_cancer().feature_names)\n",
|
||||
"data['target'] = load_breast_cancer().target\n",
|
||||
"data = pd.DataFrame(\n",
|
||||
" load_breast_cancer().data, columns=load_breast_cancer().feature_names\n",
|
||||
")\n",
|
||||
"data[\"target\"] = load_breast_cancer().target\n",
|
||||
"data.head()"
|
||||
]
|
||||
},
|
||||
@@ -619,7 +621,7 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"ax = sns.countplot(x='target', data=data)\n",
|
||||
"ax = sns.countplot(x=\"target\", data=data)\n",
|
||||
"ax.set_title(\"Type of Tumors (Malignant/Benign)\")"
|
||||
]
|
||||
},
|
||||
@@ -683,14 +685,16 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = data.melt(id_vars='target', var_name='feature', value_name='value')\n",
|
||||
"df = data.melt(id_vars=\"target\", var_name=\"feature\", value_name=\"value\")\n",
|
||||
"\n",
|
||||
"GROUP_SIZE = 10\n",
|
||||
"for start in range(0, len(load_breast_cancer().feature_names), GROUP_SIZE):\n",
|
||||
" end = start + GROUP_SIZE\n",
|
||||
" subset = df[df['feature'].isin(load_breast_cancer().feature_names[start:end])]\n",
|
||||
" sns.violinplot(x='feature', y='value', hue='target', data=subset, split=True, inner='quart')\n",
|
||||
" plt.xticks(rotation=45, ha='right')\n",
|
||||
" subset = df[df[\"feature\"].isin(load_breast_cancer().feature_names[start:end])]\n",
|
||||
" sns.violinplot(\n",
|
||||
" x=\"feature\", y=\"value\", hue=\"target\", data=subset, split=True, inner=\"quart\"\n",
|
||||
" )\n",
|
||||
" plt.xticks(rotation=45, ha=\"right\")\n",
|
||||
" plt.tight_layout()\n",
|
||||
" plt.savefig(f\"violin_plot_features_{start}.pdf\")\n",
|
||||
"\n",
|
||||
|
||||
@@ -7,7 +7,16 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from easynn.feedforward import Layer, ReLU, Regularization, Linear, FFNN, MSELoss, Softmax, CrossEntropyLoss\n",
|
||||
"from easynn.feedforward import (\n",
|
||||
" Layer,\n",
|
||||
" ReLU,\n",
|
||||
" Regularization,\n",
|
||||
" Linear,\n",
|
||||
" FFNN,\n",
|
||||
" MSELoss,\n",
|
||||
" Softmax,\n",
|
||||
" CrossEntropyLoss,\n",
|
||||
")\n",
|
||||
"from easynn.schedulers import AdamScheduler\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
@@ -39,7 +48,7 @@
|
||||
"X = StandardScaler().fit_transform(X)\n",
|
||||
"y = y.reshape(-1, 1)\n",
|
||||
"y = StandardScaler().fit_transform(y)\n",
|
||||
"X_train, X_test, y_train, y_test = train_test_split(X, y)\n"
|
||||
"X_train, X_test, y_train, y_test = train_test_split(X, y)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -49,7 +58,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"layers = [Layer(5, 5, ReLU()), Layer(5, 25, ReLU()), Layer(25, 1, Linear())]\n",
|
||||
"layers = [Layer(5, 5, ReLU()), Layer(5, 25, ReLU()), Layer(25, 1, Linear())]\n",
|
||||
"network = FFNN(layers, AdamScheduler(learning_rate=0.001, epochs=1000), MSELoss())\n",
|
||||
"network.fit(X_train, y_train)\n",
|
||||
"predictions = network.predict(X_test)"
|
||||
@@ -119,7 +128,7 @@
|
||||
],
|
||||
"source": [
|
||||
"plt.scatter(y_test, predictions)\n",
|
||||
"plt.plot([y_test.min(), y_test.max()], [y_test.min(), y_test.max()], 'k--', lw=2)\n",
|
||||
"plt.plot([y_test.min(), y_test.max()], [y_test.min(), y_test.max()], \"k--\", lw=2)\n",
|
||||
"plt.xlabel(\"True Values\")\n",
|
||||
"plt.ylabel(\"Predictions\")\n",
|
||||
"plt.title(\"True vs Predicted Values\")"
|
||||
@@ -165,7 +174,7 @@
|
||||
"X = StandardScaler().fit_transform(X)\n",
|
||||
"y = y.reshape(-1, 1)\n",
|
||||
"y = OneHotEncoder(sparse_output=False).fit_transform(y)\n",
|
||||
"X_train, X_test, y_train, y_test = train_test_split(X, y)\n"
|
||||
"X_train, X_test, y_train, y_test = train_test_split(X, y)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -175,11 +184,15 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"layers = [Layer(5, 10, ReLU()), Layer(10, 5, ReLU()), Layer(5, 2, Softmax())]\n",
|
||||
"network = FFNN(layers, AdamScheduler(learning_rate=0.001, epochs=1000), CrossEntropyLoss())\n",
|
||||
"layers = [Layer(5, 10, ReLU()), Layer(10, 5, ReLU()), Layer(5, 2, Softmax())]\n",
|
||||
"network = FFNN(\n",
|
||||
" layers, AdamScheduler(learning_rate=0.001, epochs=1000), CrossEntropyLoss()\n",
|
||||
")\n",
|
||||
"network.fit(X_train, y_train)\n",
|
||||
"predictions = network.predict(X_test)\n",
|
||||
"single_class_predictions = OneHotEncoder(sparse_output=False).fit_transform(np.argmax(predictions, axis=1).reshape(-1, 1))\n"
|
||||
"single_class_predictions = OneHotEncoder(sparse_output=False).fit_transform(\n",
|
||||
" np.argmax(predictions, axis=1).reshape(-1, 1)\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -17,11 +17,18 @@
|
||||
"data = load_breast_cancer()\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"feature_names = [n for n in data.feature_names if 'radius' in n or 'area' in n]\n",
|
||||
"feature_names = [n for n in data.feature_names if \"radius\" in n or \"area\" in n]\n",
|
||||
"\n",
|
||||
"X = data.data[:, [data.feature_names.tolist().index(n) for n in feature_names]]\n",
|
||||
"y = data.data[:, [data.feature_names.tolist().index(n) for n in data.feature_names if n not in feature_names]]\n",
|
||||
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\n"
|
||||
"y = data.data[\n",
|
||||
" :,\n",
|
||||
" [\n",
|
||||
" data.feature_names.tolist().index(n)\n",
|
||||
" for n in data.feature_names\n",
|
||||
" if n not in feature_names\n",
|
||||
" ],\n",
|
||||
"]\n",
|
||||
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -31,7 +38,15 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from easynn.feedforward import Layer, FFNN, ReLU, LeakyReLU, Linear, MSELoss, Regularization\n",
|
||||
"from easynn.feedforward import (\n",
|
||||
" Layer,\n",
|
||||
" FFNN,\n",
|
||||
" ReLU,\n",
|
||||
" LeakyReLU,\n",
|
||||
" Linear,\n",
|
||||
" MSELoss,\n",
|
||||
" Regularization,\n",
|
||||
")\n",
|
||||
"from easynn.schedulers import AdamScheduler"
|
||||
]
|
||||
},
|
||||
@@ -44,14 +59,17 @@
|
||||
"source": [
|
||||
"feature_dim = X.shape[1]\n",
|
||||
"target_dim = y.shape[1]\n",
|
||||
"def get_regression_model(n_hidden_layers: int, n_neurons: int, activation: type=ReLU) -> list[Layer]:\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def get_regression_model(\n",
|
||||
" n_hidden_layers: int, n_neurons: int, activation: type = ReLU\n",
|
||||
") -> list[Layer]:\n",
|
||||
" layers = []\n",
|
||||
" layers.append(Layer(feature_dim, n_neurons, activation_function=activation()))\n",
|
||||
" for _ in range(n_hidden_layers - 1):\n",
|
||||
" layers.append(Layer(n_neurons, n_neurons, activation_function=activation()))\n",
|
||||
" layers.append(Layer(n_neurons, target_dim, activation_function=Linear()))\n",
|
||||
" return layers\n",
|
||||
"\n"
|
||||
" return layers"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -63,7 +81,11 @@
|
||||
"source": [
|
||||
"# First network test\n",
|
||||
"\n",
|
||||
"model = FFNN(get_regression_model(n_hidden_layers=3, n_neurons=32, activation=LeakyReLU), AdamScheduler(epochs=10000, learning_rate=1e-2), MSELoss())\n",
|
||||
"model = FFNN(\n",
|
||||
" get_regression_model(n_hidden_layers=3, n_neurons=32, activation=LeakyReLU),\n",
|
||||
" AdamScheduler(epochs=10000, learning_rate=1e-2),\n",
|
||||
" MSELoss(),\n",
|
||||
")\n",
|
||||
"model.fit(X_train, y_train)"
|
||||
]
|
||||
},
|
||||
@@ -100,10 +122,14 @@
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"\n",
|
||||
"y_pred = model.predict(X_test)\n",
|
||||
"mse = mean_squared_error(y_test, y_pred, multioutput='raw_values')\n",
|
||||
"mse = mean_squared_error(y_test, y_pred, multioutput=\"raw_values\")\n",
|
||||
"\n",
|
||||
"sns.barplot(x=np.arange(len(mse)), y=mse)\n",
|
||||
"plt.xticks(ticks=np.arange(len(mse)), labels=[n for n in data.feature_names if n not in feature_names], rotation=90)\n",
|
||||
"plt.xticks(\n",
|
||||
" ticks=np.arange(len(mse)),\n",
|
||||
" labels=[n for n in data.feature_names if n not in feature_names],\n",
|
||||
" rotation=90,\n",
|
||||
")\n",
|
||||
"plt.yscale(\"log\")\n",
|
||||
"plt.ylabel(\"Mean Squared Error\")"
|
||||
]
|
||||
@@ -131,15 +157,23 @@
|
||||
"n_neurons_list = [4, 8, 16, 32, 64, 128]\n",
|
||||
"mse_results = np.zeros((len(n_layers_list), len(n_neurons_list)))\n",
|
||||
"for idx, (n_layers, n_neurons) in enumerate(\n",
|
||||
" tqdm(product(n_layers_list, n_neurons_list),\n",
|
||||
" total=len(n_layers_list) * len(n_neurons_list))\n",
|
||||
" tqdm(\n",
|
||||
" product(n_layers_list, n_neurons_list),\n",
|
||||
" total=len(n_layers_list) * len(n_neurons_list),\n",
|
||||
" )\n",
|
||||
"):\n",
|
||||
" i, j = divmod(idx, len(n_neurons_list))\n",
|
||||
" model = FFNN(get_regression_model(n_hidden_layers=n_layers, n_neurons=n_neurons, activation=LeakyReLU), AdamScheduler(epochs=15000, learning_rate=1e-3), MSELoss())\n",
|
||||
" model = FFNN(\n",
|
||||
" get_regression_model(\n",
|
||||
" n_hidden_layers=n_layers, n_neurons=n_neurons, activation=LeakyReLU\n",
|
||||
" ),\n",
|
||||
" AdamScheduler(epochs=15000, learning_rate=1e-3),\n",
|
||||
" MSELoss(),\n",
|
||||
" )\n",
|
||||
" model.fit(X_train, y_train)\n",
|
||||
" y_pred = model.predict(X_test)\n",
|
||||
" mse = mean_squared_error(y_test, y_pred)\n",
|
||||
" mse_results[i, j] = mse\n"
|
||||
" mse_results[i, j] = mse"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -160,10 +194,16 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"sns.heatmap(mse_results, xticklabels=n_neurons_list, yticklabels=n_layers_list, annot=True, fmt=\".2f\")\n",
|
||||
"sns.heatmap(\n",
|
||||
" mse_results,\n",
|
||||
" xticklabels=n_neurons_list,\n",
|
||||
" yticklabels=n_layers_list,\n",
|
||||
" annot=True,\n",
|
||||
" fmt=\".2f\",\n",
|
||||
")\n",
|
||||
"plt.xlabel(\"Number of Neurons\")\n",
|
||||
"plt.ylabel(\"Number of Layers\")\n",
|
||||
"plt.savefig(\"nodenumber_tuning_regression.pdf\")\n"
|
||||
"plt.savefig(\"nodenumber_tuning_regression.pdf\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -177,10 +217,16 @@
|
||||
"parameters = [(1, 32), (1, 64), (2, 32), (2, 64), (3, 64), (3, 128)]\n",
|
||||
"mse_results_slow_scan = np.zeros((len(parameters), target_dim))\n",
|
||||
"for k, (n_layers, n_neurons) in enumerate(parameters):\n",
|
||||
" model = FFNN(get_regression_model(n_hidden_layers=n_layers, n_neurons=n_neurons, activation=LeakyReLU), AdamScheduler(epochs=20000, learning_rate=5e-4), MSELoss())\n",
|
||||
" model = FFNN(\n",
|
||||
" get_regression_model(\n",
|
||||
" n_hidden_layers=n_layers, n_neurons=n_neurons, activation=LeakyReLU\n",
|
||||
" ),\n",
|
||||
" AdamScheduler(epochs=20000, learning_rate=5e-4),\n",
|
||||
" MSELoss(),\n",
|
||||
" )\n",
|
||||
" model.fit(X_train, y_train)\n",
|
||||
" y_pred = model.predict(X_test)\n",
|
||||
" mse = mean_squared_error(y_test, y_pred, multioutput='raw_values')\n",
|
||||
" mse = mean_squared_error(y_test, y_pred, multioutput=\"raw_values\")\n",
|
||||
" mse_results_slow_scan[k, :] = mse"
|
||||
]
|
||||
},
|
||||
@@ -202,12 +248,16 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"sns.heatmap(np.log10(mse_results_slow_scan), xticklabels=[n for n in data.feature_names if n not in feature_names], yticklabels=[f\"L{l}_N{n}\" for l, n in parameters],)\n",
|
||||
"plt.xticks(rotation=45, ha='right')\n",
|
||||
"sns.heatmap(\n",
|
||||
" np.log10(mse_results_slow_scan),\n",
|
||||
" xticklabels=[n for n in data.feature_names if n not in feature_names],\n",
|
||||
" yticklabels=[f\"L{l}_N{n}\" for l, n in parameters],\n",
|
||||
")\n",
|
||||
"plt.xticks(rotation=45, ha=\"right\")\n",
|
||||
"plt.xlabel(\"Target Features\")\n",
|
||||
"plt.ylabel(\"Model (Layers_Neurons)\")\n",
|
||||
"plt.tight_layout()\n",
|
||||
"plt.savefig(\"regression_detailed_hyperparameter_scan.pdf\")\n"
|
||||
"plt.savefig(\"regression_detailed_hyperparameter_scan.pdf\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -241,7 +291,7 @@
|
||||
" x=log_mse.ravel(),\n",
|
||||
" y=np.repeat(np.arange(len(parameters)), log_mse.shape[1]),\n",
|
||||
" bins=[np.linspace(-5, 2, 15), len(parameters)],\n",
|
||||
" cmap=cmap\n",
|
||||
" cmap=cmap,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Label axes\n",
|
||||
@@ -266,7 +316,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"n_hidden_layers = 2\n",
|
||||
"n_neurons = 32\n"
|
||||
"n_neurons = 32"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -276,14 +326,15 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"\n",
|
||||
"# regularization test\n",
|
||||
"layers = get_regression_model(n_hidden_layers=n_hidden_layers, n_neurons=n_neurons, activation=LeakyReLU)\n",
|
||||
"layers = get_regression_model(\n",
|
||||
" n_hidden_layers=n_hidden_layers, n_neurons=n_neurons, activation=LeakyReLU\n",
|
||||
")\n",
|
||||
"regularization_strengths = [0.0, 1e-5, 1e-4, 1e-3, 1e-2]\n",
|
||||
"mse_results_reg = np.zeros(len(regularization_strengths))\n",
|
||||
"for i, reg_strength in enumerate(regularization_strengths):\n",
|
||||
" for layer in layers:\n",
|
||||
" layer.regularization = Regularization(reg_strength, 'l2')\n",
|
||||
" layer.regularization = Regularization(reg_strength, \"l2\")\n",
|
||||
" model = FFNN(layers, AdamScheduler(epochs=20000, learning_rate=1e-4), MSELoss())\n",
|
||||
" model.fit(X_train, y_train)\n",
|
||||
" y_pred = model.predict(X_test)\n",
|
||||
@@ -335,9 +386,11 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Check final model training\n",
|
||||
"layers = get_regression_model(n_hidden_layers=n_hidden_layers, n_neurons=n_neurons, activation=LeakyReLU)\n",
|
||||
"layers = get_regression_model(\n",
|
||||
" n_hidden_layers=n_hidden_layers, n_neurons=n_neurons, activation=LeakyReLU\n",
|
||||
")\n",
|
||||
"for layer in layers:\n",
|
||||
" layer.regularization = Regularization(reg_strength, 'l2')\n",
|
||||
" layer.regularization = Regularization(reg_strength, \"l2\")\n",
|
||||
"model = FFNN(layers, AdamScheduler(epochs=20000, learning_rate=1e-4), MSELoss())\n",
|
||||
"model.fit(X_train, y_train)"
|
||||
]
|
||||
@@ -361,7 +414,7 @@
|
||||
],
|
||||
"source": [
|
||||
"sns.lineplot(x=np.arange(20000), y=model.scheduler.loss_history)\n",
|
||||
"plt.yscale(\"log\")\n"
|
||||
"plt.yscale(\"log\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -383,10 +436,14 @@
|
||||
],
|
||||
"source": [
|
||||
"y_pred = model.predict(X_test)\n",
|
||||
"mse = mean_squared_error(y_test, y_pred, multioutput='raw_values')\n",
|
||||
"mse = mean_squared_error(y_test, y_pred, multioutput=\"raw_values\")\n",
|
||||
"\n",
|
||||
"sns.barplot(x=np.arange(len(mse)), y=mse)\n",
|
||||
"plt.xticks(ticks=np.arange(len(mse)), labels=[n for n in data.feature_names if n not in feature_names], rotation=90)\n",
|
||||
"plt.xticks(\n",
|
||||
" ticks=np.arange(len(mse)),\n",
|
||||
" labels=[n for n in data.feature_names if n not in feature_names],\n",
|
||||
" rotation=90,\n",
|
||||
")\n",
|
||||
"plt.yscale(\"log\")\n",
|
||||
"plt.ylabel(\"Mean Squared Error\")\n",
|
||||
"plt.tight_layout()\n",
|
||||
@@ -425,16 +482,23 @@
|
||||
"source": [
|
||||
"beta = optimizers.OLS_parameters(X_train, y_train)\n",
|
||||
"y_ols_pred = X_test @ beta\n",
|
||||
"mse_ols = mean_squared_error(y_test, y_ols_pred, multioutput='raw_values')\n",
|
||||
"mse_ols = mean_squared_error(y_test, y_ols_pred, multioutput=\"raw_values\")\n",
|
||||
"\n",
|
||||
"mse_df = pd.DataFrame({\n",
|
||||
" \"Feature\": [n for n in data.feature_names if n not in feature_names],\n",
|
||||
" \"Neural Network\": mse,\n",
|
||||
" \"OLS\": mse_ols\n",
|
||||
"})\n",
|
||||
"mse_df = pd.DataFrame(\n",
|
||||
" {\n",
|
||||
" \"Feature\": [n for n in data.feature_names if n not in feature_names],\n",
|
||||
" \"Neural Network\": mse,\n",
|
||||
" \"OLS\": mse_ols,\n",
|
||||
" }\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"sns.barplot(x=\"Feature\", y=\"value\", hue=\"Model\", data=pd.melt(mse_df, id_vars=[\"Feature\"]).rename(columns={\"variable\": \"Model\"}))\n",
|
||||
"plt.xticks(rotation=45, ha='right')\n",
|
||||
"sns.barplot(\n",
|
||||
" x=\"Feature\",\n",
|
||||
" y=\"value\",\n",
|
||||
" hue=\"Model\",\n",
|
||||
" data=pd.melt(mse_df, id_vars=[\"Feature\"]).rename(columns={\"variable\": \"Model\"}),\n",
|
||||
")\n",
|
||||
"plt.xticks(rotation=45, ha=\"right\")\n",
|
||||
"plt.yscale(\"log\")\n",
|
||||
"plt.ylabel(\"Mean Squared Error\")\n",
|
||||
"plt.xlabel(\"Target Feature\")\n",
|
||||
|
||||
@@ -24,27 +24,50 @@
|
||||
"data = load_breast_cancer()\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"feature_names = [n for n in data.feature_names if 'radius' in n or 'area' in n]\n",
|
||||
"feature_names = [n for n in data.feature_names if \"radius\" in n or \"area\" in n]\n",
|
||||
"\n",
|
||||
"X = data.data[:, [data.feature_names.tolist().index(n) for n in feature_names]]\n",
|
||||
"X = StandardScaler().fit_transform(X)\n",
|
||||
"y = data.data[:, [data.feature_names.tolist().index(n) for n in data.feature_names if n not in feature_names]]\n",
|
||||
"y = data.data[\n",
|
||||
" :,\n",
|
||||
" [\n",
|
||||
" data.feature_names.tolist().index(n)\n",
|
||||
" for n in data.feature_names\n",
|
||||
" if n not in feature_names\n",
|
||||
" ],\n",
|
||||
"]\n",
|
||||
"y = StandardScaler().fit_transform(y)\n",
|
||||
"feature_dim = X.shape[1]\n",
|
||||
"target_dim = y.shape[1]\n",
|
||||
"\n",
|
||||
"def get_regression_model(n_hidden_layers: int, n_neurons: int, activation: type=LeakyReLU, regularization_strength: float = 1e-3) -> list[Layer]:\n",
|
||||
"\n",
|
||||
"def get_regression_model(\n",
|
||||
" n_hidden_layers: int,\n",
|
||||
" n_neurons: int,\n",
|
||||
" activation: type = LeakyReLU,\n",
|
||||
" regularization_strength: float = 1e-3,\n",
|
||||
") -> list[Layer]:\n",
|
||||
" layers = []\n",
|
||||
" layers.append(Layer(feature_dim, n_neurons, activation_function=activation()))\n",
|
||||
" for _ in range(n_hidden_layers - 1):\n",
|
||||
" layers.append(Layer(n_neurons, n_neurons, activation_function=activation()))\n",
|
||||
" layers.append(Layer(n_neurons, target_dim, activation_function=Linear()))\n",
|
||||
" for layer in layers:\n",
|
||||
" layer.regularization = Regularization(regularization_strength, 'l2')\n",
|
||||
" layer.regularization = Regularization(regularization_strength, \"l2\")\n",
|
||||
" return layers\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def get_model():\n",
|
||||
" return FFNN(get_regression_model(n_hidden_layers=2, n_neurons=32, activation=LeakyReLU, regularization_strength=1e-3), AdamScheduler(epochs=10000, learning_rate=1e-2), MSELoss())"
|
||||
" return FFNN(\n",
|
||||
" get_regression_model(\n",
|
||||
" n_hidden_layers=2,\n",
|
||||
" n_neurons=32,\n",
|
||||
" activation=LeakyReLU,\n",
|
||||
" regularization_strength=1e-3,\n",
|
||||
" ),\n",
|
||||
" AdamScheduler(epochs=10000, learning_rate=1e-2),\n",
|
||||
" MSELoss(),\n",
|
||||
" )"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -57,8 +80,12 @@
|
||||
"crossvalidation_groups = 5\n",
|
||||
"group_length = X.shape[0] // crossvalidation_groups\n",
|
||||
"predictions = np.zeros_like(y)\n",
|
||||
"for g in range(crossvalidation_groups + 1): # One final smaller group to catch the rest\n",
|
||||
" indices = [i for i in range(X.shape[0]) if i < g*group_length or i > (g+1)*group_length]\n",
|
||||
"for g in range(crossvalidation_groups + 1): # One final smaller group to catch the rest\n",
|
||||
" indices = [\n",
|
||||
" i\n",
|
||||
" for i in range(X.shape[0])\n",
|
||||
" if i < g * group_length or i > (g + 1) * group_length\n",
|
||||
" ]\n",
|
||||
" prediction_indices = [i for i in range(X.shape[0]) if i not in indices]\n",
|
||||
" X_train = X[indices]\n",
|
||||
" X_pred = X[prediction_indices]\n",
|
||||
@@ -66,8 +93,7 @@
|
||||
"\n",
|
||||
" model = get_model()\n",
|
||||
" model.fit(X_train, y_train)\n",
|
||||
" predictions[prediction_indices] = model.predict(X_pred)\n",
|
||||
"\n"
|
||||
" predictions[prediction_indices] = model.predict(X_pred)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -122,15 +148,21 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"combined_data = np.zeros_like(data.data)\n",
|
||||
"combined_data[:,:6] = X\n",
|
||||
"combined_data[:, :6] = X\n",
|
||||
"combined_data[:, 6:] = predictions\n",
|
||||
"\n",
|
||||
"combined_df = pd.DataFrame(combined_data, columns=[*feature_names, *[n for n in data.feature_names if n not in feature_names]])\n",
|
||||
"combined_df['target'] = data.target\n",
|
||||
"combined_df = pd.DataFrame(\n",
|
||||
" combined_data,\n",
|
||||
" columns=[\n",
|
||||
" *feature_names,\n",
|
||||
" *[n for n in data.feature_names if n not in feature_names],\n",
|
||||
" ],\n",
|
||||
")\n",
|
||||
"combined_df[\"target\"] = data.target\n",
|
||||
"combined_df.to_csv(\"breast_cancer_regression_results.csv\", index=False)\n",
|
||||
"\n",
|
||||
"original_df = pd.DataFrame(data.data, columns=data.feature_names)\n",
|
||||
"original_df['target'] = data.target\n",
|
||||
"original_df[\"target\"] = data.target\n",
|
||||
"original_df.to_csv(\"breast_cancer_original_data.csv\", index=False)"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -7,8 +7,21 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from easynn.feedforward import FFNN, Layer, Regularization, LeakyReLU, Softmax, CrossEntropyLoss\n",
|
||||
"from easynn.schedulers import GradientDescentScheduler, AdamScheduler, RMSPropScheduler, AdaGradScheduler, MomentumScheduler\n"
|
||||
"from easynn.feedforward import (\n",
|
||||
" FFNN,\n",
|
||||
" Layer,\n",
|
||||
" Regularization,\n",
|
||||
" LeakyReLU,\n",
|
||||
" Softmax,\n",
|
||||
" CrossEntropyLoss,\n",
|
||||
")\n",
|
||||
"from easynn.schedulers import (\n",
|
||||
" GradientDescentScheduler,\n",
|
||||
" AdamScheduler,\n",
|
||||
" RMSPropScheduler,\n",
|
||||
" AdaGradScheduler,\n",
|
||||
" MomentumScheduler,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -18,7 +31,12 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def get_classification_model(n_hidden_layers: int, n_neurons: int, activation: type=LeakyReLU, reg_strength: float=0.0) -> list[Layer]:\n",
|
||||
"def get_classification_model(\n",
|
||||
" n_hidden_layers: int,\n",
|
||||
" n_neurons: int,\n",
|
||||
" activation: type = LeakyReLU,\n",
|
||||
" reg_strength: float = 0.0,\n",
|
||||
") -> list[Layer]:\n",
|
||||
" target_dim = 2\n",
|
||||
" feature_dim = 30\n",
|
||||
" layers = []\n",
|
||||
@@ -2337,21 +2355,32 @@
|
||||
"source": [
|
||||
"from sklearn.metrics import accuracy_score, roc_auc_score\n",
|
||||
"\n",
|
||||
"def test_classification_model(n_hidden_layers: int, n_neurons: int, reg_strength: float, n_iterations: int=50) -> tuple[float, float, FFNN]:\n",
|
||||
"\n",
|
||||
"def test_classification_model(\n",
|
||||
" n_hidden_layers: int, n_neurons: int, reg_strength: float, n_iterations: int = 50\n",
|
||||
") -> tuple[float, float, FFNN]:\n",
|
||||
" assert n_iterations > 0, \"n_iterations must be greater than 0\"\n",
|
||||
" accs = []\n",
|
||||
" aucs = []\n",
|
||||
" for _ in range(n_iterations): # Run n_iterations trials\n",
|
||||
" X_train, X_test, y_train, y_test = train_test_split(X, y_encoded, test_size=0.2)\n",
|
||||
" layers = get_classification_model(n_hidden_layers=n_hidden_layers, n_neurons=n_neurons, reg_strength=reg_strength)\n",
|
||||
" model = FFNN(layers, AdamScheduler(epochs=500, learning_rate=0.05), loss_fn=CrossEntropyLoss())\n",
|
||||
" layers = get_classification_model(\n",
|
||||
" n_hidden_layers=n_hidden_layers,\n",
|
||||
" n_neurons=n_neurons,\n",
|
||||
" reg_strength=reg_strength,\n",
|
||||
" )\n",
|
||||
" model = FFNN(\n",
|
||||
" layers,\n",
|
||||
" AdamScheduler(epochs=500, learning_rate=0.05),\n",
|
||||
" loss_fn=CrossEntropyLoss(),\n",
|
||||
" )\n",
|
||||
" model.fit(X_train, y_train)\n",
|
||||
" y_pred = model.predict(X_test)\n",
|
||||
" acc = accuracy_score(y_test.argmax(axis=1), y_pred.argmax(axis=1))\n",
|
||||
" auc = roc_auc_score(y_test, y_pred)\n",
|
||||
" accs.append(acc)\n",
|
||||
" aucs.append(auc)\n",
|
||||
" return np.mean(accs), np.mean(aucs), model\n"
|
||||
" return np.mean(accs), np.mean(aucs), model"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -2389,14 +2418,18 @@
|
||||
"):\n",
|
||||
" ...\n",
|
||||
"\n",
|
||||
" acc, auc, model = test_classification_model(n_hidden_layers, n_neurons, reg_strength, 2)\n",
|
||||
" results.append({\n",
|
||||
" \"n_hidden_layers\": n_hidden_layers,\n",
|
||||
" \"n_neurons\": n_neurons,\n",
|
||||
" \"reg_strength\": reg_strength,\n",
|
||||
" \"accuracy\": acc,\n",
|
||||
" \"auc\": auc,\n",
|
||||
" })\n",
|
||||
" acc, auc, model = test_classification_model(\n",
|
||||
" n_hidden_layers, n_neurons, reg_strength, 2\n",
|
||||
" )\n",
|
||||
" results.append(\n",
|
||||
" {\n",
|
||||
" \"n_hidden_layers\": n_hidden_layers,\n",
|
||||
" \"n_neurons\": n_neurons,\n",
|
||||
" \"reg_strength\": reg_strength,\n",
|
||||
" \"accuracy\": acc,\n",
|
||||
" \"auc\": auc,\n",
|
||||
" }\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" # You can use AUC as the main criterion, or combine them\n",
|
||||
" score = auc + 0.1 * acc\n",
|
||||
@@ -2449,21 +2482,16 @@
|
||||
" if i == j:\n",
|
||||
" # Diagonal: 1D line plot of AUC vs parameter\n",
|
||||
" sns.lineplot(\n",
|
||||
" data=results_df,\n",
|
||||
" x=xparam,\n",
|
||||
" y=\"auc\",\n",
|
||||
" marker=\"o\",\n",
|
||||
" ax=ax,\n",
|
||||
" color=\"C0\"\n",
|
||||
" data=results_df, x=xparam, y=\"auc\", marker=\"o\", ax=ax, color=\"C0\"\n",
|
||||
" )\n",
|
||||
" ax.set_xlabel(xparam)\n",
|
||||
" ax.set_ylabel(\"AUC\")\n",
|
||||
" if i == 2:\n",
|
||||
" ax.set_xscale('log')\n",
|
||||
" ax.set_xscale(\"log\")\n",
|
||||
" else:\n",
|
||||
" # Off-diagonal: 2D heatmap colored by AUC\n",
|
||||
" hm = results_df.pivot_table(index=yparam, columns=xparam, values=\"auc\")\n",
|
||||
" sc = ax.imshow(hm, origin='lower', aspect='auto', cmap=cmap)\n",
|
||||
" sc = ax.imshow(hm, origin=\"lower\", aspect=\"auto\", cmap=cmap)\n",
|
||||
" ax.set_xticks(np.arange(len(hm.columns)))\n",
|
||||
" ax.set_yticks(np.arange(len(hm.index)))\n",
|
||||
" ax.set_xticklabels(hm.columns)\n",
|
||||
@@ -2478,7 +2506,7 @@
|
||||
"fig.suptitle(\"Pairwise Hyperparameter Relationships (AUC)\", fontsize=18)\n",
|
||||
"plt.tight_layout(rect=[0, 0, 0.9, 0.97])\n",
|
||||
"plt.savefig(\"classification_hyperparameter_scan.pdf\")\n",
|
||||
"plt.show()\n"
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -2500,8 +2528,10 @@
|
||||
"n_neurons = 64\n",
|
||||
"reg_strength = 1e-6\n",
|
||||
"\n",
|
||||
"acc, auc, best_model = test_classification_model(n_hidden_layers, n_neurons, reg_strength)\n",
|
||||
"print(f\"Best Model - Accuracy: {acc:.4f}, AUC: {auc:.4f}\")\n"
|
||||
"acc, auc, best_model = test_classification_model(\n",
|
||||
" n_hidden_layers, n_neurons, reg_strength\n",
|
||||
")\n",
|
||||
"print(f\"Best Model - Accuracy: {acc:.4f}, AUC: {auc:.4f}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -2545,7 +2575,7 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"sns.heatmap(cm, annot=True, fmt='d')\n",
|
||||
"sns.heatmap(cm, annot=True, fmt=\"d\")\n",
|
||||
"plt.xlabel(\"Predicted Label\")\n",
|
||||
"plt.ylabel(\"True Label\")\n",
|
||||
"plt.tight_layout()\n",
|
||||
@@ -2572,7 +2602,7 @@
|
||||
],
|
||||
"source": [
|
||||
"sns.lineplot(x=fpr, y=tpr)\n",
|
||||
"plt.plot([0, 1], [0, 1], linestyle='--', color='gray')\n",
|
||||
"plt.plot([0, 1], [0, 1], linestyle=\"--\", color=\"gray\")\n",
|
||||
"plt.xlabel(\"False Positive Rate\")\n",
|
||||
"plt.ylabel(\"True Positive Rate\")\n",
|
||||
"plt.tight_layout()\n",
|
||||
|
||||
@@ -7,7 +7,14 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from easynn.feedforward import FFNN, Layer, Regularization, LeakyReLU, Softmax, CrossEntropyLoss\n",
|
||||
"from easynn.feedforward import (\n",
|
||||
" FFNN,\n",
|
||||
" Layer,\n",
|
||||
" Regularization,\n",
|
||||
" LeakyReLU,\n",
|
||||
" Softmax,\n",
|
||||
" CrossEntropyLoss,\n",
|
||||
")\n",
|
||||
"from easynn.schedulers import AdamScheduler\n",
|
||||
"\n",
|
||||
"import pandas as pd\n",
|
||||
@@ -38,9 +45,12 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"logistic_model = FFNN([\n",
|
||||
" Layer(30, 2, activation_function=Softmax()),\n",
|
||||
"], loss_fn=CrossEntropyLoss(), scheduler=AdamScheduler(learning_rate=5e-4, epochs=25000)\n",
|
||||
"logistic_model = FFNN(\n",
|
||||
" [\n",
|
||||
" Layer(30, 2, activation_function=Softmax()),\n",
|
||||
" ],\n",
|
||||
" loss_fn=CrossEntropyLoss(),\n",
|
||||
" scheduler=AdamScheduler(learning_rate=5e-4, epochs=25000),\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
@@ -155,7 +165,7 @@
|
||||
"\n",
|
||||
"cm = confusion_matrix(y_test.argmax(axis=1), y_pred.argmax(axis=1))\n",
|
||||
"\n",
|
||||
"sns.heatmap(cm, annot=True, fmt='d')\n",
|
||||
"sns.heatmap(cm, annot=True, fmt=\"d\")\n",
|
||||
"plt.xlabel(\"Predicted Label\")\n",
|
||||
"plt.ylabel(\"True Label\")\n",
|
||||
"plt.tight_layout()\n",
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
|
||||
def get_rc_params():
|
||||
colors = ["FF220C", "70D6FF", "8AAA79", "666370", "1C1F33"]
|
||||
rcParams = plt.rcParams
|
||||
@@ -29,4 +30,5 @@ def get_rc_params():
|
||||
rcParams["ytick.right"] = True
|
||||
return rcParams
|
||||
|
||||
plt.rcParams.update(get_rc_params())
|
||||
|
||||
plt.rcParams.update(get_rc_params())
|
||||
|
||||
Reference in New Issue
Block a user