Created
October 25, 2018 01:22
-
-
Save zabadiel071/7f9103e4fa6d576c72650fe89f1c8798 to your computer and use it in GitHub Desktop.
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 R = require('ramda') | |
//Returns escalar product | |
let escalarProduct = (x1, x2) => R.zipWith( (x1 , x2) => x1 * x2, x1, x2).reduce((x1,x2) => x1 + x2 , 0 ) | |
//Simple step function | |
let step = ( f , thresold) => ( f < thresold) ? 0 : 1 | |
//Delta value calculation | |
let delta = (e, x, learningRate) => e * x * learningRate | |
//Gets a single error | |
let _error = (desirable, real) => desirable - real; | |
//Gets all realOutpus in vector | |
let realOutputs = (inputs, w, thresold) => inputs.map(x => step( escalarProduct(x, w) , thresold )) | |
// function, vector, vector | |
let vectorError = (f, desirable, real) => R.zipWith(f, desirable, real) | |
// Gets all the erros in a vector | |
let error = (outputs, realOutputs) => R.zipWith(_error, outputs, realOutputs) | |
let stepTrain = (w , inputs, outputs, thresold, learningRate) => { | |
let errorVector = error(outputs, realOutputs(inputs,w,thresold)) | |
let vectorTest = R.zipWith( (error, input) => input.map( i => delta(error, i, learningRate) ), errorVector, inputs) | |
.find( x => !x.every(x => x == 0) ) | |
if (vectorTest !== undefined) { | |
return result = R.zipWith( | |
(a,b) => a + b, | |
w, | |
vectorTest | |
) | |
}else{ | |
return w | |
} | |
} | |
// | |
let train = (w) => { | |
let newW = stepTrain(w, inputs, outputs, thresold, learningRate) | |
if (equalArrays(w, newW)) { | |
return newW | |
}else{ | |
return train(newW) | |
} | |
} | |
function testNetwork() { | |
var trainedNetwork = train(w); | |
console.log(trainedNetwork) | |
} | |
let isEmpty = (array) => array.length == 0 || array === undefined | |
let equalArrays = (a , b) => isEmpty(R.difference(a, b)) | |
//Real outputs, just memoization of pun | |
const thresold = 0.4 | |
const learningRate = 0.1 | |
let inputs = [[0,0] , [0,1] , [1,0], [1,1] ] | |
let outputs = [0,1,1,1] | |
var w = [0.1,0.6] | |
testNetwork() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment