-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
61 lines (44 loc) · 1.33 KB
/
main.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
package main
import (
"github.com/gokadin/ai-backpropagation/algorithm"
"github.com/gokadin/ai-backpropagation/layer"
"math/rand"
"time"
)
const learningRate = 0.01
func main() {
rand.Seed(time.Now().UTC().UnixNano())
network := buildNetwork()
inputs := buildInputs()
expectedOutputs := buildExpectedOutputs()
algorithm.Learn(network, inputs, expectedOutputs, learningRate)
algorithm.Test(network, inputs)
}
func buildNetwork() *layer.Collection {
inputLayer := layer.NewLayer(2, layer.FunctionIdentity)
hiddenLayer := layer.NewLayer(2, layer.FunctionSigmoid)
outputLayer := layer.NewOutputLayer(1, layer.FunctionIdentity)
inputLayer.ConnectTo(hiddenLayer)
hiddenLayer.ConnectTo(outputLayer)
network := layer.NewCollection()
network.Layers = append(network.Layers, inputLayer)
network.Layers = append(network.Layers, hiddenLayer)
network.Layers = append(network.Layers, outputLayer)
return network
}
func buildInputs() [][]float64 {
inputs := make([][]float64, 4)
inputs[0] = []float64{1.0, 0.0}
inputs[1] = []float64{1.0, 1.0}
inputs[2] = []float64{0.0, 1.0}
inputs[3] = []float64{0.0, 0.0}
return inputs
}
func buildExpectedOutputs() [][]float64 {
outputs := make([][]float64, 4)
outputs[0] = []float64{1.0}
outputs[1] = []float64{0.0}
outputs[2] = []float64{1.0}
outputs[3] = []float64{0.0}
return outputs
}