4
$\begingroup$

I want to predict value in every 120 minutes continuous using LSTM model. Here I wrote the code for predction. But I'm not getting proper prediction values . Here from start time I need to predict values in every 120 minutes.

model = Sequential()
model.add(LSTM(4, return_sequences=True,input_shape=(x_train_n.shape[2])))  
# returns a sequence of vectors of dimension 32
model.add(LSTM(8, return_sequences=True))  # returns a sequence of vectors 
of dimension 32
model.add(LSTM(8))  # return a single vector of dimension 32
model.add(Dense(1))
batchsize = 1
model.compile(loss="mean_squared_error",optimizer="adam")
#model.compile(loss='mean_squared_error', optimizer='adadelta',metrics= ['accuracy'])
history = model.fit(x_train_n,y_train_n, batch_size = batchsize, 
nb_epoch=30,validation_data=(x_test_n, y_test_n),shuffle =True)\
model.reset_states()
pred1=model.predict(x_test_n)
pred2= model.predict(x_train_n)

#end of the sequences
model.reset_states()

#data=pd.DataFrame(fit1.predict(x_test))

pred1 = scaler_y.inverse_transform(np.array(pred1).reshape ((len(pred1), 1)))
real_test = scaler_y.inverse_transform(np.array(y_test_n).reshape 
((len(y_test_n), 1))).astype(int)
real = scaler_y.inverse_transform(np.array(y_train_n).reshape 
((len(y_train_n), 1))).astype(int)
pred1 = pred1[:,0]
real_test = real_test[:,0]

#predition in every 120 minutes

sequence_timestep = 120
last_sequence_train = x_train_n[-1]
pred1 = []
def sequence_constructor():
   if len(pred1) >= sequence_timestep:
     new_sequence = pred1[-dimension_seq:]
   else:
      splitter = sequence_timestep - len(pred1)
      part_1 = last_sequence_train[-splitter:]
      new_sequence = np.append(part_1,pred1) #Concatenate 2 list
  new_sequence = np.array(new_sequence)
  return new_sequence  
for i in range(1440):
new_sequence = sequence_constructor()
new_prediction = model.predict(new_sequence)
pred1.append(new_prediction)  

when I wrote this code error is coming and it's not predicting the values properly. Here I upload my csv file also, and I wrote what I'm trying to do to predict my value. In my csv file g and p in my two inputs. When the prediction values in every 120 minutes will be the next input of my LSTM . According to my csv file 10/3/2018 start time = 6:00:00 a.m from that predict the g values in every 120 minutes. Then again next day start time is again 6:00:00 a.m from that time again new prediction values in every 120 minutes.

enter image description here

$\endgroup$
1
$\begingroup$

if you want to predict exactly at a particular timestamp, & you do not want to train the model again and again then may be you can run a crontab on a python file to load the model and predict the value, with cron job starting at 6 AM & execute again at every 120 minutes

Even if you have a stream of data coming continuously you can re-train the model in between those 120 minutes.

| improve this answer | |
$\endgroup$
  • $\begingroup$ If my starting time is not 6:00 a.m in every time . Then how to train the model according to that? $\endgroup$ – user59020 May 28 '19 at 5:22
  • $\begingroup$ I didn't get it what you meant in the comment but I will suggest you to use a proper (time series analysis) tsa algorithm. Usually people use sliding window approach with LSTM & can predict in future till the size of the sliding window. With algos like ARIMA,SARIMA,holt you can predict values for any timestamp in future. Then you do not even need to run a crontab. $\endgroup$ – Rohan Kumar May 28 '19 at 6:35
  • $\begingroup$ Got it what you are trying to say. I will try that method. Thank you for responsing me. $\endgroup$ – user59020 May 28 '19 at 8:24

Your Answer

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