This repository was archived by the owner on Dec 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathneural_network.go
164 lines (142 loc) · 4.94 KB
/
neural_network.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// neural_network.go implementation of the neural network.
//
// Copyright (C) 2017 Jin Yeom
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package neat
import (
"fmt"
"sort"
)
// Neuron is an implementation of a single neuron of a neural network.
type Neuron struct {
ID int // neuron ID
Type string // neuron type
Signal float64 // signal held by this neuron
Synapses map[*Neuron]float64 // synapse from input neurons
Activation *ActivationFunc // activation function
activated bool // true if it has been activated
}
// NewNeuron returns a new instance of neuron, given a node gene.
func NewNeuron(nodeGene *NodeGene) *Neuron {
return &Neuron{
ID: nodeGene.ID,
Type: nodeGene.Type,
Signal: 0.0,
Synapses: make(map[*Neuron]float64),
Activation: nodeGene.Activation,
activated: false,
}
}
// String returns the string representation of Neuron.
func (n *Neuron) String() string {
if len(n.Synapses) == 0 {
return fmt.Sprintf("[%s(%d, %s)]", n.Type, n.ID, n.Activation.Name)
}
str := fmt.Sprintf("[%s(%d, %s)] (\n", n.Type, n.ID, n.Activation.Name)
for neuron, weight := range n.Synapses {
str += fmt.Sprintf(" <--{%.3f}--[%s(%d, %s)]\n",
weight, neuron.Type, neuron.ID, neuron.Activation.Name)
}
return str + ")"
}
// Activate retrieves signal from neurons that are connected to this neuron and
// return its signal.
func (n *Neuron) Activate() float64 {
// if the neuron's already activated, or it isn't connected from any neurons,
// return its current signal.
if n.activated || len(n.Synapses) == 0 {
return n.Signal
}
n.activated = true
inputSum := 0.0
for neuron, weight := range n.Synapses {
inputSum += neuron.Activate() * weight
}
n.Signal = n.Activation.Fn(inputSum)
return n.Signal
}
// NeuralNetwork is an implementation of the phenotype neural network that is
// decoded from a genome.
type NeuralNetwork struct {
Neurons []*Neuron // all neurons in the network
inputNeurons []*Neuron // input neurons
outputNeurons []*Neuron // output neurons
}
// NewNeuralNetwork returns a new instance of NeuralNetwork given a genome to
// decode from.
func NewNeuralNetwork(g *Genome) *NeuralNetwork {
sort.Slice(g.NodeGenes, func(i, j int) bool {
return g.NodeGenes[i].ID < g.NodeGenes[j].ID
})
inputNeurons := make([]*Neuron, 0, len(g.NodeGenes))
outputNeurons := make([]*Neuron, 0, len(g.NodeGenes))
neurons := make([]*Neuron, 0, len(g.NodeGenes))
for _, nodeGene := range g.NodeGenes {
neuron := NewNeuron(nodeGene)
// record input and output neurons separately
if nodeGene.Type == "input" {
inputNeurons = append(inputNeurons, neuron)
} else if nodeGene.Type == "output" {
outputNeurons = append(outputNeurons, neuron)
}
neurons = append(neurons, neuron)
}
for _, connGene := range g.ConnGenes {
if !connGene.Disabled {
if in := sort.Search(len(neurons), func(i int) bool {
return neurons[i].ID >= connGene.From
}); in < len(neurons) && neurons[in].ID == connGene.From {
if out := sort.Search(len(neurons), func(i int) bool {
return neurons[i].ID >= connGene.To
}); out < len(neurons) && neurons[out].ID == connGene.To {
neurons[out].Synapses[neurons[in]] = connGene.Weight
}
}
}
}
return &NeuralNetwork{neurons, inputNeurons, outputNeurons}
}
// String returns the string representation of NeuralNetwork.
func (n *NeuralNetwork) String() string {
str := fmt.Sprintf("NeuralNetwork(%d, %d):\n",
len(n.inputNeurons), len(n.outputNeurons))
for _, neuron := range n.Neurons {
str += neuron.String() + "\n"
}
return str[:len(str)-1]
}
// FeedForward propagates inputs signals from input neurons to output neurons,
// and return output signals.
func (n *NeuralNetwork) FeedForward(inputs []float64) ([]float64, error) {
if len(inputs) != len(n.inputNeurons) {
errStr := "Invalid number of inputs: %d != %d"
return nil, fmt.Errorf(errStr, len(n.inputNeurons), len(inputs))
}
// register sensor inputs
for i, neuron := range n.inputNeurons {
neuron.Signal = inputs[i]
}
// recursively propagate from input neurons to output neurons
outputs := make([]float64, 0, len(n.outputNeurons))
for _, neuron := range n.outputNeurons {
outputs = append(outputs, neuron.Activate())
}
// reset all neurons
for _, neuron := range n.Neurons {
neuron.Signal = 0.0
neuron.activated = false
}
return outputs, nil
}