<jupyter_start><jupyter_text>Sine LSTM predictionThe aim of this notebook is to train a LSTM model being able to predict the values of a sine.<jupyter_code>import numpy as np
from matplotlib import pyplot as pl

from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout
from keras.layers import LSTM
from keras.optimizers import RMSprop
from keras.utils import np_utils

from sklearn import metrics as me

%matplotlib inline
import math

step_radians = 0.01
steps_of_history = 200
steps_in_future = 1
index = 0

# Training set
x = np.sin(np.arange(0, 20*math.pi, step_radians))

seq = []
next_val = []

for i in range(0, len(x) - steps_of_history, steps_in_future):
    seq.append(x[i: i + steps_of_history])
    next_val.append(x[i + steps_of_history])

seq = np.reshape(seq, [-1, steps_of_history, 1])
next_val = np.reshape(next_val, [-1, 1])
print(np.shape(seq))

trainX = np.array(seq)
trainY = np.array(next_val)

# Testing set
x = np.sin(np.arange(20*math.pi, 24*math.pi, step_radians))

seq = []

for i in range(0, len(x) - steps_of_history, steps_in_future):
    seq.append(x[i: i + steps_of_history])

seq = np.reshape(seq, [-1, steps_of_history, 1])
testX = np.array(seq)
batch_size = 234
# create and fit the LSTM network
model = Sequential()
model.add(LSTM(4, input_shape=(200, 1)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.summary()

model.fit(trainX, trainY, epochs=25, batch_size=batch_size, verbose=1)
import matplotlib.pyplot as plt

# Predict the future values
predictY = model.predict(testX)
print(predictY)

# Plot the results
plt.figure(figsize=(20,4))
plt.suptitle('Prediction')
plt.title('History='+str(steps_of_history)+', Future='+str(steps_in_future))

# The data is shiffted of 200 values, it is due to the fact that the 1st value is predicted from the 200 previous values. 
# Therefore, we have to remove from the plot the first 200 values of the training set.
plt.plot(x[200:], 'r-', label='Actual')
plt.plot(predictY, 'gx', label='Predicted')
plt.legend()<jupyter_output>[[ 0.92619288]
 [ 0.92308122]
 [ 0.91986775]
 ..., 
 [-0.06815492]
 [-0.05748086]
 [-0.04681687]]
{'fraction_non_alphanumeric': 0.1289134438305709, 'fraction_numerical': 0.04834254143646409, 'mean_word_length': 5.173295454545454, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '491849', 'license': 'MIT', 'n_tokens_mistral': 886, 'n_tokens_neox': 803, 'n_words': 222, 'path': 'src/SimpleLSTM.ipynb', 'repo_name': 'axelfahy/RacePrediction', 'size': '2172'}