Пример #1
0
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)
Пример #2
0
def predict(currency, current_price, historical_data):
    X_train, y_train, X_test, y_test = lstm.load_data(
        prepare_data(historical_data), 50, True)
    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=512,
              nb_epoch=1,
              validation_split=0.05)
    predictions = lstm.predict_sequences_multiple(model, X_test, 50, 50)
    #lstm.plot_results_multiple(predictions, y_test, 50)

    p = predictions[len(predictions) - 1]
    pi = p[len(p) - 1]

    prediction = 'down'

    if pi >= 0:
        prediction = 'up'

    return prediction, str(int(round(abs(pi * 100))))
Пример #3
0
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)
Пример #4
0
def main():
    global_start_time = time.time()
    epochs = 1
    seq_len = 50

    print('> Loading data... ')

    X_train, y_train, X_test, y_test = lstm.load_data('lux500.csv', seq_len,
                                                      True)

    print('> Data Loaded. Compiling...')

    model = lstm.build_model([1, 50, 100, 1])

    model.fit(X_train,
              y_train,
              batch_size=512,
              nb_epoch=epochs,
              validation_split=0.05)

    model_json = model.to_json()
    with open("model.json", "w") as json_file:
        json_file.write(model_json)
    model.save_weights("model.h5")
    print('Model saved to disk')

    predictions = lstm.predict_sequences_multiple(model, X_test, seq_len, 50)
    #predicted = lstm.predict_sequence_full(model, X_test, seq_len)
    #predicted = lstm.predict_point_by_point(model, X_test)
    print('Training duration (s) : ', time.time() - global_start_time)
    data = plot_results_multiple(predictions, y_test, 50)
    #print(data)
    return data
Пример #5
0
def test():
	X_train, y_train, X_test, y_test = lstm.load_data('lux500.csv', 50,True)

	json_file = open("model.json")
	loaded_model = json_file.read()
	json_file.close()

	model = model_from_json(loaded_model)
	model.load_weights("model.h5")

	pred = lstm.predict_sequences_multiple(model, X_test, 50, 50)
	return run.plot_results_multiple(pred, y_test, 50)
def predict(X, forecast, model, horizon, scaler):
    predictions, predicted_frames = lstm.predict_sequences_multiple(
        model, X, forecast, horizon)
    try:
        predicted_load = lstm.inverse_transform1(predicted_frames, scaler)
        # true_load = lstm.inverse_transform(dataset[2], dataset[3], scaler)
        # rmse = sqrt(mean_squared_error(true_load, predicted_load))
        # mape = np.mean(np.abs((true_load - predicted_load) / true_load)) * 100
    except Exception as e:
        print(e)
        # rmse=100.0
        # mape=100.0
    return predictions, predicted_load
Пример #7
0
def run_test(filename):
    X_train, y_train, X_test, y_test, d_X_test, d_y_test = lstm.load_data(
        filename, seq_len, True)
    predictions = lstm.predict_sequences_multiple(model,
                                                  X_test,
                                                  d_X_test,
                                                  seq_len,
                                                  60,
                                                  denormalise=True)
    actual_pos = d_y_test
    print(filename)
    for i, prediction in enumerate(predictions):
        distances = 0
        for j, predic in enumerate(prediction):
            distances += np.linalg.norm(np.array(predic) - actual_pos[i][j])**2
        distance = np.sqrt(distances)
        print(distance)
