-
Notifications
You must be signed in to change notification settings - Fork 65
/
neural.go
152 lines (134 loc) · 3.25 KB
/
neural.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
package deep
import (
"fmt"
)
// Neural is a neural network
type Neural struct {
Layers []*Layer
Biases [][]*Synapse
Config *Config
}
// Config defines the network topology, activations, losses etc
type Config struct {
// Number of inputs
Inputs int
// Defines topology:
// For instance, [5 3 3] signifies a network with two hidden layers
// containing 5 and 3 nodes respectively, followed an output layer
// containing 3 nodes.
Layout []int
// Activation functions: {ActivationTanh, ActivationReLU, ActivationSigmoid}
Activation ActivationType
// Solver modes: {ModeRegression, ModeBinary, ModeMultiClass, ModeMultiLabel}
Mode Mode
// Initializer for weights: {NewNormal(σ, μ), NewUniform(σ, μ)}
Weight WeightInitializer `json:"-"`
// Loss functions: {LossCrossEntropy, LossBinaryCrossEntropy, LossMeanSquared}
Loss LossType
// Apply bias nodes
Bias bool
}
// NewNeural returns a new neural network
func NewNeural(c *Config) *Neural {
if c.Weight == nil {
c.Weight = NewUniform(0.5, 0)
}
if c.Activation == ActivationNone {
c.Activation = ActivationSigmoid
}
if c.Loss == LossNone {
switch c.Mode {
case ModeMultiClass, ModeMultiLabel:
c.Loss = LossCrossEntropy
case ModeBinary:
c.Loss = LossBinaryCrossEntropy
default:
c.Loss = LossMeanSquared
}
}
layers := initializeLayers(c)
var biases [][]*Synapse
if c.Bias {
biases = make([][]*Synapse, len(layers))
for i := 0; i < len(layers); i++ {
if c.Mode == ModeRegression && i == len(layers)-1 {
continue
}
biases[i] = layers[i].ApplyBias(c.Weight)
}
}
return &Neural{
Layers: layers,
Biases: biases,
Config: c,
}
}
func initializeLayers(c *Config) []*Layer {
layers := make([]*Layer, len(c.Layout))
for i := range layers {
act := c.Activation
if i == (len(layers)-1) && c.Mode != ModeDefault {
act = OutputActivation(c.Mode)
}
layers[i] = NewLayer(c.Layout[i], act)
}
for i := 0; i < len(layers)-1; i++ {
layers[i].Connect(layers[i+1], c.Weight)
}
for _, neuron := range layers[0].Neurons {
neuron.In = make([]*Synapse, c.Inputs)
for i := range neuron.In {
neuron.In[i] = NewSynapse(c.Weight())
}
}
return layers
}
func (n *Neural) fire() {
for _, b := range n.Biases {
for _, s := range b {
s.fire(1)
}
}
for _, l := range n.Layers {
l.fire()
}
}
// Forward computes a forward pass
func (n *Neural) Forward(input []float64) error {
if len(input) != n.Config.Inputs {
return fmt.Errorf("Invalid input dimension - expected: %d got: %d", n.Config.Inputs, len(input))
}
for _, n := range n.Layers[0].Neurons {
for i := 0; i < len(input); i++ {
n.In[i].fire(input[i])
}
}
n.fire()
return nil
}
// Predict computes a forward pass and returns a prediction
func (n *Neural) Predict(input []float64) []float64 {
n.Forward(input)
outLayer := n.Layers[len(n.Layers)-1]
out := make([]float64, len(outLayer.Neurons))
for i, neuron := range outLayer.Neurons {
out[i] = neuron.Value
}
return out
}
// NumWeights returns the number of weights in the network
func (n *Neural) NumWeights() (num int) {
for _, l := range n.Layers {
for _, n := range l.Neurons {
num += len(n.In)
}
}
return
}
func (n *Neural) String() string {
var s string
for _, l := range n.Layers {
s = fmt.Sprintf("%s\n%s", s, l)
}
return s
}