Esempio 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)
Esempio 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()
    def _make_gluon_function_evaluation_rand_param(self, dtype, broadcastable):
        class Dot(HybridBlock):
            def __init__(self, params=None, prefix=None):
                super(Dot, self).__init__(params=params, prefix=prefix)
                with self.name_scope():
                    self.const = self.params.get('const',
                                                 shape=(1, ),
                                                 dtype=dtype,
                                                 init=Zero())

            def hybrid_forward(self, F, a, b, const):
                return F.broadcast_add(F.linalg.gemm2(a, b), const)

        dot = Dot(prefix='dot_')
        dot.initialize()
        dot.hybridize()
        print(dot.collect_params())
        func_wrapper = MXFusionGluonFunction(dot,
                                             1,
                                             dtype=dtype,
                                             broadcastable=broadcastable)
        from mxfusion.components.distributions.normal import Normal
        rand_var = Normal.define_variable(shape=(1, ))
        out = func_wrapper(Variable(shape=(3, 4)),
                           Variable(shape=(4, 5)),
                           dot_const=rand_var)

        return out.factor
Esempio n. 4
0
 def test_add_unresolved_components_distribution(self):
     v = mfc.Variable()
     m = mfc.Variable()
     f = Normal.define_variable(mean=m, variance=v)
     component_set = set((v, m, f))
     self.fg.f = f
     self.assertTrue(component_set <= set(self.fg.components_graph.nodes().keys()),
                     "Variables are all added to _components_graph {} {}".format(component_set, self.fg.components_graph.nodes().keys()))
     self.assertTrue(set((v.uuid, m.uuid, f.uuid)) <= self.fg.components.keys(), "Variable is added to _components dict. {} {}".format(v.uuid, self.fg.components))
Esempio n. 5
0
 def make_simple_model(self):
     m = Model()
     mean = Variable()
     variance = Variable()
     m.r = Normal.define_variable(mean=mean, variance=variance)
     return m