-
Notifications
You must be signed in to change notification settings - Fork 53
/
main.go
130 lines (114 loc) · 3.14 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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"math"
"math/rand"
"net/http"
"os"
"runtime/pprof"
"github.com/fumin/ntm"
"github.com/fumin/ntm/repeatcopy"
)
var (
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
weightsChan = make(chan chan []byte)
lossChan = make(chan chan []float64)
printDebugChan = make(chan struct{})
)
func main() {
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
http.HandleFunc("/Weights", func(w http.ResponseWriter, r *http.Request) {
c := make(chan []byte)
weightsChan <- c
w.Write(<-c)
})
http.HandleFunc("/Loss", func(w http.ResponseWriter, r *http.Request) {
c := make(chan []float64)
lossChan <- c
json.NewEncoder(w).Encode(<-c)
})
http.HandleFunc("/PrintDebug", func(w http.ResponseWriter, r *http.Request) {
printDebugChan <- struct{}{}
})
port := 8096
go func() {
log.Printf("Listening on port %d", port)
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil); err != nil {
log.Fatalf("%v", err)
}
}()
var seed int64 = 16
rand.Seed(seed)
genFunc := "bt"
x, y := repeatcopy.G[genFunc](1, 1)
h1Size := 100
numHeads := 2
n := 128
m := 20
c := ntm.NewEmptyController1(len(x[0]), len(y[0]), h1Size, numHeads, n, m)
weights := c.WeightsVal()
for i := range weights {
weights[i] = 1 * (rand.Float64() - 0.5)
}
losses := make([]float64, 0)
doPrint := false
rmsp := ntm.NewRMSProp(c)
log.Printf("genFunc: %s, seed: %d, numweights: %d, numHeads: %d", genFunc, seed, len(c.WeightsVal()), c.NumHeads())
for i := 1; ; i++ {
x, y := repeatcopy.G[genFunc](rand.Intn(10)+1, rand.Intn(10)+1)
model := &ntm.LogisticModel{Y: y}
machines := rmsp.Train(x, model, 0.95, 0.5, 1e-3, 1e-3)
l := model.Loss(ntm.Predictions(machines))
if i%1000 == 0 {
bpc := l / float64(len(y)*len(y[0]))
losses = append(losses, bpc)
log.Printf("%d, bpc: %f, seq length: %d", i, bpc, len(y))
}
handleHTTP(c, losses, &doPrint)
if i%1000 == 0 && doPrint {
printDebug(y, machines)
}
}
}
func handleHTTP(c ntm.Controller, losses []float64, doPrint *bool) {
select {
case cn := <-weightsChan:
b, err := json.Marshal(c.WeightsVal())
if err != nil {
log.Fatalf("%v", err)
}
cn <- b
case cn := <-lossChan:
cn <- losses
case <-printDebugChan:
*doPrint = !*doPrint
default:
return
}
}
func printDebug(y [][]float64, machines []*ntm.NTM) {
log.Printf("y: %+v", y)
log.Printf("pred: %s", ntm.Sprint2(ntm.Predictions(machines)))
n := machines[0].Controller.MemoryN()
//outputT := len(machines) - (len(machines) - 2) / 2
outputT := 0
for t := outputT; t < len(machines); t++ {
h := machines[t].Controller.Heads()[0]
beta := math.Exp(*h.BetaVal())
g := ntm.Sigmoid(*h.GVal())
shift := math.Mod(2*ntm.Sigmoid(*h.SVal())-1+float64(n), float64(n))
gamma := math.Log(math.Exp(*h.GammaVal())+1) + 1
log.Printf("beta: %.3g(%v), g: %.3g(%v), s: %.3g(%v), gamma: %.3g(%v), erase: %+v, add: %+v, k: %+v", beta, *h.BetaVal(), g, *h.GVal(), shift, *h.SVal(), gamma, *h.GammaVal(), h.EraseVal(), h.AddVal(), h.KVal())
}
}