fork download
  1. import numpy as np
  2. from keras.models import Sequential
  3. from keras.layers import Dense, LSTM
  4.  
  5. # Input Sequence
  6. data = np.array([77, 8, 31, 89, 13, 96, 53, 62, 99, 33, 91, 22, 62, 17, 82, 50, 66, 98, 26])
  7.  
  8. # Preprocessing Data
  9. X = data[:-1].reshape(-1, 1, 1) # Input
  10. Y = data[1:].reshape(-1, 1) # Output
  11.  
  12. # LSTM Model
  13. model = Sequential()
  14. model.add(LSTM(50, activation='relu', input_shape=(1, 1)))
  15. model.add(Dense(1))
  16. model.compile(optimizer='adam', loss='mse')
  17.  
  18. # Training
  19. model.fit(X, Y, epochs=100, verbose=0)
  20.  
  21. # Prediction
  22. new_sequence = np.array([62]).reshape(1, 1, 1)
  23. predicted = model.predict(new_sequence)
  24. print("Next Number Prediction:", predicted)
Success #stdin #stdout 4.42s 371996KB
stdin
Standard input is empty
stdout
Next Number Prediction: [[37.811157]]