forked from patrikeh/go-deep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
neuron.go
56 lines (47 loc) · 1.09 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
package deep
// Neuron is a neural network node
type Neuron struct {
A ActivationType `json:"-"`
In []*Synapse
Out []*Synapse
Value float64 `json:"-"`
}
// NewNeuron returns a neuron with the given activation
func NewNeuron(activation ActivationType) *Neuron {
return &Neuron{
A: activation,
}
}
func (n *Neuron) fire() {
var sum float64
for _, s := range n.In {
sum += s.Out
}
n.Value = n.Activate(sum)
nVal := n.Value
for _, s := range n.Out {
s.fire(nVal)
}
}
// Activate applies the neurons activation
func (n *Neuron) Activate(x float64) float64 {
return GetActivation(n.A).F(x)
}
// DActivate applies the derivative of the neurons activation
func (n *Neuron) DActivate(x float64) float64 {
return GetActivation(n.A).Df(x)
}
// Synapse is an edge between neurons
type Synapse struct {
Weight float64
In, Out float64 `json:"-"`
IsBias bool
}
// NewSynapse returns a synapse with the specified initialized weight
func NewSynapse(weight float64) *Synapse {
return &Synapse{Weight: weight}
}
func (s *Synapse) fire(value float64) {
s.In = value
s.Out = s.In * s.Weight
}