Skip to content

Instantly share code, notes, and snippets.

@de1mos242
Last active June 7, 2018 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save de1mos242/fceac447945c25ee9afba06673bca15f to your computer and use it in GitHub Desktop.
Save de1mos242/fceac447945c25ee9afba06673bca15f to your computer and use it in GitHub Desktop.
Simple example of recurrent network using LSTM on keras
from math import sin
import numpy as np
from keras.layers import Dense
from keras.layers import LSTM
from keras.models import Sequential
from matplotlib import pyplot
def seq_func(x):
return sin(x)
def fit_lstm(train_x, train_y, nb_epoch, neurons):
x, y = np.array(train_x), np.array(train_y)
x = x.reshape(len(x), 1, 1)
model = Sequential()
model.add(LSTM(neurons, batch_input_shape=(1, 1, 1), stateful=True))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
for i in range(nb_epoch):
model.fit(x, y, epochs=1, batch_size=1, verbose=1, shuffle=False)
model.reset_states()
return model
series = [seq_func(x) for x in range(36)]
supervised = [seq_func(x + 1) for x in range(len(series))]
lstm_model = fit_lstm(series, supervised, 1000, 16)
train_reshaped = np.array(series[:-1]).reshape(len(series)-1, 1, 1)
lstm_model.predict(train_reshaped, batch_size=1)
predictions = []
expectations = []
forecast_result = series[-1]
for i in range(36):
forecast_result_reshaped = np.array([forecast_result]).reshape(1, 1, 1)
forecast_result = lstm_model.predict(forecast_result_reshaped)[0, 0]
predictions.append(forecast_result)
expected = seq_func(len(series) + i)
expectations.append(expected)
print('x=%d, Predicted=%f, Expected=%f' % (len(series) + i, forecast_result, expected))
pyplot.plot(series + expectations)
pyplot.plot(series + predictions)
pyplot.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment