Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LSTM model is giving an ValueError while predicting based on X_test data #44

Open
mayupova opened this issue Jul 28, 2022 · 1 comment

Comments

@mayupova
Copy link

Hi need a help to solve value erorr wile running LSTM. It seems everything works fine on training data but prediction generates less then expected dimensions
my x_train data shape is (846, 30, 3), my y_train data shape is (846,) my x_test 363, 30, 3), my y_test (363)
hat = modell.predict(test_X) generates (363, 100)

part of the code

reshape input to be 3D [samples, timesteps, features]

train_X = tr_xval.reshape((tr_xval.shape[0], 30, 3))
test_X = ts_xval.reshape((ts_xval.shape[0], 30, 3))
train_y=tr_yval
test_y=ts_yval
print(train_X.shape, train_y.shape, test_X.shape, test_y.shape)

design network

modell = Sequential()
modell.add(LSTM(200, activation='relu',input_shape=(train_X.shape[1], train_X.shape[2]),return_sequences=False,stateful=False))

#model.add(LSTM(neurons, batch_input_shape=(batch_size, X.shape[1], X.shape[2]), stateful=True))
modell.add(Dense(100, activation='relu'))
modell.compile(loss='mae', optimizer='adam',metrics=['accuracy'])
modell.summary()

fit network

history = modell.fit(train_X, train_y, epochs=200, batch_size=72, validation_data=(test_X, test_y), verbose=2, shuffle=False)

#works fin until here but then
yhat = modell.predict(test_X)

from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler(feature_range=(0, 1))
values=lnorm.values.astype('float32')
scaled = scaler.fit_transform(values)

invert scaling for forecast

inv_yhat = np.concatenate((yhat, test_X[:, -2:]), axis=1)
inv_yhat = scaler.inverse_transform(inv_yhat)
inv_yhat = inv_yhat[:,0]

invert scaling for actual

test_y = test_y.reshape((len(test_y), 1))
inv_y = np.concatenate((test_y, test_X[:, -2:]), axis=1)
inv_y = scaler.inverse_transform(inv_y)
inv_y = inv_y[:,0]


ValueError Traceback (most recent call last)
C:\Users\M55F1~1.AYU\AppData\Local\Temp/ipykernel_7572/1751946881.py in
5
6 # invert scaling for forecast
----> 7 inv_yhat = np.concatenate((yhat, test_X[:, -2:]), axis=1)
8 inv_yhat = scaler.inverse_transform(inv_yhat)
9 inv_yhat = inv_yhat[:,0]

<array_function internals> in concatenate(*args, **kwargs)

ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 3 dimension(s)

@xaxfan
Copy link

xaxfan commented Jul 28, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants