0
$\begingroup$

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)))
train_df.columns=['var1(t-3)','var2(t-3)','var3(t-3)','var4(t-3)','var1(t-2)','var2(t-2)','var3(t-2)','var4(t-2)','var1(t-1)','var2(t-1)','var3(t-1)','var4(t-1)','var1(t)','var2(t)','var3(t)','var4(t)']
train_X=train_df.drop(['var1(t)'],axis=1)
train_y=train_df[['var1(t)']]

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_X=train_X.reshape(train_X.shape[0],1,train_X.shape[1])
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.

$\endgroup$

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.