Skip to content

Instantly share code, notes, and snippets.

@goloroden
Created August 21, 2016 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goloroden/1f6fbb07d5f196cebe4731300b0c3eff to your computer and use it in GitHub Desktop.
Save goloroden/1f6fbb07d5f196cebe4731300b0c3eff to your computer and use it in GitHub Desktop.
A very, very simple neural network (with only 1 neuron ;-))
'use strict';
const input = 0.5;
const expected = 0.8;
let weight = 0.5;
const alpha = 0.1;
const iterations = 100;
for (let i = 0; i < iterations; i++) {
const actual = input * weight;
const error = (expected - actual) * (expected - actual);
console.log(`i=${i}, weight=${weight}, prediction=${actual}, error=${error}`);
const derivative = 2 * (expected - actual);
const scaledDerivative = alpha * input * derivative;
weight = weight + scaledDerivative;
}
console.log();
console.log(`f(${input}) = ${input * weight}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment