Skip to content

Commit

Permalink
Fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
pobonomo committed Jan 2, 2025
1 parent 8893a6e commit 2259241
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 164 deletions.
121 changes: 21 additions & 100 deletions Generate_keras_test_network.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 12,
"execution_count": null,
"id": "c8d57e80-9075-4d63-bc36-f9aaad08ea2f",
"metadata": {},
"outputs": [],
Expand All @@ -12,66 +12,55 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": null,
"id": "90fa4efb-f9d5-40fb-8e4a-5e3ed1094740",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'3.7.0'"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"keras.__version__"
]
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": null,
"id": "5d98d000-661e-4495-bef0-49c5eb180aff",
"metadata": {},
"outputs": [],
"source": [
"nn = keras.models.Sequential(\n",
" [\n",
" keras.layers.InputLayer((8,)),\n",
" keras.layers.Dense(30, activation='relu'),\n",
" keras.layers.Dense(1),\n",
" keras.layers.InputLayer((8,)),\n",
" keras.layers.Dense(30, activation=\"relu\"),\n",
" keras.layers.Dense(1),\n",
" ]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": null,
"id": "ba3cf3ee-bd25-4180-95c0-2ff42d858a34",
"metadata": {},
"outputs": [],
"source": [
"nn.compile(loss='mean_squared_error', optimizer='adam')"
"nn.compile(loss=\"mean_squared_error\", optimizer=\"adam\")"
]
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": null,
"id": "247bd200-8026-4f08-8739-9aabb3c37e99",
"metadata": {},
"outputs": [],
"source": [
"(X_train, y_train), (X_test, y_test) = tf.keras.datasets.california_housing.load_data(\n",
"(X_train, y_train), (X_test, y_test) = keras.datasets.california_housing.load_data(\n",
" version=\"small\"\n",
")\n"
")"
]
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": null,
"id": "a29325dd-1ab1-4cce-81c0-2528e892adb6",
"metadata": {},
"outputs": [],
Expand All @@ -81,7 +70,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": null,
"id": "cbecbd91-e100-4568-9424-efd9e3b6d5fc",
"metadata": {},
"outputs": [],
Expand All @@ -93,100 +82,32 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": null,
"id": "5656d2da-ee2d-4a8f-aef3-65876c20193b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1/20\n",
"\u001b[1m15/15\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 36ms/step - loss: 51257974784.0000 - val_loss: 48780189696.0000\n",
"Epoch 2/20\n",
"\u001b[1m15/15\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 34ms/step - loss: 51058966528.0000 - val_loss: 48779857920.0000\n",
"Epoch 3/20\n",
"\u001b[1m15/15\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 30ms/step - loss: 56175738880.0000 - val_loss: 48779501568.0000\n",
"Epoch 4/20\n",
"\u001b[1m15/15\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 28ms/step - loss: 48874921984.0000 - val_loss: 48779141120.0000\n",
"Epoch 5/20\n",
"\u001b[1m15/15\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 25ms/step - loss: 52104830976.0000 - val_loss: 48778752000.0000\n",
"Epoch 6/20\n",
"\u001b[1m15/15\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 41ms/step - loss: 53767278592.0000 - val_loss: 48778342400.0000\n",
"Epoch 7/20\n",
"\u001b[1m15/15\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 25ms/step - loss: 51997323264.0000 - val_loss: 48777920512.0000\n",
"Epoch 8/20\n",
"\u001b[1m15/15\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 34ms/step - loss: 52127023104.0000 - val_loss: 48777490432.0000\n",
"Epoch 9/20\n",
"\u001b[1m15/15\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 31ms/step - loss: 55014318080.0000 - val_loss: 48777023488.0000\n",
"Epoch 10/20\n",
"\u001b[1m15/15\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 27ms/step - loss: 50627502080.0000 - val_loss: 48776540160.0000\n",
"Epoch 11/20\n",
"\u001b[1m15/15\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 28ms/step - loss: 52081172480.0000 - val_loss: 48776024064.0000\n",
"Epoch 12/20\n",
"\u001b[1m15/15\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 21ms/step - loss: 55939633152.0000 - val_loss: 48775487488.0000\n",
"Epoch 13/20\n",
"\u001b[1m15/15\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 34ms/step - loss: 51670016000.0000 - val_loss: 48774975488.0000\n",
"Epoch 14/20\n",
"\u001b[1m15/15\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 26ms/step - loss: 55131279360.0000 - val_loss: 48774389760.0000\n",
"Epoch 15/20\n",
"\u001b[1m15/15\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 30ms/step - loss: 51200266240.0000 - val_loss: 48773820416.0000\n",
"Epoch 16/20\n",
"\u001b[1m15/15\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 30ms/step - loss: 53789458432.0000 - val_loss: 48773218304.0000\n",
"Epoch 17/20\n",
"\u001b[1m15/15\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 29ms/step - loss: 50551488512.0000 - val_loss: 48772616192.0000\n",
"Epoch 18/20\n",
"\u001b[1m15/15\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 28ms/step - loss: 50127593472.0000 - val_loss: 48771956736.0000\n",
"Epoch 19/20\n",
"\u001b[1m15/15\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 31ms/step - loss: 48622862336.0000 - val_loss: 48771301376.0000\n",
"Epoch 20/20\n",
"\u001b[1m15/15\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 30ms/step - loss: 53927636992.0000 - val_loss: 48770617344.0000\n"
]
},
{
"data": {
"text/plain": [
"<keras.src.callbacks.history.History at 0x7eb858fa50>"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"nn.fit(X_train, y_train, epochs=20, validation_data=(X_test, y_test))"
]
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": null,
"id": "128b1ba9-55e9-4d78-9b31-d2a0da9bb165",
"metadata": {},
"outputs": [],
"source": [
"nn.save('toto.keras')"
"nn.save(\"toto.keras\")"
]
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": null,
"id": "9c820767-db55-48ba-8dd3-675d06fb5c3d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Sequential name=sequential_1, built=True>"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"keras.saving.load_model('toto.keras')"
"keras.saving.load_model(\"toto.keras\")"
]
},
{
Expand Down
1 change: 0 additions & 1 deletion docs/examples/example2_student_admission.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
"""

import gurobipy as gp
import gurobipy_pandas as gppd
import numpy as np
import pandas as pd
from sklearn.linear_model import LogisticRegression
Expand Down
2 changes: 1 addition & 1 deletion notebooks/Janos/Decision Tree.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@
"metadata": {},
"outputs": [],
"source": [
"m.write('toto.lp')"
"m.write(\"toto.lp\")"
]
},
{
Expand Down
14 changes: 2 additions & 12 deletions notebooks/Janos/Random Forest.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,6 @@
"We print the error.."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7a746e47-4ff4-4595-82ab-a10cf5e4e597",
"metadata": {},
"outputs": [],
"source": [
"m.write("
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -279,7 +269,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": ".venv",
"language": "python",
"name": "python3"
},
Expand All @@ -293,7 +283,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.10"
"version": "3.11.11"
},
"license": {
"full_text": "# Copyright © 2023 Gurobi Optimization, LLC\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n# http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n# =============================================================================="
Expand Down
25 changes: 14 additions & 11 deletions notebooks/adversarial/adversarial_logistic_regression.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
"\n",
"from sklearn.datasets import fetch_openml\n",
"from sklearn.linear_model import LogisticRegression\n",
"from sklearn.pipeline import make_pipeline\n",
"from sklearn.preprocessing import StandardScaler"
"from sklearn.pipeline import make_pipeline"
]
},
{
Expand Down Expand Up @@ -66,7 +65,8 @@
"outputs": [],
"source": [
"import sys\n",
"sys.path.append('../../src')"
"\n",
"sys.path.append(\"../../src\")"
]
},
{
Expand All @@ -77,8 +77,7 @@
},
"outputs": [],
"source": [
"from gurobi_ml import add_predictor_constr\n",
"from gurobi_ml.sklearn import logistic_regression"
"from gurobi_ml import add_predictor_constr"
]
},
{
Expand All @@ -98,7 +97,9 @@
"# Flatten and scale the data\n",
"\n",
"X = X.astype(np.float64) / 255.0\n",
"clf = LogisticRegression(C=50.0 / 500, penalty=\"l1\", solver=\"saga\", tol=0.1, random_state=4)\n",
"clf = LogisticRegression(\n",
" C=50.0 / 500, penalty=\"l1\", solver=\"saga\", tol=0.1, random_state=4\n",
")\n",
"pipeline = make_pipeline(clf)\n",
"pipeline.fit(X, y)"
]
Expand All @@ -122,7 +123,7 @@
},
"outputs": [],
"source": [
"ex_prob = clf.predict_proba(X[imageno:imageno+1, :])\n",
"ex_prob = clf.predict_proba(X[imageno : imageno + 1, :])\n",
"sorted_labels = np.argsort(ex_prob)[0]\n",
"right_label = sorted_labels[-1]\n",
"wrong_label = sorted_labels[-2]"
Expand All @@ -138,7 +139,7 @@
"source": [
"image = X[imageno, :]\n",
"plt.imshow(image.reshape((28, 28)), cmap=\"gray\")\n",
"label = clf.predict(image.reshape(1,-1))\n",
"label = clf.predict(image.reshape(1, -1))\n",
"print(f\"Solution is classified as {label}\")"
]
},
Expand All @@ -165,7 +166,7 @@
"delta = 10\n",
"\n",
"x = m.addMVar(image.shape, lb=0.0, ub=1.0, name=\"x\")\n",
"y = m.addMVar(ex_prob.shape, lb=-float('inf'), name=\"y\")\n",
"y = m.addMVar(ex_prob.shape, lb=-float(\"inf\"), name=\"y\")\n",
"\n",
"abs_diff = m.addMVar(image.shape, lb=0, ub=1, name=\"abs_diff\")\n",
"\n",
Expand All @@ -177,7 +178,9 @@
"m.addConstr(abs_diff >= -x + image)\n",
"m.addConstr(abs_diff.sum() <= delta)\n",
"\n",
"pred_constr = add_predictor_constr(m, pipeline.steps[-1][1], x, y, epsilon=0, predict_function='predict_proba')\n",
"pred_constr = add_predictor_constr(\n",
" m, pipeline.steps[-1][1], x, y, epsilon=0, predict_function=\"predict_proba\"\n",
")\n",
"\n",
"pred_constr.print_stats()"
]
Expand Down Expand Up @@ -214,7 +217,7 @@
"outputs": [],
"source": [
"plt.imshow(x.X.reshape((28, 28)), cmap=\"gray\")\n",
"label = pipeline.predict(x.X.reshape(1,-1))\n",
"label = pipeline.predict(x.X.reshape(1, -1))\n",
"print(f\"Solution is classified as {label}\")"
]
},
Expand Down
2 changes: 1 addition & 1 deletion notebooks/adversarial/adversarial_pytorch.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
"metadata": {},
"outputs": [],
"source": [
"ex_prob = nn_model.forward(x_train[imageno:imageno+1, :])[0]"
"ex_prob = nn_model.forward(x_train[imageno : imageno + 1, :])[0]"
]
},
{
Expand Down
Loading

0 comments on commit 2259241

Please sign in to comment.