Last active
December 9, 2021 21:56
-
-
Save cossio/5797ad35aeeb7ccbebab4d90a25026a8 to your computer and use it in GitHub Desktop.
Attempt at making an online version of softmax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function softmax_online(x::AbstractArray) # seems slower than softmax | |
max_ = fill(convert(eltype(x), -Inf), 1, tail(size(x))...) | |
sum_ = zeros(eltype(x), 1, tail(size(x))...) | |
for j in CartesianIndices(tail(size(x))), i = 1:size(x,1) | |
if x[i,j] > max_[1,j] | |
sum_[1,j] *= exp(max_[1,j] - x[i,j]) | |
max_[1,j] = x[i,j] | |
end | |
sum_[1,j] += exp(x[i,j] - max_[1,j]) | |
end | |
exp_ = exp.(x .- max_) | |
return exp_ ./ sum_ | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment