-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
58 lines (50 loc) · 1.09 KB
/
model.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
package training
import "math/rand"
// Example is an input-target pair
type Example struct {
Input []float32
Response []float32
}
// Examples is a set of input-output pairs
type Examples []Example
// Shuffle shuffles slice in-place
func (e Examples) Shuffle() {
for i := range e {
j := rand.Intn(i + 1)
e[i], e[j] = e[j], e[i]
}
}
// Split assigns each element to two new slices
// according to probability p
func (e Examples) Split(p float32) (first, second Examples) {
for i := 0; i < len(e); i++ {
if p > rand.Float32() {
first = append(first, e[i])
} else {
second = append(second, e[i])
}
}
return
}
// SplitSize splits slice into parts of size size
func (e Examples) SplitSize(size int) []Examples {
res := make([]Examples, 0)
for i := 0; i < len(e); i += size {
res = append(res, e[i:min(i+size, len(e))])
}
return res
}
// SplitN splits slice into n parts
func (e Examples) SplitN(n int) []Examples {
res := make([]Examples, n)
for i, el := range e {
res[i%n] = append(res[i%n], el)
}
return res
}
func min(a, b int) int {
if a <= b {
return a
}
return b
}