Flux.jl/src/layers/conv.jl

38 lines
1.1 KiB
Julia
Raw Normal View History

2017-12-18 18:05:48 +00:00
"""
Conv2D(size, in=>out)
Conv2d(size, in=>out, relu)
Standard convolutional layer. `size` should be a tuple like `(2, 2)`.
`in` and `out` specify the number of input and output channels respectively.
Data should be stored in HWCN order. In other words, a 100×100 RGB image would
be a `100×100×3` array, and a batch of 50 would be a `100×100×3×50` array.
Takes the keyword arguments `pad` and `stride`.
"""
2018-02-15 20:15:41 +00:00
struct Conv2D{F,A,V}
2017-12-15 13:22:57 +00:00
σ::F
weight::A
2018-02-15 20:15:41 +00:00
bias::V
2017-12-15 13:22:57 +00:00
stride::Int
2017-12-18 18:05:38 +00:00
pad::Int
2017-12-15 13:22:57 +00:00
end
Conv2D(k::NTuple{2,Integer}, ch::Pair{<:Integer,<:Integer}, σ = identity;
2017-12-18 18:05:38 +00:00
init = initn, stride = 1, pad = 0) =
2018-02-15 20:15:41 +00:00
Conv2D(σ, param(init(k..., ch...)), param(zeros(ch[2])), stride, pad)
2017-12-15 13:22:57 +00:00
Flux.treelike(Conv2D)
2018-02-15 20:15:41 +00:00
function (c::Conv2D)(x)
σ, b = c.σ, reshape(c.bias, 1, 1, :)
σ.(conv2d(x, c.weight, stride = c.stride, padding = c.pad) .+ b)
end
2017-12-15 16:24:45 +00:00
function Base.show(io::IO, l::Conv2D)
print(io, "Conv2D((", size(l.weight, 1), ", ", size(l.weight, 2), ")")
print(io, ", ", size(l.weight, 3), "=>", size(l.weight, 4))
l.σ == identity || print(io, ", ", l.σ)
print(io, ")")
end