@@ -6,6 +6,10 @@ _paddims(x::Tuple, y::Tuple) = (x..., y[(end - (length(y) - length(x) - 1)):end]
6
6
expand (N, i:: Tuple ) = i
7
7
expand (N, i:: Integer ) = ntuple (_ -> i, N)
8
8
9
+ conv_reshape_bias (c) = c. bias isa AbstractVector ?
10
+ reshape (c. bias, map (_-> 1 , c. stride)... , :, 1 ) :
11
+ c. bias
12
+
9
13
"""
10
14
SamePad()
11
15
96
100
97
101
"""
98
102
Conv(weight::AbstractArray, bias, [activation; stride, pad, dilation])
99
-
103
+
100
104
Constructs a convolutional layer with the given weight and bias.
101
105
Accepts the same keywords (and has the same defaults) as the `Conv((4,4), 3=>7, relu)`
102
106
method.
@@ -117,7 +121,7 @@ julia> params(c1) |> length
117
121
2
118
122
```
119
123
"""
120
- function Conv (w:: AbstractArray{T,N} , b:: Union{Bool, Zeros, AbstractVector{T}} , σ = identity;
124
+ function Conv (w:: AbstractArray{T,N} , b:: Union{Bool,AbstractVector{T}} , σ = identity;
121
125
stride = 1 , pad = 0 , dilation = 1 ) where {T,N}
122
126
stride = expand (Val (N- 2 ), stride)
123
127
dilation = expand (Val (N- 2 ), dilation)
@@ -152,9 +156,8 @@ convfilter(filter::NTuple{N,Integer}, ch::Pair{<:Integer,<:Integer};
152
156
function (c:: Conv )(x:: AbstractArray )
153
157
# TODO : breaks gpu broadcast :(
154
158
# ndims(x) == ndims(c.weight)-1 && return squeezebatch(c(reshape(x, size(x)..., 1)))
155
- σ, b = c. σ, reshape (c. bias, ntuple (_-> 1 , length (c. stride))... , :, 1 )
156
159
cdims = DenseConvDims (x, c. weight; stride= c. stride, padding= c. pad, dilation= c. dilation)
157
- σ .(conv (x, c. weight, cdims) .+ b )
160
+ (c . σ) . (conv (x, c. weight, cdims) .+ conv_reshape_bias (c) )
158
161
end
159
162
160
163
function Base. show (io:: IO , l:: Conv )
@@ -207,16 +210,16 @@ end
207
210
208
211
"""
209
212
ConvTranspose(weight::AbstractArray, bias, [activation; stride, pad, dilation])
210
-
213
+
211
214
Constructs a layer with the given weight and bias arrays.
212
215
Accepts the same keywords as the `ConvTranspose((4,4), 3=>7, relu)` method.
213
216
"""
214
- function ConvTranspose (w:: AbstractArray{T,N} , b:: Union{Bool, Zeros, AbstractVector{T}} , σ = identity;
217
+ function ConvTranspose (w:: AbstractArray{T,N} , b:: Union{Bool, AbstractVector{T}} , σ = identity;
215
218
stride = 1 , pad = 0 , dilation = 1 ) where {T,N}
216
219
stride = expand (Val (N- 2 ), stride)
217
220
dilation = expand (Val (N- 2 ), dilation)
218
221
pad = calc_padding (ConvTranspose, pad, size (w)[1 : N- 2 ], dilation, stride)
219
- bias = create_bias (b, zeros, size (w, N- 1 ))
222
+ bias = create_bias (b, zeros, size (w, N- 1 ))
220
223
return ConvTranspose (σ, w, bias, stride, pad, dilation)
221
224
end
222
225
248
251
249
252
function (c:: ConvTranspose )(x:: AbstractArray )
250
253
# ndims(x) == ndims(c.weight)-1 && return squeezebatch(c(reshape(x, size(x)..., 1)))
251
- σ, b = c. σ, reshape (c. bias, map (_-> 1 , c. stride)... , :, 1 )
252
254
cdims = conv_transpose_dims (c, x)
253
- σ .(∇conv_data (x, c. weight, cdims) .+ b )
255
+ (c . σ) . (∇conv_data (x, c. weight, cdims) .+ conv_reshape_bias (c) )
254
256
end
255
257
256
258
function Base. show (io:: IO , l:: ConvTranspose )
@@ -304,11 +306,11 @@ end
304
306
305
307
"""
306
308
DepthwiseConv(weight::AbstractArray, bias, [activation; stride, pad, dilation])
307
-
309
+
308
310
Constructs a layer with the given weight and bias arrays.
309
311
Accepts the same keywords as the `DepthwiseConv((4,4), 3=>6, relu)` method.
310
312
"""
311
- function DepthwiseConv (w:: AbstractArray{T,N} , b:: Union{Bool, Zeros, AbstractVector{T}} , σ = identity;
313
+ function DepthwiseConv (w:: AbstractArray{T,N} , b:: Union{Bool,AbstractVector{T}} , σ = identity;
312
314
stride = 1 , pad = 0 , dilation = 1 ) where {T,N}
313
315
stride = expand (Val (N- 2 ), stride)
314
316
dilation = expand (Val (N- 2 ), dilation)
@@ -341,9 +343,8 @@ depthwiseconvfilter(filter::NTuple{N,Integer}, ch::Pair{<:Integer,<:Integer};
341
343
init = glorot_uniform) where N = init (filter... , div (ch[2 ], ch[1 ]), ch[1 ])
342
344
343
345
function (c:: DepthwiseConv )(x)
344
- σ, b = c. σ, reshape (c. bias, map (_-> 1 , c. stride)... , :, 1 )
345
346
cdims = DepthwiseConvDims (x, c. weight; stride= c. stride, padding= c. pad, dilation= c. dilation)
346
- σ .(depthwiseconv (x, c. weight, cdims) .+ b )
347
+ (c . σ) . (depthwiseconv (x, c. weight, cdims) .+ conv_reshape_bias (c) )
347
348
end
348
349
349
350
function Base. show (io:: IO , l:: DepthwiseConv )
@@ -392,11 +393,11 @@ end
392
393
393
394
"""
394
395
CrossCor(weight::AbstractArray, bias, [activation; stride, pad, dilation])
395
-
396
+
396
397
Constructs a layer with the given weight and bias arrays.
397
398
Accepts the same keywords as the `CrossCor((4,4), 3=>7, relu)` method.
398
399
"""
399
- function CrossCor (w:: AbstractArray{T,N} , b:: Union{Bool, Zeros, AbstractVector{T}} , σ = identity;
400
+ function CrossCor (w:: AbstractArray{T,N} , b:: Union{Bool,AbstractVector{T}} = true , σ = identity;
400
401
stride = 1 , pad = 0 , dilation = 1 ) where {T,N}
401
402
stride = expand (Val (N- 2 ), stride)
402
403
dilation = expand (Val (N- 2 ), dilation)
422
423
function (c:: CrossCor )(x:: AbstractArray )
423
424
# TODO : breaks gpu broadcast :(
424
425
# ndims(x) == ndims(c.weight)-1 && return squeezebatch(c(reshape(x, size(x)..., 1)))
425
- σ, b = c. σ, reshape (c. bias, map (_-> 1 , c. stride)... , :, 1 )
426
426
cdims = DenseConvDims (x, c. weight; stride= c. stride, padding= c. pad, dilation= c. dilation)
427
- σ .(crosscor (x, c. weight, cdims) .+ b )
427
+ (c . σ) . (crosscor (x, c. weight, cdims) .+ conv_reshape_bias (c) )
428
428
end
429
429
430
430
function Base. show (io:: IO , l:: CrossCor )
0 commit comments