From 980a5350b5a4845db2bd5d6feb9f463a3c1a3aa6 Mon Sep 17 00:00:00 2001 From: sanine Date: Sun, 11 Jun 2023 22:50:42 -0500 Subject: add hidden neuron state --- src/mind/topology.js | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'src/mind/topology.js') diff --git a/src/mind/topology.js b/src/mind/topology.js index 1cd52d3..19eb399 100644 --- a/src/mind/topology.js +++ b/src/mind/topology.js @@ -72,6 +72,8 @@ function incident_edges(n, adj) { } +// get the indices of the ends of an edge +// in the case of self-loops, both values are the same function edge_ends(n, edge) { const ends = n.adjacency .map((adj, index) => adj[edge] !== 0 ? index : null) @@ -79,7 +81,13 @@ function edge_ends(n, edge) { ends.sort((a, b) => n.adjacency[a][edge] < n.adjacency[b][edge] ? -1 : 1); - return ends; + if (ends.length === 1) { + return { source: ends[0], sink: ends[0] }; + } else if (ends.length === 2) { + return { source: ends[1], sink: ends[0] }; + } else { + throw new Error("something bad happened with the ends"); + } } @@ -91,8 +99,7 @@ function get_value(n, index, input) { const incident = incident_edges(n, adj); const weight = incident.map(x => n.weight[x]); const sources = incident - .map(x => edge_ends(n, x)) - .map(x => x.length === 2 ? x[1] : x[0]); + .map(x => edge_ends(n, x).source); const sum = sources .reduce((acc, x, i) => acc + (weight[i] * get_value(n, x, input)), 0); @@ -102,6 +109,13 @@ function get_value(n, index, input) { function network_compute(n, input, state) { + const hidden = n.adjacency + .map((x, i) => + ( + (!(is_input(n, i))) && + (!is_output(n, i))) ? i : null) + .filter(i => i !== null); + const outputs = n.adjacency .map((x, i) => is_output(n, i) ? i : null) .filter(i => i !== null); @@ -110,7 +124,9 @@ function network_compute(n, input, state) { outputs.map(x => get_value(n, x, input)) ); - const newstate = Object.freeze([]); + const newstate = Object.freeze( + hidden.map(x => get_value(n, x, input)) + ); return Object.freeze([output, newstate]); } -- cgit v1.2.1