summaryrefslogtreecommitdiff
path: root/src/activationFunctions.js
blob: a5dedde3b7fafa450e154afe5d7c90d598ec6575 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const math = require('mathjs');

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)
	}
};

module.exports = activationFunctionList;