-
Notifications
You must be signed in to change notification settings - Fork 9
/
perceptron.go
86 lines (72 loc) · 2 KB
/
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package varis
import (
"fmt"
"math/rand"
)
// Perceptron implement Neural Network Perceptron by collect layers with Neurons and input/output channels.
type Perceptron struct {
layers [][]Neuron
input []chan float64
output []chan float64
}
// Calculate run Network calculations by broadcasting signals to input channels and wait signals from output array of chan.
func (n *Perceptron) Calculate(input Vector) Vector {
if len(input) != len(n.layers[0]) {
panic("Check count of input value")
}
input.broadcast(n.input)
output := collectVector(n.output)
if PrintCalculation == true {
fmt.Printf("Input: %v Output: %v\n", input, output)
}
return output
}
// RunNeurons create goroutines for all Neuron in Perceptron.
func (n *Perceptron) RunNeurons() {
for _, l := range n.layers {
for _, neuron := range l {
go neuron.live()
}
}
}
// ConnectLayers create all to all connection between layers.
func (n *Perceptron) ConnectLayers() {
for l := 0; l < len(n.layers)-1; l++ {
now := n.layers[l]
next := n.layers[l+1]
for i := range now {
for o := range next {
ConnectNeurons(now[i], next[o], rand.Float64())
}
}
}
}
// CreatePerceptron make new Perceptron NN with count of neurons in each Layer.
func CreatePerceptron(layers ...int) Perceptron {
network := Perceptron{}
network.input = make([]chan float64, 0)
network.output = make([]chan float64, 0)
for index, neurons := range layers {
layer := []Neuron{}
for i := 0; i < neurons; i++ {
var neuron Neuron
switch index {
case 0:
channel := make(chan float64)
neuron = INeuron(rand.Float64(), channel)
network.input = append(network.input, channel)
case len(layers) - 1:
channel := make(chan float64)
neuron = ONeuron(rand.Float64(), channel)
network.output = append(network.output, channel)
default:
neuron = HNeuron(rand.Float64())
}
layer = append(layer, neuron)
}
network.layers = append(network.layers, layer)
}
network.ConnectLayers()
network.RunNeurons()
return network
}