-
Notifications
You must be signed in to change notification settings - Fork 1
/
evolve.go
66 lines (55 loc) · 1.52 KB
/
evolve.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
package main
import (
"fmt"
"github.com/lukks/neural-go/v3"
"time"
)
const fmtColor = "\033[0;36m%s\033[0m"
func main() {
xor := neural.NewNeural([]*neural.Layer{
{Inputs: 2, Units: 3},
{Units: 3},
{Units: 1, Loss: "mse"},
})
fmt.Printf(fmtColor, "think some values:\n")
fmt.Printf("0, 0 [0] -> %f\n", xor.Think([]float64{0, 0}))
fmt.Printf("1, 0 [1] -> %f\n", xor.Think([]float64{1, 0}))
fmt.Printf("0, 1 [1] -> %f\n", xor.Think([]float64{0, 1}))
fmt.Printf("1, 1 [0] -> %f\n", xor.Think([]float64{1, 1}))
fmt.Printf(fmtColor, "learning:\n")
start := millis()
xor = xor.Evolve(neural.Evolve{
Population: 20,
Mutate: 0.05,
Crossover: 0.5,
Elitism: 5,
Epochs: 100,
Iterations: 50,
Threshold: 0.00005,
Dataset: [][][]float64{
{{0, 0}, {0}},
{{1, 0}, {1}},
{{0, 1}, {1}},
{{1, 1}, {0}},
},
Callback: func(epoch int, loss float64) bool {
if epoch%10 == 0 || epoch == 99 {
fmt.Printf("epoch=%v loss=%f elapsed=%v\n", epoch, loss, millis()-start)
}
return true
},
})
fmt.Printf(fmtColor, "think some values:\n")
fmt.Printf("0, 0 [0] -> %f\n", xor.Think([]float64{0, 0}))
fmt.Printf("1, 0 [1] -> %f\n", xor.Think([]float64{1, 0}))
fmt.Printf("0, 1 [1] -> %f\n", xor.Think([]float64{0, 1}))
fmt.Printf("1, 1 [0] -> %f\n", xor.Think([]float64{1, 1}))
fmt.Printf(fmtColor, "export:\n")
json, _ := xor.Export()
fmt.Printf("%s\n", json)
// stream the json over network
// or just xor.ToFile("./evolve.json")
}
func millis() int64 {
return time.Now().UnixNano() / 1e6
}