-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcnn_functions_test.go
100 lines (89 loc) · 2.88 KB
/
cnn_functions_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
package convnet
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("CcnFunctions", func() {
Context("when testing maxpool", func() {
It("complains if filterSize is invalid", func() {
matrix := &Matrix{}
res, err := matrix.MaxPool(0, 2)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("filterSize is invalid"))
Expect(res).To(BeNil())
})
It("complains if stride is invalid", func() {
matrix := &Matrix{}
res, err := matrix.MaxPool(2, 0)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("stride is invalid"))
Expect(res).To(BeNil())
})
It("calculates correctly with filter 2x2 and stride 2", func() {
matrix := &Matrix{
content: [][]float64{
[]float64{2.0, 3.0, 4.0, 0.0},
[]float64{1.0, 5.0, 3.0, 2.0},
[]float64{0.0, 4.0, 2.0, 3.0},
[]float64{1.0, 0.0, 6.0, 1.0},
},
}
res, err := matrix.MaxPool(2, 2)
Expect(err).NotTo(HaveOccurred())
Expect(res).NotTo(BeNil())
Expect(res.content[0]).To(Equal([]float64{5.0, 4.0}))
Expect(res.content[1]).To(Equal([]float64{4.0, 6.0}))
})
It("calculates correctly with filter 3x3 and stride 1", func() {
matrix := &Matrix{
content: [][]float64{
[]float64{2.0, 3.0, 4.0, 0.0},
[]float64{1.0, 5.0, 3.0, 2.0},
[]float64{0.0, 4.0, 2.0, 3.0},
[]float64{1.0, 0.0, 6.0, 1.0},
},
}
res, err := matrix.MaxPool(3, 1)
Expect(err).NotTo(HaveOccurred())
Expect(res).NotTo(BeNil())
Expect(res.content[0]).To(Equal([]float64{5.0, 5.0, 4.0, 3.0}))
Expect(res.content[1]).To(Equal([]float64{6.0, 6.0, 6.0, 3.0}))
Expect(res.content[2]).To(Equal([]float64{6.0, 6.0, 6.0, 3.0}))
Expect(res.content[3]).To(Equal([]float64{6.0, 6.0, 6.0, 1.0}))
})
It("calculates correctly with filter 3x3 and stride 2", func() {
matrix := &Matrix{
content: [][]float64{
[]float64{2.0, 3.0, 4.0, 0.0},
[]float64{1.0, 5.0, 3.0, 2.0},
[]float64{0.0, 4.0, 2.0, 3.0},
[]float64{1.0, 0.0, 6.0, 1.0},
},
}
res, err := matrix.MaxPool(3, 2)
Expect(err).NotTo(HaveOccurred())
Expect(res).NotTo(BeNil())
Expect(res.content[0]).To(Equal([]float64{5.0, 4.0}))
Expect(res.content[1]).To(Equal([]float64{6.0, 6.0}))
})
})
Context("when testing ReLU", func() {
It("applies correctly the max(0,x)", func() {
matrix := &Matrix{
content: [][]float64{
[]float64{2.0, 3.0, 4.0, 0.0},
[]float64{1.0, -5.0, 3.0, 2.0},
[]float64{-0.5, 4.2, -2.0, 3.0},
[]float64{-1.0, 0.0, -6.0, 1.0},
},
}
res, err := matrix.ReLU()
Expect(err).NotTo(HaveOccurred())
Expect(res).NotTo(BeNil())
Expect(res.content[0]).To(Equal([]float64{2.0, 3.0, 4.0, 0.0}))
Expect(res.content[1]).To(Equal([]float64{1.0, 0.0, 3.0, 2.0}))
Expect(res.content[2]).To(Equal([]float64{0.0, 4.2, 0.0, 3.0}))
Expect(res.content[3]).To(Equal([]float64{0.0, 0.0, 0.0, 1.0}))
})
})
})