I am having a training data set for a time-series dataset like below:

import numpy as np
import pandas as pd
train_df = pd.DataFrame(np.random.randint(0,100,size=(100, 16)))

So as you see for training I am inputting the network with past three timesteps of the all 4 variables and the current timestep of the three remaining variables which makes up to 15 variables.

I want to train this with an LSTM with functional API in Keras because I cannot use sequential API in my case due to different input size. So I tried the below:

import tensorflow
from tensorflow.keras.utils import plot_model
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import LSTM
visible = Input(shape=(100,15))
hidden1 = LSTM(10)(visible)
hidden2 = Dense(10, activation='relu')(hidden1)
output = Dense(1, activation='sigmoid')(hidden2)
model = Model(inputs=visible, outputs=output)
model.compile(loss='mae', optimizer='adam')

history = model.fit(train_X, train_y, batch_size=64, epochs=2, validation_split=0.2)

But this throws the error like below:

ValueError: Input 0 of layer lstm_2 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 15]

Then I reshape the train and test like below:

train_y = train_y.reshape(train_y.shape[0],1)

train_X shape is (100,15)
train_y shape is (100,1)

But this gave a very huge MAPE and RMSE.So I think my reshaping is wrong here.

Any help is appreciated.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.