-
Notifications
You must be signed in to change notification settings - Fork 1
/
sgd_test.go
65 lines (62 loc) · 1.79 KB
/
sgd_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
package main
import (
"math/rand"
"sort"
"testing"
)
func TestSGD_ChooseManyEven(t *testing.T) {
batchsize = 128
samplesize := 32000
source := rand.NewSource(1337)
pRNG := rand.New(source)
sgd := NewSGD(samplesize, pRNG)
batches := sgd.ChooseMany(samplesize / batchsize)
if len(batches) != samplesize/batchsize {
t.Errorf("Expected %v Got %v For batchsize = %v and samplesize = %v",
samplesize/batchsize, len(batches), batchsize, samplesize)
}
var samples sort.IntSlice
for _, batch := range batches {
if len(batch) < batchsize-1 || len(batch) > batchsize+1 {
t.Errorf("Expected %v or %v Got %v For numbatches = %v and samplesize = %v",
batchsize, batchsize-1, len(batch), samplesize/batchsize, samplesize)
}
for _, sample := range batch {
samples = append(samples, sample)
}
}
samples.Sort()
for idx := range samples {
if idx != samples[idx] {
t.Error("Missing sample %v in batches", idx)
}
}
}
func TestSGD_ChooseManyOdd(t *testing.T) {
batchsize = 10
samplesize := 105
source := rand.NewSource(1337)
pRNG := rand.New(source)
sgd := NewSGD(samplesize, pRNG)
batches := sgd.ChooseMany(samplesize / batchsize)
if len(batches) != samplesize/batchsize {
t.Errorf("Expected %v Got %v For batchsize = %v and samplesize = %v",
samplesize/batchsize, len(batches), batchsize, samplesize)
}
var samples sort.IntSlice
for _, batch := range batches {
if len(batch) < batchsize-1 || len(batch) > batchsize+1 {
t.Errorf("Expected %v or %v Got %v For numbatches = %v and samplesize = %v",
batchsize, batchsize-1, len(batch), samplesize/batchsize, samplesize)
}
for _, sample := range batch {
samples = append(samples, sample)
}
}
samples.Sort()
for idx := range samples {
if idx != samples[idx] {
t.Error("Missing sample %v in batches", idx)
}
}
}