single-batch convolution

This commit is contained in:
Mike J Innes 2018-02-28 14:25:32 +00:00
parent 6bdc2b37a9
commit 7606b1a399
3 changed files with 7 additions and 0 deletions

View File

@ -33,6 +33,7 @@ Conv(k::NTuple{N,Integer}, ch::Pair{<:Integer,<:Integer}, σ = identity; init =
Flux.treelike(Conv)
function (c::Conv)(x)
ndims(x) == ndims(c.weight)-1 && return squeezebatch(c(reshape(x, size(x)..., 1)))
σ, b = c.σ, reshape(c.bias, map(_->1, c.stride)..., :, 1)
σ.(conv(x, c.weight, stride = c.stride, pad = c.pad) .+ b)
end

View File

@ -46,6 +46,10 @@ function frequencies(xs)
return fs
end
head(x::Tuple) = reverse(Base.tail(reverse(x)))
squeezebatch(x) = reshape(x, head(size(x)))
"""
batch(xs)

View File

@ -92,4 +92,6 @@ end
@test @sprintf("%.2f", sum(param([1,2,3]))) == "6.00"
@inferred NNlib.conv(param(rand(10,10,3,2)),randn(2,2,3,4))
end #testset