diff options
author | TinWoodman92 <chrhodgden@gmail.com> | 2024-02-08 16:09:56 -0600 |
---|---|---|
committer | TinWoodman92 <chrhodgden@gmail.com> | 2024-02-08 16:09:56 -0600 |
commit | 62993dd59bbbfcdb07dbb5836d359fad6334f23e (patch) | |
tree | 4f0d310442c66050d0886eb82bebd9475e63803c /src/activationFunctions.js |
initial commit
Diffstat (limited to 'src/activationFunctions.js')
-rw-r--r-- | src/activationFunctions.js | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/activationFunctions.js b/src/activationFunctions.js new file mode 100644 index 0000000..6ccc046 --- /dev/null +++ b/src/activationFunctions.js @@ -0,0 +1,54 @@ +console.log('Hello from activationFunctions.js'); + +let sigmoidExpression = '1 / (1 + exp(-x))'; +let dSigmoidExpression = math.derivative(sigmoidExpression, 'x'); + +sigmoidExpression = math.compile(sigmoidExpression); +dSigmoidExpression = dSigmoidExpression.toString(); +dSigmoidExpression = math.compile(dSigmoidExpression); + +const sigmoid = function(xValue) { + return sigmoidExpression.evaluate({x: xValue}); +}; + +const dSigmoid_dx = function(xValue) { + return dSigmoidExpression.evaluate({x: xValue}); +}; + +const relu = function(x) { + return x * (x > 0); +}; + +const dRelu_dx = function(x) { + return Number(x > 0); +}; + +const identity = function(x) { + return x; +}; + +const dIdentity_dx = function(x) { + return 1; +}; + +const matrixMethod = function(elementMethod) { + const method = function(matrix) { + return math.map(matrix, (element) => elementMethod(element)); + }; + return method; +}; + +activationFunctionList = { + 'relu': { + gx: matrixMethod(relu), + dg_dx: matrixMethod(dRelu_dx) + }, + 'sigmoid': { + gx: matrixMethod(sigmoid), + dg_dx: matrixMethod(dSigmoid_dx) + }, + 'identity': { + gx: matrixMethod(identity), + dg_dx: matrixMethod(dIdentity_dx) + } +};
\ No newline at end of file |