Ejemplo n.º 1
0
def test_em():
    
    tstamps, Trace, previous_stamps, Count_zh, Count_sz, count_h, count_z, \
            prob_topics_aux, Theta_zh, Psi_sz, hyper2id, source2id = \
            dataio.initialize_trace(files.SIZE10, 2, 10)
    kernel = NoopKernel()
    kernel.build(Trace.shape[0], Count_zh.shape[1], np.zeros(0, dtype='d'))
    
    alpha_zh = .1
    beta_zs = .1
    
    assert (Theta_zh == 0).all()
    assert (Psi_sz == 0).all()
    
    old_Count_zh = Count_zh.copy()
    old_Count_sz = Count_sz.copy()
    old_count_h = count_h.copy()
    old_count_z = count_z.copy()

    _learn.em(tstamps, Trace, previous_stamps, Count_zh, Count_sz, count_h, \
            count_z, alpha_zh, beta_zs, prob_topics_aux, Theta_zh, Psi_sz, \
            10, 2, kernel)
    
    assert (Theta_zh > 0).sum() > 0
    assert (Psi_sz > 0).sum() > 0
    
    assert_almost_equal(1, Theta_zh.sum(axis=0))
    assert_almost_equal(1, Psi_sz.sum(axis=0))

    assert (old_Count_zh != Count_zh).any()
    assert (old_Count_sz != Count_sz).any()
    
    assert (old_count_h == count_h).all() #the count_h should not change
Ejemplo n.º 2
0
def test_full_learn_null():
    kernel = NoopKernel()
    kernel.build(1, 20, np.zeros(0, dtype='d'))
    rv = learn.fit(files.SIZE1K, 20, .1, .1, kernel, \
            np.zeros(0, dtype='d'), 1000, 200)
    
    Count_zh = rv['Count_zh']
    Count_sz = rv['Count_sz'] 
    
    assert_equal(Count_zh.sum(), 1000)
    assert_equal(Count_sz.sum(), 2000) 
    
    count_h = rv['count_h']
    count_z = rv['count_z']

    assert_equal(count_h.sum(), 1000)
    assert_equal(count_z.sum(), 2000)

    assert rv['assign'].shape == (1000, )

    Theta_zh = rv['Theta_zh']
    Psi_sz = rv['Psi_sz']
    
    assert_almost_equal(1, Theta_zh.sum(axis=0))
    assert_almost_equal(1, Psi_sz.sum(axis=0))
Ejemplo n.º 3
0
def test_estep():
    tstamps, Trace, previous_stamps, Count_zh, Count_sz, count_h, count_z, \
            prob_topics_aux, Theta_zh, Psi_sz, hyper2id, source2id = \
            dataio.initialize_trace(files.SIZE10, 2, 10)
    kernel = NoopKernel()
    kernel.build(Trace.shape[0], Count_zh.shape[1], np.zeros(0, dtype='d'))
    
    alpha_zh = .1
    beta_zs = .1

    assert_equal(Count_zh.sum(), 10)
    assert_equal(Count_sz.sum(), 20)
    
    assert_equal(count_h[0], 4)
    assert_equal(count_h[1], 4)
    assert_equal(count_h[2], 2)
    
    new_state = _learn._e_step(tstamps, Trace, previous_stamps, Count_zh, \
            Count_sz, count_h, count_z, alpha_zh, beta_zs, prob_topics_aux, \
            kernel)

    assert_equal(count_h[0], 4)
    assert_equal(count_h[1], 4)
    assert_equal(count_h[2], 2)
    
    assert_equal(Count_zh.sum(), 10)
    assert_equal(Count_sz.sum(), 20)
Ejemplo n.º 4
0
def test_em():

    tstamps, Trace, previous_stamps, Count_zh, Count_sz, count_h, count_z, \
            prob_topics_aux, Theta_zh, Psi_sz, hyper2id, source2id = \
            dataio.initialize_trace(files.SIZE10, 2, 10)
    kernel = NoopKernel()
    kernel.build(Trace.shape[0], Count_zh.shape[1], np.zeros(0, dtype='d'))

    alpha_zh = .1
    beta_zs = .1

    assert (Theta_zh == 0).all()
    assert (Psi_sz == 0).all()

    old_Count_zh = Count_zh.copy()
    old_Count_sz = Count_sz.copy()
    old_count_h = count_h.copy()
    old_count_z = count_z.copy()

    _learn.em(tstamps, Trace, previous_stamps, Count_zh, Count_sz, count_h, \
            count_z, alpha_zh, beta_zs, prob_topics_aux, Theta_zh, Psi_sz, \
            10, 2, kernel)

    assert (Theta_zh > 0).sum() > 0
    assert (Psi_sz > 0).sum() > 0

    assert_almost_equal(1, Theta_zh.sum(axis=0))
    assert_almost_equal(1, Psi_sz.sum(axis=0))

    assert (old_Count_zh != Count_zh).any()
    assert (old_Count_sz != Count_sz).any()

    assert (old_count_h == count_h).all()  #the count_h should not change
Ejemplo n.º 5
0
def test_estep():
    tstamps, Trace, previous_stamps, Count_zh, Count_sz, count_h, count_z, \
            prob_topics_aux, Theta_zh, Psi_sz, hyper2id, source2id = \
            dataio.initialize_trace(files.SIZE10, 2, 10)
    kernel = NoopKernel()
    kernel.build(Trace.shape[0], Count_zh.shape[1], np.zeros(0, dtype='d'))

    alpha_zh = .1
    beta_zs = .1

    assert_equal(Count_zh.sum(), 10)
    assert_equal(Count_sz.sum(), 20)

    assert_equal(count_h[0], 4)
    assert_equal(count_h[1], 4)
    assert_equal(count_h[2], 2)

    new_state = _learn._e_step(tstamps, Trace, previous_stamps, Count_zh, \
            Count_sz, count_h, count_z, alpha_zh, beta_zs, prob_topics_aux, \
            kernel)

    assert_equal(count_h[0], 4)
    assert_equal(count_h[1], 4)
    assert_equal(count_h[2], 2)

    assert_equal(Count_zh.sum(), 10)
    assert_equal(Count_sz.sum(), 20)
Ejemplo n.º 6
0
def test_sample():
    tstamps, Trace, previous_stamps, Count_zh, Count_sz, count_h, count_z, \
            prob_topics_aux, Theta_zh, Psi_sz, hyper2id, source2id = \
            dataio.initialize_trace(files.SIZE10, 2, 10)
    kernel = NoopKernel()
    kernel.build(Trace.shape[0], Count_zh.shape[1], np.zeros(0, dtype='d'))

    tstamp_idx = 3
    hyper = Trace[tstamp_idx, 0]
    source = Trace[tstamp_idx, 1]
    dest = Trace[tstamp_idx, 2]
    old_topic = Trace[tstamp_idx, 3]

    new_topic = _learn._sample(tstamp_idx, tstamps, Trace, \
            previous_stamps, Count_zh, Count_sz, count_h, \
            count_z, .1, .1, prob_topics_aux, kernel)
    
    assert new_topic <= 3
Ejemplo n.º 7
0
def test_sample():
    tstamps, Trace, previous_stamps, Count_zh, Count_sz, count_h, count_z, \
            prob_topics_aux, Theta_zh, Psi_sz, hyper2id, source2id = \
            dataio.initialize_trace(files.SIZE10, 2, 10)
    kernel = NoopKernel()
    kernel.build(Trace.shape[0], Count_zh.shape[1], np.zeros(0, dtype='d'))

    tstamp_idx = 3
    hyper = Trace[tstamp_idx, 0]
    source = Trace[tstamp_idx, 1]
    dest = Trace[tstamp_idx, 2]
    old_topic = Trace[tstamp_idx, 3]

    new_topic = _learn._sample(tstamp_idx, tstamps, Trace, \
            previous_stamps, Count_zh, Count_sz, count_h, \
            count_z, .1, .1, prob_topics_aux, kernel)

    assert new_topic <= 3