Exemplo n.º 1
0
    def test_draw_samples(self):
        np.random.seed(0)
        samples_1_np = np.random.randn(5)
        samples_1 = mx.nd.array(samples_1_np)
        samples_2_np = np.random.randn(50)
        samples_2 = mx.nd.array(samples_2_np)
        m = Model()
        v = Variable(shape=(1,))
        m.v2 = Normal.define_variable(mean=v, variance=mx.nd.array([1]), rand_gen=MockMXNetRandomGenerator(samples_1))
        m.v3 = Normal.define_variable(mean=m.v2, variance=mx.nd.array([0.1]), shape=(10,), rand_gen=MockMXNetRandomGenerator(samples_2))
        np.random.seed(0)
        v_np =np.random.rand(1)
        v_mx = mx.nd.array(v_np)

        v_rt = add_sample_dimension(mx.nd, v_mx)
        variance = m.v2.factor.variance
        variance2 = m.v3.factor.variance
        variance_rt = add_sample_dimension(mx.nd, variance.constant)
        variance2_rt = add_sample_dimension(mx.nd, variance2.constant)
        samples = m.draw_samples(F=mx.nd, num_samples=5, targets=[m.v3.uuid],
        variables={v.uuid: v_rt, variance.uuid: variance_rt, variance2.uuid: variance2_rt})[0]

        samples_np = v_np + samples_1_np[:, None] + np.sqrt(0.1)*samples_2_np.reshape(5,10)

        assert array_has_samples(mx.nd, samples) and get_num_samples(mx.nd, samples)==5
        assert np.allclose(samples.asnumpy(), samples_np)
Exemplo n.º 2
0
    def test_compute_log_prob(self):
        m = Model()
        v = Variable(shape=(1,))
        m.v2 = Normal.define_variable(mean=v, variance=mx.nd.array([1]))
        m.v3 = Normal.define_variable(mean=m.v2, variance=mx.nd.array([1]), shape=(10,))
        np.random.seed(0)
        v_mx = mx.nd.array(np.random.randn(1))
        v2_mx = mx.nd.array(np.random.randn(1))
        v3_mx = mx.nd.array(np.random.randn(10))

        v_rt = add_sample_dimension(mx.nd, v_mx)
        v2_rt = add_sample_dimension(mx.nd, v2_mx)
        v3_rt = add_sample_dimension(mx.nd, v3_mx)
        variance = m.v2.factor.variance
        variance2 = m.v3.factor.variance
        variance_rt = add_sample_dimension(mx.nd, variance.constant)
        variance2_rt = add_sample_dimension(mx.nd, variance2.constant)
        log_pdf = m.log_pdf(F=mx.nd, variables={m.v2.uuid: v2_rt, m.v3.uuid:v3_rt, variance.uuid: variance_rt, variance2.uuid: variance2_rt, v.uuid: v_rt}).asscalar()

        variables = {m.v2.factor.mean.uuid: v_rt, m.v2.factor.variance.uuid: variance_rt, m.v2.factor.random_variable.uuid: v2_rt}
        log_pdf_1 = mx.nd.sum(m.v2.factor.log_pdf(F=mx.nd, variables=variables))
        variables = {m.v3.factor.mean.uuid: v2_rt, m.v3.factor.variance.uuid: variance2_rt, m.v3.factor.random_variable.uuid: v3_rt}
        log_pdf_2 = mx.nd.sum(m.v3.factor.log_pdf(F=mx.nd, variables=variables))

        assert log_pdf == (log_pdf_1 + log_pdf_2).asscalar()