forked from rai-project/dlframework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
features.go
173 lines (138 loc) · 3.84 KB
/
features.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
package dlframework
import (
"math"
"sort"
"github.com/k0kubun/pp/v3"
"github.com/pkg/errors"
)
//easyjson:json
type Features []*Feature
type PredictionHandle int64
// Len is the number of elements in the collection.
func (p Features) Len() int {
return len(p)
}
// Less reports whether the element with
// index i should sort before the element with index j.
func (p Features) Less(i, j int) bool {
pi := p[i].Probability
pj := p[j].Probability
return !(pi < pj || math.IsNaN(float64(pi)) && !math.IsNaN(float64(pj)))
}
// Swap swaps the elements with indexes i and j.
func (p Features) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func (p Features) Sort() {
sort.Sort(p)
}
func (p Features) Take(n int) Features {
if p.Len() <= n {
return p
}
return Features(p[:n])
}
func (p Features) ProbabilitiesFloat32() []float32 {
pProbs := make([]float32, p.Len())
for ii := 0; ii < p.Len(); ii++ {
pProbs[ii] = p[ii].Probability
}
return pProbs
}
func (p Features) ProbabilitiesApplySoftmaxFloat32() Features {
newProbs := p.ProbabilitiesSoftmaxFloat32()
for ii, np := range newProbs {
p[ii].Probability = np
}
return p
}
func (p Features) ProbabilitiesSoftmaxFloat32() []float32 {
pProbs := make([]float32, p.Len())
accum := float32(0.0)
for ii := 0; ii < p.Len(); ii++ {
pProbs[ii] = float32(math.Exp(float64(p[ii].Probability)))
accum += pProbs[ii]
if float64(accum) == math.Inf(+1) {
pp.Println(ii, p[ii].Probability)
break
}
}
for ii, p := range pProbs {
pProbs[ii] = p / accum
}
return pProbs
}
func (p Features) ProbabilitiesFloat64() []float64 {
pProbs := make([]float64, p.Len())
for ii := 0; ii < p.Len(); ii++ {
pProbs[ii] = float64(p[ii].Probability)
}
return pProbs
}
func (p Features) ProbabilitiesApplySoftmaxFloat64() Features {
newProbs := p.ProbabilitiesSoftmaxFloat64()
for ii, np := range newProbs {
p[ii].Probability = float32(np)
}
return p
}
func (p Features) ProbabilitiesSoftmaxFloat64() []float64 {
pProbs := make([]float64, p.Len())
accum := 0.0
for ii := 0; ii < p.Len(); ii++ {
pProbs[ii] = math.Exp(float64(p[ii].Probability))
accum += pProbs[ii]
}
for ii, p := range pProbs {
pProbs[ii] = p / accum
}
return pProbs
}
func (p Features) KullbackLeiblerDivergence(q Features) (float64, error) {
if p.Len() != q.Len() {
return 0, errors.Errorf("length mismatch %d != %d", p.Len(), q.Len())
}
pProbs := p.ProbabilitiesFloat64()
qProbs := q.ProbabilitiesFloat64()
return KullbackLeibler(pProbs, qProbs), nil
}
func (p Features) Correlation(q Features) (float64, error) {
if p.Len() != q.Len() {
return 0, errors.Errorf("length mismatch %d != %d", p.Len(), q.Len())
}
pProbs := p.ProbabilitiesFloat64()
qProbs := q.ProbabilitiesFloat64()
return Correlation(pProbs, qProbs, nil), nil
}
func (p Features) Covariance(q Features) (float64, error) {
if p.Len() != q.Len() {
return 0, errors.Errorf("length mismatch %d != %d", p.Len(), q.Len())
}
pProbs := p.ProbabilitiesFloat64()
qProbs := q.ProbabilitiesFloat64()
return Covariance(pProbs, qProbs, nil), nil
}
func (p Features) JensenShannon(q Features) (float64, error) {
if p.Len() != q.Len() {
return 0, errors.Errorf("length mismatch %d != %d", p.Len(), q.Len())
}
pProbs := p.ProbabilitiesFloat64()
qProbs := q.ProbabilitiesFloat64()
return JensenShannon(pProbs, qProbs), nil
}
func (p Features) Bhattacharyya(q Features) (float64, error) {
if p.Len() != q.Len() {
return 0, errors.Errorf("length mismatch %d != %d", p.Len(), q.Len())
}
pProbs := p.ProbabilitiesFloat64()
qProbs := q.ProbabilitiesFloat64()
return Bhattacharyya(pProbs, qProbs), nil
}
func (p Features) Hellinger(q Features) (float64, error) {
if p.Len() != q.Len() {
return 0, errors.Errorf("length mismatch %d != %d", p.Len(), q.Len())
}
pProbs := p.ProbabilitiesFloat64()
qProbs := q.ProbabilitiesFloat64()
return Hellinger(pProbs, qProbs), nil
}