Search code examples
pythontensorflowkeraslstmtflearn

LSTM/RNN Predicting cosine from sine


I am trying to code a simple LSTM/RNN. Given a sine input, can I predict the cosine signal?

While running my code, I can accurately predict next value of sine given historical sine values; but I am unable to accurately predict the next value of cosine given historical sine values.

I heavily borrowed from this following code example, which is used to predict the next character from the alphabet.

Since I am using a LSTM/RNN, I define windows (of length seq_length) of sequence input data corresponding to an output data point.

For example,

Input Sequence -> Output Sequence

[ 0. , 0.00314198, 0.00628393, 0.00942582, 0.01256761] -> 1.0

In the above sample sequence, sin(0) is 0, and then we have the sine values for the next 4 points. These values have an associated cos(0).

Corresponding code,

import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM

time_points = np.linspace(0, 8*np.pi, 8000)

seq_length = 5
dataX = []
dataY = []
for i in range(0, len(time_points) - seq_length, 1):
    seq_in = np.sin(time_points)[i:i + seq_length]
    seq_out = np.cos(time_points)[i]
    dataX.append([seq_in])
    dataY.append(seq_out)

X = np.reshape(dataX, (len(dataX), seq_length, 1)) #numpy as np
y = np.reshape(dataY, (len(dataY), 1))

LSTM Keras code

model = Sequential()
model.add(LSTM(16, input_shape=(X.shape[1], X.shape[2])))


model.add(Dense(y.shape[1], activation='linear'))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(X[:6000], y[:6000], epochs=20, batch_size=10, verbose=2, validation_split=0.3)

The following figure shows the prediction and the ground truth, when we try to learn cosine from sequential sine data.

enter image description here

However, if we were to learn sine using sequential sine data (i.e. have seq_out = np.sin(time_points)[i]), the prediction is accurate as shown below.

enter image description here

I was wondering what could be going wrong.

Or, how can I get a more accurate prediction?


Solution

  • Answering my own question. It was a matter of increasing the number of epochs and fiddling with the batch size. For example, here is the prediction for #epochs = 200, batch size = 10.

    enter image description here