Пример #8
0
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)
Пример #9
0
    def callback():
        global_start_time = time.time()
        epochs = 1
        seq_len = 50

        a = pdr.get_data_yahoo(e.get(), '1980-12-01')
        a.to_csv('a.csv', header=False, index=False, columns=['Close'])
        t.delete("1.0", END)
        t.insert(END, '> Loading data... \n')

        X_train, y_train, X_test, y_test = lstm.load_data(
            'a.csv', seq_len, True)

        t.insert(END, '> Data Loaded. Compiling... \n')

        model = lstm.build_model([1, 50, 100, 1])

        model.fit(
            X_train,
            y_train,
            batch_size=512,
            nb_epoch=epochs,
            validation_split=0.05,
        )

        predictions = lstm.predict_sequences_multiple(model, X_test, seq_len,
                                                      50)
        #predicted = lstm.predict_sequence_full(model, X_test, seq_len)
        predicted = lstm.predict_point_by_point(model, X_test)

        score = model.evaluate(X_test, y_test, batch_size=30, verbose=0)
        t.insert(END, '\n > LSTM test accuracy :' + str(score))
        t.insert(
            END, '\n > Training duration (s) :' +
            str(time.time() - global_start_time))
        plot_results(predicted, y_test)
Пример #10
0
# =============================================================================
# #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 
Пример #11
0
def predict():
    global_start_time = time.time()
    epochs = 10
    seq_len = 10
    num_predict = 5

    print('> Loading data... ')

    # X_train, y_train, X_test, Y_test = lstm.load_data('sp500_2.csv', seq_len, True)
    # X_train_, y_train_, X_test_, Y_test_ = lstm.load_data('sp500_2.csv', seq_len, False)
    X_train, y_train, X_test, Y_test = lstm.load_data('ibermansa.csv', seq_len,
                                                      True)
    X_train_, y_train_, X_test_, Y_test_ = lstm.load_data(
        'ibermansa.csv', seq_len, False)

    print('> Data Loaded. Compiling...')

    model = lstm.build_model([1, seq_len, 100, 1])

    model.fit(X_train,
              y_train,
              batch_size=100,
              nb_epoch=epochs,
              validation_split=0.40)

    predictions2, full_predicted = lstm.predict_sequences_multiple(
        model, X_test, seq_len, num_predict)
    # predictions = lstm.predict_sequence_full(model, X_test, seq_len)
    predictions = lstm.predict_point_by_point(model,
                                              X_test,
                                              Y_test,
                                              batch_size=100)

    # sequence_length = seq_len + 1
    # result = []
    # for index in range(len(predictions) - sequence_length):
    #	result.append(predictions[index: index + sequence_length])
    # result = lstm.unnormalise_windows(result)
    # predictions = np.array(result)

    # result = []
    # for index in range(len(Y_test) - sequence_length):
    #	result.append(Y_test[index: index + sequence_length])
    # result = lstm.unnormalise_windows(result)
    # Y_test = np.array(result)

    # Y_test = Y_test+Y_test_.astype(np.float)
    # Y_test = Y_test.astype(np.float)[:296]
    # aux = predictions[:]+Y_test_
    # print(aux)

    # mape = mean_absolute_percentage_error(Y_test[-42:-1], np.array(predictions2)[:,0])
    # mse = mean_squared_error(Y_test[-42:-1],np.array(predictions2)[:,0])
    # mae = mean_absolute_percentage_error(Y_test[-42:-1],np.array(predictions2)[:,0])

    mape = mean_absolute_percentage_error(Y_test[-2050:-1],
                                          full_predicted[0:-1])
    mse = mean_squared_error(Y_test[-2050:-1], full_predicted[0:-1])
    mae = mean_absolute_percentage_error(Y_test[-2050:-1],
                                         full_predicted[0:-1])

    # msle = mean_squared_logarithmic_error(Y_test, predictions)

    # print(mape)

    init_op = tf.initialize_all_variables()
    # def weighted_mape_tf(Y_test,predictions):
    #tot = tf.reduce_sum(Y_test)
    #tot = tf.clip_by_value(tot, clip_value_min=1,clip_value_max=1000)
    #wmape = tf.realdiv(tf.reduce_sum(tf.abs(tf.subtract(Y_test,predictions))),tot)*100#/tot
    #return(wmape)

    # mape = weighted_mape_tf(Y_test,predictions)

    # run the graph
    with tf.Session() as sess:
        sess.run(init_op)
        print('mape -> {} '.format(sess.run(mape)))
        print('mse -> {}'.format(sess.run(mse)))
        print('mae -> {} '.format(sess.run(mae)))
    # print ('msle -> {} %'.format(sess.run(msle)))

    print('Training duration (s) : ', time.time() - global_start_time)
    print(predictions)
    im1 = plot_results(predictions, Y_test)
    im2 = plot_results(np.array(Y_test_) + np.array(predictions), Y_test_)
    im3 = plot_results_multiple(predictions2, Y_test, num_predict)
    im4 = plot_results(
        np.array(Y_test_)[-118:-1] + np.array(full_predicted)[-118:-1],
        Y_test_)
    return im1, im2, im3, im4
