Keras Model / Keras Sequential Network Constraint
- pyscipopt_ml.keras.add_keras_constr(scip_model, keras_model, input_vars, output_vars=None, unique_naming_prefix='', output_type='regression', **kwargs)
Formulate keras_model into scip_model.
- Parameters:
scip_model (PySCIPOpt Model) – The SCIP model where the sequential model should be inserted.
keras_model (keras.Model <https://keras.io/api/models/model/>) – The keras model to insert as predictor.
input_vars (np.ndarray) – Decision variables used as input for the sequential neural network in PySCIPOpt Model.
output_vars (np.ndarray) – Decision variables used as output for the sequential neural network in the PySCIPOpt Model
unique_naming_prefix (str, optional) – A unique naming prefix that is used before all variable and constraint names. This parameter is important if the SCIP model is later printed to file and many predictors are added to the same SCIP model.
output_type ({"classification", "regression"}, default="regression") – If the option chosen is “classification” the output is 1 for exactly one class and 0 for all others. If the option chosen is “regression” then the output for each node of the final layer is the value from the keras model.
- Returns:
Object containing information about what was added to scip_model to formulate keras_model into it
- Return type:
- Raises:
NoModel – If the translation for some of the Keras model structure (layer or activation) is not implemented.
Warning
Only Dense (with relu / tanh / sigmoid / softplus / softmax activation) are supported.
Notes
See
add_predictor_constrfor acceptable values for input_vars and output_vars
- class pyscipopt_ml.keras.keras.KerasNetworkConstr(scip_model, predictor, input_vars, output_vars, unique_naming_prefix, output_type, **kwargs)
Transform a keras dense Neural Network to SCIP constraints with input and output as matrices of variables.
Stores the changes to the SCIP Model for representing an instance into it. Inherits from
AbstractPredictorConstr..- get_error(eps=None)
Returns error in SCIP’s solution with respect to the actual output of the keras model
- Parameters:
eps (float or int or None, optional) – The maximum allowed tolerance for a mismatch between the actual predictive model and SCIP. If the error is larger than eps an appropriate warning is printed
- Returns:
error – The absolute values of the difference between SCIP’s solution and the trained ML model’s output given the input as defined by SCIP. The matrix is the same dimension as the output of the trained predictor. Using torch / pyscipopt, the absolute difference between model.forward(input) and scip.getVal(output).
- Return type:
np.ndarray
- Raises:
NoSolution – If SCIP has no solution (either was not optimized or is infeasible).