wilsonrljr / sysidentpy

A Python Package For System Identification Using NARMAX Models
https://sysidentpy.org
BSD 3-Clause "New" or "Revised" License
380 stars 77 forks source link

predict bug #90

Closed AntoineDubois closed 1 year ago

AntoineDubois commented 1 year ago

Hello, I use a neural network to predict 1-dim Y from 30-dim X. Fitting works. However, narx_net.predict(X=X_valid, y=Y_valid) returns "RuntimeError: input must have 3 dimensions, got 2" How can I fix this error?

Thank you for this beautiful package

AntoineDubois commented 1 year ago

I have found the bug. When the input is multi-dimensional, model.predict does: model.network(x[i]). Thus, x[i] has 1 dimension instead of 2. As a quick fix, I did: if x.dim() == 1: x = x[None,:]

I think it would be nice to fix this 😀

wilsonrljr commented 1 year ago

Hey @AntoineDubois! Thanks for reaching out.

Could you share a code snippet? I don't know if I understood your problem completely. You should pass 2 dimensional arrays in both fit and predict methods. The y array must be a 2 dimensional array with 1 column. If the X array has only one input, it must be a 2 dimensional array with 1 column as well. If the X array is a multiple input array, it must be a 2 dimensional array with n columns, where n is the number of inputs you have. That is how it should be used. Is that your case?

AntoineDubois commented 1 year ago

Y.dim() =2, X.dim()=2 and the shapes are coherent. The NARXNN.fit works well. With the same inputs X and Y, NARXNN.predict does X[i]. X[i].dim() =1 and the neural network cannot pass the input. I am suspecting the problem comes from _narmax_predict(self, X, y_initial, forecast_horizon) in class ModelPredict.

Capture d’écran 2023-02-01 à 09 28 07

To fix this issue, I added if x.dim() == 1: x = x[None,:] in ModelNN.forward: def forward(self, x):\\ if x.dim() == 1: x = x[None,:]\\ x = self.layer(x)\\ x = self.act(x) I hope it helps.

wilsonrljr commented 1 year ago

fixed in v0.3.0