Skip to content

Commit ad08798

Browse files
committed
new xor challenge code
1 parent 5a937a4 commit ad08798

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

examples/xor/sketch.js

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,53 @@
11
let nn;
2-
let training_data = [{
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-
}];
15-
162
let lr_slider;
173

4+
let training_data = [{
5+
inputs: [0, 0],
6+
outputs: [0]
7+
},
8+
{
9+
inputs: [0, 1],
10+
outputs: [1]
11+
},
12+
{
13+
inputs: [1, 0],
14+
outputs: [1]
15+
},
16+
{
17+
inputs: [1, 1],
18+
outputs: [0]
19+
}
20+
];
21+
1822
function setup() {
1923
createCanvas(400, 400);
20-
nn = new NeuralNetwork(2, 2, 1);
21-
lr_slider = createSlider(0.01, 0.1, 0.05, 0.01);
24+
nn = new NeuralNetwork(2, 4, 1);
25+
lr_slider = createSlider(0.01, 0.5, 0.1, 0.01);
26+
2227
}
2328

2429
function draw() {
2530
background(0);
2631

27-
nn.learning_rate = lr_slider.value();
28-
29-
for (let i = 0; i < 5000; i++) {
32+
for (let i = 0; i < 10; i++) {
3033
let data = random(training_data);
31-
nn.train(data.inputs, data.targets);
34+
nn.train(data.inputs, data.outputs);
3235
}
3336

34-
let resolution = 20;
35-
let cols = floor(width / resolution);
36-
let rows = floor(height / resolution);
37+
nn.setLearningRate(lr_slider.value());
3738

39+
let resolution = 10;
40+
let cols = width / resolution;
41+
let rows = height / resolution;
3842
for (let i = 0; i < cols; i++) {
3943
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.predict([input_1, input_2]);
45-
let col = output[0] * 255;
46-
fill(col);
44+
let x1 = i / cols;
45+
let x2 = j / rows;
46+
let inputs = [x1, x2];
47+
let y = nn.predict(inputs);
4748
noStroke();
48-
rect(x, y, resolution, resolution);
49+
fill(y * 255);
50+
rect(i * resolution, j * resolution, resolution, resolution);
4951
}
5052
}
5153

0 commit comments

Comments
 (0)