-
Notifications
You must be signed in to change notification settings - Fork 1
/
logloss_test.go
101 lines (81 loc) · 2.36 KB
/
logloss_test.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
package mlmetrics_test
import (
"fmt"
"math/rand"
"testing"
. "github.com/bsm/ginkgo"
. "github.com/bsm/gomega"
"github.com/bsm/mlmetrics"
)
var _ = Describe("LogLoss", func() {
var subject *mlmetrics.LogLoss
BeforeEach(func() {
subject = mlmetrics.NewLogLoss()
})
It("should calculate score", func() {
subject.Observe(0.5)
subject.Observe(0.1)
subject.Observe(0.01)
subject.Observe(0.1)
subject.Observe(0.25)
subject.Observe(0.999)
Expect(subject.Score()).To(BeNumerically("~", 1.882, 0.001))
})
It("should calculate score (variable)", func() {
subject.Observe(0.8)
subject.Observe(0.9)
subject.Observe(0.1)
subject.Observe(0.6)
Expect(subject.Score()).To(BeNumerically("~", 0.785, 0.001))
subject.Observe(0.0)
Expect(subject.Score()).To(BeNumerically("~", 7.536, 0.001))
subject.Observe(0.99)
Expect(subject.Score()).To(BeNumerically("~", 6.282, 0.001))
})
It("should calculate on empty", func() {
Expect(subject.Score()).To(BeNumerically("~", 34.539, 0.001))
})
It("should calculate perfect match", func() {
subject.ObserveWeight(1.0, 10)
Expect(subject.Score()).To(BeNumerically("~", 0.0, 0.001))
subject.ObserveWeight(1.0, 10)
Expect(subject.Score()).To(BeNumerically("~", 0.0, 0.001))
})
It("should calculate perfect failure", func() {
subject.ObserveWeight(0.0, 10)
Expect(subject.Score()).To(BeNumerically("~", 34.539, 0.001))
subject.ObserveWeight(0.0, 10)
Expect(subject.Score()).To(BeNumerically("~", 34.539, 0.001))
})
})
func ExampleLogLoss() {
// assuming the following three predictions
predictions := []map[string]float64{
{"dog": 0.2, "cat": 0.5, "fish": 0.3},
{"dog": 0.8, "cat": 0.1, "fish": 0.1},
{"dog": 0.6, "cat": 0.1, "fish": 0.4},
}
// create metric, feed with probabilities of actual observations
metric := mlmetrics.NewLogLoss()
for i, actual := range []string{"cat", "dog", "fish"} {
probability := predictions[i][actual]
metric.Observe(probability)
}
// print score
fmt.Printf("log-loss : %.3f\n", metric.Score())
// Output:
// log-loss : 0.611
}
func BenchmarkLogLoss(b *testing.B) {
rn := rand.New(rand.NewSource(10))
ll := mlmetrics.NewLogLoss()
for i := 0; i < 1000; i++ {
ll.Observe(0.5 + rn.Float64()/2)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if v := ll.Score(); int(v*1000) != 301 {
b.Fatalf("expected result to be 0.301 but was %v", v)
}
}
}