Skip to content

Instantly share code, notes, and snippets.

@hunghg255
Forked from fabiospampinato/xor.js
Created April 30, 2023 11:14
Show Gist options
  • Save hunghg255/f3c4c80a36e142602ea12334c70bb6fa to your computer and use it in GitHub Desktop.
Save hunghg255/f3c4c80a36e142602ea12334c70bb6fa to your computer and use it in GitHub Desktop.
A 99% precise neural network that learned the XOR function. There's about a 1kb fixed overhead, the model is ~just those two hex strings.
function xor(t){const _=xor;return _._||(_._=!0,_.p=(e,t)=>{const r=[];for(let n=0,c=e.length;n<c;n++){r[n]=[];for(let c=0,a=t[0].length;c<a;c++){let a=0;for(let r=0,l=e[0].length;r<l;r++)a+=e[n][r]*t[r][c];r[n][c]=a}}return r},_.m=(e,t)=>{const r=new Array(e.length);for(let n=0,c=e.length;n<c;n++){r[n]=new Array(e[n].length);for(let c=0,a=e[n].length;c<a;c++)r[n][c]=t(e[n][c],n,c)}return r},_.d=(e,t)=>{const r="float64"===t?Float64Array:Float32Array;return e.split("|").map((e=>{const t=e.length/2,n=r.BYTES_PER_ELEMENT,c=new ArrayBuffer(t+(t%n?n-t%n:0)),a=new Uint8Array(c),l=new r(c);for(let t=0,r=e.length;t<r;t+=2)a[t/2]=parseInt(e.slice(t,t+2),16);return Array.from(l).slice(0,t)}))},_.a0=(e,t)=>{const r=1/(1+Math.exp(-e));return t?r*(1-r):r},_.a1=(e,t)=>{const r=1/(1+Math.exp(-e));return t?r*(1-r):r},_.w0=_.d("cf70bbc08a93cfc043c6bb40c6ee8240|de3eb8c06b4a4e4080843bc0480001c1"),_.w1=_.d("c98b9fc1|eb82af40|dd0cbac0|e8d02d41")),_.m(_.p(_.m(_.p([t],_.w0),_.a0),_.w1),_.a1)[0][0]}
console.log(xor([0,0]));
console.log(xor([1,0]));
console.log(xor([0,1]));
console.log(xor([1,1]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment