-
Notifications
You must be signed in to change notification settings - Fork 0
/
layer.go
76 lines (68 loc) · 1.74 KB
/
layer.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
package deep
import "fmt"
// Layer is a set of neurons and corresponding activation
type Layer struct {
Neurons []*Neuron
A ActivationType
}
// NewLayer creates a new layer with n nodes
func NewLayer(n int, activation ActivationType) *Layer {
neurons := make([]*Neuron, n)
for i := 0; i < n; i++ {
act := activation
if activation == ActivationSoftmax {
act = ActivationLinear
}
neurons[i] = NewNeuron(act)
}
return &Layer{
Neurons: neurons,
A: activation,
}
}
func (l *Layer) fire() {
for _, n := range l.Neurons {
n.fire()
}
if l.A == ActivationSoftmax {
outs := make([]float64, len(l.Neurons))
for i, neuron := range l.Neurons {
outs[i] = neuron.Value
}
sm := Softmax(outs)
for i, neuron := range l.Neurons {
neuron.Value = sm[i]
}
}
}
// Connect fully connects layer l to next, and initializes each
// synapse with the given weight function
func (l *Layer) Connect(next *Layer, weight WeightInitializer) {
for i := range l.Neurons {
for j := range next.Neurons {
syn := NewSynapse(weight())
l.Neurons[i].Out = append(l.Neurons[i].Out, syn)
next.Neurons[j].In = append(next.Neurons[j].In, syn)
}
}
}
// ApplyBias creates and returns a bias synapse for each neuron in l
func (l *Layer) ApplyBias(weight WeightInitializer) []*Synapse {
biases := make([]*Synapse, len(l.Neurons))
for i := range l.Neurons {
biases[i] = NewSynapse(weight())
biases[i].IsBias = true
l.Neurons[i].In = append(l.Neurons[i].In, biases[i])
}
return biases
}
func (l Layer) String() string {
weights := make([][]float64, len(l.Neurons))
for i, n := range l.Neurons {
weights[i] = make([]float64, len(n.In))
for j, s := range n.In {
weights[i][j] = s.Weight
}
}
return fmt.Sprintf("%+v", weights)
}