OMLT Block
OmltBlock.
The omlt.block module contains the implementation of the OmltBlock class. This class is used in combination with a formulation object to construct the necessary constraints and variables to represent ML models.
Example
import tensorflow.keras as keras
from omlt import OmltBlock
from omlt.neuralnet import FullSpaceNNFormulation
from omlt.io import load_keras_sequential
nn = keras.models.load_model(keras_fname)
net = load_keras_sequential(nn)
m = pyo.ConcreteModel()
m.neural_net_block = OmltBlock()
m.neural_net_block.build_formulation(FullSpaceNNFormulation(net))
m.obj = pyo.Objective(expr=(m.neural_net_block.outputs[2]-4.0)**2)
status = pyo.SolverFactory('ipopt').solve(m, tee=True)
pyo.assert_optimal_termination(status)
- class omlt.block.OmltBlock(*args, **kwargs)
Bases:
CustomBlock
Note
OmltBlock is the name used to declare the custom Pyomo block which is exposed to the user. The block functionality is given by OmltBlockData which inherits from Pyomo BlockData.
- class omlt.block.OmltBlockData(component)[source]
Bases:
BlockData
- _setup_inputs_outputs(*, input_indexes, output_indexes)[source]
Setup inputs and outputs.
This function should be called by the derived class to create the inputs and outputs on the block
- Parameters:
input_indexes – list list of indexes (can be tuples) defining the set to be used for the input variables
output_indexes – list list of indexes (can be tuples) defining the set to be used for the input variables
- build_formulation(formulation)[source]
Build formulation.
Call this method to construct the constraints (and possibly intermediate variables) necessary for the particular neural network formulation. The formulation object can be accessed later through the “formulation” attribute.
- Parameters:
formulation (instance of _PyomoFormulation) – see, for example, FullSpaceNNFormulation