def test_Z2():
  """ Test of implementation 2. """
  traj = simple_traj2()
  theta = np.array([0.0, -1.0])
  choices = [0, 1]
  elts = LearningElements(traj, theta, choices)
  elts.computeLogZ()
  elts_ref = LearningElementsRef(traj, theta, choices)
  elts_ref.computeLogZ()
  assert(within(elts.logZ, elts_ref.logZ, 1e-5))
def test_grad_Z2():
  """ Test of implementation 2 of gradient. """
  traj = simple_traj2()
  theta = np.array([0.0, -1.0])
  choices = [0, 1]
  elts = LearningElements(traj, theta, choices)
  elts.computeGradientLogZ()
  elts_ref = LearningElementsRef(traj, theta, choices)
  elts_ref.computeGradientLogZ()
  g = elts.grad_logZ
  g_ref = elts_ref.grad_logZ
  assert(np.abs(g - g_ref).max() < 1e-3), (g, g_ref)
def test_hess_traj4_1():
  """ test_hess_traj4_1 """
  traj = simple_traj4()
  theta = np.array([0.0, -1.0])
  choices = [1, 0, 2]
  elts = LearningElements(traj, theta, choices)
  elts.computeHessianLogZ()
  elts_ref = LearningElementsRef(traj, theta, choices)
  elts_ref.computeHessianLogZ()
  h = elts.hess_logZ
  h_ref = elts_ref.hess_logZ
  assert(np.abs(h - h_ref).max() < 1e-3), (h, h_ref)
def test_traj_1_2():
  """ test_traj_1_2 """
  traj = simple_traj1()
  theta = np.array([1.0, -1.0])
  choices = [1, 0]
  elts = LearningElementsSecure(traj, theta, choices)
  elts.computeLogZ()
  elts_ref = LearningElementsRef(traj, theta, choices)
  elts_ref.computeLogZ()
  assert(within(elts.Z, elts_ref.Z, 1e-5)), (elts.Z, elts_ref.Z, 1e-5)
  assert(within(elts.logZ, elts_ref.logZ, 1e-5)), \
    (elts.logZ, elts_ref.logZ, 1e-5)
def test_traj_5_1():
  """ test_traj_5_1 """
  traj = simple_traj5()
  theta = np.array([-1.0])
  choices = [1, 0, 2]
  elts = LearningElements(traj, theta, choices)
  elts.computeLogZ()
  elts_ref = LearningElementsRef(traj, theta, choices)
  elts_ref.computeLogZ()
  assert(within(elts.Z, elts_ref.Z, 1e-5)), (elts.Z, elts_ref.Z, 1e-5)
  assert(within(elts.logZ, elts_ref.logZ, 1e-5)), \
    (elts.logZ, elts_ref.logZ, 1e-5)
def test_grad_traj5_2():
  """ test_grad_traj5_2
  Test of implementation 1 of gradient. """
  traj = simple_traj5()
  theta = np.array([-1.0])
  choices = [1, 0, 2]
  elts = LearningElementsSecure(traj, theta, choices)
  elts.computeGradientLogZ()
  elts_ref = LearningElementsRef(traj, theta, choices)
  elts_ref.computeGradientLogZ()
  g = elts.grad_logZ
  g_ref = elts_ref.grad_logZ
  assert(np.abs(g - g_ref).max() < 1e-3), (g, g_ref)
def test_hess_traj8_2():
  """ test_hess_traj8_2 """
  traj = simple_traj8()
  theta = np.array([0.0])
  choices = [1, 0]
  elts = LearningElementsSecure(traj, theta, choices)
  elts.computeHessianLogZ()
  elts_ref = LearningElementsRef(traj, theta, choices)
  elts_ref.computeHessianLogZ()
  h = elts.hess_logZ
  h_ref = elts_ref.hess_logZ
  assert(np.abs(h - h_ref).max() < 1e-3), \
    (h, h_ref, elts.hess_Z, elts_ref.hess_Z, \
     elts.grad_Z, elts_ref.grad_Z, elts.Z, elts_ref.Z,)
Exemple #8
0
 def inner(theta):
     """ Returned closure. """
     y = 0.0
     g = np.zeros_like(theta)
     n = len(theta)
     h = np.zeros((n, n))
     for (traj, choice) in traj_choices:
         elts = LearningElementsRef(traj, theta, choice)
         elts.computeValue()
         y += elts.logValue
         elts.computeGradientValue()
         g += elts.grad_logValue
         elts.computeHessianValue()
         h += elts.hess_logValue
     return (y, g, h)
 def inner(theta):
   """ Returned closure. """
   y = 0.0
   g = np.zeros_like(theta)
   n = len(theta)
   h = np.zeros((n, n))
   for (traj, choice) in traj_choices:
     elts = LearningElementsRef(traj, theta, choice)
     elts.computeValue()
     y += elts.logValue
     elts.computeGradientValue()
     g += elts.grad_logValue
     elts.computeHessianValue()
     h += elts.hess_logValue
   return (y, g, h)
 def res(theta):
   """ inner closure. """
   elts = LearningElementsRef(traj, theta, choices)
   elts.computeGradientLogZ()
   return elts.grad_logZ
 def res(theta):
   """ Inner closure. """
   elts = LearningElementsRef(traj, theta, choices)
   elts.computeLogZ()
   return elts.Z
 def res(theta):
   """ inner closure. """
   elts = LearningElementsRef(traj, theta, choices)
   elts.computeHessianLogZ()
   return elts.hess_Z