for i in range(1, 5):
df_slice = df[:-50*i]
Data = np.asarray(df_slice['Returns'])
Data = np.atleast_2d(Data)
Data = Data.T
X = np.atleast_3d(np.array([Data[start:start + Obs] for start in range(0, Data.shape[0] - Obs)]))
y = Data[Obs:]
model = Sequential()
model.add(LSTM(input_shape=(1,), input_dim=1, output_dim=20, return_sequences=True))
model.add(LSTM(input_shape=(1,), input_dim=1, output_dim=20, return_sequences=False))
model.add(Dense(1))
model.add(Activation('linear'))
model.compile(loss="mse", optimizer="nadam")
model.fit(X, y, epochs=600, batch_size=50, verbose=2, shuffle=False)
lossname = "Model_" + str(5-i) + "_loss.csv"
paramsname = "Model_" + str(5-i) + "_params.json"
name = "Model_" + str(5-i) + ".json"
h5name = "Model_" + str(5-i) + "_weights.h5"
pd.DataFrame(NN.history).to_csv(lossname)
model_params = json.dumps(NN.params)
with open(paramsname, "w") as json_file:
json_file.write(model_params)
with open(name, "w") as json_file:
json_file.write(model_json)
model.save_weights(h5name)
print "Done with model number", 5-i
time.sleep(120)