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 variabe RANDOM_SEED which will be the seed value. Set the random seed value using the value stored in the variable RANDOM_SEED.
RANDOM_SEED = 40
tf.random.set_seed(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:
break
# gather input and output parts of the pattern
seq_x, seq_y = sequence[i:end_ix], sequence[end_ix]
X.append(seq_x)
y.append(seq_y)
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')) model.add(Dense(1))
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.
model.fit(X_train, 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:
MSE=str(MSE)
text_file.write(MSE)
with open("RMSE.txt", "w") as text_file:
RMSE=str(RMSE)
text_file.write(RMSE)
with open("MAE.txt", "w") as text_file:
MAE=str(MAE)
text_file.write(MAE)
# serialize model to JSON
model_json = model.to_json()
with open("model.json", "w") as json_file:
json_file.write(model_json)
After all we are running -
from hashlib import md5
f = open("MSE.txt", "r")
s=f.read()
s=float(s)
s=round(s,3)
f1=open("RMSE.txt","r")
s1=f1.read()
s1=float(s1)
s1=round(s1,3)
f2=open("MAE.txt","r")
s2=f2.read()
s2=float(s2)
s2=round(s2,3)
if (md5(str(s).encode()).hexdigest() == '51ad543f7ac467cb8b518f1a04cc06af') and (md5(str(s1).encode()).hexdigest() == '6ad48a76bec847ede2ad2c328978bcfa') and (md5(str(s2).encode()).hexdigest() == '64bd1e146726e9f8622756173ab27831'):
print("Your MSE,RMSE and MAE Scores matched the expected output")
else :
print("Your MSE,RMSE and MAE Scores does not match the expected output")
Here our output should be match but coming as unmatched.
Try this:
def square_it(n):
global rv
rv+=str(n*n)+'\n'
print(rv)
it will work but it give extra line as output but it does not matter