def test_opt0_traj9_1():
  """ test_opt0_traj9_1 """
  traj = simple_traj9()
  start_theta = np.array([-1.0])
  choices = [0, 0]
  traj_choices = [(traj, choices)]
  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_0bis = trajs_obj_fun_0bis(traj_choices)
  obj_fun_ref = trajs_obj_fun_ref(traj_choices)
  max_iters = 5
  (theta_ref, ys_ref) = \
    optimize_function(obj_fun_ref, start_theta, max_iters, regularizer=1e-4)
  print ys_ref, theta_ref
  (theta_0bis, ys_0bis) = \
    optimize_function(obj_fun_0bis, start_theta, max_iters, regularizer=1e-4)
  (theta_0, ys_0) = \
    optimize_function(obj_fun_0, start_theta, max_iters, regularizer=1e-4)
  (theta_1, ys_1) = \
    optimize_function(obj_fun_1, start_theta, max_iters, regularizer=1e-4)
  (theta_2, ys_2) = \
    optimize_function(obj_fun_2, start_theta, max_iters, regularizer=1e-4)
  assert(np.abs(theta_0bis - theta_ref).max() < 1e-3), (theta_0bis, theta_ref)
  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 within(ys_0bis[0], ys_ref[0], 1e-3), \
    (theta_ref, ys_ref, theta_0bis, ys_0bis)
  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)
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)
Example #3
0
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)
Example #4
0
def test_opt0_traj9_1():
    """ test_opt0_traj9_1 """
    traj = simple_traj9()
    start_theta = np.array([-1.0])
    choices = [0, 0]
    traj_choices = [(traj, choices)]
    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_0bis = trajs_obj_fun_0bis(traj_choices)
    obj_fun_ref = trajs_obj_fun_ref(traj_choices)
    max_iters = 5
    (theta_ref, ys_ref) = \
      optimize_function(obj_fun_ref, start_theta, max_iters, regularizer=1e-4)
    print ys_ref, theta_ref
    (theta_0bis, ys_0bis) = \
      optimize_function(obj_fun_0bis, start_theta, max_iters, regularizer=1e-4)
    (theta_0, ys_0) = \
      optimize_function(obj_fun_0, start_theta, max_iters, regularizer=1e-4)
    (theta_1, ys_1) = \
      optimize_function(obj_fun_1, start_theta, max_iters, regularizer=1e-4)
    (theta_2, ys_2) = \
      optimize_function(obj_fun_2, start_theta, max_iters, regularizer=1e-4)
    assert (np.abs(theta_0bis - theta_ref).max() < 1e-3), (theta_0bis,
                                                           theta_ref)
    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 within(ys_0bis[0], ys_ref[0], 1e-3), \
      (theta_ref, ys_ref, theta_0bis, ys_0bis)
    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)
def test_opt0_traj5_1():
  """ test_opt0_traj5_1 """
  traj = simple_traj5()
  start_theta = np.array([-1.0])
  choices = [1, 0, 2]
  traj_choices = [(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)
  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_ref, ys_ref) = optimize_function(obj_fun_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 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)
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_1():
  """ Basic test for the optimizer. """
  start = np.zeros(2)
  opt_fun = wrap(fun1)
  (x, ys) = optimize_function(opt_fun, start, 10)
  assert(np.abs(x-np.ones_like(x)).max() < 1e-3)
  assert ys[-1] >= -1e-3
  assert ys[-1] <= 0
  assert np.abs(start).max() == 0, (start, x)
Example #8
0
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))
Example #9
0
def test_1():
    """ Basic test for the optimizer. """
    start = np.zeros(2)
    opt_fun = wrap(fun1)
    (x, ys) = optimize_function(opt_fun, start, 10)
    assert (np.abs(x - np.ones_like(x)).max() < 1e-3)
    assert ys[-1] >= -1e-3
    assert ys[-1] <= 0
    assert np.abs(start).max() == 0, (start, x)
Example #10
0
def test_opt0_traj5_1():
    """ test_opt0_traj5_1 """
    traj = simple_traj5()
    start_theta = np.array([-1.0])
    choices = [1, 0, 2]
    traj_choices = [(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)
    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_ref, ys_ref) = optimize_function(obj_fun_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 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)
def learn_em(opt_function_provider, trajs, start_value, \
             max_iters=10, max_inner_iters=5):
  """ Learn EM function.
  
  Returns the history of the progression: list of:
  - log likelihood
  - learned parameter
  
  Arguments:
  - opt_function_provider: traj_estims -> (optimizable function)
  - trajs: a set of trajectories
  - start_value: numpy array, start vector
  - max_iters: maximum number of iterations
  """
  theta = np.array(start_value)
  history = []
  for step in range(max_iters):
    traj_estims = get_traj_estims(trajs, theta)
    opt_fun = opt_function_provider(traj_estims)
    (theta1, ys) = optimize_function(opt_fun, theta, max_inner_iters)
    print 'Epoch %i : %f' % (step, ys[-1])
    history.append((ys[-1], theta1))
    theta = theta1
  return history
Example #12
0
def learn_em(opt_function_provider, trajs, start_value, \
             max_iters=10, max_inner_iters=5):
    """ Learn EM function.
  
  Returns the history of the progression: list of:
  - log likelihood
  - learned parameter
  
  Arguments:
  - opt_function_provider: traj_estims -> (optimizable function)
  - trajs: a set of trajectories
  - start_value: numpy array, start vector
  - max_iters: maximum number of iterations
  """
    theta = np.array(start_value)
    history = []
    for step in range(max_iters):
        traj_estims = get_traj_estims(trajs, theta)
        opt_fun = opt_function_provider(traj_estims)
        (theta1, ys) = optimize_function(opt_fun, theta, max_inner_iters)
        print 'Epoch %i : %f' % (step, ys[-1])
        history.append((ys[-1], theta1))
        theta = theta1
    return history