Example #1
0
def learn_nn_and_save(training_data: StockData, test_data: StockData,
                      filename_to_save: str):
    network = create_model()

    network.compile(loss='mean_squared_error', optimizer='sgd')

    values = training_data.get_values()
    setCount = len(values) - (WINDOW_SIZE + 1)

    xtrain = []
    for element in range(0, setCount):
        xtrain.append(numpy.array(values[element:WINDOW_SIZE + element]))

    X_TRAIN = numpy.array(xtrain)

    Y_TRAIN = numpy.empty(setCount, dtype=numpy.float)

    offset = WINDOW_SIZE - 1
    for element in range(0, setCount):
        current = values[element + offset]
        next = values[element + offset + 1]
        Y_TRAIN[element] = 1.0 if next > current else -1.0

    history = network.fit(X_TRAIN,
                          Y_TRAIN,
                          epochs=EPOCHS,
                          batch_size=BATCH_SIZE)
    draw_history(history)

    # Save trained model: separate network structure (stored as JSON) and trained weights (stored as HDF5)
    save_keras_sequential(network, RELATIVE_PATH, filename_to_save)
Example #2
0
def learn_nn_and_save(training_data: StockData, test_data: StockData, filename_to_save: str):
    network = create_model()

    # TODO: learn network and draw results

    # Save trained model: separate network structure (stored as JSON) and trained weights (stored as HDF5)
    save_keras_sequential(network, RELATIVE_PATH, filename_to_save)
Example #3
0
def learn_nn_and_save(data: StockData, filename_to_save: str):
    """
    Starts the training of the neural network and saves it to the file system

    Args:
        data: The data to train on
        filename_to_save: The filename to save the trained NN to
    """
    dates = data.get_dates()
    prices = data.get_values()

    # Generate training data
    # Build chunks of prices from 100 consecutive days (input_prices) and 101th day (current_prices_for_plot)
    current_prices_for_plot, input_prices, wanted_results = get_data(prices)

    # Shape and configuration of network is optimized for binary classification problems
    # see: https://keras.io/getting-started/sequential-model-guide/
    network = create_model()

    network.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])

    # Train the neural network
    reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.9, patience=5, min_lr=0.000001, verbose=1)
    history = network.fit(input_prices, wanted_results, epochs=500, batch_size=128, verbose=1,
                          validation_data=(input_prices, wanted_results), shuffle=True, callbacks=[reduce_lr])

    # Evaluate the trained neural network and plot results
    score = network.evaluate(input_prices, wanted_results, batch_size=128, verbose=0)
    logger.debug(f"Test score: {score}")

    # Draw
    plt.figure()
    plt.plot(history.history['loss'])
    plt.plot(history.history['val_loss'])
    plt.plot(history.history['acc'])
    plt.title('training loss / testing loss by epoch')
    plt.ylabel('loss/acc')
    plt.xlabel('epoch')
    plt.legend(['loss', 'val_loss', 'acc'], loc='best')
    plt.figure()
    current_price_prediction = network.predict(input_prices, batch_size=128)

    logger.debug(f"current_price_prediction:")
    iteration = 0
    for x in current_price_prediction:
        logger.debug(f"iteration {iteration} - output: {x}")
        iteration = iteration + 1

    plt.plot(dates[INPUT_SIZE:], current_prices_for_plot, color="black")  # current prices in reality
    plt.plot(dates[INPUT_SIZE:], [calculate_delta(x) for x in current_price_prediction],
             color="green")  # predicted prices by neural network
    plt.title('current prices / predicted prices by date')
    plt.ylabel('price')
    plt.xlabel('date')
    plt.legend(['current', 'predicted'], loc='best')
    plt.show()

    # Save trained model: separate network structure (stored as JSON) and trained weights (stored as HDF5)
    save_keras_sequential(network, RELATIVE_PATH, filename_to_save)
Example #4
0
def learn_nn_and_save(data: StockData, filename_to_save: str):
    """
    Starts the training of the neural network and saves it to the file system

    Args:
        data: The data to train on
        filename_to_save: The filename to save the trained NN to
    """
    dates = data.get_dates()
    prices = data.get_values()

    # Generate training data
    # Build chunks of prices from 100 consecutive days (last_prices) and 101th day (current_price)
    last_prices, current_price = [], []
    for i in range(0, len(prices) - 100):
        last_prices.append(prices[i:100 + i])
        current_price.append(float(prices[100 + i]))

    network = create_model()

    network.compile(loss='mean_squared_error', optimizer='adam')

    # Train the neural network
    history = network.fit(last_prices,
                          current_price,
                          epochs=10,
                          batch_size=128,
                          verbose=1)

    # Evaluate the trained neural network and plot results
    score = network.evaluate(np.array(last_prices),
                             current_price,
                             batch_size=128,
                             verbose=0)
    logger.debug(f"Test score: {score}")
    plt.figure()
    plt.plot(history.history['loss'])
    plt.title('training loss / testing loss by epoch')
    plt.ylabel('loss')
    plt.xlabel('epoch')
    plt.legend(['training', 'testing'], loc='best')
    plt.figure()
    current_price_prediction = network.predict(last_prices, batch_size=128)
    plt.plot(dates[100:], current_price,
             color="black")  # current prices in reality
    plt.plot(dates[100:], current_price_prediction,
             color="green")  # predicted prices by neural network
    plt.title('current prices / predicted prices by date')
    plt.ylabel('price')
    plt.xlabel('date')
    plt.legend(['current', 'predicted'], loc='best')
    plt.show()

    # Save trained model: separate network structure (stored as JSON) and trained weights (stored as HDF5)
    save_keras_sequential(network, RELATIVE_PATH, filename_to_save)
