Skip to content

Commit d26ebdb

Browse files
authored
Merge pull request #1978 from Saransh-cpp/some-docstrings
Update docstrings of `basic.jl` and `conv.jl`
2 parents 97f981b + 297b822 commit d26ebdb

File tree

4 files changed

+103
-34
lines changed

4 files changed

+103
-34
lines changed

docs/src/models/layers.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ These layers are used to build convolutional neural networks (CNNs).
1313

1414
```@docs
1515
Conv
16+
Conv(weight::AbstractArray)
1617
AdaptiveMaxPool
1718
MaxPool
1819
GlobalMaxPool
@@ -21,10 +22,11 @@ MeanPool
2122
GlobalMeanPool
2223
DepthwiseConv
2324
ConvTranspose
25+
ConvTranspose(weight::AbstractArray)
2426
CrossCor
27+
CrossCor(weight::AbstractArray)
2528
SamePad
2629
Flux.flatten
27-
Flux.convfilter
2830
```
2931

3032
## Upsampling Layers

src/layers/basic.jl

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,17 @@ end
4848

4949
@functor Chain
5050

51-
(c::Chain)(x) = applychain(c.layers, x)
51+
(c::Chain)(x) = _applychain(c.layers, x)
5252

53-
@generated function applychain(layers::Tuple{Vararg{<:Any,N}}, x) where {N}
53+
@generated function _applychain(layers::Tuple{Vararg{<:Any,N}}, x) where {N}
5454
symbols = vcat(:x, [gensym() for _ in 1:N])
5555
calls = [:($(symbols[i+1]) = layers[$i]($(symbols[i]))) for i in 1:N]
5656
Expr(:block, calls...)
5757
end
5858

59-
applychain(layers::NamedTuple, x) = applychain(Tuple(layers), x)
59+
_applychain(layers::NamedTuple, x) = _applychain(Tuple(layers), x)
6060

61-
function applychain(layers::AbstractVector, x) # type-unstable path, helps compile times
61+
function _applychain(layers::AbstractVector, x) # type-unstable path, helps compile times
6262
for f in layers
6363
x = f(x)
6464
end
@@ -73,6 +73,7 @@ function Base.show(io::IO, c::Chain)
7373
_show_layers(io, c.layers)
7474
print(io, ")")
7575
end
76+
7677
_show_layers(io, layers::Tuple) = join(io, layers, ", ")
7778
_show_layers(io, layers::NamedTuple) = join(io, ["$k = $v" for (k, v) in pairs(layers)], ", ")
7879
_show_layers(io, layers::AbstractVector) = (print(io, "["); join(io, layers, ", "); print(io, "]"))
@@ -85,15 +86,27 @@ _show_layers(io, layers::AbstractVector) = (print(io, "["); join(io, layers, ",
8586
"""
8687
activations(c::Chain, input)
8788
88-
Calculate the forward results of each layers in Chain `c` with `input` as model input.
89+
Like calling a `Chain`, but saves the result of each layer as an output.
90+
91+
# Examples
92+
93+
```jldoctest
94+
julia> using Flux: activations
95+
96+
julia> c = Chain(x -> x + 1, x -> x * 2, x -> x ^ 3);
97+
98+
julia> activations(c, 1)
99+
(2, 4, 64)
100+
```
89101
"""
90-
activations(c::Chain, input) = extraChain(Tuple(c.layers), input)
102+
activations(c::Chain, input) = _extraChain(Tuple(c.layers), input)
91103

92-
function extraChain(fs::Tuple, x)
104+
# Calculates the forward results of each layer provided in a `Tuple` with `x` as model input.
105+
function _extraChain(fs::Tuple, x)
93106
res = first(fs)(x)
94-
return (res, extraChain(Base.tail(fs), res)...)
107+
return (res, _extraChain(Base.tail(fs), res)...)
95108
end
96-
extraChain(::Tuple{}, x) = ()
109+
_extraChain(::Tuple{}, x) = ()
97110

98111

99112
"""
@@ -254,7 +267,7 @@ See Goodfellow, Warde-Farley, Mirza, Courville & Bengio "Maxout Networks"
254267
See also [`Parallel`](@ref) to reduce with other operators.
255268
256269
# Examples
257-
```
270+
```jldoctest
258271
julia> m = Maxout(x -> abs2.(x), x -> x .* 3);
259272
260273
julia> m([-2 -1 0 1 2])

src/layers/conv.jl

Lines changed: 76 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,33 @@ the padding to be chosen such that the input and output sizes agree
1919
When `stride≠1`, the output size equals `ceil(input_size/stride)`.
2020
2121
See also [`Conv`](@ref), [`MaxPool`](@ref).
22+
23+
# Examples
24+
```jldoctest
25+
julia> xs = rand(Float32, 100, 100, 3, 50); # a batch of images
26+
27+
julia> layer = Conv((2,2), 3 => 7, pad=SamePad())
28+
Conv((2, 2), 3 => 7, pad=(1, 0, 1, 0)) # 91 parameters
29+
30+
julia> layer(xs) |> size # notice how the dimensions stay the same with this padding
31+
(100, 100, 7, 50)
32+
33+
julia> layer2 = Conv((2,2), 3 => 7)
34+
Conv((2, 2), 3 => 7) # 91 parameters
35+
36+
julia> layer2(xs) |> size # the output dimension changes as the padding was not "same"
37+
(99, 99, 7, 50)
38+
39+
julia> layer3 = Conv((5, 5), 3 => 7, stride=2, pad=SamePad())
40+
Conv((5, 5), 3 => 7, pad=2, stride=2) # 532 parameters
41+
42+
julia> layer3(xs) |> size # output size = `ceil(input_size/stride)` = 50
43+
(50, 50, 7, 50)
44+
```
2245
"""
2346
struct SamePad end
2447

25-
calc_padding(lt, pad, k::NTuple{N,T}, dilation, stride) where {T,N}= expand(Val(2*N), pad)
48+
calc_padding(lt, pad, k::NTuple{N,T}, dilation, stride) where {T,N} = expand(Val(2*N), pad)
2649
function calc_padding(lt, ::SamePad, k::NTuple{N,T}, dilation, stride) where {N,T}
2750
#Ref: "A guide to convolution arithmetic for deep learning" https://arxiv.org/abs/1603.07285
2851

@@ -108,22 +131,21 @@ end
108131
Conv(weight::AbstractArray, [bias, activation; stride, pad, dilation])
109132
110133
Constructs a convolutional layer with the given weight and bias.
111-
Accepts the same keywords (and has the same defaults) as the `Conv((4,4), 3 => 7, relu)`
112-
method.
134+
Accepts the same keywords and has the same defaults as
135+
[`Conv(k::NTuple{N,Integer}, ch::Pair{<:Integer,<:Integer}, σ; ...)`](@ref Conv).
113136
114-
# Examples
115137
```jldoctest
116138
julia> weight = rand(3, 4, 5);
117139
118140
julia> bias = zeros(5);
119141
120-
julia> c1 = Conv(weight, bias, sigmoid) # expects 1 spatial dimension
142+
julia> layer = Conv(weight, bias, sigmoid) # expects 1 spatial dimension
121143
Conv((3,), 4 => 5, σ) # 65 parameters
122144
123-
julia> c1(randn(100, 4, 64)) |> size
145+
julia> layer(randn(100, 4, 64)) |> size
124146
(98, 5, 64)
125147
126-
julia> Flux.params(c1) |> length
148+
julia> Flux.params(layer) |> length
127149
2
128150
```
129151
"""
@@ -154,6 +176,8 @@ channels from `in` to `out`.
154176
155177
Accepts the keyword `init` (default: `glorot_uniform`) to control the sampling
156178
distribution.
179+
180+
This is internally used by the [`Conv`](@ref) layer.
157181
"""
158182
function convfilter(filter::NTuple{N,Integer}, ch::Pair{<:Integer,<:Integer};
159183
init = glorot_uniform, groups = 1) where N
@@ -176,7 +200,7 @@ function (c::Conv)(x::AbstractArray)
176200
σ.(conv(x, c.weight, cdims) .+ conv_reshape_bias(c))
177201
end
178202

179-
_channels_in(l ::Conv) = size(l.weight, ndims(l.weight)-1) * l.groups
203+
_channels_in(l::Conv) = size(l.weight, ndims(l.weight)-1) * l.groups
180204
_channels_out(l::Conv) = size(l.weight, ndims(l.weight))
181205

182206
function Base.show(io::IO, l::Conv)
@@ -215,10 +239,10 @@ See also [`Conv`](@ref) for more detailed description of keywords.
215239
```jldoctest
216240
julia> xs = rand(Float32, 100, 100, 3, 50); # a batch of 50 RGB images
217241
218-
julia> lay = ConvTranspose((5,5), 3 => 7, relu)
242+
julia> layer = ConvTranspose((5,5), 3 => 7, relu)
219243
ConvTranspose((5, 5), 3 => 7, relu) # 532 parameters
220244
221-
julia> lay(xs) |> size
245+
julia> layer(xs) |> size
222246
(104, 104, 7, 50)
223247
224248
julia> ConvTranspose((5,5), 3 => 7, stride=2)(xs) |> size
@@ -244,8 +268,25 @@ _channels_out(l::ConvTranspose) = size(l.weight)[end-1]*l.groups
244268
"""
245269
ConvTranspose(weight::AbstractArray, [bias, activation; stride, pad, dilation, groups])
246270
247-
Constructs a layer with the given weight and bias arrays.
248-
Accepts the same keywords as the `ConvTranspose((4,4), 3 => 7, relu)` method.
271+
Constructs a ConvTranspose layer with the given weight and bias.
272+
Accepts the same keywords and has the same defaults as
273+
[`ConvTranspose(k::NTuple{N,Integer}, ch::Pair{<:Integer,<:Integer}, σ; ...)`](@ref ConvTranspose).
274+
275+
# Examples
276+
```jldoctest
277+
julia> weight = rand(3, 4, 5);
278+
279+
julia> bias = zeros(4);
280+
281+
julia> layer = ConvTranspose(weight, bias, sigmoid)
282+
ConvTranspose((3,), 5 => 4, σ) # 64 parameters
283+
284+
julia> layer(randn(100, 5, 64)) |> size # transposed convolution will increase the dimension size (upsampling)
285+
(102, 4, 64)
286+
287+
julia> Flux.params(layer) |> length
288+
2
289+
```
249290
"""
250291
function ConvTranspose(w::AbstractArray{T,N}, bias = true, σ = identity;
251292
stride = 1, pad = 0, dilation = 1, groups=1) where {T,N}
@@ -299,7 +340,6 @@ function Base.show(io::IO, l::ConvTranspose)
299340
print(io, ")")
300341
end
301342

302-
303343
function calc_padding(::Type{ConvTranspose}, pad::SamePad, k::NTuple{N,T}, dilation, stride) where {N,T}
304344
calc_padding(Conv, pad, k .- stride .+ 1, dilation, stride)
305345
end
@@ -318,10 +358,10 @@ See [`Conv`](@ref) for a description of the arguments.
318358
```jldoctest
319359
julia> xs = rand(Float32, 100, 100, 3, 50); # a batch of 50 RGB images
320360
321-
julia> lay = DepthwiseConv((5,5), 3 => 6, relu; bias=false)
361+
julia> layer = DepthwiseConv((5,5), 3 => 6, relu; bias=false)
322362
Conv((5, 5), 3 => 6, relu, groups=3, bias=false) # 150 parameters
323363
324-
julia> lay(xs) |> size
364+
julia> layer(xs) |> size
325365
(96, 96, 6, 50)
326366
327367
julia> DepthwiseConv((5, 5), 3 => 9, stride=2, pad=2)(xs) |> size
@@ -343,7 +383,7 @@ end
343383
"""
344384
CrossCor(filter, in => out, σ=identity; stride=1, pad=0, dilation=1, [bias, init])
345385
346-
Standard cross convolutional layer. `filter` is a tuple of integers
386+
Standard cross correlation layer. `filter` is a tuple of integers
347387
specifying the size of the convolutional kernel;
348388
`in` and `out` specify the number of input and output channels.
349389
@@ -357,10 +397,10 @@ See also [`Conv`](@ref) for more detailed description of keywords.
357397
```jldoctest
358398
julia> xs = rand(Float32, 100, 100, 3, 50); # a batch of 50 RGB images
359399
360-
julia> lay = CrossCor((5,5), 3 => 6, relu; bias=false)
400+
julia> layer = CrossCor((5,5), 3 => 6, relu; bias=false)
361401
CrossCor((5, 5), 3 => 6, relu, bias=false) # 450 parameters
362402
363-
julia> lay(xs) |> size
403+
julia> layer(xs) |> size
364404
(96, 96, 6, 50)
365405
366406
julia> CrossCor((5,5), 3 => 7, stride=3, pad=(2,0))(xs) |> size
@@ -379,8 +419,22 @@ end
379419
"""
380420
CrossCor(weight::AbstractArray, [bias, activation; stride, pad, dilation])
381421
382-
Constructs a layer with the given weight and bias arrays.
383-
Accepts the same keywords as the `CrossCor((4,4), 3 => 7, relu)` method.
422+
Constructs a CrossCor layer with the given weight and bias.
423+
Accepts the same keywords and has the same defaults as
424+
[`CrossCor(k::NTuple{N,Integer}, ch::Pair{<:Integer,<:Integer}, σ; ...)`](@ref CrossCor).
425+
426+
# Examples
427+
```jldoctest
428+
julia> weight = rand(3, 4, 5);
429+
430+
julia> bias = zeros(5);
431+
432+
julia> layer = CrossCor(weight, bias, relu)
433+
CrossCor((3,), 4 => 5, relu) # 65 parameters
434+
435+
julia> layer(randn(100, 4, 64)) |> size
436+
(98, 5, 64)
437+
```
384438
"""
385439
function CrossCor(w::AbstractArray{T,N}, bias = true, σ = identity;
386440
stride = 1, pad = 0, dilation = 1) where {T,N}
@@ -611,10 +665,10 @@ julia> m[1](xs) |> size
611665
julia> m(xs) |> size
612666
(20, 20, 7, 50)
613667
614-
julia> lay = MaxPool((5,), pad=2, stride=(3,)) # one-dimensional window
668+
julia> layer = MaxPool((5,), pad=2, stride=(3,)) # one-dimensional window
615669
MaxPool((5,), pad=2, stride=3)
616670
617-
julia> lay(rand(Float32, 100, 7, 50)) |> size
671+
julia> layer(rand(Float32, 100, 7, 50)) |> size
618672
(34, 7, 50)
619673
```
620674
"""

src/layers/normalise.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ function Base.show(io::IO, l::GroupNorm)
566566
end
567567

568568
"""
569-
hasaffine(l)
569+
hasaffine(l)
570570
571571
Return `true` if a normalisation layer has trainable shift and
572572
scale parameters, `false` otherwise.

0 commit comments

Comments
 (0)