Created
May 12, 2014 06:43
-
-
Save sakatam/30dcdb6d7111c9cbe2cc to your computer and use it in GitHub Desktop.
(simplified) Softmax Annealing in CoffeeScript
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
# https://github.com/johnmyleswhite/BanditsBook/blob/master/ruby/algorithms/softmax/annealing.rb | |
class AnnealingSoftmax | |
constructor: (@counts, @values) -> | |
selectArm: -> | |
t = (@_sum @counts) + 1 | |
temperature = 1 / Math.log(t + 0.0000001) | |
tmp_values = $.map @values, (value) -> Math.exp(value / temperature) | |
z = @_sum tmp_values | |
probs = $.map @values, (value) -> Math.exp(value / temperature) / z | |
@_categoricalDraw probs | |
_categoricalDraw: (probs) -> | |
z = Math.random() | |
cumProb = 0.0 | |
for prob, idx in probs | |
cumProb += prob | |
return idx if cumProb > z | |
probs.length - 1 | |
_sum = (arr) -> | |
total = 0.0 | |
total += i for i in arr | |
total |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment