-
Notifications
You must be signed in to change notification settings - Fork 0
/
neuronNetwork.js
97 lines (75 loc) · 2.06 KB
/
neuronNetwork.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* Neuron Network
* A neural network, which can be load from a 'weight' set.
* It has a set of layer containing of the node.
* The first layer is represented, but not used. (directly inputs)
*/
var NeuronNetwork = function()
{
/** Initializer **/
/**
* Create the neuron network going throug a 'Weight' object.
* You can also set the threshold of the neuron. Set null if you just want the system to used only the sum.
* The output will not be based on threshold (true result, for comparison).
*/
this.init = function( weightSet, threshold )
{
weights = weightSet;
var layerNbr = weightSet.get_layer_nbr();
var neuronNbr;
layers = new Array(layerNbr);
//Going throug available layer
for(var i=0; i<layerNbr; i++)
{
//Create the layer
layers[i] = new NeuronLayer();
//Creating the neuronlist
neuronNbr = weightSet.get_neuron_nbr(i);
var neuronList = new Array(neuronNbr);
for(var j=0; j<neuronNbr; j++)
{
neuronList[j] = new Neuron();
//Set the threshold if there is one and not for the last one
if(threshold != null && i<layerNbr-1)
{ neuronList[j].set_threshold( threshold ); }
//console.log("NeuronNetwork creation: layer "+i+" adding "+ weightSet.get_weight(i,j));
neuronList[j].init_weigh( weightSet.get_weight(i,j) );
}
//Init the layer
layers[i].init_layer(neuronList);
}
}
/** Attributes **/
/**
* A list of layer.
* Created at initialization.
*/
var layers;
/**
* The weight structure
*/
var weights;
/** Methods **/
/**
* Compute the result of the neural network from inputs
*/
this.compute_result = function( input )
{
//Cuurent data (output from the previous, input for then next)
var prevOutput = input;
//Transmitting the previous layer outpur as the input of the next level
for(var i=1; i<layers.length; i++)
{
//console.log(prevOutput);
prevOutput = layers[i].get_output(prevOutput);
}
return prevOutput;
}
/**
* Weight getter
*/
this.get_weights = function()
{
return weights;
}
}