-
Notifications
You must be signed in to change notification settings - Fork 1
/
xor.mjs
52 lines (43 loc) · 1.58 KB
/
xor.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { NeuralNetwork } from "./NeuralNetwork";
import { VisualiserServer } from './visualiser/server';
import { Tanh, Sigmoid, Relu } from './Activators';
import readline from 'readline'
let Network = new NeuralNetwork(2, 10, 1, 0.3, Sigmoid);
let inputData = [[0, 0], [0, 1], [1, 0], [1, 1]];
let expectedOutput = [[0], [1], [1], [0]];
debugger;
Network.train(inputData, expectedOutput, 20000)
.then(() => {
const testers = inputData;
testers.forEach((test) => {
const answer = Network.ask(test);
clearInterval(interval);
console.log(`'${test.join(' xor ')}' possibly ${answer.answer}`);
});
askQuestion();
});
function askQuestion() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('What do you want to know? ', (input) => {
// TODO: Log the answer in a database
const questionAsArray = input.split(',').map(parseFloat);
const answer = Network.ask(questionAsArray);
console.log(`'${questionAsArray.join(' , ')}' value is ${answer.answer}`);
rl.close();
askQuestion();
});
}
const interval = setInterval(() => {
const testers = inputData;
testers.forEach((test) => {
const answer = Network.ask(test);
console.log(`'${test.join(' xor ')}' possibly ${answer.answer}`);
});
}, 500);
const visualiserServer = new VisualiserServer(Network);
visualiserServer.responseFormatter = (question, answer) => {
return `'${question.join(' xor ')}' possibly ${answer.answer}`;
};