summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2023-06-11 23:42:51 -0500
committersanine <sanine.not@pm.me>2023-06-11 23:42:51 -0500
commit251f39da74c8d5707eaeef8d5e63ce442720b01f (patch)
tree8039761a7f849d56839534f3b667452a46236cb2
parent7e92bd5b292b99c5f5a3f1b05d2870be32732d92 (diff)
add comments
-rw-r--r--src/mind/topology.js18
1 files changed, 15 insertions, 3 deletions
diff --git a/src/mind/topology.js b/src/mind/topology.js
index 56bc498..128b7e9 100644
--- a/src/mind/topology.js
+++ b/src/mind/topology.js
@@ -4,7 +4,8 @@
const DEFAULT_WEIGHT_MAX = 4;
-const graph_proto = {
+// prototype for network objects
+const network_proto = {
connect: function(source, sink, weight) {
return network_connect(this, source, sink, weight);
},
@@ -13,9 +14,11 @@ const graph_proto = {
},
};
+
+// create a new network
export function network(input_count, internal_count, output_count, weight_max = 4) {
const count = input_count + internal_count + output_count;
- const n = Object.create(graph_proto);
+ const n = Object.create(network_proto);
n.input_count = input_count;
n.output_count = output_count;
n.adjacency = new Array(count).fill([]);
@@ -24,14 +27,18 @@ export function network(input_count, internal_count, output_count, weight_max =
}
+// check index is an input
function is_input(n, index) {
return index < n.input_count;
}
+// check if index is an output
function is_output(n, index) {
return index >= (n.adjacency.length - n.output_count);
}
+// returns a new network with an edge between the given nodes
+// with the given weight
function network_connect(n, source, sink, weight) {
if (is_input(n, sink)) {
// inputs cannot be sinks
@@ -42,7 +49,7 @@ function network_connect(n, source, sink, weight) {
throw new Error("attempt to use output as source");
}
- const nn = Object.create(graph_proto);
+ const nn = Object.create(network_proto);
nn.input_count = n.input_count;
nn.output_count = n.output_count;
nn.adjacency = n.adjacency.map((row, i) => {
@@ -63,6 +70,7 @@ function network_connect(n, source, sink, weight) {
}
+// gets the indices of the edges incident on the given adjacency list
function incident_edges(n, adj) {
const incident = adj
.map((edge, index) => (edge < 0) || (edge === 2) ? index : null)
@@ -93,6 +101,8 @@ function edge_ends(n, edge) {
}
+// recursively get the value of a node from the input nodes,
+// optionally caching the computed values
function get_value(n, index, input, prev, cache) {
if (cache !== undefined && cache[index]) {
return cache[index];
@@ -125,6 +135,8 @@ function get_value(n, index, input, prev, cache) {
}
+// compute a network's output and new hidden state
+// given the input and previous hidden state
function network_compute(n, input, state) {
// !!! impure caching !!!
const value_cache = {};