def predict(): #Step 1 Load Data X_train, y_train, X_test, y_test = lstm.load_data('aapl.csv', 50, True) #Step 2 Build Model model = Sequential() model.add(LSTM(input_dim=1, output_dim=2, return_sequences=True)) model.add(Dropout(0.2)) model.add(LSTM(100, return_sequences=False)) model.add(Dropout(0.2)) model.add(Dense(output_dim=1)) model.add(Activation('linear')) start = time.time() model.compile(loss='mse', optimizer='rmsprop') print 'compilation time : ', time.time() - start #Step 3 Train the model model.fit(X_train, y_train, batch_size=512, nb_epoch=1, validation_split=0.05) #Step 4 - Plot the predictions! predictions = lstm.predict_sequences_multiple(model, X_test, 50, 50) lstm.plot_results_multiple(predictions, y_test, 50)
def treina_modelo(model, x_train, y_train, existe=False): if existe == False: model.add(LSTM(input_dim=1, output_dim=50, return_sequences=True)) model.add(Dropout(0.2)) model.add(LSTM(50, return_sequences=False)) model.add(Dropout(0.2)) model.add(Dense(20, activation='relu')) model.add(Dense(output_dim=1)) model.add(Activation('linear')) start = time.time() model.compile(loss='mse', optimizer='adam', metrics=['accuracy']) print("Compilation time: ", time.time() - start) # # Para quando não melhorar es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=5) callbacks_list = [es] # Treinando o modelo model.fit(x_train, y_train, batch_size=512, epochs=10, validation_split=0.05, callbacks=callbacks_list, verbose=1) model.save('meu_modelo.h5') #Step 4 - Plot the predictions! predictions = lstm.predict_sequences_multiple(model, x_test, 50, 50) lstm.plot_results_multiple(predictions, y_test, 50)
def predict(batch, dropout): x_train, y_train, x_test, y_test = lstm.load_data('SP500_5YR.csv',50,True) model = Sequential() model.add(LSTM(input_dim = 1, output_dim = 50, return_sequences=True)) model.add(Dropout(dropout)) model.add(LSTM(100,return_sequences=False)) model.add(Dropout(dropout)) model.add(Dense(output_dim=1)) model.add(Activation('linear')) model.compile(loss = 'mse', optimizer = 'rmsprop') model.fit(x_train,y_train,batch_size=batch, nb_epoch = 1, validation_split = 0.05) predict = lstm.predict_sequence_multiple(model, x_test, 50, 50) lstm.plot_results_multiple(predict, y_test, 50)
def main(): outdir = './data' if not os.path.exists(outdir): os.mkdir(outdir) aapl = pdr.get_data_yahoo('AAPL', start=datetime.datetime(2006, 10, 1), end=datetime.datetime(2019, 1, 1)) aapl.to_csv('data/aapl_ohlc.csv') df = pd.read_csv('data/aapl_ohlc.csv', index_col='Date', header=0, parse_dates=True) close = df['Close'] window_size = 30 predict_len = 15 batch_size = 80 epochs = 1 x_train, y_train, x_test, y_test = lstm.load_data(close, window_size, True) #date_price = df['Close'] #date_price = np.array(date_price).astype('float32') #train, test = split_train_test(date_price, 0.8) #x_train, y_train = create_dataset(train, 1) #x_test, y_test = create_dataset(test, 1) #x_train = np.reshape(x_train, (x_train.shape[0], 1, x_train.shape[1])) #x_test = np.reshape(x_test, (x_test.shape[0], 1, x_test.shape[1])) model = Sequential() model.add(LSTM(input_dim=1, output_dim=50, return_sequences=True)) model.add(Dropout(0.2)) model.add(LSTM(100, return_sequences=False)) model.add(Dropout(0.2)) model.add(Dense(output_dim=1)) model.add(Activation('linear')) start = time.time() model.compile(loss='mse', optimizer='rmsprop') print('compilation time:', time.time() - start) model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.05) predictions = lstm.predict_sequences_multiple(model, x_test, window_size, predict_len) lstm.plot_results_multiple(predictions, y_test, predict_len)
def exp_layer_depth(layers): prediction_len = 20 predict(batch_size = 512, nb_epoch = 70,timestep = 50,hidden_state = 50, layers = layers, save = True, predict_multiple = True, prediction_len = prediction_len, predict_full = True) # Plot the predictions orig = np.load("./data/y_test.npy") predictions = np.load("./data/predictions.npy") rmse,tp_acc,tpp_acc,cm1,cm2 = performance(orig, predictions) print "Tendency Prediction Confusion Matrix (0:down; 1:up or equal): " print cm1 print "Turning Point Prediction Confusion Matrix (0:not a turning point; 1:summit; 2:vale): " print cm2 print "rmse: " + str(rmse) + "TPA: " + str(tp_acc) + "TPPA: " + str(tpp_acc) predictions_multi = np.load("./data/predictions_multi.npy") predictions_full = np.load("./data/predictions_full.npy") fig = plt.figure(facecolor='white') plot_results(predictions, orig, fig, sublocation = 121, show = False) # plot_results(predictions_full, orig, fig, sublocation = 222, show = False) plot_results_multiple(predictions_multi, orig, prediction_len = prediction_len, fig = fig, sublocation = 122, show = True)
# ============================================================================= # #Step 3 Train the model # ============================================================================= model.fit( X_train, y_train, batch_size=512, nb_epoch=1, validation_split=0.05) # ============================================================================= # #Step 4 - Plot the predictions! # ============================================================================= predictions = lstm.predict_sequences_multiple(model, X_test, 50, 50) lstm.plot_results_multiple(predictions, y_test, 50) # we will train our model with the function then we can test it to see what it predicts # for the next 50 steps at several points in our graph # and visualize it using that with mapplotlit # it seems that for a lot of the price movement espacially the big ones # there is quite a correlation between our models prediction and the actual data # so it's time to some money!! # but will our model be able to correcly predict the closing price one hundred percent of time? # the answer is no # it's an analystical tool to help us make educated guesses about the direction of the market, # that's slightly better than random # so to break it down, conclusions: # 1. rnn can model sequential data because the hidden state is affected by the input and # the previous hidden state
#Step 3 Train the model model.fit( X_train, y_train, batch_size=512, nb_epoch=1, validation_split=0.05) X_test = X_test[:30] y_test = y_test[:30] # Step 4 - Plot the predictions! prediction_len = 3 predictions = lstm.predict_sequences_multiple(model, X_test, 30, prediction_len) lstm.plot_results_multiple(predictions, y_test, prediction_len) #hilo_pred = [] #for prediction in predictions: # if prediction[0] > prediction[-1]: # hilo_pred.append(0) # else: # hilo_pred.append(1) # # #hilo_real = [] #for i in range(0, 147, prediction_len): # if y_test[i] > y_test[i + prediction_len]: # hilo_real.append(0) # else: # hilo_real.append(1)
model.add(LSTM(64, return_sequences=False)) model.add(Dense(output_dim=1)) model.add(Activation('linear')) start = time.time() model.compile(loss='mse', optimizer='rmsprop') print('Compilation time:', time.time() - start) #Set hyperparameter model.fit(X_train, y_train, batch_size=512, nb_epoch=100, validation_split=0.1) #Make prediction predictions = lstm.predict_sequences_multiple(model, X_test, seqence_length, seqence_length) lstm.plot_results_multiple(predictions, y_test, seqence_length) #Save Model model_json = model.to_json() with open("model.json", "w") as json_file: json_file.write(model_json) # serialize weights to HDF5 model.save_weights("model.h5") p = model.predict(X_test) #Show result plt2.plot(p, color='red', label='prediction') plt2.plot(y_test, color='blue', label='actural') plt2.legend(loc='upper left') plt2.title("388 Days Prediction")