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_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 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 = LearningElements0(traj, theta, choice) elts.computeValue() y += elts.logValue elts.computeGradientValue() g += elts.grad_logValue elts.computeHessianValue() h += elts.hess_logValue return (y, g, h)