Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix notebook; re-add workflow #145

Merged
merged 10 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
name: CI

on:
push:
branches: ["main","github-actions"]
pull_request:
branches: ["main"]
workflow_dispatch:

jobs:
tests:
name: "Python ${{ matrix.python-version }}"
runs-on: "ubuntu-latest"

strategy:
matrix:
python-version: ["3.8", "3.9", "3.10"]

steps:
- uses: "actions/checkout@v2"
- uses: "actions/setup-python@v2"
- uses: "s-weigand/setup-conda@v1"
with:
python-version: "${{ matrix.python-version }}"

- name: Install solvers
run: sudo apt-get install -y glpk-utils coinor-cbc

- name: "Install dependencies"
run: |
set -xe
python -VV
python -m site
python -m pip install --upgrade pip setuptools wheel
python -m pip install --upgrade coverage[toml] virtualenv tox tox-gh-actions
conda install -c conda-forge ipopt
conda install -c conda-forge pyscipopt

- name: "Run tox targets with lean testing environment for ${{ matrix.python-version }}"
run: "tox -re leanenv"

- name: "Run tox targets for ${{ matrix.python-version }}"
run: "tox"

- name: "Convert coverage"
run: "python -m coverage xml"

- name: "Upload coverage to Codecov"
uses: "codecov/codecov-action@v4"
with:
fail_ci_if_error: true
8 changes: 4 additions & 4 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
# All configuration values have a default; values that are commented out
# serve to show the default.

import os
import sys
import inspect
import os
import shutil
import sys

# -- Path setup --------------------------------------------------------------

Expand Down Expand Up @@ -77,7 +77,7 @@
"sphinx.ext.doctest",
"sphinx.ext.ifconfig",
"sphinx.ext.mathjax",
"sphinx.ext.napoleon"
"sphinx.ext.napoleon",
]

# Add any paths that contain templates here, relative to this directory.
Expand Down Expand Up @@ -147,7 +147,7 @@

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#html_theme = "furo"
# html_theme = "furo"
html_theme = "sphinx_rtd_theme"

# Theme options are theme-specific and customize the look and feel of a theme
Expand Down
17 changes: 11 additions & 6 deletions docs/notebooks/data/build_sin_quadratic_csv.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
from random import random

import numpy as np
import pandas as pd
from random import random

n_samples = 10000
w = 5

x = np.linspace(-2,2,n_samples)
df = pd.DataFrame(x, columns=['x'])
df['y'] = np.sin(w*x) + x**2 + np.array([np.random.uniform()*0.1 for _ in range(n_samples)])
x = np.linspace(-2, 2, n_samples)
df = pd.DataFrame(x, columns=["x"])
df["y"] = (
np.sin(w * x)
+ x**2
+ np.array([np.random.uniform() * 0.1 for _ in range(n_samples)])
)

plt.plot(df['x'],df['y'])
plt.plot(df["x"], df["y"])
plt.show()

df.to_csv("sin_quadratic.csv")
df.to_csv("sin_quadratic.csv")
1,492 changes: 895 additions & 597 deletions docs/notebooks/neuralnet/neural_network_formulations.ipynb

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ python_requires = >=3.7
install_requires =
importlib-metadata; python_version<"3.8"
networkx
pyomo
pyomo==6.6.2
numpy
protobuf==3.20.3

Expand All @@ -72,14 +72,14 @@ testing =
nbmake
tox
flake8
tensorflow
tensorflow-cpu
ipywidgets
jupyter
lightgbm
linear-tree
matplotlib
pandas
keras
keras==2.9.0
onnx
onnxruntime
onnxmltools
Expand Down
1 change: 1 addition & 0 deletions src/omlt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
sequential Keras and general ONNX models.

"""

import sys

if sys.version_info[:2] >= (3, 8):
Expand Down
1 change: 1 addition & 0 deletions src/omlt/gbt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
F_{t,l} &:= \text{Weight of leaf $l$ in tree $t$}\\
\end{align*}
"""

