wagenaartje / neataptic

:rocket: Blazing fast neuro-evolution & backpropagation for the browser and Node.js

Home Page:https://wagenaartje.github.io/neataptic/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

What is the ideal way to train a lot of data with the Neat algorithm.

dan-ryan opened this issue · comments

I'm using the Neat algorithm to build a day trading bot, I'm using market data (2800 days worth) but getting a little stuck on how to correctly train. The market data contains the date and price. I make sure the neural network stays small enough to prevent over-fitting.

The first part of the problem:

If I just calculate the score with all the raw data and then evolve (1 cycle); the model is good for that whole set of data. When using the model, you have to start on day one. If you start on day 10/etc, the model breaks.

So I modified the model so after each cycle, it increases the starting day by one and then starts again at one when it's on the last day. But once it starts again (day one) the model breaks in training. It has had too many mutations and no longer can do the beginning.

So I modified the model so it would only evolve when the starting point starts again (day one) i.e after its going through the data 3921400 (2800+2799+...) times. But doing it this way seems like it would take forever. It seems to get stuck too easily.

Basic diagam

What is the best way to solve this kind of problem? I'm guessing that I need to shuffle the data somehow? But what is the correct way?

2nd part of the problem:

I was lucky and got a genome that worked good enough without the 3rd modifications. But when I used the testing data, it failed badly.

How would do I do some kind of cross-validation?

Example of the data:

    {"2013-05-14": 111.4,
    "2013-05-15": 114.22,
    "2013-05-16": 118.21,
    "2013-05-17": 123.5,
    "2013-05-18": 123.211,
    "2013-05-19": 122.5,
    "2013-05-20": 122.02,
    "2013-05-21": 122.89,
    "2013-05-22": 123.8,
    "2013-05-23": 126.3,
    "2013-05-24": 133.1,
    "2013-05-25": 131.986,
    "2013-05-26": 133.5,
    "2013-05-27": 129.77,
    "2013-05-28": 129,
    "2013-05-29": 132.25,
    "2013-05-30": 128.799,
    "2013-05-31": 128.8151,
    "2013-06-01": 129.3,
    "2013-06-02": 122.5,
    "2013-06-03": 120.7372,
    "2013-06-04": 121.4,
    "2013-06-05": 121.9,
    "2013-06-06": 118.9699,
    "2013-06-07": 111,
    "2013-06-08": 107.8924,
    "2013-06-09": 100.4374,
    "2013-06-10": 106.35,
    "2013-06-11": 109,
    "2013-06-12": 108.7759,
    "2013-06-13": 103.949,
    "2013-06-14": 100}

Code

Here is some of the code that might help understand my problem.

function learnData() {
    const dataLength = trainingSet.length;
    let learnLoops = 0; 
    while (true) {
        for (let i = learnLoops; i < dataLength; i++) {
            const data = trainingSet[i];
            for (let j = 0; j < bots.length; j++) {
                const bot = bots[j];
                bot.update(data[0], data[1], data[2], data[3], data[4]);
            }
        }
        learnLoops++;

        if (learnLoops + 10 >= dataLength) { //Reset the loop
            learnLoops = 0;
            endEvaluation();
        }

        //Save some cycles by reusing the bot objects. 
        for (let i = 0; i < neat.population.length; i++) {
            bots[i].reset(neat.population[i]);
        }
    }
    console.log("Finished learning.");
}

 class Bot {
    update(dayInMonth, monthInYear, tokenPrice, token PriceChange, runningAveragePriceChange) {    
        //This genome is bad so penalise every day it has missed.
        if (this.stop || this.shouldRemove(this.day)) {
            this.genome.score -= 1000;
            this.stop = true;
            return;
        }

        const normalisedData = this.normaliseData(dayInMonth, monthInYear, tokenPriceChange, runningAveragePriceChange);
        const output = this.genome.activate(normalisedData);
        const trade = output[0];

        if (trade !== 0) {
            if (trade > 0.5 && this.token > 0) {
                this.usd = this.token * tokenPrice - 0.16;
                this.token = 0;
            } else if (trade <= 0.5 && this.usd > 0) {
                const buyingToken = this.usd / token Price;
                const fee = buyingToken * .03;
                this.token = buyingToken - fee;
                this.usd = 0;
            }
        }
        this.bankInUsd = (this.token * token Price) + this.usd;
        this.genome.score += this.bankInUsd / this.day;
    }
}