model.compile(loss = 'mse', optimizer = 'rmsprop')
print("compilation time: ", time.time() - start)

start = time.time()
model2.compile(loss = 'mse', optimizer = 'rmsprop')
print("compilation time: ", time.time() - start)
start = time.time()
model3.compile(loss = 'mse', optimizer = 'rmsprop')
print("compilation time: ", time.time() - start)

model.fit(X_train, y_train, batch_size = 512, nb_epoch = 7, validation_split = 0.05)

model2.fit(X_train, y_train, batch_size = 512, nb_epoch = 7, validation_split = 0.05)

model3.fit(X_train, y_train, batch_size = 512, nb_epoch = 7, validation_split = 0.05)

score1 = model.evaluate(X_test, y_test)
print("\n Mean Squared Error: ", score1)
score2 = model2.evaluate(X_test, y_test)
print("\n Mean Squared Error: ",score2)
score3 = model3.evaluate(X_test, y_test)
print("\n Mean Squared Error: ",score3)

predictions1 = lstm.predict_sequences_multiple(model,X_test, 50,50)
lstm.plot_results_multiple(predictions1,y_test, 50)

predictions2 = lstm.predict_sequences_multiple(model2,X_test, 50,50)
lstm.plot_results_multiple(predictions2,y_test, 50)

predictions3 = lstm.predict_sequences_multiple(model3,X_test, 50,50)
lstm.plot_results_multiple(predictions3,y_test, 50)
Пример #13
0
def predict(batch_size,
            nb_epoch,
            timestep,
            hidden_state,
            layers=[1],
            save=False,
            predict_multiple=False,
            prediction_len=1,
            predict_full=False):
    # Load Data
    normalise = True
    X_train, y_train, X_test, y_test, y_test_restorer = lstm.load_data(
        './input/sp500.csv', timestep, normalise)
    print "X_train length" + str(len(X_train))
    print "X_test length" + str(len(X_test))

    # Build Model
    if (len(layers) == 3):
        model = lstm.build_model_single_layer_LSTM(layers)
    elif (len(layers) == 4):
        model = lstm.build_model_double_layer_LSTM(layers)
    elif (len(layers) == 5):
        model = lstm.build_model_triple_layer_LSTM(layers)
    else:
        model = lstm.build_model_single_layer_LSTM([1, hidden_state, 1])

    model.compile(loss='mse', optimizer='rmsprop')

    # Train the model
    model.fit(X_train,
              y_train,
              batch_size=batch_size,
              nb_epoch=nb_epoch,
              validation_split=0.05)

    model.save('./data/modelMetaData.h5')

    # Predict test data with trained model
    predictions = lstm.predict_point_by_point(model, X_test)
    if predict_multiple:
        predictions_multiple = lstm.predict_sequences_multiple(
            model, X_test, window_size=timestep, prediction_len=prediction_len)
    if predict_full:
        predictions_full = lstm.predict_sequence_full(model,
                                                      X_test,
                                                      window_size=timestep)
    if normalise:  # restore the normalised data and predictions to the original value=
        for i in range(len(y_test)):
            y_test[i] = (y_test[i] + 1) * float(y_test_restorer[i])
            predictions[i] = (predictions[i] + 1) * float(y_test_restorer[i])
        if predict_multiple:
            col = len(predictions_multiple[0])
            tmp = np.asarray(predictions_multiple).reshape(1, -1)
            for i in range(len(tmp)):
                tmp[i] = (tmp[i] + 1) * float(y_test_restorer[i])
            predictions_multiple = tmp.reshape(-1, col).tolist()
        if predict_full:
            for i in range(len(predictions_full)):
                predictions_full[i] = (predictions_full[i] + 1) * float(
                    y_test_restorer[i])

    # save predictions and the test data for further experiments
    if save:
        np.save('./data/y_test', y_test)
        np.save('./data/predictions', predictions)
        if predict_multiple:
            np.save('./data/predictions_multi', predictions_multiple)
        if predict_full:
            np.save('./data/predictions_full', predictions_full)

    return y_test, predictions
