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