cog-imperial / OMLT

Represent trained machine learning models as Pyomo optimization formulations
Other
257 stars 56 forks source link

Question: Reshaping input tensor x before eval? #94

Closed adi4656 closed 1 year ago

adi4656 commented 1 year ago

Hi @fracek, I'm having an issue with figuring out whether the input tensor x should be reshaped to the layer's input_size (as opposed to keeping it as the input layer's output shape) before being passed to Layer._eval. Right now this is what happens, in neuralnet/layer.py::90-93 (in Layer.eval). This reshaping also seems to be what is assumed by the dense layer code. However, the convolutional layer code seems to assume this reshaping should not happen. In neuralnet/layer.py::296 (in ConvLayer._eval), x[index] is used to access elements of the input tensor x, x being the reshaped tensor passed to Layer._eval. However, index here is yielded from the ConvLayer.kernel_with_input_indexes generator. At neuralnet/layer.py::265, this index is mapped back to an input layer output index. Since this index is used to index x, this would suggest x should not be reshaped, as should remain the input layer output shape. As a result, the status quo (reshaping x before passing it to Layer._eval) keeps the dense layer code fine but breaks convolutional layer code with index mapping. Not reshaping x fixes this issue but then breaks the dense layer code. I cannot figure out whether the convolutional layer assumption or the dense layer assumption is the one I should be using, as index mapping is not thoroughly covered by the tests for the input and convolutional layers. Any help would be greatly appreciated! :)