-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
196 lines (172 loc) · 3.8 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"bufio"
"errors"
"fmt"
"os"
"strconv"
"strings"
)
var str2id = make(map[string]int)
func GetId(str string) int {
id, ok := str2id[str]
if ok {
return id
} else {
l := len(str2id)
str2id[str] = l
return l
}
}
type Instance struct {
label int
features map[int]float64
}
type Model struct {
weight map[int]float64
cumWeight map[int]float64
count int
}
func ParseLine(line string) (*Instance, error) {
tmp := strings.Split(strings.TrimSpace(line), " ")
if len(tmp) < 2 {
return nil, errors.New("Invalid line")
}
label, err := strconv.ParseInt(tmp[0], 10, 32)
if err != nil {
return nil, err
}
features := make(map[int]float64)
for _, v := range tmp[1:] {
tmp := strings.Split(v, ":")
n, err := strconv.ParseFloat(tmp[1], 64)
if err != nil {
return nil, err
}
features[GetId(tmp[0])] = n
}
return &Instance{int(label), features}, nil
}
func GetAccuracy(gold []int, predict []int) float64 {
if len(gold) != len(predict) {
return 0.0
}
sum := 0.0
for i, v := range gold {
if v == predict[i] {
sum += 1.0
}
}
return sum / float64(len(gold))
}
func (model *Model) Learn(instance Instance) {
predict := model.predictForTraining(instance.features)
if instance.label != predict {
for k, v := range instance.features {
w, _ := model.weight[k]
cumW, _ := model.cumWeight[k]
model.weight[k] = w + float64(instance.label)*v
model.cumWeight[k] = cumW + float64(model.count)*float64(instance.label)*v
}
model.count += 1
}
}
func (model *Model) predictForTraining(features map[int]float64) int {
result := 0.0
for k, v := range features {
w, ok := model.weight[k]
if ok {
result = result + w*v
}
}
if result > 0 {
return 1
}
return -1
}
func (model Model) Predict(features map[int]float64) int {
result := 0.0
for k, v := range features {
w, ok := model.weight[k]
if ok {
result = result + w*v
}
w, ok = model.cumWeight[k]
if ok {
result = result - w*v/float64(model.count)
}
}
if result > 0 {
return 1
}
return -1
}
func Readln(r *bufio.Reader) (string, error) {
var (
isPrefix bool = true
err error = nil
line, ln []byte
)
for isPrefix && err == nil {
line, isPrefix, err = r.ReadLine()
ln = append(ln, line...)
}
return string(ln), err
}
func ReadData(r *bufio.Reader) []Instance {
result := make([]Instance, 0)
s, e := Readln(r)
for e == nil {
instance, err := ParseLine(s)
if err == nil { // skip invalid line
result = append(result, *instance)
}
s, e = Readln(r)
}
return result
}
func ExtractGoldLabels(data []Instance) []int {
golds := make([]int, 0, 0)
for _, instance := range data {
golds = append(golds, instance.label)
}
return golds
}
func main() {
bio := bufio.NewReader(os.Stdin)
data := ReadData(bio)
n := int(float64(len(data)) * float64(0.8))
train := data[:n]
test := data[n+1:]
model := Model{make(map[int]float64), make(map[int]float64), 1}
trainGolds := ExtractGoldLabels(train)
testGolds := ExtractGoldLabels(test)
for iter := 0; iter < 10; iter++ {
for _, instance := range train {
model.Learn(instance)
}
trainPredicts := make([]int, len(train))
sem := make(chan struct{}, len(train))
for i, instance := range train {
go func(i int, instance Instance) {
trainPredicts[i] = model.Predict(instance.features)
sem <- struct{}{}
}(i, instance)
}
for i := 0; i < len(train); i++ {
<-sem
}
testPredicts := make([]int, len(test))
sem = make(chan struct{}, len(test))
for i, instance := range test {
go func(i int, instance Instance) {
testPredicts[i] = model.Predict(instance.features)
sem <- struct{}{}
}(i, instance)
}
for i := 0; i < len(test); i++ {
<-sem
}
fmt.Printf("%d\t%0.3f\t%0.3f\n", iter, GetAccuracy(trainGolds, trainPredicts), GetAccuracy(testGolds, testPredicts))
}
}