Skip to content

Instantly share code, notes, and snippets.

@cossio
Last active December 9, 2021 21:56
Show Gist options
  • Save cossio/5797ad35aeeb7ccbebab4d90a25026a8 to your computer and use it in GitHub Desktop.
Save cossio/5797ad35aeeb7ccbebab4d90a25026a8 to your computer and use it in GitHub Desktop.
Attempt at making an online version of softmax
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