-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxorNN.js
76 lines (64 loc) · 1.22 KB
/
xorNN.js
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
let trainingData =
[
{
input: [0,0],
answer: [0]
},
{
input: [1,0],
answer: [1]
},
{
input: [0,1],
answer: [1]
},
{
input: [1,1],
answer: [0]
},
]
var nn;
var learningRateSlider;
function setup()
{
createCanvas(600,600);
nn = new NeuralNetwork(2,4,1,0.1);
learningRateSlider = createSlider(0.01,0.5,0.1,0.01);
learningRateSlider.style('width', '600px');
}
function draw()
{
background(0);
// training
for(var i=0;i<1000;i++)
{
let data = random(trainingData);
nn.train(data.input,data.answer);
}
nn.setLearningRate(learningRateSlider.value());
// visualization
let res = 5;
let cols = (width-50) / res;
let rows = (height-50) / res;
for(let i=0;i<cols;i++)
{
for(let j=0;j<rows;j++)
{
let x = i / cols;
let y = j / rows;
let inputs = [x,y];
let prediction = nn.feedforward(inputs);
fill(prediction * 255);
noStroke();
rect(i * res,j * res, res, res);
}
}
fill(255,0,0);
textSize(20);
text("Learning Rate: "+learningRateSlider.value(),0,575);
text("0,0",0,15);
text("0,1",0,540);
text("1,1",520,540);
text("1,0",520,15);
text("Learning Rate Slider (min-0.01 , max-0.5)",0,595);
}