Last active
February 13, 2016 11:33
-
-
Save ruemic/49b13d1394e417f9be3e to your computer and use it in GitHub Desktop.
This file contains hidden or 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 map = require('lodash/map') | |
const reduce = require('lodash/reduce') | |
const validateLengths = (a,b,action) => { | |
if (a.length !== b.length) { | |
throw Error(`Can\'t ${action} arrays of length ${a.length} and ${b.length}`); | |
} | |
} | |
const arrayMultiply = (arr1, arr2) => { | |
validateLengths(arr1, arr2, "multiply") | |
return reduce(arr1, (accumulator, arrayItem, i) => | |
accumulator += arrayItem * arr2[i] | |
, 0) | |
} | |
const arrayAdd = (arr1, arr2) => { | |
validateLengths(arr1, arr2, "add") | |
return map(arr1, (arrayItem, i) => | |
arrayItem + arr2[i] | |
) | |
} | |
const arraySubtract = (arr1, arr2) => { | |
validateLengths(arr1, arr2, "subtract") | |
return map(arr1, (arrayItem, i) => | |
arrayItem - arr2[i] | |
) | |
} | |
const sigmoid = (z) => | |
1 / (1 + Math.exp(z * -1)) | |
const sigmoidGradient = (z) => | |
sigmoid(z) * (1 - sigmoid(z)) | |
const sigmoidInverse = (a) => | |
Math.log(a) - Math.log(1 - a) | |
exports.arrayMultiply = arrayMultiply; | |
exports.arrayAdd = arrayAdd; | |
exports.arraySubtract = arraySubtract; | |
exports.sigmoid = sigmoid; | |
exports.sigmoidGradient = sigmoidGradient; | |
exports.sigmoidInverse = sigmoidInverse; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PS. haven't tested for any regressions other than getting similar output from the network