def test_opt0_traj4_1ref(): """ test_opt0_traj4_1 """ traj = simple_traj4() start_theta = np.array([0.0, 0.0]) choices = [1, 0, 2] estims = [np.array([0.0, 1.0]), np.array([1.0]), np.array([0.0, 0.0, 1.0])] traj_choices = [(traj, choices)] traj_estims = [(traj, estims)] obj_fun_2 = trajs_obj_fun_2(traj_choices) obj_fun_1 = trajs_obj_fun_1(traj_choices) obj_fun_0 = trajs_obj_fun_0(traj_choices) obj_fun_ref = trajs_obj_fun_ref(traj_choices) obj_fun_estim_ref = trajs_estim_obj_fun_ref(traj_estims) max_iters = 5 (theta_0, ys_0) = optimize_function(obj_fun_0, start_theta, max_iters) (theta_1, ys_1) = optimize_function(obj_fun_1, start_theta, max_iters) (theta_2, ys_2) = optimize_function(obj_fun_2, start_theta, max_iters) (theta_ref, ys_ref) = optimize_function(obj_fun_ref, start_theta, max_iters) (theta_estim_ref, ys_estim_ref) = optimize_function(obj_fun_estim_ref, \ start_theta, max_iters) assert(np.abs(theta_0 - theta_ref).max() < 1e-3), (theta_0, theta_ref) assert(np.abs(theta_1 - theta_ref).max() < 1e-3), (theta_1, theta_ref) assert(np.abs(theta_2 - theta_ref).max() < 1e-3), (theta_2, theta_ref) assert(np.abs(theta_estim_ref - theta_ref).max() < 1e-3), (theta_estim_ref, \ theta_ref) assert within(ys_0[0], ys_ref[0], 1e-3), (theta_ref, ys_ref, theta_0, ys_0) assert within(ys_1[0], ys_ref[0], 1e-3), (theta_ref, ys_ref, theta_1, ys_1) assert within(ys_2[0], ys_ref[0], 1e-3), (theta_ref, ys_ref, theta_2, ys_2) assert within(ys_estim_ref[0], ys_ref[0], 1e-3), (theta_ref, ys_ref, \ theta_estim_ref, \ ys_estim_ref)
def test_opt0_traj4_1ref(): """ test_opt0_traj4_1 """ traj = simple_traj4() start_theta = np.array([0.0, 0.0]) choices = [1, 0, 2] estims = [np.array([0.0, 1.0]), np.array([1.0]), np.array([0.0, 0.0, 1.0])] traj_choices = [(traj, choices)] traj_estims = [(traj, estims)] obj_fun_2 = trajs_obj_fun_2(traj_choices) obj_fun_1 = trajs_obj_fun_1(traj_choices) obj_fun_0 = trajs_obj_fun_0(traj_choices) obj_fun_ref = trajs_obj_fun_ref(traj_choices) obj_fun_estim_ref = trajs_estim_obj_fun_ref(traj_estims) max_iters = 5 (theta_0, ys_0) = optimize_function(obj_fun_0, start_theta, max_iters) (theta_1, ys_1) = optimize_function(obj_fun_1, start_theta, max_iters) (theta_2, ys_2) = optimize_function(obj_fun_2, start_theta, max_iters) (theta_ref, ys_ref) = optimize_function(obj_fun_ref, start_theta, max_iters) (theta_estim_ref, ys_estim_ref) = optimize_function(obj_fun_estim_ref, \ start_theta, max_iters) assert (np.abs(theta_0 - theta_ref).max() < 1e-3), (theta_0, theta_ref) assert (np.abs(theta_1 - theta_ref).max() < 1e-3), (theta_1, theta_ref) assert (np.abs(theta_2 - theta_ref).max() < 1e-3), (theta_2, theta_ref) assert(np.abs(theta_estim_ref - theta_ref).max() < 1e-3), (theta_estim_ref, \ theta_ref) assert within(ys_0[0], ys_ref[0], 1e-3), (theta_ref, ys_ref, theta_0, ys_0) assert within(ys_1[0], ys_ref[0], 1e-3), (theta_ref, ys_ref, theta_1, ys_1) assert within(ys_2[0], ys_ref[0], 1e-3), (theta_ref, ys_ref, theta_2, ys_2) assert within(ys_estim_ref[0], ys_ref[0], 1e-3), (theta_ref, ys_ref, \ theta_estim_ref, \ ys_estim_ref)
def test_opt_traj4_1ref(): """ test_opt_traj4_1 """ traj = simple_traj4() start_theta = np.array([0.0, 0.0]) choices = [1, 0, 2] traj_choices = [(traj, choices)] obj_fun = trajs_obj_fun_ref(traj_choices) (theta, ys) = optimize_function(obj_fun, start_theta, max_iters=20) assert ys[0] <= ys[-1], (theta, ys, len(ys))
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_gradient_ref_traj4(): """ test_gradient_ref_traj4 Simple test to check that the reference implementation of the gradient is equal to a empirical definition based on logZ. """ traj = simple_traj4() choices = [1, 0, 2] fun = get_fun_ref(traj, choices) grad_fun = get_grad_ref(traj, choices) # Do not forget to define the vector as floating point numbers!!! theta = np.array([0.0, 0.0]) g_emp = compute_grad(fun, theta) g = grad_fun(theta) assert(np.abs(g - g_emp).max() < 1e-3), (g, g_emp)
def test_em_1(): """ Very simple test: we pick some trajectories and verify that the LL increases with EM. """ trajs = [simple_traj1(), simple_traj4(), simple_traj3()] theta_start = 0.1 * np.ones(2) history = learn_em(trajs_estim_obj_fun_1, trajs, theta_start) # (ll_end, theta_end) = history[-1] # Very simple check here: we verify the progression goes upward # the likelihood: for t in range(len(history)-1): (ll_1, _) = history[t] (ll_2, _) = history[t+1] assert ll_1 <= ll_2
def test_em_1(): """ Very simple test: we pick some trajectories and verify that the LL increases with EM. """ trajs = [simple_traj1(), simple_traj4(), simple_traj3()] theta_start = 0.1 * np.ones(2) history = learn_em(trajs_estim_obj_fun_1, trajs, theta_start) # (ll_end, theta_end) = history[-1] # Very simple check here: we verify the progression goes upward # the likelihood: for t in range(len(history) - 1): (ll_1, _) = history[t] (ll_2, _) = history[t + 1] assert ll_1 <= ll_2