Please let us know anything wrong in below code, not getting desire result -

from numpy import sqrt
from numpy import asarray
from pandas import read_csv
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import LSTM
import tensorflow as tf
from sklearn import metrics
from sklearn.model_selection import train_test_split
  • Assign the value as 40 to the variabel RANDOM_SEED which will be the seed value.
  • Set the random seed value using the value stored in the variable RANDOM_SEED .

# split a univariate sequence into samples

def split_sequence(sequence, n_steps):
    X, y = list(), list()
    for i in range(len(sequence)):
        # find the end of this pattern
        end_ix = i + n_steps
        # check if we are beyond the sequence
        if end_ix > len(sequence)-1:
        # gather input and output parts of the pattern
        seq_x, seq_y = sequence[i:end_ix], sequence[end_ix]
    return asarray(X), asarray(y)
  • Read the dataset airline-passengers.csv and give parameter index_col as 0 and save it in variable df.

df = read_csv("airline-passengers.csv", index_col=0)

  • Convert the data type of the values dataframe df to float32 and save it in variable values .
  • Assign the value 5 to the variable n_steps which is the window size.
  • Split the samples using the function split_sequence and pass the parameters values and n_steps and save it in variables X and y
values = df.values.astype('float32')
n_steps = 5
X, y = split_sequence(values, n_steps)

  • Split the data X , y with the train_test_split function of sklearn with parameters test_size=0.33 and random_state=RANDOM_SEED.**

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=RANDOM_SEED)

Construct a fully-connected network structure defined using dense class

  • Create a sequential model
  • Add a LSTM layer which has 200 nodes with activation function as relu and input shape as (n_steps,1).
  • The first hidden layer has 100 nodes and uses the relu activation function.
  • The second hidden layer has 50 nodes and uses the relu activation function.
  • The output layer has 1 node.
model = Sequential()
model.add(LSTM(200, activation='relu',  input_shape=(n_steps,1)))
model.add(Dense(100, activation='relu'))
model.add(Dense(50, activation='relu'))
  • While comipling the model pass the following parameters -
    -optimizer as Adam
    -loss as mse
    -metrics as mae

model.compile(optimizer='Adam', loss='mse', metrics=['mae'])

  • fit the model with X_train, y_train, epochs=350, batch_size=32,verbose=0., y_train, epochs=350, batch_size=32, verbose=0)

  • Perform prediction on the test data (i.e) on X_test and save the predictions in the variable y_pred .
row = ([X_test])
y_pred = model.predict(row)
  • Calculate the mean squared error on the variables y_test and y_pred using the mean_squared_error function in sklearn metrics and save it in variable MSE .
  • Calculate the Root mean squared error on the variables y_test and y_pred by performing square root on the above result and save it in variable RMSE .
  • Calculate the mean absolute error on the variables y_test and y_pred using the mean_absolute_error function in sklearn metrics and save it in variable MAE .
MSE  = metrics.mean_squared_error(y_test,y_pred)
RMSE = sqrt(metrics.mean_squared_error(y_test,y_pred))
MAE  = metrics.mean_absolute_error(y_test,y_pred)
print('MSE: %.3f, RMSE: %.3f, MAE: %.3f' % (MSE, RMSE,MAE))

MSE: 665.522, RMSE: 25.798, MAE: 17.127 … this we getting and it is wrong.

with open("MSE.txt", "w") as text_file:
with open("RMSE.txt", "w") as text_file:
with open("MAE.txt", "w") as text_file:
# serialize model to JSON
model_json = model.to_json()
with open("model.json", "w") as json_file: