gurobi-machinelearning icon indicating copy to clipboard operation
gurobi-machinelearning copied to clipboard

Bug in adding PyTorch models as constraints

Open fuadcan opened this issue 8 months ago • 2 comments

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.

fuadcan avatar Jun 19 '24 21:06 fuadcan