from omlt.gbt.gbt_formulation import GBTBigMFormulation
from omlt.gbt.model import GradientBoostedTreeModel
2 changes: 1 addition & 1 deletion src/omlt/io/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from omlt.dependencies import (
onnx_available,
keras_available,
onnx_available,
torch_available,
torch_geometric_available,
)
Expand Down
7 changes: 3 additions & 4 deletions src/omlt/io/torch_geometric/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from omlt.io.torch_geometric.torch_geometric_reader import (
load_torch_geometric_sequential,
)

from omlt.io.torch_geometric.build_gnn_formulation import (
gnn_with_fixed_graph,
gnn_with_non_fixed_graph,
)
from omlt.io.torch_geometric.torch_geometric_reader import (
load_torch_geometric_sequential,
)
5 changes: 4 additions & 1 deletion src/omlt/io/torch_geometric/build_gnn_formulation.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import numpy as np
import pyomo.environ as pyo

from omlt.io.torch_geometric.torch_geometric_reader import (
load_torch_geometric_sequential,
)
from omlt.neuralnet import FullSpaceNNFormulation
from omlt.io.torch_geometric import load_torch_geometric_sequential


def gnn_with_non_fixed_graph(
Expand Down
5 changes: 3 additions & 2 deletions src/omlt/io/torch_geometric/torch_geometric_reader.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import warnings

import numpy as np

from omlt.neuralnet.layer import DenseLayer, InputLayer, GNNLayer
from omlt.neuralnet.layer import DenseLayer, GNNLayer, InputLayer
from omlt.neuralnet.network_definition import NetworkDefinition
import warnings


def _compute_gcn_norm(A):
Expand Down
3 changes: 2 additions & 1 deletion src/omlt/linear_tree/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
b_{\ell} &:= \text{Bias term learned by the tree for leaf } \ell \in L\\
\end{align*}
"""

from omlt.linear_tree.lt_definition import LinearTreeDefinition
from omlt.linear_tree.lt_formulation import (
LinearTreeGDPFormulation,
LinearTreeHybridBigMFormulation,
)
from omlt.linear_tree.lt_definition import LinearTreeDefinition
2 changes: 1 addition & 1 deletion src/omlt/linear_tree/lt_definition.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import numpy as np
import lineartree
import numpy as np


class LinearTreeDefinition:
Expand Down
1 change: 1 addition & 0 deletions src/omlt/neuralnet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
where :math:`\mathbf z^{(0)}` is the output of `InputLayer`, :math:`\hat{\mathbf z}^{(l)}` is the pre-activation output of :math:`l`-th layer, :math:`\mathbf z^{(l)}` is the post-activation output of :math:`l`-th layer.

"""

from omlt.neuralnet.network_definition import NetworkDefinition
from omlt.neuralnet.nn_formulation import (
FullSpaceNNFormulation,
Expand Down
1 change: 1 addition & 0 deletions src/omlt/neuralnet/activations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Since all activation functions are element-wised, we only consider how to formulate activation functions for a single neuron, where :math:`x` denotes pre-activation variable, and :math:`y` denotes post-activation variable.

"""

from .linear import linear_activation_constraint, linear_activation_function
from .relu import ComplementarityReLUActivation, bigm_relu_activation_constraint
from .smooth import (
Expand Down
3 changes: 2 additions & 1 deletion src/omlt/neuralnet/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
\end{align*}

"""

import itertools

import numpy as np
Expand Down Expand Up @@ -254,7 +255,7 @@ class GNNLayer(DenseLayer):
.. math::

\begin{align*}
y_j = \sigma \left(\sum\limits_{i=0}^{F_{in}-1}A_{u,v}w_{ij}x_i+b_j\right), && \forall 0\le j<F_{out},
y_j = \sigma \left(\sum\limits_{i=0}^{F_{in}-1}A_{u,v}w_{ij}x_i+b_j\right), && \forall 0\le j<F_{out},
\end{align*}


Expand Down
1 change: 1 addition & 0 deletions src/omlt/neuralnet/layers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
\end{align*}

"""

from .full_space import full_space_conv2d_layer, full_space_dense_layer
from .reduced_space import reduced_space_dense_layer
4 changes: 2 additions & 2 deletions src/omlt/neuralnet/nn_formulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
from omlt.neuralnet.layer import (
ConvLayer2D,
DenseLayer,
GNNLayer,
InputLayer,
PoolingLayer2D,
GNNLayer,
)
from omlt.neuralnet.layers.full_space import (
full_space_conv2d_layer,
full_space_dense_layer,
full_space_maxpool2d_layer,
full_space_gnn_layer,
full_space_maxpool2d_layer,
)
from omlt.neuralnet.layers.partition_based import (
default_partition_split_func,
Expand Down
17 changes: 12 additions & 5 deletions tests/io/test_torch_geometric.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pytest
import numpy as np
import pyomo.environ as pyo
from omlt import OmltBlock
import pytest

from omlt import OmltBlock
from omlt.dependencies import (
torch,
torch_available,
Expand All @@ -12,12 +12,19 @@

if torch_available and torch_geometric_available:
from torch.nn import Linear, ReLU, Sigmoid, Softplus, Tanh
from torch_geometric.nn import Sequential, GCNConv, SAGEConv
from torch_geometric.nn import global_mean_pool, global_add_pool, global_max_pool
from torch_geometric.nn import (
GCNConv,
SAGEConv,
Sequential,
global_add_pool,
global_max_pool,
global_mean_pool,
)

from omlt.io.torch_geometric import (
load_torch_geometric_sequential,
gnn_with_fixed_graph,
gnn_with_non_fixed_graph,
load_torch_geometric_sequential,
)


Expand Down
4 changes: 2 additions & 2 deletions tests/linear_tree/test_lt_formulation.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import numpy as np
import pyomo.environ as pe
import pytest
from pytest import approx

from omlt.dependencies import lineartree_available
from pytest import approx

if lineartree_available:
from lineartree import LinearTreeRegressor
Expand All @@ -14,8 +14,8 @@
LinearTreeDefinition,
)

from omlt import OmltBlock
import omlt
from omlt import OmltBlock

scip_available = pe.SolverFactory("scip").available()
cbc_available = pe.SolverFactory("cbc").available()
Expand Down
2 changes: 1 addition & 1 deletion tests/neuralnet/test_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
from omlt.neuralnet.layer import (
ConvLayer2D,
DenseLayer,
GNNLayer,
IndexMapper,
InputLayer,
PoolingLayer2D,
GNNLayer,
)


Expand Down
6 changes: 3 additions & 3 deletions tests/neuralnet/test_nn_formulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@
from omlt.neuralnet.layer import (
ConvLayer2D,
DenseLayer,
GNNLayer,
IndexMapper,
InputLayer,
PoolingLayer2D,
GNNLayer,
)
from omlt.neuralnet.layers.full_space import (
full_space_maxpool2d_layer,
_input_layer_and_block,
full_space_maxpool2d_layer,
)
from omlt.neuralnet.layers.partition_based import (
partition_based_dense_relu_layer,
default_partition_split_func,
partition_based_dense_relu_layer,
)
from omlt.neuralnet.layers.reduced_space import reduced_space_dense_layer

Expand Down
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ envlist = py36, py37, py38, py39, py310, lint
python =
3.6: py36
3.7: py37
3.8: py38, lint
3.8: lint, py38
3.9: py39
3.10: py310

Expand Down Expand Up @@ -100,7 +100,7 @@ deps =
pep8-naming
commands =
flake8 --config=tox.ini src/omlt tests/
black --check --diff src/omlt tests
black --check --diff src/omlt tests/

[testenv:format]
description = Format Python files using isort and black
Expand Down
Loading