-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathneuron.go
94 lines (81 loc) · 2.42 KB
/
neuron.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
package varis
// Neuron - interface for all Neuron.
// Each Neuron must have:
// - coreNeuron is a basic neuron for all types
// - getCore() is a the function for getting pointer to CoreNeuron
// - live() - method for running neuron's goroutine. All kind of Neurons implement functionality live
// - changeWeight is the method for training
type Neuron interface {
live()
getCore() *CoreNeuron
changeWeight(neuronDelta float64)
}
// CoreNeuron - entity with float64 weight (it is bias) and connection.
// Activation result store in cache for training.
type CoreNeuron struct {
conn connection
weight float64
cache float64
}
// changeWeight - change weight of CoreNeuron and change weight for all related synapses (with connection.changeWeight).
func (n *CoreNeuron) changeWeight(neuronDelta float64) {
n.weight += neuronDelta
n.conn.changeWeight(neuronDelta)
}
// getCore - return core of Neuron.
func (n *CoreNeuron) getCore() *CoreNeuron {
return n
}
// This kind of Neuron get signal from connectTo channel and broadcast it to all output synapses without Activation.
type inputNeuron struct {
CoreNeuron
connectTo chan float64
}
// INeuron - creates inputNeuron.
func INeuron(weight float64, connectTo chan float64) Neuron {
return &inputNeuron{
CoreNeuron: CoreNeuron{weight: weight},
connectTo: connectTo,
}
}
func (neuron *inputNeuron) live() {
for {
neuron.conn.broadcastSignals(<-neuron.connectTo)
}
}
// This kind of Neuron get signal from input Synapses channel, activate and broadcast it to all output synapses.
type hiddenNeuron struct {
CoreNeuron
}
// HNeuron - creates hiddenNeuron.
func HNeuron(weight float64) Neuron {
return &hiddenNeuron{
CoreNeuron: CoreNeuron{weight: weight},
}
}
func (neuron *hiddenNeuron) live() {
for {
vector := neuron.conn.collectSignals()
neuron.cache = vector.sum() + neuron.weight
neuron.conn.broadcastSignals(ACTIVATION(neuron.cache))
}
}
// This kind of Neuron get signal from input Synapses channel, activate and send it to connectTo channel.
type outputNeuron struct {
CoreNeuron
connectTo chan float64
}
// ONeuron - creates outputNeuron.
func ONeuron(weight float64, connectTo chan float64) Neuron {
return &outputNeuron{
CoreNeuron: CoreNeuron{weight: weight},
connectTo: connectTo,
}
}
func (neuron *outputNeuron) live() {
for {
vector := neuron.conn.collectSignals()
neuron.cache = vector.sum() + neuron.weight
neuron.connectTo <- ACTIVATION(neuron.cache)
}
}