Skip to content

Commit

Permalink
Added fix to LogSoftMax to handle large values without going to Inf
Browse files Browse the repository at this point in the history
  • Loading branch information
abeschneider committed Jan 8, 2017
1 parent 0d6618e commit bca9ec3
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Sources/Tensor/tensorops.swift
Original file line number Diff line number Diff line change
Expand Up @@ -968,8 +968,10 @@ public func logsoftmax<S:Storage>
// log[ exp(input) / sum(exp(input)) ]
// = log(exp(input)) - log(sum(exp(input)))
// = input - log(sum(exp(input)))
let s = Tensor<S>([sum(exp(input))])
let logsum = log(s)
// maxInput scales all values so we don't get Inf due to the exp
let maxInput = Tensor<S>([max(input)])
let s = Tensor<S>([sum(exp(input - maxInput))])
let logsum = maxInput + log(s)
sub(input, logsum, result: result)
}

0 comments on commit bca9ec3

Please sign in to comment.