-
Notifications
You must be signed in to change notification settings - Fork 3
/
softmax.go
64 lines (56 loc) · 1.16 KB
/
softmax.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
package math
import (
"gopkg.in/sensorbee/sensorbee.v0/data"
"math"
"sort"
)
// Softmax calculates softmax.
func Softmax(v data.Map) (data.Map, error) {
ret := make(data.Map)
if len(v) == 0 {
return ret, nil
}
// copy values to an array to sort in logSumExp().
values, err := mapToValues(v)
if err != nil {
return nil, err
}
lse := logSumExp(values)
for k, x := range v {
val, err := data.AsFloat(x)
if err != nil {
return nil, err
}
ret[k] = data.Float(math.Exp(val - lse))
}
return ret, nil
}
func mapToValues(v data.Map) ([]float64, error) {
values := make([]float64, 0, len(v))
for _, x := range v {
val, err := data.AsFloat(x)
if err != nil {
return nil, err
}
values = append(values, val)
}
return values, nil
}
// logSumExp calculates logsumexp. When calling this function with an empty
// slice, the behavior is undefined.
func logSumExp(v []float64) float64 {
if len(v) == 0 {
return math.NaN()
}
// TODO: consider better calculation order to reduce floating-point error.
sort.Float64s(v)
x := v[0]
for i := 1; i < len(v); i++ {
y := v[i]
if x < y {
x, y = y, x
}
x += math.Log(1 + math.Exp(y-x))
}
return x
}