-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayer.go
More file actions
73 lines (66 loc) · 1.65 KB
/
layer.go
File metadata and controls
73 lines (66 loc) · 1.65 KB
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
package nn
import (
"fmt"
)
type layer struct {
neurons []*neuron
inputChans []chan float64
connections int
}
func newLayer(prevL *layer, numNeurons, numNextL int, weights []float64, wCount *int) *layer {
l := &layer{
neurons: make([]*neuron, numNeurons),
inputChans: make([]chan float64, numNeurons),
}
for i := 0; i < numNeurons; i++ {
out := make([]chan float64, numNextL)
for k := range out {
out[k] = make(chan float64)
}
if prevL != nil {
inp := make([]chan float64, len(prevL.neurons))
for k, prevLNeur := range prevL.neurons {
inp[k] = prevLNeur.output[i]
}
//fmt.Println("creating hidden neuron", numNeur)
subWeights := weights[*wCount+l.connections : *wCount+l.connections+len(inp)]
l.neurons[i] = newNeuron(inp, out, false, subWeights)
l.connections += len(l.neurons[i].weight)
} else {
inp := make([]chan float64, 1)
inp[0] = make(chan float64)
subWeights := make([]float64, 1)
l.inputChans[i] = inp[0]
//fmt.Println("creating input neuron", numNeur)
l.neurons[i] = newNeuron(inp, out, true, subWeights)
}
}
*wCount += l.connections
return l
}
func (l *layer) String() string {
str := ""
for nNum, neur := range l.neurons {
str += fmt.Sprintln("\tneur", nNum, "holds\n", neur)
}
return str
}
func (l *layer) activate() {
for _, neuron := range l.neurons {
go neuron.activate()
}
}
func (l *layer) getWeights() (w []float64) {
for _, neur := range l.neurons {
w = append(w, neur.weight...)
}
return w
}
func (l *layer) setWeigths(w []float64) {
start, end := 0, 0
for _, neur := range l.neurons {
end += len(neur.input)
neur.weight = w[start:end]
start = end
}
}