Skip to content

Commit 560197a

Browse files
committed
xor example
1 parent 3bd8d66 commit 560197a

1 file changed

Lines changed: 42 additions & 22 deletions

File tree

examples/xor/sketch.js

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,54 @@
1+
let nn;
12
let training_data = [{
2-
inputs: [0, 1],
3-
targets: [1]
4-
},
5-
{
6-
inputs: [1, 0],
7-
targets: [1]
8-
},
9-
{
10-
inputs: [0, 0],
11-
targets: [0]
12-
},
13-
{
14-
inputs: [1, 1],
15-
targets: [0]
16-
}
17-
];
3+
inputs: [0, 0],
4+
targets: [0]
5+
}, {
6+
inputs: [1, 0],
7+
targets: [1]
8+
}, {
9+
inputs: [0, 1],
10+
targets: [1]
11+
}, {
12+
inputs: [1, 1],
13+
targets: [0]
14+
}];
1815

16+
let lr_slider;
1917

2018
function setup() {
21-
let nn = new NeuralNetwork(2, 2, 1);
19+
createCanvas(400, 400);
20+
nn = new NeuralNetwork(2, 2, 1);
21+
lr_slider = createSlider(0.01, 0.1, 0.05, 0.01);
22+
}
23+
24+
function draw() {
25+
background(0);
26+
27+
nn.learning_rate = lr_slider.value();
2228

23-
for (let i = 0; i < 50000; i++) {
29+
for (let i = 0; i < 5000; i++) {
2430
let data = random(training_data);
2531
nn.train(data.inputs, data.targets);
2632
}
2733

28-
console.log(nn.feedforward([1, 0]));
29-
console.log(nn.feedforward([0, 1]));
30-
console.log(nn.feedforward([1, 1]));
31-
console.log(nn.feedforward([0, 0]));
34+
let resolution = 20;
35+
let cols = floor(width / resolution);
36+
let rows = floor(height / resolution);
37+
38+
for (let i = 0; i < cols; i++) {
39+
for (let j = 0; j < rows; j++) {
40+
let x = i * resolution;
41+
let y = j * resolution;
42+
let input_1 = i / (cols - 1);
43+
let input_2 = j / (rows - 1);
44+
let output = nn.feedforward([input_1, input_2]);
45+
let col = output[0] * 255;
46+
fill(col);
47+
noStroke();
48+
rect(x, y, resolution, resolution);
49+
}
50+
}
51+
3252

3353

3454
}

0 commit comments

Comments
 (0)