1
1
using NNlib: pad_constant, pad_repeat, pad_zeros, pad_reflect, pad_symmetric, pad_circular
2
2
3
3
@testset " padding constant" begin
4
- x = rand (2 , 2 , 2 )
5
-
4
+ x = rand (2 , 2 , 2 )
5
+
6
6
p = NNlib. gen_pad ((1 ,2 ,3 ,4 ,5 ,6 ), (1 ,2 ,3 ), 4 )
7
7
@test p == ((1 , 2 ), (3 , 4 ), (5 , 6 ), (0 , 0 ))
8
-
8
+
9
9
@test_throws ArgumentError NNlib. gen_pad ((1 ,2 ,3 ,4 ,5 ,), (1 ,2 ,3 ), 4 )
10
-
10
+
11
11
p = NNlib. gen_pad ((1 ,3 ), (1 ,3 ), 4 )
12
12
@test p == ((1 , 1 ), (0 , 0 ), (3 , 3 ), (0 , 0 ))
13
-
13
+
14
14
p = NNlib. gen_pad (1 , (1 ,2 ,3 ), 4 )
15
15
@test p == ((1 , 1 ), (1 , 1 ), (1 , 1 ), (0 , 0 ))
16
-
16
+
17
17
p = NNlib. gen_pad (3 , :, 2 )
18
18
@test p == ((3 , 3 ), (3 , 3 ))
19
19
20
20
p = NNlib. gen_pad ((1 ,0 ), 1 , 2 )
21
21
@test p == ((1 ,0 ), (0 ,0 ))
22
-
22
+
23
23
y = pad_constant (x, (3 , 2 , 4 ))
24
24
@test size (y) == (8 , 6 , 10 )
25
25
@test y[4 : 5 , 3 : 4 , 5 : 6 ] ≈ x
26
26
y[4 : 5 , 3 : 4 , 5 : 6 ] .= 0
27
27
@test all (y .== 0 )
28
-
28
+
29
29
@test pad_constant (x, (3 , 2 , 4 )) ≈ pad_zeros (x, (3 , 2 , 4 ))
30
- @test pad_zeros (x, 2 ) ≈ pad_zeros (x, (2 ,2 ,2 ))
31
-
30
+ @test pad_zeros (x, 2 ) ≈ pad_zeros (x, (2 ,2 ,2 ))
31
+
32
32
y = pad_constant (x, (3 , 2 , 4 , 5 ), 1.2 , dims = (1 ,3 ))
33
33
@test size (y) == (7 , 2 , 11 )
34
34
@test y[4 : 5 , 1 : 2 , 5 : 6 ] ≈ x
35
35
y[4 : 5 , 1 : 2 , 5 : 6 ] .= 1.2
36
36
@test all (y .== 1.2 )
37
-
37
+
38
38
@test pad_constant (x, (2 ,2 ,2 ,2 ), 1.2 , dims = (1 ,3 )) ≈
39
39
pad_constant (x, 2 , 1.2 , dims = (1 ,3 ))
40
-
40
+
41
41
@test pad_constant (x, 1 , dims = 1 : 2 ) ==
42
- pad_constant (x, 1 , dims = (1 ,2 ))
43
-
42
+ pad_constant (x, 1 , dims = (1 ,2 ))
43
+
44
44
@test size (pad_constant (x, 1 , dims = 1 )) == (4 ,2 ,2 )
45
-
45
+
46
46
@test all (pad_zeros (randn (2 ), (1 , 2 ))[[1 , 4 , 5 ]] .== 0 )
47
-
47
+
48
48
gradtest (x -> pad_constant (x, 2 ), rand (2 ,2 ,2 ))
49
49
gradtest (x -> pad_constant (x, (2 , 1 , 1 , 2 )), rand (2 ,2 ))
50
50
gradtest (x -> pad_constant (x, (2 , 1 ,)), rand (2 ))
51
51
end
52
52
53
53
@testset " padding repeat" begin
54
- x = rand (2 , 2 , 2 )
55
-
54
+ x = rand (2 , 2 , 2 )
55
+
56
56
# y = @inferred pad_repeat(x, (3, 2, 4, 5))
57
57
y = pad_repeat (x, (3 , 2 , 4 , 5 ))
58
58
@test size (y) == (7 , 11 , 2 )
59
59
@test y[4 : 5 , 5 : 6 , :] ≈ x
60
-
60
+
61
61
# y = @inferred pad_repeat(x, (3, 2, 4, 5), dims=(1,3))
62
62
y = pad_repeat (x, (3 , 2 , 4 , 5 ), dims= (1 ,3 ))
63
63
@test size (y) == (7 , 2 , 11 )
64
64
@test y[4 : 5 , :, 5 : 6 ] ≈ x
65
-
65
+
66
66
@test pad_repeat (reshape (1 : 9 , 3 , 3 ), (1 ,2 )) ==
67
67
[1 4 7
68
68
1 4 7
69
69
2 5 8
70
70
3 6 9
71
71
3 6 9
72
72
3 6 9 ]
73
-
73
+
74
74
@test pad_repeat (reshape (1 : 9 , 3 , 3 ), (2 ,2 ), dims= 2 ) ==
75
75
[1 1 1 4 7 7 7
76
76
2 2 2 5 8 8 8
77
77
3 3 3 6 9 9 9 ]
78
-
78
+
79
79
@test pad_repeat (x, (2 , 2 , 2 , 2 ), dims= (1 ,3 )) ≈
80
80
pad_repeat (x, 2 , dims= (1 ,3 ))
81
-
81
+
82
82
gradtest (x -> pad_repeat (x, (2 ,2 ,2 ,2 )), rand (2 ,2 ,2 ))
83
83
end
84
84
87
87
@test y == [7 4 1 4 7 4 1
88
88
8 5 2 5 8 5 2
89
89
9 6 3 6 9 6 3 ]
90
-
90
+
91
91
y = pad_reflect (reshape (1 : 9 , 3 , 3 ), (2 ,2 ,2 ,2 ))
92
92
@test y == [9 6 3 6 9 6 3
93
93
8 5 2 5 8 5 2
96
96
9 6 3 6 9 6 3
97
97
8 5 2 5 8 5 2
98
98
7 4 1 4 7 4 1 ]
99
-
100
- x = rand (4 , 4 , 4 )
99
+
100
+ x = rand (4 , 4 , 4 )
101
101
@test pad_reflect (x, (2 , 2 , 2 , 2 ), dims= (1 ,3 )) ≈
102
102
pad_reflect (x, 2 , dims= (1 ,3 ))
103
-
104
- # pad_reflect needs larger test input as padding must
103
+
104
+ # pad_reflect needs larger test input as padding must
105
105
# be strictly less than array size in that dimension
106
106
gradtest (x -> pad_reflect (x, (2 ,2 ,2 ,2 )), rand (3 ,3 ,3 ))
107
+
108
+ x = reshape (1 : 9 , 3 , 3 , 1 , 1 )
109
+ @test NNlib. pad_reflect (x, (1 , 0 , 1 , 0 ); dims= 1 : 2 ) == [
110
+ 5 2 5 8 ;
111
+ 4 1 4 7 ;
112
+ 5 2 5 8 ;
113
+ 6 3 6 9 ;;;;]
114
+ @test NNlib. pad_reflect (x, (0 , 1 , 0 , 1 ); dims= 1 : 2 ) == [
115
+ 1 4 7 4 ;
116
+ 2 5 8 5 ;
117
+ 3 6 9 6 ;
118
+ 2 5 8 5 ;;;;]
107
119
end
108
120
109
121
@testset " padding symmetric" begin
110
122
y = pad_symmetric (reshape (1 : 9 , 3 , 3 ), (2 ,2 ), dims= 2 )
111
123
@test y == [4 1 1 4 7 7 4
112
124
5 2 2 5 8 8 5
113
125
6 3 3 6 9 9 6 ]
114
-
126
+
115
127
y = pad_symmetric (reshape (1 : 9 , 3 , 3 ), (2 ,2 ,2 ,2 ))
116
128
@test y == [5 2 2 5 8 8 5
117
129
4 1 1 4 7 7 4
@@ -120,20 +132,32 @@ end
120
132
6 3 3 6 9 9 6
121
133
6 3 3 6 9 9 6
122
134
5 2 2 5 8 8 5 ]
123
-
124
- x = rand (4 , 4 , 4 )
135
+
136
+ x = rand (4 , 4 , 4 )
125
137
@test pad_symmetric (x, (2 , 2 , 2 , 2 ), dims= (1 ,3 )) ≈
126
138
pad_symmetric (x, 2 , dims= (1 ,3 ))
127
-
139
+
128
140
gradtest (x -> pad_symmetric (x, (2 ,2 ,2 ,2 )), rand (2 ,2 ,2 ))
141
+
142
+ x = reshape (1 : 9 , 3 , 3 , 1 , 1 )
143
+ @test NNlib. pad_symmetric (x, (1 , 0 , 1 , 0 ); dims= 1 : 2 ) == [
144
+ 1 1 4 7 ;
145
+ 1 1 4 7 ;
146
+ 2 2 5 8 ;
147
+ 3 3 6 9 ;;;;]
148
+ @test NNlib. pad_symmetric (x, (0 , 1 , 0 , 1 ); dims= 1 : 2 ) == [
149
+ 1 4 7 7 ;
150
+ 2 5 8 8 ;
151
+ 3 6 9 9 ;
152
+ 3 6 9 9 ;;;;]
129
153
end
130
154
131
155
@testset " padding circular" begin
132
156
y = pad_circular (reshape (1 : 9 , 3 , 3 ), (2 ,2 ), dims= 2 )
133
157
@test y == [4 7 1 4 7 1 4
134
158
5 8 2 5 8 2 5
135
159
6 9 3 6 9 3 6 ]
136
-
160
+
137
161
y = pad_circular (reshape (1 : 9 , 3 , 3 ), (2 ,2 ,2 ,2 ))
138
162
@test y == [5 8 2 5 8 2 5
139
163
6 9 3 6 9 3 6
@@ -142,10 +166,10 @@ end
142
166
6 9 3 6 9 3 6
143
167
4 7 1 4 7 1 4
144
168
5 8 2 5 8 2 5 ]
145
-
146
- x = rand (4 , 4 , 4 )
169
+
170
+ x = rand (4 , 4 , 4 )
147
171
@test pad_circular (x, (2 , 2 , 2 , 2 ), dims= (1 ,3 )) ≈
148
172
pad_circular (x, 2 , dims= (1 ,3 ))
149
-
173
+
150
174
gradtest (x -> pad_circular (x, (2 ,2 ,2 ,2 )), rand (2 ,2 ,2 ))
151
175
end
0 commit comments