Example #1
0
def _test(shape, n):
    K = shape[-1]
    rv = Dirichlet(shape, alpha=tf.constant(1.0 / K, shape=shape))
    rv_sample = rv.sample(n)
    x = rv_sample.eval()
    x_tf = tf.constant(x, dtype=tf.float32)
    alpha = rv.alpha.eval()
    if len(shape) == 1:
        assert np.allclose(
            rv.log_prob_idx((), x_tf).eval(),
            dirichlet_logpdf_vec(x[:, :], alpha[:]))
    elif len(shape) == 2:
        for i in range(shape[0]):
            assert np.allclose(
                rv.log_prob_idx((i, ), x_tf).eval(),
                dirichlet_logpdf_vec(x[:, i, :], alpha[i, :]))
    else:
        assert False
def _test(shape, n):
    K = shape[-1]
    rv = Dirichlet(shape, alpha=tf.constant(1.0/K, shape=shape))
    rv_sample = rv.sample(n)    
    x = rv_sample.eval()
    x_tf = tf.constant(x, dtype=tf.float32)
    alpha = rv.alpha.eval()
    if len(shape) == 1:
        assert np.allclose(
            rv.log_prob_idx((), x_tf).eval(),
            dirichlet_logpdf_vec(x[:, :], alpha[:]))
    elif len(shape) == 2:
        for i in range(shape[0]):
            assert np.allclose(
                rv.log_prob_idx((i, ), x_tf).eval(),
                dirichlet_logpdf_vec(x[:, i, :], alpha[i, :]))
    else:
        assert False