Skip to content

Commit

Permalink
fix stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasturcani committed Aug 20, 2024
1 parent a0ee1ba commit 39c6608
Show file tree
Hide file tree
Showing 30 changed files with 119 additions and 122 deletions.
14 changes: 7 additions & 7 deletions docs/notebooks/neuralnet/auto-thermal-reformer-relu.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,16 @@
"os.environ[\"TF_CPP_MIN_LOG_LEVEL\"] = \"2\" # suppress CUDA warnings from tensorflow\n",
"\n",
"# import the necessary packages\n",
"from omlt import OmltBlock, OffsetScaling\n",
"from omlt.io.keras import load_keras_sequential\n",
"from omlt.neuralnet import ReluBigMFormulation\n",
"import pyomo.environ as pyo\n",
"import pandas as pd\n",
"import tensorflow.keras as keras\n",
"from tensorflow.keras.models import Sequential\n",
"import pyomo.environ as pyo\n",
"from tensorflow import keras\n",
"from tensorflow.keras.layers import Dense\n",
"from tensorflow.keras.models import Sequential\n",
"from tensorflow.keras.optimizers import Adam\n",
"from tensorflow.keras.callbacks import ModelCheckpoint"
"\n",
"from omlt import OffsetScaling, OmltBlock\n",
"from omlt.io.keras import load_keras_sequential\n",
"from omlt.neuralnet import ReluBigMFormulation"
]
},
{
Expand Down
14 changes: 7 additions & 7 deletions docs/notebooks/neuralnet/auto-thermal-reformer.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,16 @@
"os.environ[\"TF_CPP_MIN_LOG_LEVEL\"] = \"2\" # suppress CUDA warnings from tensorflow\n",
"\n",
"# import the necessary packages\n",
"from omlt import OmltBlock, OffsetScaling\n",
"from omlt.io.keras import load_keras_sequential\n",
"from omlt.neuralnet import FullSpaceSmoothNNFormulation\n",
"import pyomo.environ as pyo\n",
"import pandas as pd\n",
"import tensorflow.keras as keras\n",
"from tensorflow.keras.models import Sequential\n",
"import pyomo.environ as pyo\n",
"from tensorflow import keras\n",
"from tensorflow.keras.layers import Dense\n",
"from tensorflow.keras.models import Sequential\n",
"from tensorflow.keras.optimizers import Adam\n",
"from tensorflow.keras.callbacks import ModelCheckpoint"
"\n",
"from omlt import OffsetScaling, OmltBlock\n",
"from omlt.io.keras import load_keras_sequential\n",
"from omlt.neuralnet import FullSpaceSmoothNNFormulation"
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions docs/notebooks/neuralnet/build_network.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
"metadata": {},
"outputs": [],
"source": [
"import pyomo.environ as pyo\n",
"import numpy as np\n",
"import pyomo.environ as pyo\n",
"\n",
"from omlt.neuralnet import NetworkDefinition\n",
"from omlt.neuralnet.layer import InputLayer, DenseLayer, IndexMapper"
"from omlt.neuralnet.layer import DenseLayer, IndexMapper, InputLayer"
]
},
{
Expand Down
17 changes: 8 additions & 9 deletions docs/notebooks/neuralnet/graph_neural_network_formulation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@
],
"source": [
"import numpy as np\n",
"import pyomo.environ as pyo\n",
"import torch\n",
"from torch.nn import Linear, ReLU, Sigmoid\n",
"from torch_geometric.nn import Sequential, GCNConv\n",
"from torch_geometric.nn import global_mean_pool\n",
"from omlt.io.torch_geometric import gnn_with_fixed_graph\n",
"import pyomo.environ as pyo\n",
"from torch_geometric.nn import GCNConv, Sequential, global_mean_pool\n",
"\n",
"from omlt import OmltBlock\n",
"from omlt.io.torch_geometric import gnn_with_fixed_graph\n",
"\n",
"\n",
"def GCN_Sequential(activation, pooling):\n",
Expand Down Expand Up @@ -478,14 +478,13 @@
"outputs": [],
"source": [
"import numpy as np\n",
"import pyomo.environ as pyo\n",
"import torch\n",
"from torch.nn import Linear, ReLU\n",
"from torch_geometric.nn import Sequential, SAGEConv\n",
"from torch_geometric.nn import global_add_pool\n",
"from omlt.io.torch_geometric import gnn_with_non_fixed_graph\n",
"from torch.nn import ReLU\n",
"from torch_geometric.nn import SAGEConv, global_add_pool\n",
"\n",
"import pyomo.environ as pyo\n",
"from omlt import OmltBlock\n",
"from omlt.io.torch_geometric import gnn_with_non_fixed_graph\n",
"\n",
"\n",
"def SAGE_Sequential(activation, pooling):\n",
Expand Down
9 changes: 4 additions & 5 deletions docs/notebooks/neuralnet/import_network.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@
}
],
"source": [
"from omlt.io import write_onnx_model_with_bounds, load_onnx_neural_network_with_bounds"
"from omlt.io import load_onnx_neural_network_with_bounds, write_onnx_model_with_bounds"
]
},
{
Expand Down Expand Up @@ -352,9 +352,8 @@
"import os\n",
"\n",
"os.environ[\"TF_CPP_MIN_LOG_LEVEL\"] = \"3\" # or any {'0', '1', '2'}\n",
"import keras\n",
"from keras.models import Sequential\n",
"from keras.layers import Dense\n",
"from keras.models import Sequential\n",
"\n",
"model = Sequential()\n",
"model.add(Dense(12, input_dim=8, activation=\"relu\"))\n",
Expand Down Expand Up @@ -731,8 +730,8 @@
"# Add output_names for compatibility:\n",
"model.output_names = [output.name for output in model.outputs]\n",
"\n",
"from tensorflow import TensorSpec\n",
"import tf2onnx\n",
"from tensorflow import TensorSpec\n",
"\n",
"spec = [TensorSpec(input.shape, input.dtype, input.name) for input in model.inputs]\n",
"onnx_model, _ = tf2onnx.convert.from_keras(model, input_signature=spec)\n",
Expand Down Expand Up @@ -817,8 +816,8 @@
],
"source": [
"import torch\n",
"import torch.nn as nn\n",
"import torch.nn.functional as F\n",
"from torch import nn\n",
"from torch.utils.data import DataLoader, TensorDataset\n",
"\n",
"\n",
Expand Down
3 changes: 2 additions & 1 deletion docs/notebooks/neuralnet/index_handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"from omlt.neuralnet import NetworkDefinition\n",
"from omlt.neuralnet.layer import IndexMapper, InputLayer, DenseLayer, PoolingLayer2D"
"from omlt.neuralnet.layer import DenseLayer, IndexMapper, InputLayer, PoolingLayer2D"
]
},
{
Expand Down
37 changes: 13 additions & 24 deletions docs/notebooks/neuralnet/mnist_example_convolutional.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,27 @@
"source": [
"# Import requisite packages\n",
"# data manipulation\n",
"import numpy as np\n",
"import tempfile\n",
"\n",
"# pytorch for training neural network\n",
"import torch, torch.onnx\n",
"import torch.nn as nn\n",
"import torch.nn.functional as F\n",
"import torch.optim as optim\n",
"from torchvision import datasets, transforms\n",
"from torch.optim.lr_scheduler import StepLR\n",
"import numpy as np\n",
"\n",
"# pyomo for optimization\n",
"import pyomo.environ as pyo\n",
"\n",
"# pytorch for training neural network\n",
"import torch\n",
"import torch.onnx\n",
"from torch import nn, optim\n",
"from torch.optim.lr_scheduler import StepLR\n",
"from torchvision import datasets, transforms\n",
"\n",
"# omlt for interfacing our neural network with pyomo\n",
"from omlt import OmltBlock\n",
"from omlt.neuralnet import FullSpaceNNFormulation\n",
"from omlt.io.onnx import (\n",
" write_onnx_model_with_bounds,\n",
" load_onnx_neural_network_with_bounds,\n",
")"
" write_onnx_model_with_bounds,\n",
")\n",
"from omlt.neuralnet import FullSpaceNNFormulation"
]
},
{
Expand Down Expand Up @@ -165,13 +165,7 @@
" optimizer.step()\n",
" if batch_idx % 200 == 0:\n",
" print(\n",
" \"Train Epoch: {} [{}/{} ({:.0f}%)]\\tLoss: {:.6f}\".format(\n",
" epoch,\n",
" batch_idx * len(data),\n",
" len(train_loader.dataset),\n",
" 100.0 * batch_idx / len(train_loader),\n",
" loss.item(),\n",
" )\n",
" f\"Train Epoch: {epoch} [{batch_idx * len(data)}/{len(train_loader.dataset)} ({100.0 * batch_idx / len(train_loader):.0f}%)]\\tLoss: {loss.item():.6f}\"\n",
" )\n",
"\n",
"\n",
Expand All @@ -189,12 +183,7 @@
" correct += pred.eq(target.view_as(pred)).sum().item()\n",
" test_loss /= len(test_loader.dataset)\n",
" print(\n",
" \"\\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\\n\".format(\n",
" test_loss,\n",
" correct,\n",
" len(test_loader.dataset),\n",
" 100.0 * correct / len(test_loader.dataset),\n",
" )\n",
" f\"\\nTest set: Average loss: {test_loss:.4f}, Accuracy: {correct}/{len(test_loader.dataset)} ({100.0 * correct / len(test_loader.dataset):.0f}%)\\n\"\n",
" )"
]
},
Expand Down
37 changes: 13 additions & 24 deletions docs/notebooks/neuralnet/mnist_example_dense.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,27 @@
"source": [
"# Import requisite packages\n",
"# data manipulation\n",
"import numpy as np\n",
"import tempfile\n",
"\n",
"# pytorch for training neural network\n",
"import torch, torch.onnx\n",
"import torch.nn as nn\n",
"import torch.nn.functional as F\n",
"import torch.optim as optim\n",
"from torchvision import datasets, transforms\n",
"from torch.optim.lr_scheduler import StepLR\n",
"import numpy as np\n",
"\n",
"# pyomo for optimization\n",
"import pyomo.environ as pyo\n",
"\n",
"# pytorch for training neural network\n",
"import torch\n",
"import torch.onnx\n",
"from torch import nn, optim\n",
"from torch.optim.lr_scheduler import StepLR\n",
"from torchvision import datasets, transforms\n",
"\n",
"# omlt for interfacing our neural network with pyomo\n",
"from omlt import OmltBlock\n",
"from omlt.neuralnet import FullSpaceNNFormulation\n",
"from omlt.io.onnx import (\n",
" write_onnx_model_with_bounds,\n",
" load_onnx_neural_network_with_bounds,\n",
")"
" write_onnx_model_with_bounds,\n",
")\n",
"from omlt.neuralnet import FullSpaceNNFormulation"
]
},
{
Expand Down Expand Up @@ -161,13 +161,7 @@
" optimizer.step()\n",
" if batch_idx % 200 == 0:\n",
" print(\n",
" \"Train Epoch: {} [{}/{} ({:.0f}%)]\\tLoss: {:.6f}\".format(\n",
" epoch,\n",
" batch_idx * len(data),\n",
" len(train_loader.dataset),\n",
" 100.0 * batch_idx / len(train_loader),\n",
" loss.item(),\n",
" )\n",
" f\"Train Epoch: {epoch} [{batch_idx * len(data)}/{len(train_loader.dataset)} ({100.0 * batch_idx / len(train_loader):.0f}%)]\\tLoss: {loss.item():.6f}\"\n",
" )\n",
"\n",
"\n",
Expand All @@ -185,12 +179,7 @@
" correct += pred.eq(target.view_as(pred)).sum().item()\n",
" test_loss /= len(test_loader.dataset)\n",
" print(\n",
" \"\\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\\n\".format(\n",
" test_loss,\n",
" correct,\n",
" len(test_loader.dataset),\n",
" 100.0 * correct / len(test_loader.dataset),\n",
" )\n",
" f\"\\nTest set: Average loss: {test_loss:.4f}, Accuracy: {correct}/{len(test_loader.dataset)} ({100.0 * correct / len(test_loader.dataset):.0f}%)\\n\"\n",
" )"
]
},
Expand Down
19 changes: 9 additions & 10 deletions docs/notebooks/neuralnet/neural_network_formulations.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -65,36 +65,35 @@
"source": [
"# Start by importing the following libraries\n",
"# data manipulation and plotting\n",
"import pandas as pd\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"matplotlib.rc(\"font\", size=24)\n",
"plt.rc(\"axes\", titlesize=24)\n",
"\n",
"# tensorflow objects\n",
"from tensorflow.keras.models import Sequential, Model\n",
"# pyomo for optimization\n",
"import pyomo.environ as pyo\n",
"from tensorflow.keras.layers import Dense, Input\n",
"from tensorflow.keras.models import Sequential\n",
"from tensorflow.keras.optimizers import Adam\n",
"\n",
"# pyomo for optimization\n",
"import pyomo.environ as pyo\n",
"import omlt\n",
"\n",
"# omlt for interfacing our neural network with pyomo\n",
"from omlt import OmltBlock\n",
"from omlt.io.keras import keras_reader\n",
"from omlt.neuralnet import (\n",
" NetworkDefinition,\n",
" FullSpaceNNFormulation,\n",
" FullSpaceSmoothNNFormulation,\n",
" ReducedSpaceSmoothNNFormulation,\n",
" ReluBigMFormulation,\n",
" ReluComplementarityFormulation,\n",
" ReluPartitionFormulation,\n",
")\n",
"from omlt.neuralnet.activations import ComplementarityReLUActivation\n",
"from omlt.io.keras import keras_reader\n",
"import omlt"
"from omlt.neuralnet.activations import ComplementarityReLUActivation"
]
},
{
Expand Down
8 changes: 5 additions & 3 deletions docs/notebooks/trees/bo_with_trees.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import random\n",
"\n",
"import numpy as np\n",
"\n",
"\n",
"def f(x, y):\n",
" return (1 - x) ** 2 + 100 * (y - x**2) ** 2\n",
Expand Down Expand Up @@ -92,9 +93,10 @@
"metadata": {},
"outputs": [],
"source": [
"import lightgbm as lgb\n",
"import warnings\n",
"\n",
"import lightgbm as lgb\n",
"\n",
"\n",
"def train_tree(data):\n",
" with warnings.catch_warnings():\n",
Expand Down Expand Up @@ -184,6 +186,7 @@
"outputs": [],
"source": [
"import pyomo.environ as pe\n",
"\n",
"from omlt.block import OmltBlock\n",
"from omlt.gbt import GBTBigMFormulation, GradientBoostedTreeModel\n",
"\n",
Expand Down Expand Up @@ -213,7 +216,6 @@
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"\n",
"def add_unc_metric(opt_model, data):\n",
Expand Down
Loading

0 comments on commit 39c6608

Please sign in to comment.