/
passive_aggressive.go
249 lines (204 loc) · 4.99 KB
/
passive_aggressive.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package regression
import (
"errors"
"fmt"
"github.com/sensorbee/jubatus/internal/nested"
"github.com/ugorji/go/codec"
"gopkg.in/sensorbee/sensorbee.v0/data"
"io"
"math"
"sync"
)
// PassiveAggressive holds a model for regression.
type PassiveAggressive struct {
model model
sum float32
sqSum float32
count uint64
m sync.RWMutex
regWeight float32
sensitivity float32
}
// NewPassiveAggressive creates a PassiveAggressive model. regWeight must be greater than zero.
// sensitivity must not be less than zero.
func NewPassiveAggressive(regWeight float32, sensitivity float32) (*PassiveAggressive, error) {
if regWeight <= 0 {
return nil, errors.New("regularization weight must be larger than zero")
}
if sensitivity < 0 {
return nil, errors.New("sensitivity must not be less than zero")
}
return &PassiveAggressive{
model: make(model),
regWeight: regWeight,
sensitivity: sensitivity,
}, nil
}
// Train trains a model with a feature vector and a value.
func (pa *PassiveAggressive) Train(v FeatureVector, value float32) error {
fv, err := v.toInternal()
if err != nil {
return err
}
pa.m.Lock()
defer pa.m.Unlock()
pa.sum += value
pa.sqSum += value * value
pa.count++
avg := pa.sum / float32(pa.count)
stdDev := sqrt(pa.sqSum/float32(pa.count) - avg*avg)
predict := pa.estimate(fv)
error := value - predict
loss := abs(error) - pa.sensitivity*stdDev
if loss <= 0 {
return nil
}
// zero vector generates inf or nan.
if fv.squaredNorm() < 1e-12 {
return nil
}
C := pa.regWeight
coeff := sign(error) * min(C, loss) / fv.squaredNorm()
pa.update(fv, coeff)
return nil
}
// Estimate estimates a value from a model and a feature vector.
func (pa *PassiveAggressive) Estimate(v FeatureVector) (float32, error) {
fv, err := v.toInternal()
if err != nil {
return 0, err
}
pa.m.RLock()
defer pa.m.RUnlock()
return pa.estimate(fv), nil
}
// Clear clears a model.
func (pa *PassiveAggressive) Clear() {
pa.m.Lock()
defer pa.m.Unlock()
pa.model = make(model)
pa.sum = 0
pa.sqSum = 0
pa.count = 0
}
const (
paForwatVersion = 1
)
type paMsgpack struct {
_struct struct{} `codec:",toarray"`
Model model
Sum float32
SqSum float32
Count uint64
RegWeight float32
Sensitivity float32
}
// Save saves the current state of PassiveAggressive.
func (pa *PassiveAggressive) Save(w io.Writer) error {
pa.m.RLock()
defer pa.m.RUnlock()
if _, err := w.Write([]byte{paForwatVersion}); err != nil {
return err
}
enc := codec.NewEncoder(w, regressionMsgpackHandle)
err := enc.Encode(&paMsgpack{
Model: pa.model,
Sum: pa.sum,
SqSum: pa.sqSum,
Count: pa.count,
RegWeight: pa.regWeight,
Sensitivity: pa.sensitivity,
})
return err
}
// LoadPassiveAggressive loads PassiveAggressive from the saved data.
func LoadPassiveAggressive(r io.Reader) (*PassiveAggressive, error) {
formatVersion := make([]byte, 1)
if _, err := r.Read(formatVersion); err != nil {
return nil, err
}
switch formatVersion[0] {
case 1:
return loadPassiveAggressiveFormatV1(r)
default:
return nil, fmt.Errorf("unsupported format version of PassiveAggressive container: %v", formatVersion[0])
}
}
func loadPassiveAggressiveFormatV1(r io.Reader) (*PassiveAggressive, error) {
m := paMsgpack{}
dec := codec.NewDecoder(r, regressionMsgpackHandle)
if err := dec.Decode(&m); err != nil {
return nil, err
}
return &PassiveAggressive{
model: m.Model,
sum: m.Sum,
sqSum: m.SqSum,
count: m.Count,
regWeight: m.RegWeight,
sensitivity: m.Sensitivity,
}, nil
}
// RegWeight returns regularization weight.
func (pa *PassiveAggressive) RegWeight() float32 {
return pa.regWeight
}
// Sensitivity returns sensitivity.
func (pa *PassiveAggressive) Sensitivity() float32 {
return pa.sensitivity
}
func (pa *PassiveAggressive) estimate(v fVector) float32 {
var ret float32
for i := range v {
dim := v[i].dim
x := v[i].value
ret += x * pa.model[dim]
}
return ret
}
func (pa *PassiveAggressive) update(v fVector, coeff float32) {
for i := range v {
dim := v[i].dim
x := v[i].value
pa.model[dim] += coeff * x
}
}
type dim string
type model map[dim]float32
// FeatureVector is a type for feature vectors.
type FeatureVector data.Map
func (v FeatureVector) toInternal() (fVector, error) {
ret := make(fVector, 0, len(v))
err := nested.Flatten(data.Map(v), func(key string, value float32) {
ret = append(ret, fElement{dim: dim(key), value: value})
})
if err != nil {
return nil, err
}
return ret, nil
}
type fElement struct {
dim dim
value float32
}
type fVector []fElement
func (v fVector) squaredNorm() float32 {
var norm2 float32
for i := 0; i < len(v); i++ {
val := v[i].value
norm2 += val * val
}
return norm2
}
func abs(x float32) float32 {
return float32(math.Abs(float64(x)))
}
func min(x float32, y float32) float32 {
return float32(math.Min(float64(x), float64(y)))
}
func sqrt(x float32) float32 {
return float32(math.Sqrt(float64(x)))
}
func sign(x float32) float32 {
return float32(math.Copysign(1, float64(x)))
}