diff options
author | sanine <sanine.not@pm.me> | 2023-06-11 22:50:42 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-06-11 22:50:42 -0500 |
commit | 980a5350b5a4845db2bd5d6feb9f463a3c1a3aa6 (patch) | |
tree | 409c93483388b8cede754bc69fc62804a271c045 /src/mind/topology.js | |
parent | 3b0b005b952b1092404fdd5ae1732ec9561794af (diff) |
add hidden neuron state
Diffstat (limited to 'src/mind/topology.js')
-rw-r--r-- | src/mind/topology.js | 24 |
1 files changed, 20 insertions, 4 deletions
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]); } |