Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2015
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. import utils as u
  2. import numpy as np
  3. from numpy.random import random as rand
  4.  
  5. # Hyperparameters for the distribution of mu
  6. mu_hyp = 0.0
  7. sigma_hyp = 1.0
  8.  
  9. # Hyperparameters for the distribution of sigma
  10. k_hyp = 1.0
  11. theta_hyp = 1.0
  12.  
  13. def draw_mu():
  14. return np.random.normal(loc=mu_hyp, scale=sigma_hyp)
  15. @u.attach_to(draw_mu)
  16. def logassess(m):
  17. return u.logassess_normal(m, mu_hyp, sigma_hyp)
  18.  
  19. def draw_sigma():
  20. return np.random.gamma(shape=k_hyp, scale=theta_hyp)
  21. @u.attach_to(draw_sigma)
  22. def logassess(s):
  23. return u.logassess_gamma(s, k_hyp, theta_hyp)
  24.  
  25. # Parametric form of the learned function
  26. def f_parametric(x, mu, sigma):
  27. return u.logassess_normal(x, mu, sigma)
  28.  
  29. # Parameters of the true function
  30. mu_true = draw_mu()
  31. sigma_true = draw_sigma()
  32. print "mu_true = %.2f, sigma_true = %.2f" % (mu_true, sigma_true)
  33. def f_true(x):
  34. f_true.count += 1
  35. return f_parametric(x, mu_true, sigma_true)
  36. f_true.count = 0
  37.  
  38. def logcost(x, approx_fx):
  39. COST_STDEV = 1.0
  40. return u.logassess_normal(f_true(x) - approx_fx, 0.0, COST_STDEV)
  41.  
  42. def sample_conditional_hparams(Xseen, Yseen, params_initial):
  43. # MH sampler
  44. def logassess_prior(mu, sigma):
  45. return draw_mu.logassess(mu) + draw_sigma.logassess(sigma)
  46. def local_ll(mu, sigma):
  47. return sum(logcost(x,y) for (x,y) in zip(Xseen, Yseen))
  48.  
  49. params_current = params_initial
  50. def do_step():
  51. params_prop = (draw_mu(), draw_sigma())
  52. prop_score = (logassess_prior(*params_current)
  53. - logassess_prior(*params_prop)
  54. + local_ll(*params_prop)
  55. - local_ll(*params_current))
  56. accprob = min(1.0, np.exp(prop_score))
  57. if rand() < accprob:
  58. return params_prop
  59. else:
  60. return params_current
  61.  
  62. NUM_STEPS = 10
  63. for dummy in range(NUM_STEPS):
  64. params_current = do_step()
  65. return params_current
  66.  
  67. def search_for_argmax(f):
  68. # Grid search
  69. MIN_X = -20
  70. MAX_X = 20
  71. NUM_BINS = 1000
  72. xs = np.linspace(MIN_X, MAX_X, NUM_BINS)
  73. fvec = np.vectorize(f)
  74. i = np.argmax(fvec(xs))
  75. return xs[i]
  76.  
  77.  
  78. mut = [draw_mu()]
  79. sigmat = [draw_sigma()]
  80. Xseen = []
  81. Yseen = []
  82.  
  83. def not_yet_happy():
  84. not_yet_happy.count += 1
  85. if not_yet_happy.count % 100 == 0:
  86. print "not_yet_happy.count = %d" % (not_yet_happy.count,)
  87. return not_yet_happy.count <= 500
  88. not_yet_happy.count = 0
  89.  
  90. while not_yet_happy():
  91. curf = lambda x: f_parametric(x, mut[-1], sigmat[-1])
  92. x_newt = search_for_argmax(curf)
  93. Xseen.append(x_newt)
  94. Yseen.append(f_true(x_newt))
  95. (mu_new, sigma_new) = sample_conditional_hparams(Xseen, Yseen, (mut[-1], sigmat[-1]))
  96. mut.append(mu_new)
  97. sigmat.append(sigma_new)
  98.  
  99. print "Inferred mu = %.2f, sigma = %.2f" % (mut[-1], sigmat[-1])
  100. # print "(x,y) pairs seen:", np.vstack([Xseen, Yseen])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement