This release is 125 versions behind 0.165.7 — the latest version of @stsoftware/neat-ai. Jump to latest
@stsoftware/neat-ai@0.121.0Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
Built and signed on GitHub Actions
NEAT Neural Network. This project is a unique implementation of a neural network based on the NEAT (NeuroEvolution of Augmenting Topologies) algorithm, written in DenoJS using TypeScript.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135import { assert, fail } from "jsr:@std/assert@^1.0.8"; import { ensureDirSync } from "jsr:@std/fs@^1.0.6"; import { Creature } from "../src/Creature.ts"; import { train } from "./TrainTestOnlyUtil.ts"; ((globalThis as unknown) as { DEBUG: boolean }).DEBUG = true; // Compact form: name and function Deno.test("AND", () => { // Train the AND gate const trainingSet = [ { input: [0, 0], output: [0] }, { input: [0, 1], output: [0] }, { input: [1, 0], output: [0] }, { input: [1, 1], output: [1] }, ]; for (let attempts = 0; true; attempts++) { const network = new Creature(2, 1); const results = train(network, trainingSet, { targetError: 0.1, iterations: 10_000, learningRate: 1, generations: 50, }); if (results.error > 0.1 && attempts < 100) continue; assert(results.error <= 0.1, "Error rate was: " + results.error); break; } }); Deno.test("MT", () => { // Train the AND gate const trainingSet = [ { input: [0, 0], output: [0] }, { input: [0, 1], output: [0] }, { input: [1, 0], output: [0] }, { input: [1, 1], output: [1] }, ]; for (let attempts = 0; true; attempts++) { const network = new Creature(2, 1, { layers: [ { count: 5 }, ], }); const results = train(network, trainingSet, { targetError: 0.03, iterations: 10000, }); if (results.error <= 0.26) break; if (attempts > 12) { fail(`Error rate was ${results.error}`); } else { console.warn(`Warning rate was ${results.error}`); } } }); Deno.test("train-XOR", () => { // Train the XOR gate const trainingSet = [ { input: [0, 0], output: [0] }, { input: [0, 1], output: [1] }, { input: [1, 0], output: [1] }, { input: [1, 1], output: [0] }, ]; const network = new Creature(2, 1, { layers: [ { count: 5 }, ], }); const traceDir = ".trace"; ensureDirSync(traceDir); Deno.writeTextFileSync( `.trace/start.json`, JSON.stringify(network.internalJSON(), null, 2), ); for (let attempts = 0; true; attempts++) { const results = train(network, trainingSet, { targetError: 0.03, iterations: 10000, }); Deno.writeTextFileSync( `.trace/${attempts}.json`, JSON.stringify(results.trace, null, 2), ); if (results.error <= 0.26) { break; } if (attempts > 12) { throw "Error rate was: " + results.error; } } }); /** * Train the XNOR gate */ Deno.test("XNOR - train", () => { const trainingSet = [ { input: [0, 0], output: [1] }, { input: [0, 1], output: [0] }, { input: [1, 0], output: [0] }, { input: [1, 1], output: [1] }, ]; for (let attempts = 0; attempts < 12; attempts++) { const network = new Creature(2, 1, { layers: [ { count: 5 }, ], }); const results = train(network, trainingSet, { targetError: 0.03, iterations: 10_000, }); if (results.error < 0.26) { break; } if (attempts > 10) { assert(results.error <= 0.03, "Error rate was: " + results.error); } } });