def test_Z3(): """ Test of implementation 2. """ traj = simple_traj10() theta = np.array([500.0, -100.0]) weights = [[0.1, 0.9], [0.9, 0.1]] elts = LearningElementsSecure(traj, theta, weights=weights) elts.computeLogZ()
def test_hess_traj1_2(): """ test_hess_traj1_2 """ traj = simple_traj1() theta = np.array([0.0, -1.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)
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_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_traj5_2(): """ test_hess_traj5_2 """ traj = simple_traj5() theta = np.array([-1.0]) choices = [1, 0, 2] 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,)
def inner(theta): """ Returned closure. """ y = 0.0 g = np.zeros_like(theta) n = len(theta) h = np.zeros((n, n)) for (traj, weights) in traj_estims: elts = LearningElements1(traj, theta, weights=weights) elts.computeValue() y += elts.logValue elts.computeGradientValue() g += elts.grad_logValue elts.computeHessianValue() h += elts.hess_logValue return (y, g, h)