-
Notifications
You must be signed in to change notification settings - Fork 2
/
perceptron.go
38 lines (36 loc) · 1.46 KB
/
perceptron.go
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
package automata
import (
"fmt"
)
// NewPerceptronNetwork creates a new Perceptron with the number of layers equal to the length of the provided slice.
// The elements in the slice determine the size of each layer. The first element is the input layer. The last
// element is the output layer. The inbetween elements are hidden layers. The length of the slice must be at
// least 3 to accomodate an input, hidden and output layer. For example:
//
// // create a network with:
// // - 2 neurons on the input layer
// // - 3 neurons on the first hidden layer
// // - 4 neurons on the second hidden layer
// // - 1 neuron on the output layer
// perceptron := automata.NewPerceptron([]int{2,3,4,1})
func NewPerceptronNetwork(table *LookupTable, sizesOfLayers []int) (*Network, error) {
if len(sizesOfLayers) < 3 {
return nil, fmt.Errorf("NewPerceptron: sizesOfLayers must be at least 3, got %d", len(sizesOfLayers))
}
inputLayer := NewLayer(table, sizesOfLayers[0])
outputLayer := NewLayer(table, sizesOfLayers[len(sizesOfLayers)-1])
prevLayer := &inputLayer
var hiddenLayers []Layer
for i := 1; i < len(sizesOfLayers)-1; i++ {
hidden := NewLayer(table, sizesOfLayers[i])
hiddenLayers = append(hiddenLayers, hidden)
prevLayer.Project(&hidden, LayerTypeAuto)
prevLayer = &hidden
}
prevLayer.Project(&outputLayer, LayerTypeAuto)
return &Network{
Input: &inputLayer,
Hidden: hiddenLayers,
Output: &outputLayer,
}, nil
}