Skip to content

Instantly share code, notes, and snippets.

@J-Cake
Created February 15, 2019 07:05
Show Gist options
  • Save J-Cake/b4c3d37d89ce61037dd79c04d6f4acbf to your computer and use it in GitHub Desktop.
Save J-Cake/b4c3d37d89ce61037dd79c04d6f4acbf to your computer and use it in GitHub Desktop.
Malfunctioning NN. I gave it my best shot at translating from python.
let weights = Array.from({
length: 3
}, () => Math.random() * 2 - 1)
// let weights = [-0.16595599, 0.44064899, -0.99977125]
let inputs = [
[0, 1, 1],
[1, 1, 1],
[1, 0, 1],
[0, 1, 1]
];
let outputs = [0, 1, 1, 0];
let exp = x => Math.E ** x;
let sig = x => x.map(i => 1 / (1 + (exp(-i))))
let sigP = x => x.map(i => i * (1 - i))
let add = (x, y) => x.map((i, a) => i + y[a]);
let sub = (x, y) => x.map((i, a) => i - y[a]);
let mlt = (x, y) => x.map((i, a) => i * y[a]);
let dot = (x, y) => x.map(a => a.map((b, i) => b * y[i])).map(a => a.reduce((i, b) => i + b));
// x.map(a => a.map((b, i) => b * y[i]))
console.log("weights", weights)
let outputLayer;
for (let i = 0; i < 20000; i++) {
outputLayer = sig(dot(inputs, weights));
let err = sub(outputs, outputLayer);
let change = mlt(err, sigP(outputLayer))
weights = add(weights, change);
}
let guess = inputs => sig(dot(inputs, weights))
console.log("weights", weights)
console.log("outputs", outputLayer)
console.log("guess", guess([1, 0, 0]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment