From a0314ce682945fe0e582be7cd0d92a07b305407a Mon Sep 17 00:00:00 2001 From: matsueushi Date: Fri, 22 Nov 2019 05:23:24 +0000 Subject: [PATCH] Fix logitbinarycrossentropy on CuArrays --- src/layers/stateless.jl | 3 +++ test/cuda/cuda.jl | 5 +++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/layers/stateless.jl b/src/layers/stateless.jl index 5f9c10904b..870a6cdf6e 100644 --- a/src/layers/stateless.jl +++ b/src/layers/stateless.jl @@ -53,6 +53,9 @@ but it is more numerically stable. """ logitbinarycrossentropy(logŷ, y) = (1 - y)*logŷ - logσ(logŷ) +# Re-definition to fix interaction with CuArrays. +CuArrays.@cufunc logitbinarycrossentropy(logŷ, y) = (1 - y)*logŷ - logσ(logŷ) + """ normalise(x::AbstractArray; dims=1) diff --git a/test/cuda/cuda.jl b/test/cuda/cuda.jl index ddd92e1ed3..1576d88ff5 100644 --- a/test/cuda/cuda.jl +++ b/test/cuda/cuda.jl @@ -31,9 +31,10 @@ cx = gpu(x) @test Flux.crossentropy(x,x, weight=1.0) ≈ Flux.crossentropy(cx,cx, weight=1.0) @test Flux.crossentropy(x,x, weight=[1.0;2.0;3.0]) ≈ Flux.crossentropy(cx,cx, weight=cu([1.0;2.0;3.0])) -x = σ.([-1.1491, 0.8619, 0.3127]) +x = [-1.1491, 0.8619, 0.3127] y = [1, 1, 0.] -@test Flux.binarycrossentropy.(x,y) ≈ Flux.binarycrossentropy.(cu(x),cu(y)) +@test Flux.binarycrossentropy.(σ.(x),y) ≈ Flux.binarycrossentropy.(cu(σ.(x)),cu(y)) +@test Flux.logitbinarycrossentropy.(x,y) ≈ Flux.logitbinarycrossentropy.(cu(x),cu(y)) xs = rand(5, 5) ys = Flux.onehotbatch(1:5,1:5)