From 39c66087237baf42bb81b485efa2968aeb717016 Mon Sep 17 00:00:00 2001 From: Lukas Turcani Date: Tue, 20 Aug 2024 12:45:18 +0100 Subject: [PATCH] fix stuff --- .../auto-thermal-reformer-relu.ipynb | 14 +++---- .../neuralnet/auto-thermal-reformer.ipynb | 14 +++---- docs/notebooks/neuralnet/build_network.ipynb | 4 +- .../graph_neural_network_formulation.ipynb | 17 ++++----- docs/notebooks/neuralnet/import_network.ipynb | 9 ++--- docs/notebooks/neuralnet/index_handling.ipynb | 3 +- .../mnist_example_convolutional.ipynb | 37 +++++++------------ .../neuralnet/mnist_example_dense.ipynb | 37 +++++++------------ .../neural_network_formulations.ipynb | 19 +++++----- docs/notebooks/trees/bo_with_trees.ipynb | 8 ++-- .../trees/linear_tree_formulations.ipynb | 15 ++++---- src/omlt/linear_tree/lt_formulation.py | 6 +-- src/omlt/neuralnet/layer.py | 2 +- tests/conftest.py | 7 ++-- tests/gbt/test_gbt_formulation.py | 1 + tests/io/test_keras_reader.py | 1 + tests/io/test_onnx_parser.py | 1 + tests/io/test_torch_geometric.py | 12 +++--- tests/linear_tree/test_lt_formulation.py | 4 +- tests/neuralnet/test_keras.py | 4 +- tests/neuralnet/test_layer.py | 1 + tests/neuralnet/test_network_definition.py | 1 + tests/neuralnet/test_nn_formulation.py | 3 +- tests/neuralnet/test_onnx.py | 7 +++- tests/neuralnet/test_relu.py | 1 + tests/neuralnet/train_keras_models.py | 3 +- tests/notebooks/test_run_notebooks.py | 5 ++- tests/test_block.py | 1 + tests/test_formulation.py | 3 +- tests/test_scaling.py | 1 + 30 files changed, 119 insertions(+), 122 deletions(-) diff --git a/docs/notebooks/neuralnet/auto-thermal-reformer-relu.ipynb b/docs/notebooks/neuralnet/auto-thermal-reformer-relu.ipynb index f26fdd02..dd3a0735 100644 --- a/docs/notebooks/neuralnet/auto-thermal-reformer-relu.ipynb +++ b/docs/notebooks/neuralnet/auto-thermal-reformer-relu.ipynb @@ -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" ] }, { diff --git a/docs/notebooks/neuralnet/auto-thermal-reformer.ipynb b/docs/notebooks/neuralnet/auto-thermal-reformer.ipynb index 4381e550..a53a5fc7 100644 --- a/docs/notebooks/neuralnet/auto-thermal-reformer.ipynb +++ b/docs/notebooks/neuralnet/auto-thermal-reformer.ipynb @@ -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" ] }, { diff --git a/docs/notebooks/neuralnet/build_network.ipynb b/docs/notebooks/neuralnet/build_network.ipynb index 9648436f..008042c1 100644 --- a/docs/notebooks/neuralnet/build_network.ipynb +++ b/docs/notebooks/neuralnet/build_network.ipynb @@ -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" ] }, { diff --git a/docs/notebooks/neuralnet/graph_neural_network_formulation.ipynb b/docs/notebooks/neuralnet/graph_neural_network_formulation.ipynb index d29a6f5b..2699d171 100644 --- a/docs/notebooks/neuralnet/graph_neural_network_formulation.ipynb +++ b/docs/notebooks/neuralnet/graph_neural_network_formulation.ipynb @@ -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", @@ -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", diff --git a/docs/notebooks/neuralnet/import_network.ipynb b/docs/notebooks/neuralnet/import_network.ipynb index 5201ac98..8ee97767 100644 --- a/docs/notebooks/neuralnet/import_network.ipynb +++ b/docs/notebooks/neuralnet/import_network.ipynb @@ -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" ] }, { @@ -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", @@ -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", @@ -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", diff --git a/docs/notebooks/neuralnet/index_handling.ipynb b/docs/notebooks/neuralnet/index_handling.ipynb index 36ed4338..7b3a9b6a 100644 --- a/docs/notebooks/neuralnet/index_handling.ipynb +++ b/docs/notebooks/neuralnet/index_handling.ipynb @@ -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" ] }, { diff --git a/docs/notebooks/neuralnet/mnist_example_convolutional.ipynb b/docs/notebooks/neuralnet/mnist_example_convolutional.ipynb index d2decf54..836b2a94 100644 --- a/docs/notebooks/neuralnet/mnist_example_convolutional.ipynb +++ b/docs/notebooks/neuralnet/mnist_example_convolutional.ipynb @@ -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" ] }, { @@ -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", @@ -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", " )" ] }, diff --git a/docs/notebooks/neuralnet/mnist_example_dense.ipynb b/docs/notebooks/neuralnet/mnist_example_dense.ipynb index 58abe9e0..96536702 100644 --- a/docs/notebooks/neuralnet/mnist_example_dense.ipynb +++ b/docs/notebooks/neuralnet/mnist_example_dense.ipynb @@ -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" ] }, { @@ -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", @@ -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", " )" ] }, diff --git a/docs/notebooks/neuralnet/neural_network_formulations.ipynb b/docs/notebooks/neuralnet/neural_network_formulations.ipynb index ec397cc9..07613122 100644 --- a/docs/notebooks/neuralnet/neural_network_formulations.ipynb +++ b/docs/notebooks/neuralnet/neural_network_formulations.ipynb @@ -65,26 +65,27 @@ "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", @@ -92,9 +93,7 @@ " 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" ] }, { diff --git a/docs/notebooks/trees/bo_with_trees.ipynb b/docs/notebooks/trees/bo_with_trees.ipynb index 89ba1429..af632936 100644 --- a/docs/notebooks/trees/bo_with_trees.ipynb +++ b/docs/notebooks/trees/bo_with_trees.ipynb @@ -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", @@ -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", @@ -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", @@ -213,7 +216,6 @@ "metadata": {}, "outputs": [], "source": [ - "import numpy as np\n", "\n", "\n", "def add_unc_metric(opt_model, data):\n", diff --git a/docs/notebooks/trees/linear_tree_formulations.ipynb b/docs/notebooks/trees/linear_tree_formulations.ipynb index 801fbb4c..e7f263ae 100644 --- a/docs/notebooks/trees/linear_tree_formulations.ipynb +++ b/docs/notebooks/trees/linear_tree_formulations.ipynb @@ -57,29 +57,28 @@ "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 pandas as pd\n", "\n", "matplotlib.rc(\"font\", size=24)\n", "plt.rc(\"axes\", titlesize=24)\n", "\n", "# linear-tree objects\n", + "# pyomo for optimization\n", + "import pyomo.environ as pyo\n", "from lineartree import LinearTreeRegressor\n", "from sklearn.linear_model import LinearRegression\n", "\n", - "# pyomo for optimization\n", - "import pyomo.environ as pyo\n", + "import omlt\n", "\n", "# omlt for interfacing our linear tree with pyomo\n", "from omlt import OmltBlock\n", "from omlt.linear_tree import (\n", + " LinearTreeDefinition,\n", " LinearTreeGDPFormulation,\n", " LinearTreeHybridBigMFormulation,\n", - " LinearTreeDefinition,\n", - ")\n", - "import omlt" + ")" ] }, { diff --git a/src/omlt/linear_tree/lt_formulation.py b/src/omlt/linear_tree/lt_formulation.py index 5960a442..f70e4873 100644 --- a/src/omlt/linear_tree/lt_formulation.py +++ b/src/omlt/linear_tree/lt_formulation.py @@ -206,10 +206,8 @@ def _build_output_bounds(model_def, input_bounds): else: upper_bound += slopes[k] * input_bounds[k][1] + intercept lower_bound += slopes[k] * input_bounds[k][0] + intercept - if upper_bound >= bounds[1]: - bounds[1] = upper_bound - if lower_bound <= bounds[0]: - bounds[0] = lower_bound + bounds[1] = max(bounds[1], upper_bound) + bounds[0] = min(bounds[0], lower_bound) upper_bound = 0 lower_bound = 0 diff --git a/src/omlt/neuralnet/layer.py b/src/omlt/neuralnet/layer.py index 4d764c39..40e64f3e 100644 --- a/src/omlt/neuralnet/layer.py +++ b/src/omlt/neuralnet/layer.py @@ -416,7 +416,7 @@ class Layer2D(Layer): map indexes from this layer index to the input layer index size """ - def __init__( # noqa: PLR0913 + def __init__( self, input_size, output_size, diff --git a/tests/conftest.py b/tests/conftest.py index bcea6cff..6460461f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,9 +2,10 @@ import numpy as np import pytest +from pyomo.common.fileutils import this_file_dir + from omlt.neuralnet.layer import DenseLayer, InputLayer from omlt.neuralnet.network_definition import NetworkDefinition -from pyomo.common.fileutils import this_file_dir def get_neural_network_data(desc): @@ -47,13 +48,13 @@ def file(self, filename): return str(self._basedir / filename) -@pytest.fixture() +@pytest.fixture def datadir(): basedir = Path(this_file_dir()) / "models" return _Datadir(basedir) -@pytest.fixture() +@pytest.fixture def two_node_network_relu(): """Two node network with ReLU activation. diff --git a/tests/gbt/test_gbt_formulation.py b/tests/gbt/test_gbt_formulation.py index 910c3399..1d4ba621 100644 --- a/tests/gbt/test_gbt_formulation.py +++ b/tests/gbt/test_gbt_formulation.py @@ -2,6 +2,7 @@ import pyomo.environ as pe import pytest + from omlt import OmltBlock from omlt.dependencies import onnx, onnx_available from omlt.gbt.gbt_formulation import GBTBigMFormulation diff --git a/tests/io/test_keras_reader.py b/tests/io/test_keras_reader.py index 1982063b..5f738aca 100644 --- a/tests/io/test_keras_reader.py +++ b/tests/io/test_keras_reader.py @@ -1,4 +1,5 @@ import pytest + from omlt.dependencies import keras, keras_available NUM_LAYERS_131 = 3 diff --git a/tests/io/test_onnx_parser.py b/tests/io/test_onnx_parser.py index fc74b34c..4db71e03 100644 --- a/tests/io/test_onnx_parser.py +++ b/tests/io/test_onnx_parser.py @@ -1,4 +1,5 @@ import pytest + from omlt.dependencies import onnx, onnx_available NUM_LAYERS_131 = 3 diff --git a/tests/io/test_torch_geometric.py b/tests/io/test_torch_geometric.py index 53d9db58..43703d97 100644 --- a/tests/io/test_torch_geometric.py +++ b/tests/io/test_torch_geometric.py @@ -1,6 +1,7 @@ import numpy as np import pyomo.environ as pyo import pytest + from omlt import OmltBlock from omlt.dependencies import ( torch_available, @@ -8,11 +9,6 @@ ) if torch_available and torch_geometric_available: - from omlt.io.torch_geometric import ( - gnn_with_fixed_graph, - gnn_with_non_fixed_graph, - load_torch_geometric_sequential, - ) from torch.nn import Linear, ReLU, Sigmoid, Tanh from torch_geometric.nn import ( GCNConv, @@ -23,6 +19,12 @@ global_mean_pool, ) + from omlt.io.torch_geometric import ( + gnn_with_fixed_graph, + gnn_with_non_fixed_graph, + load_torch_geometric_sequential, + ) + @pytest.mark.skipif( not (torch_available and torch_geometric_available), diff --git a/tests/linear_tree/test_lt_formulation.py b/tests/linear_tree/test_lt_formulation.py index 4a8a9873..e3d2c2b1 100644 --- a/tests/linear_tree/test_lt_formulation.py +++ b/tests/linear_tree/test_lt_formulation.py @@ -1,16 +1,18 @@ import numpy as np import pyomo.environ as pe import pytest + from omlt.dependencies import lineartree_available if lineartree_available: from lineartree import LinearTreeRegressor + from sklearn.linear_model import LinearRegression + from omlt.linear_tree import ( LinearTreeDefinition, LinearTreeGDPFormulation, LinearTreeHybridBigMFormulation, ) - from sklearn.linear_model import LinearRegression import omlt from omlt import OmltBlock diff --git a/tests/neuralnet/test_keras.py b/tests/neuralnet/test_keras.py index f83c07d1..9eda9772 100644 --- a/tests/neuralnet/test_keras.py +++ b/tests/neuralnet/test_keras.py @@ -1,13 +1,15 @@ import numpy as np import pyomo.environ as pyo import pytest -from omlt.dependencies import keras, keras_available from pyomo.common.dependencies import DeferredImportError +from omlt.dependencies import keras, keras_available + if keras_available: from omlt.io import load_keras_sequential from conftest import get_neural_network_data + from omlt import OmltBlock from omlt.neuralnet import FullSpaceNNFormulation, ReducedSpaceNNFormulation from omlt.neuralnet.activations import ComplementarityReLUActivation diff --git a/tests/neuralnet/test_layer.py b/tests/neuralnet/test_layer.py index 6cf2b6de..2f42fc59 100644 --- a/tests/neuralnet/test_layer.py +++ b/tests/neuralnet/test_layer.py @@ -1,5 +1,6 @@ import numpy as np import pytest + from omlt.neuralnet.layer import ( ConvLayer2D, DenseLayer, diff --git a/tests/neuralnet/test_network_definition.py b/tests/neuralnet/test_network_definition.py index a18d7eea..609e12ec 100644 --- a/tests/neuralnet/test_network_definition.py +++ b/tests/neuralnet/test_network_definition.py @@ -3,6 +3,7 @@ import numpy as np import pyomo.environ as pyo import pytest + from omlt import OmltBlock from omlt.neuralnet.layer import DenseLayer, InputLayer from omlt.neuralnet.network_definition import NetworkDefinition diff --git a/tests/neuralnet/test_nn_formulation.py b/tests/neuralnet/test_nn_formulation.py index cc1796c3..9d0ed37c 100644 --- a/tests/neuralnet/test_nn_formulation.py +++ b/tests/neuralnet/test_nn_formulation.py @@ -5,6 +5,8 @@ import numpy as np import pyomo.environ as pyo import pytest +from pyomo.contrib.fbbt import interval + from omlt import OmltBlock from omlt.neuralnet import ( FullSpaceNNFormulation, @@ -31,7 +33,6 @@ partition_based_dense_relu_layer, ) from omlt.neuralnet.layers.reduced_space import reduced_space_dense_layer -from pyomo.contrib.fbbt import interval if TYPE_CHECKING: from omlt.formulation import _PyomoFormulation diff --git a/tests/neuralnet/test_onnx.py b/tests/neuralnet/test_onnx.py index 7d33675f..7c8014e3 100644 --- a/tests/neuralnet/test_onnx.py +++ b/tests/neuralnet/test_onnx.py @@ -2,20 +2,23 @@ import numpy as np import pytest -from omlt.dependencies import onnx, onnx_available from pyomo.common.dependencies import DeferredImportError +from omlt.dependencies import onnx, onnx_available + if onnx_available: import onnxruntime as ort + from omlt.io.onnx import ( load_onnx_neural_network, load_onnx_neural_network_with_bounds, write_onnx_model_with_bounds, ) +from pyomo.environ import ConcreteModel, SolverFactory, value + from omlt import OffsetScaling, OmltBlock from omlt.neuralnet import FullSpaceNNFormulation -from pyomo.environ import ConcreteModel, SolverFactory, value @pytest.mark.skipif(onnx_available, reason="Test only valid when onnx not available") diff --git a/tests/neuralnet/test_relu.py b/tests/neuralnet/test_relu.py index 7ec031ed..8a842f81 100644 --- a/tests/neuralnet/test_relu.py +++ b/tests/neuralnet/test_relu.py @@ -1,6 +1,7 @@ import numpy as np import pyomo.environ as pyo import pytest + from omlt import OmltBlock from omlt.dependencies import onnx_available from omlt.neuralnet import ( diff --git a/tests/neuralnet/train_keras_models.py b/tests/neuralnet/train_keras_models.py index a2a14d94..e68e1b1a 100644 --- a/tests/neuralnet/train_keras_models.py +++ b/tests/neuralnet/train_keras_models.py @@ -3,9 +3,10 @@ from keras.layers import Conv2D, Dense from keras.models import Sequential from keras.optimizers import Adamax -from omlt.io import write_onnx_model_with_bounds from pyomo.common.fileutils import this_file_dir +from omlt.io import write_onnx_model_with_bounds + def train_models(): # noqa: PLR0915 x, y, x_test = get_neural_network_data("131") diff --git a/tests/notebooks/test_run_notebooks.py b/tests/notebooks/test_run_notebooks.py index 5fab1c1e..db45f01d 100644 --- a/tests/notebooks/test_run_notebooks.py +++ b/tests/notebooks/test_run_notebooks.py @@ -2,14 +2,15 @@ from pathlib import Path import pytest +from pyomo.common.fileutils import this_file_dir +from testbook import testbook + from omlt.dependencies import ( keras_available, onnx_available, torch_available, torch_geometric_available, ) -from pyomo.common.fileutils import this_file_dir -from testbook import testbook def _test_run_notebook(folder, notebook_fname, n_cells): diff --git a/tests/test_block.py b/tests/test_block.py index 3c05a1e4..8867032e 100644 --- a/tests/test_block.py +++ b/tests/test_block.py @@ -1,5 +1,6 @@ import pyomo.environ as pyo import pytest + from omlt import OmltBlock INPUTS_LENGTH = 3 diff --git a/tests/test_formulation.py b/tests/test_formulation.py index 155e596c..9196e5e7 100644 --- a/tests/test_formulation.py +++ b/tests/test_formulation.py @@ -1,8 +1,9 @@ import pytest +from pyomo.environ import ConcreteModel, Objective, SolverFactory, value + from omlt import OmltBlock from omlt.formulation import _setup_scaled_inputs_outputs from omlt.scaling import OffsetScaling -from pyomo.environ import ConcreteModel, Objective, SolverFactory, value def test_scaled_inputs_outputs(): diff --git a/tests/test_scaling.py b/tests/test_scaling.py index dffc2a03..1161dd1f 100644 --- a/tests/test_scaling.py +++ b/tests/test_scaling.py @@ -2,6 +2,7 @@ import numpy as np import pytest + from omlt import OffsetScaling from omlt.scaling import convert_to_dict