Пример #14
0
#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)

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:
Пример #15
0
        plt.legend()
    plt.show()

#Main Run Thread
if __name__=='__main__':
    global_start_time = time.time()
    epochs  = 1
    seq_len = 50

    print('> Loading data... ')

    X_train, y_train, X_test, y_test = lstm.load_data('data/sp500.csv', seq_len, True)

    print('> Data Loaded. Compiling...', X_train.shape, y_train.shape)

    model = lstm.build_model([1, 50, 100, 1])

    model.fit(
        X_train,
        y_train,
        batch_size=512,
        nb_epoch=epochs,
        validation_split=0.05)

    predictions = lstm.predict_sequences_multiple(model, X_test, seq_len, 50)
    #predicted = lstm.predict_sequence_full(model, X_test, seq_len)
    #predicted = lstm.predict_point_by_point(model, X_test)

    print('Training duration (s) : ', time.time() - global_start_time)
    plot_results_multiple(predictions, y_test, 50)
Пример #16
0
#Main Run Thread
if __name__ == '__main__':
    global_start_time = time.time()
    epochs = 1
    window_size = 50
    prediction_len = 50
    batch_size = 5000
    validation_split = 0.005

    print('> Loading data... ')

    X_train, y_train, X_test, y_test = lstm.load_data(
        'dice_amplified/20_mil_dos_mixed.csv', window_size, True)

    print('> Data Loaded. Compiling...')

    model = lstm.build_model([1, 50, 100, 1])

    model.fit(X_train,
              y_train,
              batch_size=batch_size,
              nb_epoch=epochs,
              validation_split=validation_split)

    predictions = lstm.predict_sequences_multiple(model, X_test, window_size,
                                                  prediction_len)
    #predicted = lstm.predict_sequence_full(model, X_test, window_size)
    #predicted = lstm.predict_point_by_point(model, X_test)

    print('Training duration (s) : ', time.time() - global_start_time)
    plot_results_multiple(predictions, y_test, 50)
        #predict trend
        predictions_trend = []
        y_test_trend = []
        for x in range(1, len(predictions)):
            if y_test[x] > y_test[x - 1]:
                y_test_trend.append(1)
            else:
                y_test_trend.append(-1)
        for x in range(1, len(y_test)):
            if predictions[x] > predictions[x - 1]:
                predictions_trend.append(1)
            else:
                predictions_trend.append(-1)
        name = "pic_features_" + str(z + 1) + "_trends"
        plot_results(predictions_trend, y_test_trend, name)

        a = predict_sequence_full(model, X_test, 20)
        for i in range(len(a)):
            a[i] = float(X_testn[i][0][features - 1]) * (a[i] + 1)
        plot_results(a, y_testn, name)

        predictions = lstm.predict_sequences_multiple(model, X_test, seq_len,
                                                      10)
        for i in range(len(predictions)):
            for j in range(len(predictions[i])):
                predictions[i][j] = int(
                    X_testn[i * len(predictions[i]) +
                            j][0][0]) * (predictions[i][j] + 1)
                print(i)
        plot_results_multiple(predictions, y_testn, 10)
Пример #18
0
model.add(Dropout(0.2))

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')