title: LSTM/RNN Predicting cosine from sine
body: <p>I am trying to code a simple LSTM/RNN. Given a <code>sine</code> input, can I predict the <code>cosine</code> signal?</p>
  <p>While running my code, I can accurately predict next value of <code>sine</code> given historical <code>sine</code> values; but I am unable to accurately predict the next value of <code>cosine</code> given historical <code>sine</code> values.</p>
  <p>I heavily borrowed from this <a href="https://machinelearningmastery.com/understanding-stateful-lstm-recurrent-neural-networks-python-keras/" rel="nofollow noreferrer">following code example</a>, which is used to predict the next character from the alphabet.</p>
  <p>Since I am using a LSTM/RNN, I define <code>windows</code> (of length <code>seq_length</code>) of sequence input data corresponding to an output data point.</p>
  <p>For example,</p>
  <blockquote>
  <p>Input Sequence -&gt; Output Sequence</p>
  <p>[ 0. , 0.00314198, 0.00628393, 0.00942582, 0.01256761] -&gt; 1.0</p>
  </blockquote>
  <p>In the above sample sequence, <code>sin(0)</code> is 0, and then we have the <code>sine</code> values for the next 4 points. These values have an associated <code>cos(0)</code>.</p>
  <p>Corresponding code,</p>
  <pre><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))
  </code></pre>
  <p>LSTM Keras code</p>
  <pre><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)
  </code></pre>
  <p>The following figure shows the prediction and the ground truth, when we try to learn cosine from sequential sine data.</p>
  <p><a href="https://i.stack.imgur.com/xFZFy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xFZFy.png" alt="enter image description here" /></a></p>
  <p>However, if we were to learn sine using sequential sine data (i.e. have <code>seq_out = np.sin(time_points)[i]</code>), the prediction is accurate as shown below.</p>
  <p><a href="https://i.stack.imgur.com/tk19O.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tk19O.png" alt="enter image description here" /></a></p>
  <p>I was wondering what could be going wrong.</p>
  <p>Or, how can I get a more accurate prediction?</p>