I am trying to solve an optimization problem that has a neural network as a constraint. I am creating that constraint within Pyomo using the package OMLT. I am using Cyipopt as the optimizer. I am doing a comparative study using the Pynumero greybox platform which is only compatible with Cyipopt, thus, I must use Cyipopt to keep the optimizer consistent. It starts to run as expected but at some point the kernel inadvertently crashes. Does anyone know any work around this issue?
I think that there must be some incompatibility issue with the packages. I installed most of them from conda, some are only available from pip. I have try different versions and changing the source, but it does not seem to resolve it. The following is a toy example where this problems occurs.
import torch
import numpy as np
import tempfile
from omlt.io import write_onnx_model_with_bounds,load_onnx_neural_network
from omlt import OmltBlock, OffsetScaling
from omlt.neuralnet import FullSpaceNNFormulation
import onnx
import pyomo.environ as pyo
import matplotlib.pyplot as plt
class neuralnetwork(torch.nn.Module):
def __init__(self):
super().__init__()
self.activ = torch.nn.Sigmoid()
self.Dense1 = torch.nn.Linear(2,5)
self.Dense2 = torch.nn.Linear(5,1)
def forward(self, x):
x = self.Dense1(x)
x = self.activ(x)
x= self.Dense2(x)
return x
def PyomoModel(NN):
x = torch.randn(1,2)
with tempfile.NamedTemporaryFile(suffix='.onnx', delete=False) as f:
torch.onnx.export(
NN,
x,
f,
input_names=['input'],
output_names=['output'],
dynamic_axes={
'input': {0: 'batch_size'},
'output': {0: 'batch_size'}
}
)
input_bounds = [(-1,1) for _ in range(2)]
scaler = OffsetScaling(
offset_inputs=np.zeros(2),
factor_inputs=np.ones(2),
offset_outputs=np.zeros(1),
factor_outputs=np.ones(1)
)
write_onnx_model_with_bounds(f.name, None, input_bounds)
onnx_model = onnx.load(f.name)
print(f"Wrote PyTorch model to {f.name}")
network_definition = load_onnx_neural_network(onnx_model, scaling_object=scaler)
formulation = FullSpaceNNFormulation(network_definition)
mdl = pyo.ConcreteModel()
mdl.nn = OmltBlock()
mdl.nn.build_formulation(formulation)
mdl.cons1 = pyo.Constraint(expr=-1<=mdl.nn.inputs[1])
mdl.cons2 =pyo.Constraint(expr=mdl.nn.inputs[1]<=1)
mdl.obj = pyo.Objective(expr=mdl.nn.outputs[0]**2)
return mdl
def main():
NN = neuralnetwork()
mdl = PyomoModel(NN)
x_1 = np.linspace(-1,1,10)
x_2 = []
y = []
solver = pyo.SolverFactory('cyipopt')
for i in range(10):
mdl.nn.inputs[0].fix(x_1[i])
results = solver.solve(mdl, tee=True)
x_2.append(mdl.nn.inputs[1].value)
y.append(mdl.nn.outputs[0].value)
plt.plot(x_1[0:i+1], x_2, 'o')
plt.plot(x_1[0:i+1], y, 'o')
plt.show()
if __name__ == "__main__":
main()
Edit
I have tried using OMLT 1.1, 1.2, 1.0 installed from pip, pynumero_libraries 1.3, 1.2 installed with conda-forge,
and pytorch 2.0.1, 2.4.0, 2.3.0 installed from pip and conda with and without cuda.
Additionally, I recently noticed that when I run it from the terminal, it shows the following error which VScode does not show.
FileNotFoundError: [Errno 2] No such file or directory: 'c:\Users\~\AppData\Local\Programs\Microsoft VS Code\Untitled-1'
OMP: Error #15: Initializing libomp.dll, but found libiomp5md.dll already initialized.
OMP: Hint: This means that multiple copies of the OpenMP runtime have been linked into the program. That is dangerous, since it can degrade performance or cause incorrect results. The best thing to do is to ensure that only a single OpenMP runtime is linked into the process, e.g. by avoiding static linking of the OpenMP runtime in any library. As an unsafe, unsupported, undocumented workaround you can set the environment variable KMP_DUPLICATE_LIB_OK=TRUE to allow the program to continue to execute, but that may cause crashes or silently produce incorrect results. For more information, please see http://openmp.llvm.org/
2