My LSTM produces a straight line when I expect a volatile result

Viewed 130

My code produces results which starts as expected but then very quickly turns into a smooth line. Am I incorrect for assuming it would produce a line such as the one it trained on or is there an issue with my code?

My Code:

def lstm(data, n=3):
# choose a number of time steps
steps = 3

# split into samples
x, y = list(), list()
for i in range(len(data)):
    # find the end of this pattern
    end_ix = i + steps
    # check if we are beyond these sequence
    if end_ix > len(data) - 1:
        break
    # gather input and output parts of the pattern
    seq_x, seq_y = data[i:end_ix], data[end_ix]
    x.append(seq_x)
    y.append(seq_y)
x, y = array(x), array(y)

# reshape from [samples, time_steps] into [samples, time_steps, features]
n_features = 1
x = x.reshape((x.shape[0], x.shape[1], n_features))

# define model
model = Sequential()
model.add(LSTM(50, activation='relu', input_shape=(steps, n_features)))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')

# fit model
model.fit(x, y, epochs=200, verbose=0)

inp = data[-3:]
results = []
for num in range(n):
    result_results = []
    for i in range(10):  # make an average of 10 results
        x_input = array(inp).reshape((1, steps, n_features))
        y_hat = model.predict(x_input, verbose=0)
        result_results.append(y_hat[0][0])
    avg_result = sum(result_results) / len(result_results)
    inp.append(avg_result)
    inp = inp[1:]
    results.append(avg_result)

# clear session
tensorflow.keras.backend.clear_session()
return results

Input and result: (blue is input, white is output) Graph Image

The output in text form:

[57512.37890625, 57511.09765625, 57510.39453125, 57512.23828125, 57512.1640625, 57512.35546875, 57513.1328125, 57513.41796875, 57513.796875, 57514.30859375, 57514.6875, 57515.10546875, 57515.546875, 57515.9609375, 57516.38671875, 57516.80859375, 57517.234375, 57517.65625, 57518.08203125, 57518.50390625, 57518.92578125, 57519.3515625, 57519.7734375, 57520.19921875, 57520.62109375, 57521.04296875, 57521.47265625, 57521.890625, 57522.3203125, 57522.73828125, 57523.16796875, 57523.58984375, 57524.01171875, 57524.4375, 57524.859375, 57525.28515625, 57525.70703125, 57526.12890625, 57526.5625, 57526.9765625, 57527.40625, 57527.828125, 57528.25, 57528.67578125, 57529.09765625, 57529.5234375, 57529.9453125, 57530.3671875, 57530.796875, 57531.21484375, 57531.64453125, 57532.0703125, 57532.49609375, 57532.921875, 57533.34375, 57533.76953125, 57534.1953125, 57534.62109375, 57535.04296875, 57535.46875, 57535.890625, 57536.31640625, 57536.7421875, 57537.16015625, 57537.5859375, 57538.01171875, 57538.43359375, 57538.859375, 57539.28515625, 57539.70703125, 57540.12890625, 57540.5546875, 57540.9765625, 57541.40234375, 57541.828125, 57542.25, 57542.671875, 57543.1015625, 57543.52734375, 57543.94921875, 57544.375, 57544.796875, 57545.21875, 57545.640625, 57546.06640625, 57546.4921875, 57546.91015625, 57547.33984375, 57547.76171875, 57548.1875, 57548.61328125, 57549.03515625, 57549.46484375, 57549.88671875, 57550.30859375, 57550.734375, 57551.15625, 57551.58203125, 57552.00390625, 57552.4296875]

I used the code from https://machinelearningmastery.com/how-to-develop-lstm-models-for-time-series-forecasting/ and modified it to produce multiple results.

0 Answers
Related