2017-09-08 21:52:41 +00:00
|
|
|
|
"""
|
|
|
|
|
Chain(layers...)
|
2017-08-19 19:52:29 +00:00
|
|
|
|
|
2017-09-08 21:52:41 +00:00
|
|
|
|
Chain multiple layers / functions together, so that they are called in sequence
|
|
|
|
|
on a given input.
|
|
|
|
|
|
|
|
|
|
`Chain` also supports indexing and slicing, e.g. `m[2]` or `m[1:end-1]`.
|
2017-09-10 00:02:48 +00:00
|
|
|
|
`m[1:3](x)` will calculate the output of the first three layers.
|
Improve docstrings
Improvements like...
- fixing typos,
- removing trailing and double whitespaces,
- using `jldoctest` blocks where applicable,
- fixing, updating or correctly setting up existing doctests,
- improving consistency (for example, always use "# Examples" instead
of other variants),
- removing empty lines between docstrings and functions,
- instead of mentioning keywords, put them into the docstring,
- adding some missing but useful keywords,
- adding references (`@ref`),
- using LaTeX math where applicable, and
- linking papers.
Debatable stuff that is untouched:
- BE/AE s/z irregularities ("normalise" versus "normalize") since
most papers use the AE version while the Flux source code was
written with BE spelling.
- Names of normalization functions are capitalized
("Batch Normalization" instead of "batch normalization").
2019-08-31 09:39:28 +00:00
|
|
|
|
|
|
|
|
|
# Examples
|
|
|
|
|
```jldoctest
|
|
|
|
|
julia> m = Chain(x -> x^2, x -> x+1);
|
|
|
|
|
|
|
|
|
|
julia> m(5) == 26
|
|
|
|
|
true
|
|
|
|
|
|
|
|
|
|
julia> m = Chain(Dense(10, 5), Dense(5, 2));
|
|
|
|
|
|
|
|
|
|
julia> x = rand(10);
|
|
|
|
|
|
|
|
|
|
julia> m(x) == m[2](m[1](x))
|
|
|
|
|
true
|
|
|
|
|
```
|
2017-09-08 21:52:41 +00:00
|
|
|
|
"""
|
2018-11-16 12:22:15 +00:00
|
|
|
|
struct Chain{T<:Tuple}
|
|
|
|
|
layers::T
|
|
|
|
|
Chain(xs...) = new{typeof(xs)}(xs)
|
2016-08-25 21:49:21 +00:00
|
|
|
|
end
|
|
|
|
|
|
2019-01-16 14:51:37 +00:00
|
|
|
|
@forward Chain.layers Base.getindex, Base.length, Base.first, Base.last,
|
|
|
|
|
Base.iterate, Base.lastindex
|
2016-08-25 21:49:21 +00:00
|
|
|
|
|
2020-04-14 14:21:45 +00:00
|
|
|
|
functor(::Type{<:Chain}, c) = c.layers, ls -> Chain(ls...)
|
2017-08-22 16:13:03 +00:00
|
|
|
|
|
2018-11-16 12:22:15 +00:00
|
|
|
|
applychain(::Tuple{}, x) = x
|
|
|
|
|
applychain(fs::Tuple, x) = applychain(tail(fs), first(fs)(x))
|
|
|
|
|
|
|
|
|
|
(c::Chain)(x) = applychain(c.layers, x)
|
2017-06-12 11:39:34 +00:00
|
|
|
|
|
2017-02-28 16:42:48 +00:00
|
|
|
|
Base.getindex(c::Chain, i::AbstractArray) = Chain(c.layers[i]...)
|
2017-08-19 19:52:29 +00:00
|
|
|
|
|
2020-02-29 22:09:59 +00:00
|
|
|
|
testmode!(m::Chain, mode = true) = (map(x -> testmode!(x, mode), m.layers); m)
|
2020-02-21 21:10:28 +00:00
|
|
|
|
|
2017-08-21 16:20:09 +00:00
|
|
|
|
function Base.show(io::IO, c::Chain)
|
|
|
|
|
print(io, "Chain(")
|
|
|
|
|
join(io, c.layers, ", ")
|
|
|
|
|
print(io, ")")
|
|
|
|
|
end
|
|
|
|
|
|
2019-12-04 04:48:48 +00:00
|
|
|
|
"""
|
2019-12-06 03:57:10 +00:00
|
|
|
|
outdims(c::Chain, isize)
|
2019-12-04 04:48:48 +00:00
|
|
|
|
|
|
|
|
|
Calculate the output dimensions given the input dimensions, `isize`.
|
|
|
|
|
|
|
|
|
|
```julia
|
|
|
|
|
m = Chain(Conv((3, 3), 3 => 16), Conv((3, 3), 16 => 32))
|
|
|
|
|
outdims(m, (10, 10)) == (6, 6)
|
|
|
|
|
```
|
|
|
|
|
"""
|
2019-12-06 04:54:25 +00:00
|
|
|
|
outdims(c::Chain, isize) = foldl(∘, map(l -> (x -> outdims(l, x)), c.layers))(isize)
|
2019-03-28 09:07:04 +00:00
|
|
|
|
|
2019-04-05 11:19:30 +00:00
|
|
|
|
# This is a temporary and naive implementation
|
|
|
|
|
# it might be replaced in the future for better performance
|
2019-04-05 10:16:44 +00:00
|
|
|
|
# see issue https://github.com/FluxML/Flux.jl/issues/702
|
2019-04-05 10:44:00 +00:00
|
|
|
|
# Johnny Chen -- @johnnychen94
|
2019-09-10 07:54:49 +00:00
|
|
|
|
# only slightly changed to better handle interaction with Zygote @dsweber2
|
2019-03-28 09:07:04 +00:00
|
|
|
|
"""
|
2019-04-05 10:44:00 +00:00
|
|
|
|
activations(c::Chain, input)
|
Improve docstrings
Improvements like...
- fixing typos,
- removing trailing and double whitespaces,
- using `jldoctest` blocks where applicable,
- fixing, updating or correctly setting up existing doctests,
- improving consistency (for example, always use "# Examples" instead
of other variants),
- removing empty lines between docstrings and functions,
- instead of mentioning keywords, put them into the docstring,
- adding some missing but useful keywords,
- adding references (`@ref`),
- using LaTeX math where applicable, and
- linking papers.
Debatable stuff that is untouched:
- BE/AE s/z irregularities ("normalise" versus "normalize") since
most papers use the AE version while the Flux source code was
written with BE spelling.
- Names of normalization functions are capitalized
("Batch Normalization" instead of "batch normalization").
2019-08-31 09:39:28 +00:00
|
|
|
|
|
2019-04-05 10:44:00 +00:00
|
|
|
|
Calculate the forward results of each layers in Chain `c` with `input` as model input.
|
2019-03-28 09:07:04 +00:00
|
|
|
|
"""
|
2019-04-05 10:44:00 +00:00
|
|
|
|
function activations(c::Chain, input)
|
2019-09-12 00:36:37 +00:00
|
|
|
|
extraChain(c.layers, input)
|
2019-03-28 09:07:04 +00:00
|
|
|
|
end
|
|
|
|
|
|
2019-09-12 00:36:37 +00:00
|
|
|
|
function extraChain(fs::Tuple, x)
|
|
|
|
|
res = first(fs)(x)
|
|
|
|
|
return (res, extraChain(Base.tail(fs), res)...)
|
|
|
|
|
end
|
|
|
|
|
|
2019-11-14 22:05:53 +00:00
|
|
|
|
extraChain(::Tuple{}, x) = ()
|
2019-09-12 00:36:37 +00:00
|
|
|
|
|
|
|
|
|
|
2018-06-26 13:30:46 +00:00
|
|
|
|
|
2017-09-08 21:52:41 +00:00
|
|
|
|
"""
|
|
|
|
|
Dense(in::Integer, out::Integer, σ = identity)
|
|
|
|
|
|
Improve docstrings
Improvements like...
- fixing typos,
- removing trailing and double whitespaces,
- using `jldoctest` blocks where applicable,
- fixing, updating or correctly setting up existing doctests,
- improving consistency (for example, always use "# Examples" instead
of other variants),
- removing empty lines between docstrings and functions,
- instead of mentioning keywords, put them into the docstring,
- adding some missing but useful keywords,
- adding references (`@ref`),
- using LaTeX math where applicable, and
- linking papers.
Debatable stuff that is untouched:
- BE/AE s/z irregularities ("normalise" versus "normalize") since
most papers use the AE version while the Flux source code was
written with BE spelling.
- Names of normalization functions are capitalized
("Batch Normalization" instead of "batch normalization").
2019-08-31 09:39:28 +00:00
|
|
|
|
Create a traditional `Dense` layer with parameters `W` and `b`.
|
2017-08-19 19:52:29 +00:00
|
|
|
|
|
2017-09-08 21:52:41 +00:00
|
|
|
|
y = σ.(W * x .+ b)
|
2017-09-09 23:58:32 +00:00
|
|
|
|
|
|
|
|
|
The input `x` must be a vector of length `in`, or a batch of vectors represented
|
2017-10-18 11:48:58 +00:00
|
|
|
|
as an `in × N` matrix. The out `y` will be a vector or batch of length `out`.
|
2017-10-18 11:47:45 +00:00
|
|
|
|
|
Improve docstrings
Improvements like...
- fixing typos,
- removing trailing and double whitespaces,
- using `jldoctest` blocks where applicable,
- fixing, updating or correctly setting up existing doctests,
- improving consistency (for example, always use "# Examples" instead
of other variants),
- removing empty lines between docstrings and functions,
- instead of mentioning keywords, put them into the docstring,
- adding some missing but useful keywords,
- adding references (`@ref`),
- using LaTeX math where applicable, and
- linking papers.
Debatable stuff that is untouched:
- BE/AE s/z irregularities ("normalise" versus "normalize") since
most papers use the AE version while the Flux source code was
written with BE spelling.
- Names of normalization functions are capitalized
("Batch Normalization" instead of "batch normalization").
2019-08-31 09:39:28 +00:00
|
|
|
|
# Examples
|
|
|
|
|
```jldoctest; setup = :(using Random; Random.seed!(0))
|
2017-10-18 14:44:06 +00:00
|
|
|
|
julia> d = Dense(5, 2)
|
|
|
|
|
Dense(5, 2)
|
2017-10-18 11:47:45 +00:00
|
|
|
|
|
2017-10-18 14:44:06 +00:00
|
|
|
|
julia> d(rand(5))
|
2020-04-04 20:55:14 +00:00
|
|
|
|
2-element Array{Float32,1}:
|
|
|
|
|
-0.16210233
|
|
|
|
|
0.12311903```
|
2017-09-08 21:52:41 +00:00
|
|
|
|
"""
|
2020-06-10 11:06:57 +00:00
|
|
|
|
struct Dense{F,S<:AbstractArray,T<:AbstractArray}
|
2017-08-19 19:52:29 +00:00
|
|
|
|
W::S
|
|
|
|
|
b::T
|
2018-02-15 20:52:29 +00:00
|
|
|
|
σ::F
|
2017-08-19 19:52:29 +00:00
|
|
|
|
end
|
|
|
|
|
|
2018-02-15 20:52:29 +00:00
|
|
|
|
Dense(W, b) = Dense(W, b, identity)
|
|
|
|
|
|
2017-12-05 07:47:03 +00:00
|
|
|
|
function Dense(in::Integer, out::Integer, σ = identity;
|
|
|
|
|
initW = glorot_uniform, initb = zeros)
|
2019-03-08 12:13:58 +00:00
|
|
|
|
return Dense(initW(out, in), initb(out), σ)
|
2017-12-05 07:47:03 +00:00
|
|
|
|
end
|
2017-08-19 19:52:29 +00:00
|
|
|
|
|
2019-09-19 14:53:31 +00:00
|
|
|
|
@functor Dense
|
2017-08-22 16:13:03 +00:00
|
|
|
|
|
2018-08-23 13:34:11 +00:00
|
|
|
|
function (a::Dense)(x::AbstractArray)
|
2017-09-27 20:58:34 +00:00
|
|
|
|
W, b, σ = a.W, a.b, a.σ
|
2018-08-20 12:08:04 +00:00
|
|
|
|
σ.(W*x .+ b)
|
2017-09-27 20:58:34 +00:00
|
|
|
|
end
|
2017-08-21 16:20:09 +00:00
|
|
|
|
|
2017-09-02 20:50:11 +00:00
|
|
|
|
function Base.show(io::IO, l::Dense)
|
|
|
|
|
print(io, "Dense(", size(l.W, 2), ", ", size(l.W, 1))
|
2017-08-21 16:20:09 +00:00
|
|
|
|
l.σ == identity || print(io, ", ", l.σ)
|
|
|
|
|
print(io, ")")
|
|
|
|
|
end
|
2017-10-10 20:33:37 +00:00
|
|
|
|
|
2019-02-27 11:46:20 +00:00
|
|
|
|
# Try to avoid hitting generic matmul in some simple cases
|
|
|
|
|
# Base's matmul is so slow that it's worth the extra conversion to hit BLAS
|
|
|
|
|
(a::Dense{<:Any,W})(x::AbstractArray{T}) where {T <: Union{Float32,Float64}, W <: AbstractArray{T}} =
|
|
|
|
|
invoke(a, Tuple{AbstractArray}, x)
|
|
|
|
|
|
2019-08-09 12:53:11 +00:00
|
|
|
|
(a::Dense{<:Any,W})(x::AbstractArray{<:AbstractFloat}) where {T <: Union{Float32,Float64}, W <: AbstractArray{T}} =
|
2019-02-27 11:46:20 +00:00
|
|
|
|
a(T.(x))
|
|
|
|
|
|
2019-12-04 04:48:48 +00:00
|
|
|
|
"""
|
|
|
|
|
outdims(l::Dense, isize)
|
|
|
|
|
|
|
|
|
|
Calculate the output dimensions given the input dimensions, `isize`.
|
|
|
|
|
|
|
|
|
|
```julia
|
|
|
|
|
m = Dense(10, 5)
|
|
|
|
|
outdims(m, (5, 2)) == (5,)
|
|
|
|
|
outdims(m, (10,)) == (5,)
|
|
|
|
|
```
|
|
|
|
|
"""
|
2019-12-06 04:54:25 +00:00
|
|
|
|
outdims(l::Dense, isize) = (size(l.W)[1],)
|
2019-12-04 04:48:48 +00:00
|
|
|
|
|
2017-10-10 20:33:37 +00:00
|
|
|
|
"""
|
2017-10-23 11:53:07 +00:00
|
|
|
|
Diagonal(in::Integer)
|
2017-10-10 20:33:37 +00:00
|
|
|
|
|
Improve docstrings
Improvements like...
- fixing typos,
- removing trailing and double whitespaces,
- using `jldoctest` blocks where applicable,
- fixing, updating or correctly setting up existing doctests,
- improving consistency (for example, always use "# Examples" instead
of other variants),
- removing empty lines between docstrings and functions,
- instead of mentioning keywords, put them into the docstring,
- adding some missing but useful keywords,
- adding references (`@ref`),
- using LaTeX math where applicable, and
- linking papers.
Debatable stuff that is untouched:
- BE/AE s/z irregularities ("normalise" versus "normalize") since
most papers use the AE version while the Flux source code was
written with BE spelling.
- Names of normalization functions are capitalized
("Batch Normalization" instead of "batch normalization").
2019-08-31 09:39:28 +00:00
|
|
|
|
Create an element-wise linear transformation layer with learnable
|
2017-11-21 16:04:04 +00:00
|
|
|
|
vectors `α` and `β`:
|
2017-10-10 20:33:37 +00:00
|
|
|
|
|
2017-11-21 16:04:04 +00:00
|
|
|
|
y = α .* x .+ β
|
2017-10-10 20:33:37 +00:00
|
|
|
|
|
2017-10-23 11:53:07 +00:00
|
|
|
|
The input `x` must be a array where `size(x, 1) == in`.
|
2017-10-10 20:33:37 +00:00
|
|
|
|
"""
|
2017-10-23 11:53:07 +00:00
|
|
|
|
struct Diagonal{T}
|
2017-10-10 20:33:37 +00:00
|
|
|
|
α::T
|
|
|
|
|
β::T
|
|
|
|
|
end
|
|
|
|
|
|
2018-07-17 15:13:55 +00:00
|
|
|
|
Diagonal(in::Integer; initα = ones, initβ = zeros) =
|
2019-03-08 12:13:58 +00:00
|
|
|
|
Diagonal(initα(in), initβ(in))
|
2017-10-10 20:33:37 +00:00
|
|
|
|
|
2019-09-19 14:53:31 +00:00
|
|
|
|
@functor Diagonal
|
2017-10-10 20:33:37 +00:00
|
|
|
|
|
2017-10-23 11:53:07 +00:00
|
|
|
|
function (a::Diagonal)(x)
|
2017-10-10 20:33:37 +00:00
|
|
|
|
α, β = a.α, a.β
|
|
|
|
|
α.*x .+ β
|
|
|
|
|
end
|
|
|
|
|
|
2017-10-23 11:53:07 +00:00
|
|
|
|
function Base.show(io::IO, l::Diagonal)
|
|
|
|
|
print(io, "Diagonal(", length(l.α), ")")
|
2017-10-10 20:33:37 +00:00
|
|
|
|
end
|
2018-09-07 00:25:32 +00:00
|
|
|
|
|
2019-12-04 04:48:48 +00:00
|
|
|
|
outdims(l::Diagonal, isize) = (length(l.α),)
|
2019-02-27 12:04:59 +00:00
|
|
|
|
|
|
|
|
|
"""
|
2019-03-06 18:22:46 +00:00
|
|
|
|
Maxout(over)
|
2019-02-27 12:04:59 +00:00
|
|
|
|
|
2020-04-04 20:59:45 +00:00
|
|
|
|
The [Maxout](https://arxiv.org/pdf/1302.4389.pdf) layer has a number of
|
|
|
|
|
internal layers which all receive the same input. It returns the elementwise
|
|
|
|
|
maximum of the internal layers' outputs.
|
2019-02-27 12:04:59 +00:00
|
|
|
|
|
|
|
|
|
Maxout over linear dense layers satisfies the univeral approximation theorem.
|
|
|
|
|
"""
|
2019-03-06 18:22:46 +00:00
|
|
|
|
struct Maxout{FS<:Tuple}
|
2019-02-27 12:04:59 +00:00
|
|
|
|
over::FS
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
"""
|
2019-03-21 17:04:52 +00:00
|
|
|
|
Maxout(f, n_alts)
|
2019-02-27 12:04:59 +00:00
|
|
|
|
|
Improve docstrings
Improvements like...
- fixing typos,
- removing trailing and double whitespaces,
- using `jldoctest` blocks where applicable,
- fixing, updating or correctly setting up existing doctests,
- improving consistency (for example, always use "# Examples" instead
of other variants),
- removing empty lines between docstrings and functions,
- instead of mentioning keywords, put them into the docstring,
- adding some missing but useful keywords,
- adding references (`@ref`),
- using LaTeX math where applicable, and
- linking papers.
Debatable stuff that is untouched:
- BE/AE s/z irregularities ("normalise" versus "normalize") since
most papers use the AE version while the Flux source code was
written with BE spelling.
- Names of normalization functions are capitalized
("Batch Normalization" instead of "batch normalization").
2019-08-31 09:39:28 +00:00
|
|
|
|
Construct a Maxout layer over `n_alts` instances of the layer given by `f`.
|
|
|
|
|
The function takes no arguments and should return some callable layer.
|
|
|
|
|
Conventionally, this is a linear dense layer.
|
2019-02-27 12:04:59 +00:00
|
|
|
|
|
Improve docstrings
Improvements like...
- fixing typos,
- removing trailing and double whitespaces,
- using `jldoctest` blocks where applicable,
- fixing, updating or correctly setting up existing doctests,
- improving consistency (for example, always use "# Examples" instead
of other variants),
- removing empty lines between docstrings and functions,
- instead of mentioning keywords, put them into the docstring,
- adding some missing but useful keywords,
- adding references (`@ref`),
- using LaTeX math where applicable, and
- linking papers.
Debatable stuff that is untouched:
- BE/AE s/z irregularities ("normalise" versus "normalize") since
most papers use the AE version while the Flux source code was
written with BE spelling.
- Names of normalization functions are capitalized
("Batch Normalization" instead of "batch normalization").
2019-08-31 09:39:28 +00:00
|
|
|
|
# Examples
|
|
|
|
|
|
|
|
|
|
This constructs a `Maxout` layer over 4 internal dense linear layers, each
|
|
|
|
|
identical in structure (784 inputs, 128 outputs):
|
2019-02-27 12:04:59 +00:00
|
|
|
|
```julia
|
Improve docstrings
Improvements like...
- fixing typos,
- removing trailing and double whitespaces,
- using `jldoctest` blocks where applicable,
- fixing, updating or correctly setting up existing doctests,
- improving consistency (for example, always use "# Examples" instead
of other variants),
- removing empty lines between docstrings and functions,
- instead of mentioning keywords, put them into the docstring,
- adding some missing but useful keywords,
- adding references (`@ref`),
- using LaTeX math where applicable, and
- linking papers.
Debatable stuff that is untouched:
- BE/AE s/z irregularities ("normalise" versus "normalize") since
most papers use the AE version while the Flux source code was
written with BE spelling.
- Names of normalization functions are capitalized
("Batch Normalization" instead of "batch normalization").
2019-08-31 09:39:28 +00:00
|
|
|
|
insize = 784
|
|
|
|
|
outsize = 128
|
|
|
|
|
Maxout(()->Dense(insize, outsize), 4)
|
2019-02-27 12:04:59 +00:00
|
|
|
|
```
|
|
|
|
|
"""
|
2019-03-21 17:04:52 +00:00
|
|
|
|
function Maxout(f, n_alts)
|
|
|
|
|
over = Tuple(f() for _ in 1:n_alts)
|
2019-03-06 18:22:46 +00:00
|
|
|
|
return Maxout(over)
|
2019-02-27 12:04:59 +00:00
|
|
|
|
end
|
|
|
|
|
|
2019-09-19 14:53:31 +00:00
|
|
|
|
@functor Maxout
|
2019-03-25 16:02:46 +00:00
|
|
|
|
|
2019-03-06 18:22:46 +00:00
|
|
|
|
function (mo::Maxout)(input::AbstractArray)
|
2019-02-27 12:04:59 +00:00
|
|
|
|
mapreduce(f -> f(input), (acc, out) -> max.(acc, out), mo.over)
|
|
|
|
|
end
|
2019-06-10 12:54:18 +00:00
|
|
|
|
|
2019-12-06 04:54:25 +00:00
|
|
|
|
outdims(l::Maxout, isize) = outdims(first(l.over), isize)
|
2019-12-06 03:57:10 +00:00
|
|
|
|
|
2018-10-20 19:36:16 +00:00
|
|
|
|
"""
|
Improve docstrings
Improvements like...
- fixing typos,
- removing trailing and double whitespaces,
- using `jldoctest` blocks where applicable,
- fixing, updating or correctly setting up existing doctests,
- improving consistency (for example, always use "# Examples" instead
of other variants),
- removing empty lines between docstrings and functions,
- instead of mentioning keywords, put them into the docstring,
- adding some missing but useful keywords,
- adding references (`@ref`),
- using LaTeX math where applicable, and
- linking papers.
Debatable stuff that is untouched:
- BE/AE s/z irregularities ("normalise" versus "normalize") since
most papers use the AE version while the Flux source code was
written with BE spelling.
- Names of normalization functions are capitalized
("Batch Normalization" instead of "batch normalization").
2019-08-31 09:39:28 +00:00
|
|
|
|
SkipConnection(layer, connection)
|
2018-10-20 19:36:16 +00:00
|
|
|
|
|
Improve docstrings
Improvements like...
- fixing typos,
- removing trailing and double whitespaces,
- using `jldoctest` blocks where applicable,
- fixing, updating or correctly setting up existing doctests,
- improving consistency (for example, always use "# Examples" instead
of other variants),
- removing empty lines between docstrings and functions,
- instead of mentioning keywords, put them into the docstring,
- adding some missing but useful keywords,
- adding references (`@ref`),
- using LaTeX math where applicable, and
- linking papers.
Debatable stuff that is untouched:
- BE/AE s/z irregularities ("normalise" versus "normalize") since
most papers use the AE version while the Flux source code was
written with BE spelling.
- Names of normalization functions are capitalized
("Batch Normalization" instead of "batch normalization").
2019-08-31 09:39:28 +00:00
|
|
|
|
Create a skip connection which consists of a layer or `Chain` of consecutive
|
|
|
|
|
layers and a shortcut connection linking the block's input to the output
|
|
|
|
|
through a user-supplied 2-argument callable. The first argument to the callable
|
|
|
|
|
will be propagated through the given `layer` while the second is the unchanged,
|
|
|
|
|
"skipped" input.
|
2018-10-20 19:36:16 +00:00
|
|
|
|
|
Improve docstrings
Improvements like...
- fixing typos,
- removing trailing and double whitespaces,
- using `jldoctest` blocks where applicable,
- fixing, updating or correctly setting up existing doctests,
- improving consistency (for example, always use "# Examples" instead
of other variants),
- removing empty lines between docstrings and functions,
- instead of mentioning keywords, put them into the docstring,
- adding some missing but useful keywords,
- adding references (`@ref`),
- using LaTeX math where applicable, and
- linking papers.
Debatable stuff that is untouched:
- BE/AE s/z irregularities ("normalise" versus "normalize") since
most papers use the AE version while the Flux source code was
written with BE spelling.
- Names of normalization functions are capitalized
("Batch Normalization" instead of "batch normalization").
2019-08-31 09:39:28 +00:00
|
|
|
|
The simplest "ResNet"-type connection is just `SkipConnection(layer, +)`,
|
2019-09-25 13:18:40 +00:00
|
|
|
|
and requires the output of the layers to be the same shape as the input.
|
|
|
|
|
Here is a more complicated example:
|
Improve docstrings
Improvements like...
- fixing typos,
- removing trailing and double whitespaces,
- using `jldoctest` blocks where applicable,
- fixing, updating or correctly setting up existing doctests,
- improving consistency (for example, always use "# Examples" instead
of other variants),
- removing empty lines between docstrings and functions,
- instead of mentioning keywords, put them into the docstring,
- adding some missing but useful keywords,
- adding references (`@ref`),
- using LaTeX math where applicable, and
- linking papers.
Debatable stuff that is untouched:
- BE/AE s/z irregularities ("normalise" versus "normalize") since
most papers use the AE version while the Flux source code was
written with BE spelling.
- Names of normalization functions are capitalized
("Batch Normalization" instead of "batch normalization").
2019-08-31 09:39:28 +00:00
|
|
|
|
```julia
|
2019-09-25 13:18:40 +00:00
|
|
|
|
m = Conv((3,3), 4=>7, pad=(1,1))
|
|
|
|
|
x = ones(5,5,4,10);
|
|
|
|
|
size(m(x)) == (5, 5, 7, 10)
|
2019-05-13 16:47:46 +00:00
|
|
|
|
|
2019-09-25 13:18:40 +00:00
|
|
|
|
sm = SkipConnection(m, (mx, x) -> cat(mx, x, dims=3))
|
|
|
|
|
size(sm(x)) == (5, 5, 11, 10)
|
2019-05-13 16:47:46 +00:00
|
|
|
|
```
|
2018-10-20 19:36:16 +00:00
|
|
|
|
"""
|
|
|
|
|
struct SkipConnection
|
|
|
|
|
layers
|
2019-05-13 16:47:46 +00:00
|
|
|
|
connection #user can pass arbitrary connections here, such as (a,b) -> a + b
|
2018-10-20 19:36:16 +00:00
|
|
|
|
end
|
|
|
|
|
|
2019-09-19 14:53:31 +00:00
|
|
|
|
@functor SkipConnection
|
2018-10-20 19:36:16 +00:00
|
|
|
|
|
2019-05-13 16:47:46 +00:00
|
|
|
|
function (skip::SkipConnection)(input)
|
2018-10-20 19:36:16 +00:00
|
|
|
|
skip.connection(skip.layers(input), input)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function Base.show(io::IO, b::SkipConnection)
|
2019-09-25 11:59:32 +00:00
|
|
|
|
print(io, "SkipConnection(", b.layers, ", ", b.connection, ")")
|
2018-10-20 19:36:16 +00:00
|
|
|
|
end
|