title: Predicting next numbers in sequence Keras - Python
body: <p>I'm new to python and neural networks. I have a simple network written in Keras that can predict the next number in a linear sequence:</p>
  
  <pre><code>import numpy as np
  
  from keras.models import Sequential
  from keras.layers import Dense
  from keras.layers import LSTM
  
  data = [[i for i in range(6)]];
  data = np.array(data, dtype=int);
  target = [[i for i in range(10, 16)]];
  target = np.array(target, dtype=int);
  
  model = Sequential();
  model.add(Dense(1, input_dim=1))
  model.add(Dense(1));
  model.compile(loss='mean_absolute_error', optimizer = 'adam', metrics = ['accuracy']);
  model.summary();
  for i in range (10000):
      dataIterator = 0;
      for targetValue in target:
          model.train_on_batch(data[dataIterator], targetValue)
          dataIterator = dataIterator + 1;
  
  predict = model.predict([28]);
  print(predict);
  </code></pre>
  
  <p>Gives me output:</p>
  
  <pre><code>[[38.0199]]
  </code></pre>
  
  <p>And that is to be expected. I'm not sure if my code has some stupid errors in it and would appreciate any feedback and explanations. I used Dense because I'm not sure what LSTM exacly does. Another thing is that my model requires input to have 2 dimensions when it's specified: </p>
  
  <pre><code>input_dim=1
  </code></pre>
  
  <p>I don't understand why. Next, I would like to create a network that can predict the next numbers in a sequence like [1, 4, 9, 16, 25]. This one doesn't.</p>
  
  <p>Note that this is my first program written in Python and first use of neural networks :). Thanks in advance!</p>
  
  <p><strong>UPDATE 1</strong></p>
  
  <p>According to tip of using scale i came up with something like this:</p>
  
  <pre><code>import numpy as np
  
  from keras.models import Sequential
  from pandas import Series
  from keras.layers import Dense
  from keras.layers import LSTM
  from sklearn.preprocessing import StandardScaler
  
  data = [[i for i in range (1,30)]];
  data = np.array(data, dtype=int);
  target = np.power(data, 2);
  target = np.array(target, dtype=int);
  target = target.reshape((len(target[0]), 1))
  data = data.reshape((len(data[0]), 1))
  
  scale = StandardScaler()
  
  dataTest = [[i for i in range (2,4)]];
  dataTest = np.array(dataTest, dtype=int);
  dataTest = dataTest.reshape((len(dataTest[0]), 1))
  
  model = Sequential();
  model.add(Dense(1, input_dim=1))
  model.add(Dense(1));
  model.compile(loss='mean_absolute_error', optimizer = 'adam');
  model.fit(scale.fit_transform(data), target, batch_size=1, epochs=200, verbose=1)
  
  print(model.predict(scale.transform(dataTest)));
  </code></pre>
  
  <p>Despite this predictions are way off ideal. For given test data, output: </p>
  
  <pre><code>[[27.616932]
   [28.265278]]
  </code></pre>
  
  <p>I'm out of ideas at the moment :(. Not feeling it at all.</p>