kuromi: add sanity checks/demo multiple thoughts
btw in last commit sam meant "demo" not "demi"
This commit is contained in:
parent
4312ee1116
commit
5e9e138be0
1 changed files with 48 additions and 17 deletions
|
@ -118,27 +118,58 @@ let targets = [
|
||||||
[6]
|
[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
|
// Training the neural network
|
||||||
for (let i = 0; i < 50000; i++) { // Increased training iterations
|
function train(nn, inputs, targets) {
|
||||||
let index = Math.floor(Math.random() * 4);
|
if (targets.length != inputs.length) {
|
||||||
nn.train(inputs[index], targets[index]);
|
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;
|
|
||||||
|
|
||||||
|
function think(nn, feeds, strings) {
|
||||||
feeds.forEach((item, index) => {
|
if (strings.length != feeds[0].length) {
|
||||||
let feededVal = Math.round(nn.feedforward(item))
|
throw new Error("You dummy, strings array length should be equal to a feed input length.")
|
||||||
console.log([feededVal, item])
|
}
|
||||||
if (Math.round(feededVal) == 1) {
|
feeds.forEach((item, index) => {
|
||||||
let sum = 0;
|
let feededVal = Math.round(nn.feedforward(item))
|
||||||
item.forEach(num => sum += num);
|
console.log([feededVal, item])
|
||||||
try { console.log(strings[sum - 1]) } catch {}
|
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)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue