Exemplo n.º 1
0
    def test_with_samples(self):
        from mxfusion.common import config
        config.DEFAULT_DTYPE = 'float64'
        dtype = 'float64'

        D, X, Y, noise_var, lengthscale, variance = self.gen_data()

        m = Model()
        m.N = Variable()
        m.X = Normal.define_variable(mean=0, variance=1, shape=(m.N, 3))
        m.noise_var = Variable(transformation=PositiveTransformation(),
                               initial_value=mx.nd.array(noise_var,
                                                         dtype=dtype))
        kernel = RBF(input_dim=3,
                     ARD=True,
                     variance=mx.nd.array(variance, dtype=dtype),
                     lengthscale=mx.nd.array(lengthscale, dtype=dtype),
                     dtype=dtype)
        m.Y = GPRegression.define_variable(X=m.X,
                                           kernel=kernel,
                                           noise_var=m.noise_var,
                                           shape=(m.N, D))

        q = create_Gaussian_meanfield(model=m, observed=[m.Y])

        infr = GradBasedInference(
            inference_algorithm=StochasticVariationalInference(
                model=m, posterior=q, num_samples=10, observed=[m.Y]))
        infr.run(Y=mx.nd.array(Y, dtype='float64'),
                 max_iter=2,
                 learning_rate=0.1,
                 verbose=True)

        infr2 = Inference(
            ForwardSamplingAlgorithm(model=m, observed=[m.X], num_samples=5))
        infr2.run(X=mx.nd.array(X, dtype='float64'))

        infr_pred = TransferInference(ModulePredictionAlgorithm(
            model=m, observed=[m.X], target_variables=[m.Y]),
                                      infr_params=infr.params)
        xt = np.random.rand(13, 3)
        res = infr_pred.run(X=mx.nd.array(xt, dtype=dtype))[0]

        gp = m.Y.factor
        gp.attach_prediction_algorithms(
            targets=gp.output_names,
            conditionals=gp.input_names,
            algorithm=GPRegressionSamplingPrediction(gp._module_graph,
                                                     gp._extra_graphs[0],
                                                     [gp._module_graph.X]),
            alg_name='gp_predict')
        gp.gp_predict.diagonal_variance = False
        gp.gp_predict.jitter = 1e-6

        infr_pred2 = TransferInference(ModulePredictionAlgorithm(
            model=m, observed=[m.X], target_variables=[m.Y]),
                                       infr_params=infr.params)
        xt = np.random.rand(13, 3)
        res = infr_pred2.run(X=mx.nd.array(xt, dtype=dtype))[0]
Exemplo n.º 2
0
    def test_gluon_func_save_and_load(self):
        m = self.make_simple_gluon_model()
        infr = Inference(ForwardSamplingAlgorithm(m, observed=[m.x]))
        infr.run(x=mx.nd.ones((1, 1)))
        infr.save(self.ZIPNAME)

        m2 = self.make_simple_gluon_model()
        infr2 = Inference(ForwardSamplingAlgorithm(m2, observed=[m2.x]))
        infr2.run(x=mx.nd.ones((1, 1)))
        infr2.load(self.ZIPNAME)
        infr2.run(x=mx.nd.ones((1, 1)))

        for n in m.f.parameter_names:
            assert np.allclose(infr.params[getattr(m.y.factor, n)].asnumpy(),
                               infr2.params[getattr(m2.y.factor, n)].asnumpy())

        os.remove(self.ZIPNAME)
Exemplo n.º 3
0
    def test_draw_samples(self):
        D, X, Y, Z, noise_var, lengthscale, variance = self.gen_data()
        dtype = 'float64'

        m = self.gen_mxfusion_model(dtype, D, Z, noise_var, lengthscale,
                                    variance)

        observed = [m.X]
        infr = Inference(ForwardSamplingAlgorithm(
            m, observed, num_samples=2, target_variables=[m.Y]), dtype=dtype)
        samples = infr.run(X=mx.nd.array(X, dtype=dtype))[0]
        assert samples.shape == (2,) + Y.shape
Exemplo n.º 4
0
    def test_gluon_parameters(self):
        self.setUp()

        m = Model()
        m.x = Variable(shape=(1, 1))
        m.f = MXFusionGluonFunction(self.net, num_outputs=1)
        m.y = m.f(m.x)

        infr = Inference(ForwardSamplingAlgorithm(m, observed=[m.x]))
        infr.run(x=mx.nd.ones((1, 1)))
        assert all([
            v.uuid in infr.params.param_dict for v in m.f.parameters.values()
        ])
Exemplo n.º 5
0
    def test_draw_samples_w_mean(self):
        D, X, Y, noise_var, lengthscale, variance = self.gen_data()
        dtype = 'float64'

        rand_gen = MockMXNetRandomGenerator(
            mx.nd.array(np.random.rand(20 * D), dtype=dtype))

        m, net = self.gen_mxfusion_model_w_mean(dtype, D, noise_var,
                                                lengthscale, variance,
                                                rand_gen)

        observed = [m.X]
        infr = Inference(ForwardSamplingAlgorithm(m,
                                                  observed,
                                                  num_samples=2,
                                                  target_variables=[m.Y]),
                         dtype=dtype)

        samples = infr.run(X=mx.nd.array(X, dtype=dtype),
                           Y=mx.nd.array(Y, dtype=dtype))[0].asnumpy()

        kern = RBF(3, True, name='rbf', dtype=dtype) + White(3, dtype=dtype)
        X_var = Variable(shape=(10, 3))
        mean_func = MXFusionGluonFunction(net,
                                          num_outputs=1,
                                          broadcastable=True)
        mean_var = mean_func(X_var)
        gp = GaussianProcess.define_variable(X=X_var,
                                             kernel=kern,
                                             mean=mean_var,
                                             shape=(10, D),
                                             dtype=dtype,
                                             rand_gen=rand_gen).factor

        variables = {
            gp.X.uuid:
            mx.nd.expand_dims(mx.nd.array(X, dtype=dtype), axis=0),
            gp.add_rbf_lengthscale.uuid:
            mx.nd.expand_dims(mx.nd.array(lengthscale, dtype=dtype), axis=0),
            gp.add_rbf_variance.uuid:
            mx.nd.expand_dims(mx.nd.array(variance, dtype=dtype), axis=0),
            gp.add_white_variance.uuid:
            mx.nd.expand_dims(mx.nd.array(noise_var, dtype=dtype), axis=0),
            mean_var.uuid:
            mx.nd.expand_dims(net(mx.nd.array(X, dtype=dtype)), axis=0)
        }
        samples_2 = gp.draw_samples(F=mx.nd,
                                    variables=variables,
                                    num_samples=2).asnumpy()

        assert np.allclose(samples, samples_2), (samples, samples_2)