fix cpu batchnorm

This commit is contained in:
CarloLucibello 2018-07-15 17:49:41 +02:00
parent a0fd91b866
commit 185e9148b6
2 changed files with 39 additions and 38 deletions

View File

@ -96,56 +96,58 @@ m = Chain(
softmax) softmax)
``` ```
""" """
mutable struct BatchNorm{F,V,W,N} mutable struct BatchNorm
λ::F # activation function λ # activation function
β::V # bias β # bias
γ::V # scale γ # scale
μ::W # moving mean μ # moving mean
σ::W # moving std σ² # moving var
ϵ::N ϵ
momentum::N momentum
active::Bool active::Bool
end end
BatchNorm(chs::Integer, λ = identity; function BatchNorm(chs::Integer, λ = identity;
initβ = zeros, initγ = ones, ϵ = 1e-8, momentum = .1) = initβ = x->zeros(Float32,x),
initγ = x->ones(Float32,x),
ϵ = 1f-8,
momentum = 0.1f0)
BatchNorm(λ, param(initβ(chs)), param(initγ(chs)), BatchNorm(λ, param(initβ(chs)), param(initγ(chs)),
zeros(chs), ones(chs), ϵ, momentum, true) zeros(Float32, chs), ones(Float32, chs), ϵ, momentum, true)
end
function (BN::BatchNorm)(x) function (BN::BatchNorm)(x)
size(x, ndims(x)-1) == length(BN.β) || size(x, ndims(x)-1) == length(BN.β) ||
error("BatchNorm expected $(length(BN.β)) channels, got $(size(x, ndims(x)-1))") error("BatchNorm expected $(length(BN.β)) channels, got $(size(x, ndims(x)-1))")
γ, β = BN.γ, BN.β γ, β = BN.γ, BN.β
dims = length(size(x)) dims = ndims(x)
channels = size(x, dims-1)
affine_shape = ones(Int, dims) affine_shape = ones(Int, dims)
affine_shape[end-1] = channels affine_shape[end-1] = size(x, dims-1)
m = prod(size(x)[1:end-2]) * size(x)[end] T = eltype(x)
if !BN.active if !BN.active
μ = reshape(BN.μ, affine_shape...) μ = reshape(BN.μ, affine_shape...)
σ = reshape(BN.σ, affine_shape...) σ² = reshape(BN.σ², affine_shape...)
else else
T = eltype(x)
ϵ = data(convert(T, BN.ϵ))
axes = [1:dims-2; dims] # axes to reduce along (all but channels axis) axes = [1:dims-2; dims] # axes to reduce along (all but channels axis)
m = prod(size(x, axes...))
μ = mean(x, axes) μ = mean(x, axes)
σ = sqrt.(mean((x .- μ).^2, axes) .+ ϵ) σ² = sum((x.-μ).^2, axes) ./ m
# update moving mean/std # update moving mean/std
mtm = data(convert(T, BN.momentum)) mtm = convert(T, BN.momentum)
BN.μ = (1 - mtm) .* BN.μ .+ mtm .* squeeze(data(μ), (axes...))
BN.σ = (1 - mtm) .* BN.σ .+ mtm .* squeeze(data(σ), (axes...)) .* m ./ (m - 1) BN.μ = ((1 - mtm) .* BN.μ .+ mtm .* squeeze(data(μ), (axes...))) |> data
BN.σ² = ((1 - mtm) .* BN.σ² .+ mtm .* squeeze(data(σ²), (axes...))*m/(m-1)) |> data
end end
let λ = BN.λ ϵ = convert(T, BN.ϵ)
λ.(reshape(γ, affine_shape...) .* ((x .- μ) ./ σ) .+ reshape(β, affine_shape...)) BN.λ.(reshape(γ, affine_shape...) .* ((x .- μ) ./ sqrt.(σ² .+ ϵ)) .+ reshape(β, affine_shape...))
end
end end
children(BN::BatchNorm) = children(BN::BatchNorm) =
(BN.λ, BN.β, BN.γ, BN.μ, BN.σ, BN.ϵ, BN.momentum, BN.active) (BN.λ, BN.β, BN.γ, BN.μ, BN.σ², BN.ϵ, BN.momentum, BN.active)
mapchildren(f, BN::BatchNorm) = # e.g. mapchildren(cu, BN) mapchildren(f, BN::BatchNorm) = # e.g. mapchildren(cu, BN)
BatchNorm(BN.λ, f(BN.β), f(BN.γ), f(BN.μ), f(BN.σ), BN.ϵ, BN.momentum, BN.active) BatchNorm(BN.λ, f(BN.β), f(BN.γ), f(BN.μ), f(BN.σ), BN.ϵ, BN.momentum, BN.active)

View File

@ -1,4 +1,5 @@
using Flux: testmode! using Flux: testmode!
using Flux.Tracker: data
@testset "Dropout" begin @testset "Dropout" begin
x = [1.,2.,3.] x = [1.,2.,3.]
@ -28,7 +29,8 @@ using Flux: testmode!
end end
@testset "BatchNorm" begin @testset "BatchNorm" begin
let m = BatchNorm(2), x = param([1 2; 3 4; 5 6]') let m = BatchNorm(2), x = param([1 3 5;
2 4 6])
@test m.β.data == [0, 0] # initβ(2) @test m.β.data == [0, 0] # initβ(2)
@test m.γ.data == [1, 1] # initγ(2) @test m.γ.data == [1, 1] # initγ(2)
@ -53,29 +55,26 @@ end
# .1 * 4 + 0 = .4 # .1 * 4 + 0 = .4
@test m.μ reshape([0.3, 0.4], 2, 1) @test m.μ reshape([0.3, 0.4], 2, 1)
# julia> .1 .* std(x, 2, corrected=false) .* (3 / 2).+ .9 .* [1., 1.] @test m.σ² 0.1 .* var(x.data, 2, corrected=false)*3/2 + 0.9 .* [1., 1.]
# 2×1 Array{Float64,2}:
# 1.14495
# 1.14495
@test m.σ .1 .* std(x.data, 2, corrected=false) .* (3 / 2).+ .9 .* [1., 1.]
testmode!(m) testmode!(m)
@test !m.active @test !m.active
x = m(x).data y = m(x).data
@test x[1] (1 - 0.3) / 1.1449489742783179 @test y data((x .- m.μ) ./ sqrt.(m.σ² .+ m.ϵ))
end end
# with activation function # with activation function
let m = BatchNorm(2, σ), x = param([1 2; 3 4; 5 6]') let m = BatchNorm(2, sigmoid), x = param([1 3 5;
2 4 6])
@test m.active @test m.active
m(x) m(x)
testmode!(m) testmode!(m)
@test !m.active @test !m.active
x = m(x).data y = m(x).data
@test x[1] σ((1 - 0.3) / 1.1449489742783179) @test y data(sigmoid.((x .- m.μ) ./ sqrt.(m.σ² .+ m.ϵ)))
end end
let m = BatchNorm(2), x = param(reshape(1:6, 3, 2, 1)) let m = BatchNorm(2), x = param(reshape(1:6, 3, 2, 1))
@ -85,7 +84,7 @@ end
end end
let m = BatchNorm(2), x = param(reshape(1:12, 2, 3, 2, 1)) let m = BatchNorm(2), x = param(reshape(1:12, 2, 3, 2, 1))
y = reshape(permutedims(x, [3, 1, 2, 4]), 2, :) y = reshape(permutedims(x, [3, 1, 2, 4]), 2, :)
y = permutedims(reshape(m(y), 2, 2, 3, 1), [2, 3, 1, 4]) y = permutedims(reshape(m(y), 2, 2, 3, 1), [2, 3, 1, 4])
@test m(x) == y @test m(x) == y
end end