OiO.lk Blog python Why does this simple (Keras) machine learning code give the wrong answer?
python

Why does this simple (Keras) machine learning code give the wrong answer?


I’m trying to learn some time-series neural network ML and was getting weird solutions, so I’m trying to model the simplest non-trivial case I can think of, which is predicting n+1 as the next number in the sequence 0,1,2,3,…n (using an LSTM model).

The training data for each data point is a series of immediately preceding numbers, and I’m assuming it should easily solve the model as long as the data for each training set has length >= 2 (since it’s an arithmetic sequence)

The code below is returning a constant for all test data regardless of the size of the training series. Can someone explain what I’m doing wrong?

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import math

import statistics

dim = 5

data = pd.Series(range(0,200))

# Setting 80 percent data for training
training_data_len = math.ceil(len(data) * .8)

# Normalize data
train_data = data[:training_data_len]

#Split dataset
train_data = data[:training_data_len]
test_data = data[training_data_len:]
print(train_data.shape, test_data.shape)

# Selecting values
dataset_train = train_data.values 
# Reshaping 1D to 2D array
dataset_train = np.reshape(dataset_train, (-1,1)) 

# Selecting values
dataset_test = test_data.values
# Reshaping 1D to 2D array
dataset_test = np.reshape(dataset_test, (-1,1))  

X_train = []
y_train = []
for i in range(dim, len(dataset_train)):
    X_train.append(dataset_train[i-dim:i, 0])
    y_train.append(dataset_train[i, 0])


X_test = []
y_test = []
for i in range(dim, len(dataset_test)):
    X_test.append(dataset_test[i-dim:i, 0])
    y_test.append(dataset_test[i, 0])


# The data is converted to Numpy array
X_train, y_train = np.array(X_train), np.array(y_train)

#Reshaping
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1],1))
y_train = np.reshape(y_train, (y_train.shape[0],1))
print("X_train :",X_train.shape,"y_train :",y_train.shape)


# The data is converted to numpy array
X_test, y_test = np.array(X_test), np.array(y_test)

#Reshaping
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1],1))
y_test = np.reshape(y_test, (y_test.shape[0],1))
print("X_test :",X_test.shape,"y_test :",y_test.shape)

# importing libraries
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
from keras.layers import SimpleRNN
from keras.layers import Dropout
from keras.layers import GRU, Bidirectional
from keras.optimizers import SGD
from sklearn import metrics
from sklearn.metrics import mean_squared_error

#Initialising the model
regressorLSTM = Sequential()

#Adding LSTM layers
regressorLSTM.add(LSTM(dim, 
                       return_sequences = True, 
                       input_shape = (X_train.shape[1],1)))
regressorLSTM.add(LSTM(dim, 
                       return_sequences = False))

#Adding the output layer
regressorLSTM.add(Dense(1))

#Compiling the model
regressorLSTM.compile(optimizer="adam",
                      loss="mean_squared_error",
                      metrics = ["accuracy"])

#Fitting the model
regressorLSTM.fit(X_train, 
                  y_train, 
                  batch_size = 1, 
                  epochs = 4)
regressorLSTM.summary()


# predictions with X_test data
y_LSTM = regressorLSTM.predict(X_test)

#Plot for LSTM predictions
plt.plot(train_data.index[dim:], train_data[dim:], label = "train_data", color = "b")
plt.plot(test_data.index, test_data, label = "test_data", color = "g")
plt.plot(test_data.index[dim:], y_LSTM, label = "y_LSTM", color = "orange")
plt.legend()
plt.xlabel("X")
plt.ylabel("Y")
plt.show()



You need to sign in to view this answers

Exit mobile version