-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhopfield.go
37 lines (34 loc) · 1.05 KB
/
hopfield.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
package automata
type Hopfield struct {
Network
}
// Activate activates the network with the given input. The output values will always be either 0 or 1 to reflect
// the semantics of hopfield networks.
func (h *Hopfield) Activate(input []float64) ([]float64, error) {
output, err := h.Network.Activate(input)
if err != nil {
return nil, err
}
for i := 0; i < len(output); i++ {
if output[i] < 0.5 {
output[i] = 0
} else {
output[i] = 1
}
}
return output, nil
}
// NewHopfieldNetwork creates a new Hopfield network. 'size' dictates the number of input neurons and the number
// of output neurons. Generally this value should match the number of boolean options in your network
// e.g. 1 neuron per pixel if recalling images.
func NewHopfieldNetwork(table *LookupTable, size int) *Hopfield {
inputLayer := NewLayer(table, size)
outputLayer := NewLayer(table, size)
inputLayer.Project(&outputLayer, LayerTypeAllToAll)
return &Hopfield{
Network: Network{
Input: &inputLayer,
Output: &outputLayer,
},
}
}