Last active
November 30, 2020 00:45
-
-
Save N8python/92b6c9ff7658ca387dc1cb5812ced640 to your computer and use it in GitHub Desktop.
6 parameter model. Learns xor blazing fast.
This file contains 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 data = [ | |
[ | |
[0, 0], 1 | |
], | |
[ | |
[0, 1], 0 | |
], | |
[ | |
[1, 0], 0 | |
], | |
[ | |
[1, 1], 1 | |
] | |
]; | |
const sigmoid = x => 1 / (1 + Math.exp(-x)); | |
const sigmoid_ = x => sigmoid(x) * (1 - sigmoid(x)); | |
const randWeight = () => Math.random() * 2 - 1; | |
let m0 = randWeight(); | |
let m1 = randWeight(); | |
let b0 = randWeight(); | |
let a = randWeight(); | |
let b = randWeight(); | |
let c = randWeight(); | |
const learningRate = 0.01; | |
for (let iteration = 0; iteration < 100000; iteration++) { | |
const m0nudges = []; | |
const m1nudges = []; | |
const b0nudges = []; | |
const anudges = []; | |
const bnudges = []; | |
const cnudges = []; | |
data.forEach(([ | |
[x0, x1], y | |
]) => { | |
const W = m0 * x0 + m1 * x1 + b0; | |
const X = a * W ** 2 + b * W + c; | |
const Z = sigmoid(X); | |
const C = ((Z - y) ** 2) / 2; | |
m0nudges.push((Z - y) * sigmoid_(X) * (2 * a * W + b) * x0); | |
m1nudges.push((Z - y) * sigmoid_(X) * (2 * a * W + b) * x1); | |
b0nudges.push((Z - y) * sigmoid_(X) * (2 * a * W + b)); | |
anudges.push((Z - y) * sigmoid_(X) * W ** 2); | |
bnudges.push((Z - y) * sigmoid_(X) * W); | |
cnudges.push((Z - y) * sigmoid_(X)); | |
}); | |
m0 += -R.mean(m0nudges) * learningRate; | |
m1 += -R.mean(m1nudges) * learningRate; | |
b0 += -R.mean(b0nudges) * learningRate; | |
a += -R.mean(anudges) * learningRate; | |
b += -R.mean(bnudges) * learningRate; | |
c += -R.mean(cnudges) * learningRate; | |
} | |
function runModel(x0, x1) { | |
const W = m0 * x0 + m1 * x1 + b0; | |
const X = a * W ** 2 + b * W + c; | |
const Z = sigmoid(X); | |
return Z; | |
} | |
console.log(runModel(0, 0)); | |
console.log(runModel(0, 1)); | |
console.log(runModel(1, 0)); | |
console.log(runModel(1, 1)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment