Skip to content

Instantly share code, notes, and snippets.

@imjacobclark
Last active January 14, 2020 07:55
Show Gist options
  • Save imjacobclark/b9eb5ee93970ffd5b9740edd6ed9e18f to your computer and use it in GitHub Desktop.
Save imjacobclark/b9eb5ee93970ffd5b9740edd6ed9e18f to your computer and use it in GitHub Desktop.
GradientDecentNeuralNetwork.js
class NeuralNetwork {
constructor(input, goal, iterations){
this.input = input;
this.goal = goal;
this.iterations = iterations;
this.weight = 0.5;
}
predict(){
let prediction;
for(let i = 0; i < this.iterations; i++){
prediction = this.input * this.weight; // try a prediction by multiplying input by weight
// delta is the derivative of weight verses the error, our goal is to minimise the error (delta^2) as much as posssible
// delta === ((this.input * this.weight) - this.goal)
// input and goal are fixed where weight is not, the goal then is the figure out how we can change this.weight to minimise error as much as possible
const delta = prediction - this.goal; // how far did we miss? the sign of delta tells us if we were too high or too low (positive verses negative)
// we get the next weight value by multiplying the delta by the input, this gives us negative reversal, stopping and scaling
const weight_delta = delta * input; // multiply the delta by the input
this.weight -= weight_delta; // deduct amount missed from the weight
console.log(
"Prediction: " + prediction +
"\n" +
"Error: " + Math.pow(delta, 2) +
"\n" +
"Missed by: " + delta +
"\n" +
"Moving weight by: " + weight_delta +
"\n" +
"New Weight: " + this.weight +
"\n\n"
);
}
return prediction;
}
}
const input = 0.5;
const goal = 0.8;
const iterations = 20;
const neuralNetwork = new NeuralNetwork(input, goal, iterations);
console.log(neuralNetwork.predict())
imjacobclark$ node NeuralNetwork.js
Prediction: 0.25
Missed by: -0.55
Moving weight by: -0.275
New Weight: 0.775
Prediction: 0.3875
Missed by: -0.41250000000000003
Moving weight by: -0.20625000000000002
New Weight: 0.9812500000000001
Prediction: 0.49062500000000003
Missed by: -0.309375
Moving weight by: -0.1546875
New Weight: 1.1359375
Prediction: 0.56796875
Missed by: -0.23203125000000002
Moving weight by: -0.11601562500000001
New Weight: 1.251953125
Prediction: 0.6259765625
Missed by: -0.17402343750000004
Moving weight by: -0.08701171875000002
New Weight: 1.33896484375
Prediction: 0.669482421875
Missed by: -0.1305175781250001
Moving weight by: -0.06525878906250004
New Weight: 1.4042236328125
Prediction: 0.70211181640625
Missed by: -0.09788818359375007
Moving weight by: -0.04894409179687503
New Weight: 1.453167724609375
Prediction: 0.7265838623046875
Missed by: -0.07341613769531252
Moving weight by: -0.03670806884765626
New Weight: 1.4898757934570312
Prediction: 0.7449378967285156
Missed by: -0.05506210327148442
Moving weight by: -0.02753105163574221
New Weight: 1.5174068450927733
Prediction: 0.7587034225463867
Missed by: -0.04129657745361337
Moving weight by: -0.020648288726806685
New Weight: 1.53805513381958
Prediction: 0.76902756690979
Missed by: -0.030972433090210028
Moving weight by: -0.015486216545105014
New Weight: 1.553541350364685
Prediction: 0.7767706751823426
Missed by: -0.023229324817657493
Moving weight by: -0.011614662408828746
New Weight: 1.5651560127735138
Prediction: 0.7825780063867569
Missed by: -0.017421993613243147
Moving weight by: -0.008710996806621574
New Weight: 1.5738670095801353
Prediction: 0.7869335047900676
Missed by: -0.013066495209932416
Moving weight by: -0.006533247604966208
New Weight: 1.5804002571851015
Prediction: 0.7902001285925507
Missed by: -0.009799871407449312
Moving weight by: -0.004899935703724656
New Weight: 1.5853001928888262
Prediction: 0.7926500964444131
Missed by: -0.007349903555586956
Moving weight by: -0.003674951777793478
New Weight: 1.5889751446666196
Prediction: 0.7944875723333098
Missed by: -0.005512427666690245
Moving weight by: -0.0027562138333451225
New Weight: 1.5917313584999646
Prediction: 0.7958656792499823
Missed by: -0.004134320750017739
Moving weight by: -0.0020671603750088696
New Weight: 1.5937985188749735
Prediction: 0.7968992594374867
Missed by: -0.0031007405625133044
Moving weight by: -0.0015503702812566522
New Weight: 1.5953488891562302
Prediction: 0.7976744445781151
Missed by: -0.0023255554218849506
Moving weight by: -0.0011627777109424753
New Weight: 1.5965116668671726
0.7976744445781151
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment