From e0c1c0e057dd9bf030f7289ad16283536f3313f4 Mon Sep 17 00:00:00 2001 From: Katharine Hyatt Date: Thu, 17 Oct 2019 11:01:28 -0400 Subject: [PATCH 1/3] Fix problem in crossentropy breaking GPU compilation --- src/layers/stateless.jl | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/layers/stateless.jl b/src/layers/stateless.jl index 4c216672..6d710c6b 100644 --- a/src/layers/stateless.jl +++ b/src/layers/stateless.jl @@ -4,10 +4,20 @@ using NNlib: logsoftmax, logσ mse(ŷ, y) = sum((ŷ .- y).^2) * 1 // length(y) -function crossentropy(ŷ::AbstractVecOrMat, y::AbstractVecOrMat; weight = 1) - -sum(y .* log.(ŷ) .* weight) * 1 // size(y, 2) +function _crossentropy(ŷ::AbstractVecOrMat, y::AbstractVecOrMat, weight::Nothing) + return -sum(y .* log.(ŷ)) * 1 // size(y, 2) end +function _crossentropy(ŷ::AbstractVecOrMat, y::AbstractVecOrMat, weight::Number) + return -sum(y .* log.(ŷ)) .* weight * 1 // size(y, 2) +end + +function _crossentropy(ŷ::AbstractVecOrMat, y::AbstractVecOrMat, weight::AbstractVector) + return -sum(y .* log.(ŷ) .* weight) * 1 // size(y, 2) +end + +crossentropy(ŷ::AbstractVecOrMat, y::AbstractVecOrMat; weight=nothing) = _crossentropy(ŷ, y, weight) + function logitcrossentropy(logŷ::AbstractVecOrMat, y::AbstractVecOrMat; weight = 1) return -sum(y .* logsoftmax(logŷ) .* weight) * 1 // size(y, 2) end From f7ce717aaa3387393a0acf2e84b9e69faacb7f94 Mon Sep 17 00:00:00 2001 From: Katharine Hyatt Date: Wed, 23 Oct 2019 09:22:22 -0400 Subject: [PATCH 2/3] Add tests --- test/cuda/cuda.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/cuda/cuda.jl b/test/cuda/cuda.jl index 59bc7f50..68820476 100644 --- a/test/cuda/cuda.jl +++ b/test/cuda/cuda.jl @@ -28,6 +28,8 @@ cm = gpu(m) x = [1,2,3] cx = gpu(x) @test Flux.crossentropy(x,x) ≈ Flux.crossentropy(cx,cx) +@test Flux.crossentropy(x,x, weight=1.0) ≈ Flux.crossentropy(cx,cx, weight=1.0) +@test_broken Flux.crossentropy(x,x, weight=[1.0;2.0;3.0]) ≈ Flux.crossentropy(cx,cx, weight=[1.0;2.0;3.0]) xs = rand(5, 5) ys = Flux.onehotbatch(1:5,1:5) From 8913c9c7413b3715f7720fc6606ea00f8dcd4c9d Mon Sep 17 00:00:00 2001 From: Katharine Hyatt Date: Wed, 23 Oct 2019 09:53:09 -0400 Subject: [PATCH 3/3] Make the vector of weights test pass on GPU --- test/cuda/cuda.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cuda/cuda.jl b/test/cuda/cuda.jl index 68820476..9bafe44a 100644 --- a/test/cuda/cuda.jl +++ b/test/cuda/cuda.jl @@ -29,7 +29,7 @@ x = [1,2,3] cx = gpu(x) @test Flux.crossentropy(x,x) ≈ Flux.crossentropy(cx,cx) @test Flux.crossentropy(x,x, weight=1.0) ≈ Flux.crossentropy(cx,cx, weight=1.0) -@test_broken Flux.crossentropy(x,x, weight=[1.0;2.0;3.0]) ≈ Flux.crossentropy(cx,cx, weight=[1.0;2.0;3.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])) xs = rand(5, 5) ys = Flux.onehotbatch(1:5,1:5)