Created
October 31, 2018 14:32
-
-
Save horitaku1124/c5fa8d13709ec80482ad8ae270cb9540 to your computer and use it in GitHub Desktop.
solver for IQ test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const inputLayer = [ | |
[1, 5], | |
[2, 7], | |
[3, 9], | |
[5, 10], | |
]; | |
const labels = [ | |
18, | |
27, | |
36, | |
45, | |
]; | |
let bias = [0, 0, 0, 0,]; | |
const output = (w, x) => w[0] * (x[0] + x[1]) + w[1] * x[0] + w[2] * x[1] + w[3]; | |
function targetFunction(bias) { | |
let error = 0; | |
for (let i = 0;i < inputLayer.length;i++) { | |
let input = inputLayer[i]; | |
let b = output(bias, input); | |
error += Math.pow(labels[i] - b, 2); | |
} | |
return error; | |
} | |
const n0 = bias.length; | |
for (let i = 0;i < 100;i++) { | |
for (let j = 0;j < n0;j++) { | |
let old = bias[j]; | |
let before = targetFunction(bias); | |
bias[j] = old + 0.001; | |
let after = targetFunction(bias); | |
bias[j] = old + (before - after) * 1; | |
} | |
if (targetFunction(bias) < 0.0001) { | |
break; | |
} | |
console.log(i, targetFunction(bias)); | |
} | |
console.log(targetFunction(bias)); | |
console.log(bias); | |
console.log("4 + 20 = ", output(bias, [4, 20])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment