Package: omlt
OMLT: Optimization and Machine Learning ToolkitOMLT is a Python package for representing machine learning models (neural networks and gradient-boosted trees) within the Pyomo optimization environment. The package provides various optimization formulations for machine learning models (such as full-space, reduced-space, and MILP) as well as an interface to import sequential Keras and general ONNX models.Please reference thepreprintof this software package as:@misc{ceccon2022omlt,
     title={OMLT: Optimization & Machine Learning Toolkit},
     author={Ceccon, F. and Jalving, J. and Haddad, J. and Thebelt, A. and Tsay, C. and Laird, C. D. and Misener, R.},
     year={2022},
     eprint={2202.02414},
     archivePrefix={arXiv},
     primaryClass={stat.ML}
}DocumentationThe latest OMLT documentation can be found at thereadthedocs page. Additionally, much of the current functionality is demonstrated using Jupyter notebooks available in thenotebooks folder.Exampleimporttensorflowimportpyomo.environaspyofromomltimportOmltBlock,OffsetScalingfromomlt.neuralnetimportFullSpaceNNFormulation,NetworkDefinitionfromomlt.ioimportload_keras_sequential#load a Keras modelnn=tensorflow.keras.models.load_model('tests/models/keras_linear_131_sigmoid',compile=False)#create a Pyomo model with an OMLT blockmodel=pyo.ConcreteModel()model.nn=OmltBlock()#the neural net contains one input and one outputmodel.input=pyo.Var()model.output=pyo.Var()#apply simple offset scaling for the input and outputscale_x=(1,0.5)#(mean,stdev) of the inputscale_y=(-0.25,0.125)#(mean,stdev) of the outputscaler=OffsetScaling(offset_inputs=[scale_x[0]],factor_inputs=[scale_x[1]],offset_outputs=[scale_y[0]],factor_outputs=[scale_y[1]])#provide bounds on the input variable (e.g. from training)scaled_input_bounds={0:(0,5)}#load the keras model into a network definitionnet=load_keras_sequential(nn,scaler,scaled_input_bounds)#multiple formulations of a neural network are possible#this uses the default NeuralNetworkFormulation objectformulation=FullSpaceNNFormulation(net)#build the formulation on the OMLT blockmodel.nn.build_formulation(formulation)#query inputs and outputs, as well as scaled inputs and outputsmodel.nn.inputsmodel.nn.outputsmodel.nn.scaled_inputsmodel.nn.scaled_outputs#connect pyomo model input and output to the neural network@model.Constraint()defconnect_input(mdl):returnmdl.input==mdl.nn.inputs[0]@model.Constraint()defconnect_output(mdl):returnmdl.output==mdl.nn.outputs[0]#solve an inverse problem to find that input that most closely matches the output value of 0.5model.obj=pyo.Objective(expr=(model.output-0.5)**2)status=pyo.SolverFactory('ipopt').solve(model,tee=False)print(pyo.value(model.input))print(pyo.value(model.output))DevelopmentOMLT usestoxto manage development tasks:tox -avto list available taskstoxto run teststox -e lintto check formatting and code stylestox -e formatto automatically format filestox -e docsto build the documentationtox -e publishto publish the package to PyPiContributorsGitHubNameAcknowledgementsJordan JalvingThis work was funded by Sandia National Laboratories, Laboratory Directed Research and Development programFrancesco CecconThis work was funded by an Engineering & Physical Sciences Research Council Research Fellowship [GrantNumber EP/P016871/1]Carl D. LairdInitial work was funded by Sandia National Laboratories, Laboratory Directed Research and Development program. Current work supported by Carnegie Mellon University.Calvin TsayThis work was funded by an Engineering & Physical Sciences Research Council Research Fellowship [GrantNumber EP/T001577/1], with additional support from an Imperial College Research Fellowship.Alexander ThebeltThis work was supported by BASF SE, Ludwigshafen am Rhein.