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