Skip to content

Instantly share code, notes, and snippets.

Created October 22, 2015 04:11
What would you like to do?
Predicting sequences of vectors (regression) in Keras using RNN - LSTM (original by - fixed for Keras 0.2.0
import pandas as pd
from random import random
flow = (list(range(1,10,1)) + list(range(10,1,-1)))*1000
pdata = pd.DataFrame({"a":flow, "b":flow})
pdata.b = pdata.b.shift(9)
data = pdata.iloc[10:] * random() # some noise
import numpy as np
def _load_data(data, n_prev = 100):
data should be pd.DataFrame()
docX, docY = [], []
for i in range(len(data)-n_prev):
alsX = np.array(docX)
alsY = np.array(docY)
return alsX, alsY
def train_test_split(df, test_size=0.1):
This just splits data to training and testing parts
ntrn = round(len(df) * (1 - test_size))
X_train, y_train = _load_data(df.iloc[0:ntrn])
X_test, y_test = _load_data(df.iloc[ntrn:])
return (X_train, y_train), (X_test, y_test)
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.layers.recurrent import LSTM
in_neurons = 2
out_neurons = 2
hidden_neurons = 20
model = Sequential()
model.add(LSTM(output_dim=hidden_neurons, input_dim=in_neurons, return_sequences=False))
model.add(Dense(output_dim=out_neurons, input_dim=hidden_neurons))
model.compile(loss="mean_squared_error", optimizer="rmsprop")
(X_train, y_train), (X_test, y_test) = train_test_split(data) # retrieve data, y_train, batch_size=450, nb_epoch=10, validation_split=0.05)
predicted = model.predict(X_test)
rmse = np.sqrt(((predicted - y_test) ** 2).mean(axis=0))
# and maybe plot it
Copy link

Nemitek commented Oct 22, 2015

Originally taken from here.

Copy link

The last line gives a "KeyError". It should either read...


Copy link

Last line is: pd.DataFrame(y_test[:100]).to_csv("test_data.csv")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment