Skip to content

Instantly share code, notes, and snippets.

@sakatam
Created May 12, 2014 06:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sakatam/30dcdb6d7111c9cbe2cc to your computer and use it in GitHub Desktop.
Save sakatam/30dcdb6d7111c9cbe2cc to your computer and use it in GitHub Desktop.
(simplified) Softmax Annealing in CoffeeScript
# 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