Skip to content

Commit

Permalink
Allow general indices in softmax! (#17)
Browse files Browse the repository at this point in the history
Co-authored-by: Milan Bouchet-Valat <[email protected]>
  • Loading branch information
devmotion and nalimilan authored Apr 24, 2021
1 parent 8f65aef commit 9e5de81
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 26 deletions.
8 changes: 5 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
name = "LogExpFunctions"
uuid = "2ab3a3ac-af41-5b50-aa03-7779005ae688"
authors = ["StatsFun.jl contributors, Tamas K. Papp <[email protected]>"]
version = "0.2.2"
version = "0.2.3"

[deps]
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

[compat]
julia = "1"
DocStringExtensions = "0.8"
julia = "1"

[extras]
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
test = ["OffsetArrays", "Test"]
1 change: 1 addition & 0 deletions src/LogExpFunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module LogExpFunctions

using DocStringExtensions: SIGNATURES
using Base: Math.@horner, @irrational
import LinearAlgebra

export loghalf, logtwo, logπ, log2π, log4π
export xlogx, xlogy, logistic, logit, log1psq, log1pexp, log1mexp, log2mexp, logexpm1,
Expand Down
13 changes: 4 additions & 9 deletions src/basicfuns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,12 @@ That is, `r` is overwritten with `exp.(x)`, normalized to sum to 1.
See the [Wikipedia entry](https://en.wikipedia.org/wiki/Softmax_function)
"""
function softmax!(r::AbstractArray{<:Real}, x::AbstractArray{<:Real})
n = length(x)
length(r) == n || throw(DimensionMismatch("Inconsistent array lengths."))
length(r) == length(x) || throw(DimensionMismatch("inconsistent array lengths"))
u = maximum(x)
s = zero(eltype(r))
@inbounds for i = 1:n
s += (r[i] = exp(x[i] - u))
end
invs = inv(s)
@inbounds for i = 1:n
r[i] *= invs
map!(r, x) do xi
return exp(xi - u)
end
LinearAlgebra.lmul!(inv(sum(r)), r)
return r
end

Expand Down
41 changes: 28 additions & 13 deletions test/basicfuns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,22 +149,37 @@ end
@testset "softmax" begin
x = [1.0, 2.0, 3.0]
r = exp.(x) ./ sum(exp.(x))
@test softmax(x) r

# in-place versions
for T in (Float32, Float64)
s = Vector{T}(undef, 3)
softmax!(s, x)
@test s r

s = Matrix{T}(undef, 1, 3)
softmax!(s, x)
@test s permutedims(r)
end
softmax!(x)
@test x r

x = [1, 2, 3]
r = exp.(x) ./ sum(exp.(x))
@test softmax(x) r
@test eltype(softmax(x)) == Float64


for (S, T) in ((Int, Float64), (Float64, Float64), (Float32, Float32))
x = S[1, 2, 3]
s = softmax(x)
@test s r
@test eltype(s) === T
end

x = [1//2, 2//3, 3//4]
r = exp.(x) ./ sum(exp.(x))
@test softmax(x) r
@test eltype(softmax(x)) == Float64
s = softmax(x)
@test s r
@test eltype(s) === Float64

x = Float32[1, 2, 3]
r = exp.(x) ./ sum(exp.(x))
@test softmax(x) r
@test eltype(softmax(x)) == Float32
# non-standard indices: #12
x = OffsetArray(1:3, -2:0)
s = softmax(x)
@test s isa OffsetArray{Float64}
@test axes(s, 1) == OffsetArrays.IdOffsetRange(-2:0)
@test collect(s) softmax(1:3)
end
4 changes: 3 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using LogExpFunctions, Test
using LogExpFunctions
using OffsetArrays
using Test

include("basicfuns.jl")

2 comments on commit 9e5de81

@devmotion
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/35200

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.3 -m "<description of version>" 9e5de8108a264fc2c1695edbad1657db0b46ee40
git push origin v0.2.3

Please sign in to comment.