Skip to main content
Home
This release is 136 versions behind 0.168.1 — the latest version of @stsoftware/neat-ai. Jump to latest

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.

This package works with Deno
This package works with Deno
JSR Score
94%
Published
5 months ago (0.121.0)
Package root>test>ifPropagation.ts
import { assert } from "jsr:@std/assert@^1.0.8"; import { ensureDirSync } from "jsr:@std/fs@^1.0.6"; import { Creature } from "../src/Creature.ts"; import type { CreatureInternal } from "../src/architecture/CreatureInterfaces.ts"; import type { SynapseTrace } from "../src/architecture/SynapseInterfaces.ts"; import type { TrainOptions } from "../src/config/TrainOptions.ts"; import { train } from "./TrainTestOnlyUtil.ts"; ((globalThis as unknown) as { DEBUG: boolean }).DEBUG = true; Deno.test("ifPropagation", () => { const json: CreatureInternal = { neurons: [ { type: "input", index: 0 }, { type: "input", index: 1 }, { type: "input", index: 2 }, { type: "output", squash: "IF", index: 3, bias: 0, }, ], synapses: [ { from: 0, to: 3, weight: 0.9, type: "condition" }, { from: 1, to: 3, weight: 1.1, type: "positive" }, { from: 2, to: 3, weight: 0.95, type: "negative" }, ], input: 3, output: 1, }; const ts = []; for (let i = 100; i--;) { for (let j = 100; j--;) { if (i == 50) continue; const condition = Math.random() * 2 - 1; const positive = Math.random(); const negative = Math.random(); const item = { input: [condition, positive, negative], output: [condition > 0 ? positive : negative], }; ts.push(item); } } const traceDir = ".trace"; ensureDirSync(traceDir); let foundUsed = false; const options: TrainOptions = { iterations: 1, targetError: 0, }; const creature = Creature.fromJSON(json); const result = train(creature, ts, options); const traceJson = result.trace; Deno.writeTextFileSync( ".trace/ifPropagation.json", JSON.stringify(traceJson, null, 2), ); let usedCount = 0; if (traceJson) { traceJson.synapses.forEach((c: SynapseTrace) => { if (c.trace && c.trace.used) { usedCount++; } }); } if (usedCount > 1) { foundUsed = true; } assert( foundUsed, "Should have traced usage", ); if (traceJson) { traceJson.neurons.forEach((n) => { assert(Math.abs(n.bias) < 1, `Invalid bias ${n.bias}`); }); } });