Example #5
0
def learn_nn_and_save(training_data: StockData, test_data: StockData,
                      filename_to_save: str):
    network = create_model()

    # TODO: learn network and draw results

    #np.append([[1, 2, 3], [4, 5, 6]], [7, 8, 9], axis=0)

    x_train = np.array([])
    y_train = np.array([])

    i = 0
    batch = 5
    while i < training_data.get_row_count() - batch:
        tmp = np.array([])
        for j in range(0, batch):
            i = i + 1
            diff = training_data.get(i)[1] - training_data.get(i + 1)[1]
            tmp = np.insert(tmp, j, diff)

        diff_y = 0
        if (i < training_data.get_row_count() - 1):
            diff_y = training_data.get(i - 1)[1] - training_data.get(i)[1]

        if (diff_y > 0):
            y_train = np.append(y_train, 1)
        else:
            y_train = np.append(y_train, 0)

        x_train = np.append(x_train, tmp, axis=0)

    x_train = x_train.reshape((int(x_train.shape[0] / 5), 5))
    y_train = y_train.reshape((-1, 1))

    #for i in range(0, training_data.get_row_count() - 1):
    #    diff = training_data.get(i)[1] - training_data.get(i + 1)[1]

    #    if (diff < 0):
    #        y_train.append([1])
    #    else:
    #        y_train.append([0])

    network.compile(loss='mean_squared_error', optimizer='sgd')
    network.fit(x_train, y_train, epochs=10, batch_size=5)

    # Save trained model: separate network structure (stored as JSON) and trained weights (stored as HDF5)
    save_keras_sequential(network, RELATIVE_PATH, filename_to_save)
def learn_nn_and_save(training_data: StockData, test_data: StockData, filename_to_save: str):
    price_histories = []
    expected_prices = []

    training_prices = __as_trend(training_data)
    for i in range(INPUT_SIZE, len(training_prices)):
        price_histories.append(training_prices[i-INPUT_SIZE:i])
        expected_prices.append(float(training_prices[i]))

    network = create_model()

    network.compile(loss='mean_squared_error', optimizer='adam')

    network.fit(price_histories, expected_prices, epochs=10)

    # Save trained model: separate network structure (stored as JSON) and trained weights (stored as HDF5)
    save_keras_sequential(network, RELATIVE_PATH, filename_to_save)
 def save_trained_model(self):
     """
     Save the trained neural network under a fixed name specific for this trader.
     """
     save_keras_sequential(self.model, self.RELATIVE_DATA_DIRECTORY,
                           self.network_filename)
Example #8
0
def learn_nn_and_save(training_data: StockData, test_data: StockData,
                      filename_to_save: str):
    network = create_model()

    stocks = training_data.get_values()

    xtrain = []
    ytrain = []

    for i in range(len(stocks) - (MODEL_LENGTH + 2)):
        tuple = []
        for j in range(MODEL_LENGTH):
            increase = (stocks[i + j + 1] - stocks[i + j]) / stocks[i + j]
            if increase > 1:
                increase = 1

            if increase < -1:
                increase = -1

            tuple.append(increase)

        xtrain.append(tuple)

        j = MODEL_LENGTH
        increase = (stocks[i + j + 1] - stocks[i + j]) / stocks[i + j]
        if increase > 1:
            increase = 1

        if increase < -1:
            increase = -1

        tuple2 = []
        tuple2.append(increase)
        ytrain.append(tuple2)

    np_xtrain = np.array(xtrain)
    np_ytrain = np.array(ytrain)

    BATCH_SIZE = 100
    EPOCHS = 50

    network.fit(np_xtrain, np_ytrain, epochs=EPOCHS, batch_size=BATCH_SIZE)

    stocks = test_data.get_values()

    xtrain = []
    ytrain = []

    for i in range(len(stocks) - (MODEL_LENGTH + 2)):
        tuple = []
        for j in range(MODEL_LENGTH):
            increase = (stocks[i + j + 1] - stocks[i + j]) / stocks[i + j]
            if increase > 1:
                increase = 1

            if increase < -1:
                increase = -1

            tuple.append(increase)

        xtrain.append(tuple)

        j = MODEL_LENGTH
        increase = (stocks[i + j + 1] - stocks[i + j]) / stocks[i + j]
        if increase > 1:
            increase = 1

        if increase < -1:
            increase = -1

        tuple2 = []
        tuple2.append(increase)
        ytrain.append(tuple2)

    np_xtrain = np.array(xtrain)
    np_ytrain = np.array(ytrain)

    score = network.evaluate(np_xtrain, np_ytrain, batch_size=BATCH_SIZE)

    # Save trained model: separate network structure (stored as JSON) and trained weights (stored as HDF5)
    save_keras_sequential(network, RELATIVE_PATH, filename_to_save)
Example #9
0
 def save_trained_model(self):
     """
     Save the trained neural network under a fixed name specific for this trader.
     """
     save_keras_sequential(self.model, self.RELATIVE_DATA_DIRECTORY, self.name)
     logger.info(f"DQL Trader: Saved trained model")