Skip to content

Instantly share code, notes, and snippets.

@ruemic
Last active February 13, 2016 11:33
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 ruemic/49b13d1394e417f9be3e to your computer and use it in GitHub Desktop.
Save ruemic/49b13d1394e417f9be3e to your computer and use it in GitHub Desktop.
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;
@ruemic
Copy link
Author

ruemic commented Feb 13, 2016

PS. haven't tested for any regressions other than getting similar output from the network

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment