Skip to content

Commit

Permalink
basic softmax gradient
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeInnes committed Aug 22, 2017
1 parent 19cbce5 commit 3fca9d0
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/activation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,12 @@ softmax!(out::AbstractVecOrMat, xs::AbstractVecOrMat) =
out .= exp.(xs) ./ sum(exp, xs, 1)

softmax!(xs) = softmax!(xs, xs)

softmax(xs) = softmax!(similar(xs), xs)

function ∇softmax!(out::AbstractVecOrMat, Δ::AbstractVecOrMat, xs::AbstractVecOrMat)
s = sum(exp, xs, 1)
out .= exp.(xs)./s.*.- sum.* exp.(xs), 1)./s)
end

∇softmax!(Δ, xs) = ∇softmax!(Δ, Δ, xs)
∇softmax(Δ, xs) = ∇softmax!(similar(Δ), Δ, xs)

0 comments on commit 3fca9d0

Please sign in to comment.