Gurobi / gurobi-machinelearning

Formulate trained predictors in Gurobi models
https://gurobi-machinelearning.readthedocs.io/
Apache License 2.0
207 stars 43 forks source link

Bug in adding PyTorch models as constraints #315

Closed fuadcan closed 4 weeks ago

fuadcan commented 3 months ago

Hi,

I have a PyTorch NN model with three 256 neuron linear hidden layers with bias=True, ReLU activation function between each layer and a 12-neuron linear output layer with bias=False. The current code seems to assume output layer has bias=True. Otherwise, it takes last bias vector as output layer's bias. Because my last bias is hidden layer's bias with dimension of (256,), it tries to add it to output layer and throws an error.

I used both add_sequential_constr and add_predictor_constr. For both, the error is at gurobi_ml/modeling/neuralnet/activations.py:48 where it calculates layer.gp_model.addConstr(output == **layer.input @ layer.coefs + layer.intercept**)

I turned output layer bias=True and added torch.zeros(12) as parameters and the problem is solved. It would be nice to have it fixed.

pobonomo commented 3 months ago

Hi, Thanks for reporting it. Yes it's clearly a bug.

Glad you found a workaround for now. I will try to fix it.

bzfwunde commented 2 months ago

@fuacan, please check the pull request to confirm that it fixes your issue. Thanks.