-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput_perceptron.go
70 lines (61 loc) · 1.13 KB
/
output_perceptron.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
package channn
import (
// "fmt"
"sync"
)
func MakePerceptronOutput(bias float64) *PerceptronOutput {
z := int32(0)
po := &PerceptronOutput{
Output{
InChan: make(chan float64),
NumIn: &z,
Control: make(chan *ControlMessage),
Bias: bias,
OutChan: make(chan float64),
mutex: &sync.Mutex{},
nType: OUTPUT_TYPE,
},
}
go po.Listen()
return po
}
type PerceptronOutput struct {
Output
}
// Fire conditionally sends a value of 1.0 or 0.0
// to the output channel.
func (po *PerceptronOutput) Fire(val float64) {
if val >= 1.0 {
po.OutChan <- 1.0
} else {
po.OutChan <- 0.0
}
}
func (po *PerceptronOutput) Listen() {
var counter = *po.NumIn
var layerTotal float64
for {
select {
case inVal := <-po.InChan:
layerTotal += inVal
counter--
if counter == 0 {
// The sum of the (Xi * Wj)
po.Fire(layerTotal + po.Bias)
layerTotal = 0.0
counter = *po.NumIn
}
case ctlMsg := <-po.Control:
switch ctlMsg.Id {
case DESTROY:
return
case INCREMENT_INPUT:
cur := (*po.NumIn + 1)
po.NumIn = &cur
counter = *po.NumIn
default:
continue
}
}
}
}