Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove deprecations in crossentropy and kldivergence #725

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 6 additions & 22 deletions src/scalarstats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -591,17 +591,10 @@ Compute the cross entropy between `p` and `q`, optionally specifying a real
number `b` such that the result is scaled by `1/log(b)`.
"""
function crossentropy(p::AbstractArray{<:Real}, q::AbstractArray{<:Real})
length(p) == length(q) || throw(DimensionMismatch("Inconsistent array length."))

# handle empty collections
length(p) == length(q) || throw(DimensionMismatch("inconsistent array length"))
if isempty(p)
Base.depwarn(
"support for empty collections will be removed since they do not " *
"represent proper probability distributions",
:crossentropy,
)
# return zero for empty arrays
return xlogy(zero(eltype(p)), zero(eltype(q)))
throw(ArgumentError("empty collections are not supported since they do not " *
"represent proper probability distributions"))
end

# use pairwise summation (https://github.com/JuliaLang/julia/pull/31020)
Expand All @@ -622,19 +615,10 @@ that is the sum `pᵢ * log(pᵢ / qᵢ)`. Optionally a real number `b`
can be specified such that the divergence is scaled by `1/log(b)`.
"""
function kldivergence(p::AbstractArray{<:Real}, q::AbstractArray{<:Real})
length(p) == length(q) || throw(DimensionMismatch("Inconsistent array length."))

# handle empty collections
length(p) == length(q) || throw(DimensionMismatch("inconsistent array length"))
if isempty(p)
Base.depwarn(
"support for empty collections will be removed since they do not "*
"represent proper probability distributions",
:kldivergence,
)
# return zero for empty arrays
pzero = zero(eltype(p))
qzero = zero(eltype(q))
return xlogy(pzero, zero(pzero / qzero))
throw(ArgumentError("empty collections are not supported since they do not " *
"represent proper probability distributions"))
end

# use pairwise summation (https://github.com/JuliaLang/julia/pull/31020)
Expand Down
14 changes: 6 additions & 8 deletions test/scalarstats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,9 @@ scale = rand()
@test @inferred(crossentropy([1//5, 3//10, 1//2], [0.3, 0.4, 0.3], 2)) ≈ 1.6124543443825532
@test @inferred(crossentropy([1//5, 3//10, 1//2], [0.3f0, 0.4f0, 0.3f0], 2f0)) isa Float32

# deprecated, should throw an `ArgumentError` at some point
logpattern = (:warn, "support for empty collections will be removed since they do not represent proper probability distributions")
@test iszero(@test_logs logpattern @inferred(crossentropy(Float64[], Float64[])))
@test iszero(@test_logs logpattern @inferred(crossentropy(Int[], Int[])))
@test_throws DimensionMismatch crossentropy([1//2, 1//2], [0.3, 0.2, 0.5])
@test_throws ArgumentError @inferred(crossentropy(Float64[], Float64[]))
@test_throws ArgumentError @inferred(crossentropy(Int[], Int[]))

##### KL divergence
@test @inferred(kldivergence([0.2, 0.3, 0.5], [0.3, 0.4, 0.3])) ≈ 0.08801516852582819
Expand All @@ -228,10 +227,9 @@ logpattern = (:warn, "support for empty collections will be removed since they d
@test @inferred(kldivergence([1//5, 3//10, 1//2], [0.3f0, 0.4f0, 0.3f0], 2f0)) isa Float32
@test iszero(@inferred(kldivergence([0, 1], [0f0, 1f0])))

# deprecated, should throw an `ArgumentError` at some point
logpattern = (:warn, "support for empty collections will be removed since they do not represent proper probability distributions")
@test iszero(@test_logs logpattern @inferred(kldivergence(Float64[], Float64[])))
@test iszero(@test_logs logpattern @inferred(kldivergence(Int[], Int[])))
@test_throws DimensionMismatch kldivergence([1//2, 1//2], [0.3, 0.2, 0.5])
@test_throws ArgumentError @inferred(kldivergence(Float64[], Float64[]))
@test_throws ArgumentError @inferred(kldivergence(Int[], Int[]))

##### summarystats

Expand Down