pyomo icon indicating copy to clipboard operation
pyomo copied to clipboard

How to optimize neural network model using pyomo?

Open wangyexiang opened this issue 2 years ago • 2 comments

I have a trained neural network model developed using the tensorflow. It is a regression problem, where I am trying to predict an output variable using some 14 input variables or features. As a next step, I would like to minimize my output and want to determine what configuration/values these 14 inputs would take to get to the minimal value of the output.

So, essentially, I would like to pass the trained model object as my objective function in a solver, and also a bunch of constraints on the input variables to optimize/minimize the objective. But, it is not successful. It gives me error: TypeError: Implicit conversion of Pyomo NumericValue type x[1] to a float is disabled. This error is often the result of using Pyomo components as arguments to one of the Python built-in math module functions when defining expressions. Avoid this error by using Pyomo-provided math functions.

How to fix it? Thanks in advance!

wangyexiang avatar Sep 01 '21 03:09 wangyexiang

Based on the error I think your problem is that the model coming back from tensorflow is using intrinsic functions coming from Python or Numpy instead of the ones provided by Pyomo. You need to either convert the model to explicit Pyomo expression trees or treat the model as an external function/blackbox. I think there are some general tools under development for doing exactly this but I'm not sure if they have been released yet.

blnicho avatar Sep 01 '21 18:09 blnicho

treat the model as an external function/blackbox.

Dear sir, Thank for your reply. Is external function/blackbox mean pyomo.core.expr.current.ExternalFunction? If yes, could you give me an example about how to use ExternalFunction API to optimize a machine learning model?Thanks in advance!@blnicho

wangyexiang avatar Sep 02 '21 06:09 wangyexiang

@wangyexiang there is a new package out called OMLT for representing machine learning models within a Pyomo model. You can find it here: https://github.com/cog-imperial/OMLT

blnicho avatar Nov 03 '22 22:11 blnicho