-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneuron.js
69 lines (57 loc) · 1.43 KB
/
neuron.js
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
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* Neuron
* Contains a list of weight applying to the links with the previous layer. A method tell if the neuron is activated or not.
* It may be binary output if there is a threshold, or the sum of all links if no threshold.
*/
var Neuron = function()
{
/** Initializer **/
/**
* Init the neuron from a weigh list
*/
this.init_weigh = function(weightListLoad)
{
weightList = weightListLoad;
}
/** Attributes **/
/**
* List of weight on the link with the previous layer.
*/
var weightList;
/**
* Threshold used to tell if the neuron is activated or not.
* If not set, the checking method will return the avg value
*/
var threshold = 0;
/** Methods **/
/**
* Set the threshold
*/
this.set_threshold = function(newThreshold)
{
threshold = newThreshold;
}
/**
* Get the output by doing the sum of the output of the previous level (our input) applying the weigh.
* @param input Array of the output of the previous layer
*/
this.get_output = function(input)
{
if(input.length != weightList.length)
{ console.log("Mismatch between input("+input.length +") and weight("+weightList.length +") ! "); }
var sum = 0;
for(var i=0; i < input.length; i++)
{
sum += input[i]*weightList[i];
}
//No threshold set
if(threshold == 0)
{ return sum; }
//Threshold passed, neuron activated
else if( sum > threshold )
{ return 1; }
//Not activated
else
{ return 0; }
}
}