-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool_layer.go
61 lines (47 loc) · 1.56 KB
/
pool_layer.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
package robonet
import (
"errors"
"log"
)
//PoolLayer will perform a downsampling operation along the spatial dimensions (width, height), resulting in volume such as [16x16x12].
type PoolLayer struct {
LayerFields
SizeR int
SizeC int
StrideR int
StrideC int
}
//Calculate for Pooling layers applies the pooling operation after the parameters have been set.
func (lay *PoolLayer) Calculate() {
if lay.SizeR == 0 || lay.SizeC == 0 || lay.StrideR == 0 || lay.StrideC == 0 {
log.Fatal(errors.New("PoolLayer: Parameters not set"))
}
if ((lay.input.Rows()%(lay.StrideR))%lay.SizeR != 0) || ((lay.input.Collumns()%(lay.StrideR))%lay.SizeC != 0) {
log.Fatal(errors.New("PoolLayer: Input Size not divisible by factor"))
}
lay.output = New((lay.input.Rows()-lay.SizeR)/lay.StrideR+1, (lay.input.Collumns()-lay.SizeC)/lay.StrideC+1, lay.input.Depth())
for r := 0; r < lay.output.Rows(); r++ {
for c := 0; c < lay.output.Collumns(); c++ {
res := maxPool(lay.input.SubVolume(r*lay.StrideR, c*lay.StrideC, lay.SizeR, lay.SizeC))
for d := 0; d < lay.input.Depth(); d++ {
lay.output.SetAt(r, c, d, res[d])
}
}
}
}
//maxPool calculates the maximums of a volume's depths and returns them as slice.
//The position in the slice matches the depth of that value in the input volume
func maxPool(vol Volume) (res []float64) {
for d := 0; d < vol.Depth(); d++ {
max := 0.0
for r := 0; r < vol.Rows(); r++ {
for c := 0; c < vol.Collumns(); c++ {
if tmp := vol.GetAt(r, c, d); tmp > max {
max = tmp
}
}
}
res = append(res, max)
}
return res
}