From 5e9e138be0cfccb9da71873c909a015c472f1446 Mon Sep 17 00:00:00 2001 From: sneedgroup-holder Date: Tue, 3 Dec 2024 23:25:54 +0000 Subject: [PATCH] kuromi: add sanity checks/demo multiple thoughts btw in last commit sam meant "demo" not "demi" --- sam-neurons-words.js | 65 ++++++++++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 17 deletions(-) diff --git a/sam-neurons-words.js b/sam-neurons-words.js index 39c909c..eda518f 100644 --- a/sam-neurons-words.js +++ b/sam-neurons-words.js @@ -118,27 +118,58 @@ let targets = [ [6] ]; -if (targets.length != inputs.length) { - throw new Error("You dummy, targets length should be equal to inputs length.") -} + + +// Example dataset (XOR problem) +let strings2 = ["i'm", "fine", "myself."] + +let inputs2 = [ + [0, 0, 0], + [1, 0, 0], + [1, 0, 1], + [1, 1, 1] +]; + +let nn2 = new NeuralNetwork(strings2.length, inputs2.length, 1); // Increased hidden nodes to 4 + +let targets2 = [ + [0], + [1], + [2], + [3] +]; + // Training the neural network -for (let i = 0; i < 50000; i++) { // Increased training iterations - let index = Math.floor(Math.random() * 4); - nn.train(inputs[index], targets[index]); +function train(nn, inputs, targets) { + if (targets.length != inputs.length) { + throw new Error("You dummy, targets length should be equal to inputs length.") + } + for (let i = 0; i < 50000; i++) { // Increased training iterations + let index = Math.floor(Math.random() * 4); + nn.train(inputs[index], targets[index]); + } } -// Testing the neural network -let feeds = inputs; - -feeds.forEach((item, index) => { -let feededVal = Math.round(nn.feedforward(item)) -console.log([feededVal, item]) -if (Math.round(feededVal) == 1) { -let sum = 0; -item.forEach(num => sum += num); -try { console.log(strings[sum - 1]) } catch {} +function think(nn, feeds, strings) { + if (strings.length != feeds[0].length) { + throw new Error("You dummy, strings array length should be equal to a feed input length.") + } + feeds.forEach((item, index) => { + let feededVal = Math.round(nn.feedforward(item)) + console.log([feededVal, item]) + if (Math.round(feededVal) == 1) { + let sum = 0; + item.forEach(num => sum += num); + try { console.log(strings[sum - 1]) } catch {} + } + }) } -}) +train(nn, inputs, targets) +think(nn, inputs, strings) + +train(nn2, inputs2, targets2) +think(nn2, inputs2, strings2) +