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

logsumexp for streaming data #91

Closed
cossio opened this issue Feb 18, 2020 · 2 comments · Fixed by #97
Closed

logsumexp for streaming data #91

cossio opened this issue Feb 18, 2020 · 2 comments · Fixed by #97

Comments

@cossio
Copy link
Contributor

cossio commented Feb 18, 2020

Consider adding a version of logsumexp for streaming data. I found this here:

http://www.nowozin.net/sebastian/blog/streaming-log-sum-exp-computation.html

which includes the following Julia code:

function logsumexp_stream(X)
    alpha = -Inf
    r = 0.0
    for x = X
        if x <= alpha
            r += exp(x - alpha)
        else
            r *= exp(alpha - x)
            r += 1.0
            alpha = x
        end
    end
    log(r) + alpha
end

It has the advantage that a single pass over the data is required.

Pinging @tpapp in case this should go into https://github.com/tpapp/LogExpFunctions.jl.

I can make a PR myself if people agree into adding this.

@cossio cossio changed the title logsumexp for stream data logsumexp for streaming data Feb 18, 2020
@nalimilan
Copy link
Member

Interesting. Have you benchmarked the two approaches?

@cossio
Copy link
Contributor Author

cossio commented Feb 28, 2020

@nalimilan The original blog post has some benchmarks:

n = 10_000_000
X = 500.0*randn(n)

julia> @btime logsumexp_batch(X)
92.695 ms (3 allocations: 76.29 MiB)
2600.631695542992

julia> @btime log(sum(exp.(X)))
136.538 ms (6 allocations: 76.29 MiB)
Inf

julia> @btime logsumexp_stream(X)
29.611 ms (1 allocation: 16 bytes)
2600.631695542992

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants