def test_e_step(): old_ips = init_ips(M, K, alpha, docs) new_ips, phi = e_step(alpha, beta, docs) # very permissive test(not sure how to test it better): # make sure new_ips changes assert_true(np.abs(new_ips - old_ips).min() >= 1e-5) # make sure sum to one for m in xrange(phi.size): assert_array_almost_equal(phi[m].sum(axis=1), 1) # make sure the lower bound increases old_lb_val = lower_bound(old_ips, phi, alpha, beta, docs, V) new_ips, new_phi = e_step(alpha, beta, docs) new_lb_val = lower_bound(new_ips, new_phi, alpha, beta, docs, V) assert_true(new_lb_val >= old_lb_val)
def test_lower_bound(): ips = init_ips(M, K, alpha, docs) actual = lower_bound(ips, phi, alpha, beta, docs, V) # how to test if we don't want to calculate by hand # 1st, <= 0 assert_true(actual <= 0) # 2nd, after training several iterations # the value should be increasing # as our goal is to maximize the lower bound old_lb_val = actual alpha_, beta_ = alpha, beta ips_, phi_ = ips, phi for i in xrange(10): ips_, phi_ = e_step(alpha_, beta_, docs) alpha_, beta_ = m_step(ips_, phi_, alpha_, docs, V) new_lb_val = lower_bound(ips_, phi_, alpha_, beta_, docs, V) assert_true(new_lb_val >= old_lb_val) old_lb_val = new_lb_val