liquidcarrot / carrot

🥕 Evolutionary Neural Networks in JavaScript
https://liquidcarrot.io/carrot/
MIT License
296 stars 34 forks source link

it says network is undefined when i do the .toJSON #247

Closed RadiantUwU closed 3 years ago

RadiantUwU commented 4 years ago

image

async function trainAI (theinput, times, error, mutationRate) {
    try {
        if (!error) error = 0.05;
        if (!mutationRate) mutationRate = 0.5;
        var trainingSet = theinput;
        for (i = 0; i < times; i++) {
            await network.evolve(trainingSet, {
                mutation: methods.mutation.FFW,
                equal: true,
                error: error,
                elitism: 5,
                mutation_rate: mutationRate
            });
        }
    }
    catch (err) {
        throw err;
    }
}

let exported = network.toJSON(); < error

fs.writeFileSync('./AI.json', exported);
christianechevarria commented 4 years ago

Hi @RadiantUwU, are you using the pure JS version of the library or the experimental TS version on the typescript branch?

If you're using the network.evolve in the pure JS version then the problem most likely stems from using the async await pattern since network.evolve is not actually async (unfortunately, we're working on this in the new TS version)

raimannma commented 4 years ago

@christianechevarria He is using the javascript version.

@RadiantUwU you have to use JSON.stringify before saving to file, and JSON.parse after loading file content.

raimannma commented 4 years ago

Can you send your full code, as we can't see the initilization of the variable network.

RadiantUwU commented 4 years ago

it was initialised tho :P

raimannma commented 4 years ago

Yes, possible.

But it seems to me that the network variable is not defined the moment you call it.

So it would be good to have your complete code or even just the relevant code where you initialize the network and where you execute toJSON().

Maybe a network variable is defined, but it is not accessible at the moment you call toJSON().

RadiantUwU commented 3 years ago

im using stuff like im using network variable, but i didnt even globalize it