-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgradient-descent.go
60 lines (49 loc) · 1.18 KB
/
gradient-descent.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 (
"optimizers/gd"
"nlp/cloglik"
"io/ioutil"
"os"
"runtime"
"encoding/json"
"fmt"
)
func main() {
var raw []byte
if len(os.Args)<3 {
raw,_ = ioutil.ReadAll(os.Stdin)
} else {
raw,_ = ioutil.ReadFile(os.Args[1])
}
// read in the training data
maxent := cloglik.FromJson(raw)
// initialize gradient descent parameters
grad_descent := gd.GradientDescent{}
grad_descent.LearningRate = 0.1
grad_descent.Threshold = 0.01
grad_descent.MaxIter = -1
// parallelism
//threads,_ := strconv.ParseInt(os.Args[3],0,0)
//fmt.Println("Threads: ",threads)
threads := runtime.NumCPU()
runtime.GOMAXPROCS(threads)
grad_descent.Threads = threads
maxent.Threads = threads
// starting point
x0 := make(gd.NDimensionalValue,maxent.DimCnt()) // x0 = [0,...,0]
// minimize
xmin := grad_descent.Optimize(maxent,x0)
fmt.Fprintln(os.Stderr,"Loglikelihood: ",maxent.Val(xmin))
// prepare the result for JSON output
rs := make(map[string]float64)
for i,v := range xmin {
rs[maxent.FeatureMap[i]] = v
}
// output
json_out, _ := json.Marshal(rs)
if len(os.Args)<3 {
os.Stdout.Write(json_out)
} else {
ioutil.WriteFile(os.Args[2],json_out,0666)
}
}