Skip to content

Instantly share code, notes, and snippets.

@longouyang
Created May 22, 2017 22:55
Show Gist options
  • Save longouyang/fa24cdde42bf1945660419df23954f76 to your computer and use it in GitHub Desktop.
Save longouyang/fa24cdde42bf1945660419df23954f76 to your computer and use it in GitHub Desktop.
gradient ascent in webppl
var f = function(theta) {
//return 30 * Math.log(theta) + 20 * Math.log(1-theta)
return Binomial({p: theta, n: 30}).score(14)
}
globalStore["x"] = 0.001;
repeat(100,
function() {
var x = globalStore["x"],
xL = ad.lift(x);
var yL = f(xL);
yL.backprop();
var dxL = ad.derivative(xL);
console.log(x, ad.value(yL), dxL);
var newX = (dxL > 0 ? x + 0.01 : x - 0.01);
globalStore["x"] = newX;
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment