Esempio n. 1
0
    def test_set_parameters(self):

        class SetValue(InferenceAlgorithm):
            def __init__(self, x, y, model, observed, extra_graphs=None):
                self.x_val = x
                self.y_val = y
                super(SetValue, self).__init__(
                    model=model, observed=observed, extra_graphs=extra_graphs)

            def compute(self, F, variables):
                self.set_parameter(variables, self.model.x, self.x_val)
                self.set_parameter(variables, self.model.y, self.y_val)

        m = Model()
        m.x = Variable(shape=(2,))
        m.y = Variable(shape=(3, 4))

        dtype = 'float64'

        np.random.seed(0)
        x_np = np.random.rand(2)
        y_np = np.random.rand(3, 4)
        x_mx = mx.nd.array(x_np, dtype=dtype)
        y_mx = mx.nd.array(y_np, dtype=dtype)

        infr = Inference(SetValue(x_mx, y_mx, m, []), dtype=dtype)
        infr.run()
        x_res = infr.params[m.x]
        y_res = infr.params[m.y]

        assert np.allclose(x_res.asnumpy(), x_np)
        assert np.allclose(y_res.asnumpy(), y_np)
Esempio n. 2
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]
    def test_gp_module_save_and_load(self):
        np.random.seed(0)
        X = np.random.rand(10, 3)
        Xt = np.random.rand(20, 3)
        Y = np.random.rand(10, 1)
        noise_var = np.random.rand(1)
        lengthscale = np.random.rand(3)
        variance = np.random.rand(1)
        dtype = 'float64'
        m = self.make_gpregr_model(lengthscale, variance, noise_var)

        observed = [m.X, m.Y]
        from mxfusion.inference import MAP, Inference
        infr = Inference(MAP(model=m, observed=observed), dtype=dtype)

        loss, _ = infr.run(X=mx.nd.array(X, dtype=dtype),
                           Y=mx.nd.array(Y, dtype=dtype))

        infr.save(prefix=self.PREFIX)

        m2 = self.make_gpregr_model(lengthscale, variance, noise_var)

        observed2 = [m2.X, m2.Y]
        infr2 = Inference(MAP(model=m2, observed=observed2), dtype=dtype)
        infr2.initialize(X=mx.nd.array(X, dtype=dtype),
                         Y=mx.nd.array(Y, dtype=dtype))

        # Load previous parameters
        infr2.load(
            graphs_file=self.PREFIX + '_graphs.json',
            parameters_file=self.PREFIX + '_params.json',
            inference_configuration_file=self.PREFIX + '_configuration.json',
            mxnet_constants_file=self.PREFIX + '_mxnet_constants.json',
            variable_constants_file=self.PREFIX + '_variable_constants.json')

        for original_uuid, original_param in infr.params.param_dict.items():
            original_data = original_param.data().asnumpy()
            reloaded_data = infr2.params.param_dict[
                infr2._uuid_map[original_uuid]].data().asnumpy()
            assert np.all(np.isclose(original_data, reloaded_data))

        for original_uuid, original_param in infr.params.constants.items():
            if isinstance(original_param, mx.ndarray.ndarray.NDArray):
                original_data = original_param.asnumpy()
                reloaded_data = infr2.params.constants[
                    infr2._uuid_map[original_uuid]].asnumpy()
            else:
                original_data = original_param
                reloaded_data = infr2.params.constants[
                    infr2._uuid_map[original_uuid]]

            assert np.all(np.isclose(original_data, reloaded_data))

        loss2, _ = infr2.run(X=mx.nd.array(X, dtype=dtype),
                             Y=mx.nd.array(Y, dtype=dtype))

        self.remove_saved_files(self.PREFIX)
Esempio 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()
        ])
    def test_sampling_prediction(self):
        D, X, Y, Z, noise_var, lengthscale, variance = self.gen_data()
        Xt = np.random.rand(20, 3)

        m_gpy = GPy.models.SparseGPRegression(X=X, Y=Y, Z=Z, kernel=GPy.kern.RBF(3, ARD=True, lengthscale=lengthscale, variance=variance), num_inducing=3)
        m_gpy.likelihood.variance = noise_var

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

        observed = [m.X, m.Y]
        infr = Inference(MAP(model=m, observed=observed), dtype=dtype)

        loss, _ = infr.run(X=mx.nd.array(X, dtype=dtype), Y=mx.nd.array(Y, dtype=dtype))

        # noise_free, diagonal

        infr_pred = TransferInference(ModulePredictionAlgorithm(model=m, observed=[m.X], target_variables=[m.Y], num_samples=5),
                                      infr_params=infr.params)
        gp = m.Y.factor
        gp.attach_prediction_algorithms(
            targets=gp.output_names, conditionals=gp.input_names,
            algorithm=SparseGPRegressionSamplingPrediction(
                gp._module_graph, gp._extra_graphs[0], [gp._module_graph.X]),
            alg_name='sgp_predict')
        gp.sgp_predict.diagonal_variance = False
        gp.sgp_predict.jitter = 1e-6

        y_samples = infr_pred.run(X=mx.nd.array(Xt, dtype=dtype))[0].asnumpy()
Esempio n. 6
0
    def test_log_pdf_w_mean(self):
        D, X, Y, noise_var, lengthscale, variance = self.gen_data()

        # MXFusion log-likelihood
        dtype = 'float64'
        m, net = self.gen_mxfusion_model_w_mean(dtype, D, noise_var,
                                                lengthscale, variance)

        mean = net(mx.nd.array(X, dtype=dtype)).asnumpy()

        # GPy log-likelihood
        m_gpy = GPy.models.GPRegression(X=X,
                                        Y=Y - mean,
                                        kernel=GPy.kern.RBF(
                                            3,
                                            ARD=True,
                                            lengthscale=lengthscale,
                                            variance=variance),
                                        noise_var=noise_var)
        l_gpy = m_gpy.log_likelihood()

        observed = [m.X, m.Y]
        infr = Inference(MAP(model=m, observed=observed), dtype=dtype)

        loss, _ = infr.run(X=mx.nd.array(X, dtype=dtype),
                           Y=mx.nd.array(Y, dtype=dtype))
        l_mf = -loss

        assert np.allclose(l_mf.asnumpy(), l_gpy)
Esempio n. 7
0
    def test_prediction_w_mean(self):
        D, X, Y, Z, noise_var, lengthscale, variance = self.gen_data()
        Xt = np.random.rand(20, 3)
        dtype = 'float64'

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

        mean = net(mx.nd.array(X, dtype=dtype)).asnumpy()
        mean_t = net(mx.nd.array(Xt, dtype=dtype)).asnumpy()

        m_gpy = GPy.models.SparseGPRegression(X=X, Y=Y-mean, Z=Z, kernel=GPy.kern.RBF(3, ARD=True, lengthscale=lengthscale, variance=variance))
        m_gpy.likelihood.variance = noise_var

        observed = [m.X, m.Y]
        infr = Inference(MAP(model=m, observed=observed), dtype=dtype)
        loss, _ = infr.run(X=mx.nd.array(X, dtype=dtype), Y=mx.nd.array(Y, dtype=dtype))

        # noise_free, diagonal
        mu_gpy, var_gpy = m_gpy.predict_noiseless(Xt)
        mu_gpy += mean_t

        infr2 = TransferInference(ModulePredictionAlgorithm(m, observed=[m.X], target_variables=[m.Y]), infr_params=infr.params, dtype=np.float64)
        res = infr2.run(X=mx.nd.array(Xt, dtype=dtype))[0]
        mu_mf, var_mf = res[0].asnumpy()[0], res[1].asnumpy()[0]

        assert np.allclose(mu_gpy, mu_mf, rtol=1e-04, atol=1e-05), (mu_gpy, mu_mf)
        assert np.allclose(var_gpy[:,0], var_mf, rtol=1e-04, atol=1e-05), (var_gpy[:,0], var_mf)
Esempio n. 8
0
    def test_sampling_prediction_w_mean(self):
        D, X, Y, Z, noise_var, lengthscale, variance, qU_mean, \
            qU_cov_W, qU_cov_diag, qU_chol = self.gen_data()
        Xt = np.random.rand(20, 3)

        dtype = 'float64'
        m, gp, net = self.gen_mxfusion_model_w_mean(dtype, D, Z, noise_var,
                                                    lengthscale, variance)

        observed = [m.X, m.Y]
        infr = Inference(MAP(model=m, observed=observed), dtype=dtype)

        loss, _ = infr.run(X=mx.nd.array(X, dtype=dtype),
                           Y=mx.nd.array(Y, dtype=dtype))

        # noise_free, diagonal
        infr_pred = TransferInference(ModulePredictionAlgorithm(
            model=m, observed=[m.X], target_variables=[m.Y], num_samples=5),
                                      infr_params=infr.params)
        gp = m.Y.factor
        gp.attach_prediction_algorithms(
            targets=gp.output_names,
            conditionals=gp.input_names,
            algorithm=SVGPRegressionSamplingPrediction(gp._module_graph,
                                                       gp._extra_graphs[0],
                                                       [gp._module_graph.X]),
            alg_name='svgp_predict')
        gp.svgp_predict.diagonal_variance = True
        gp.svgp_predict.noise_free = False
        gp.svgp_predict.jitter = 1e-6

        y_samples = infr_pred.run(X=mx.nd.array(Xt, dtype=dtype))[0].asnumpy()
    def test_prediction(self):
        D, X, Y, Z, noise_var, lengthscale, variance = self.gen_data()
        Xt = np.random.rand(20, 3)

        m_gpy = GPy.models.SparseGPRegression(X=X, Y=Y, Z=Z, kernel=GPy.kern.RBF(3, ARD=True, lengthscale=lengthscale, variance=variance), num_inducing=3)
        m_gpy.likelihood.variance = noise_var

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

        observed = [m.X, m.Y]
        infr = Inference(MAP(model=m, observed=observed), dtype=dtype)

        loss, _ = infr.run(X=mx.nd.array(X, dtype=dtype), Y=mx.nd.array(Y, dtype=dtype))

        # noise_free, diagonal
        mu_gpy, var_gpy = m_gpy.predict_noiseless(Xt)

        infr2 = TransferInference(ModulePredictionAlgorithm(m, observed=[m.X], target_variables=[m.Y]), infr_params=infr.params, dtype=np.float64)
        res = infr2.run(X=mx.nd.array(Xt, dtype=dtype))[0]
        mu_mf, var_mf = res[0].asnumpy()[0], res[1].asnumpy()[0]

        assert np.allclose(mu_gpy, mu_mf), (mu_gpy, mu_mf)
        assert np.allclose(var_gpy[:,0], var_mf), (var_gpy[:,0], var_mf)

        # noisy, diagonal
        mu_gpy, var_gpy = m_gpy.predict(Xt)

        infr2 = TransferInference(ModulePredictionAlgorithm(m, observed=[m.X], target_variables=[m.Y]), infr_params=infr.params, dtype=np.float64)
        infr2.inference_algorithm.model.Y.factor.sgp_predict.noise_free = False
        res = infr2.run(X=mx.nd.array(Xt, dtype=dtype))[0]
        mu_mf, var_mf = res[0].asnumpy()[0], res[1].asnumpy()[0]

        assert np.allclose(mu_gpy, mu_mf), (mu_gpy, mu_mf)
        assert np.allclose(var_gpy[:,0], var_mf), (var_gpy[:,0], var_mf)

        # noise_free, full_cov
        mu_gpy, var_gpy = m_gpy.predict_noiseless(Xt, full_cov=True)

        infr2 = TransferInference(ModulePredictionAlgorithm(m, observed=[m.X], target_variables=[m.Y]), infr_params=infr.params, dtype=np.float64)
        infr2.inference_algorithm.model.Y.factor.sgp_predict.diagonal_variance = False
        infr2.inference_algorithm.model.Y.factor.sgp_predict.noise_free = True
        res = infr2.run(X=mx.nd.array(Xt, dtype=dtype))[0]
        mu_mf, var_mf = res[0].asnumpy()[0], res[1].asnumpy()[0]

        assert np.allclose(mu_gpy, mu_mf), (mu_gpy, mu_mf)
        assert np.allclose(var_gpy, var_mf), (var_gpy, var_mf)

        # noisy, full_cov
        mu_gpy, var_gpy = m_gpy.predict(Xt, full_cov=True)

        infr2 = TransferInference(ModulePredictionAlgorithm(m, observed=[m.X], target_variables=[m.Y]), infr_params=infr.params, dtype=np.float64)
        infr2.inference_algorithm.model.Y.factor.sgp_predict.diagonal_variance = False
        infr2.inference_algorithm.model.Y.factor.sgp_predict.noise_free = False
        res = infr2.run(X=mx.nd.array(Xt, dtype=dtype))[0]
        mu_mf, var_mf = res[0].asnumpy()[0], res[1].asnumpy()[0]

        assert np.allclose(mu_gpy, mu_mf), (mu_gpy, mu_mf)
        assert np.allclose(var_gpy, var_mf), (var_gpy, var_mf)
Esempio n. 10
0
    def test_prediction(self):
        np.random.seed(0)
        np.random.seed(0)
        X = np.random.rand(10, 3)
        Y = np.random.rand(10, 1)
        Z = np.random.rand(3, 3)
        qU_mean = np.random.rand(3, 1)
        qU_cov_W = np.random.rand(3, 3)
        qU_cov_diag = np.random.rand(3,)
        noise_var = np.random.rand(1)
        lengthscale = np.random.rand(3)
        variance = np.random.rand(1)
        qU_chol = np.linalg.cholesky(qU_cov_W.dot(qU_cov_W.T)+np.diag(qU_cov_diag))[None,:,:]
        Xt = np.random.rand(5, 3)

        m_gpy = GPy.core.SVGP(X=X, Y=Y, Z=Z, kernel=GPy.kern.RBF(3, ARD=True, lengthscale=lengthscale, variance=variance), likelihood=GPy.likelihoods.Gaussian(variance=noise_var))
        m_gpy.q_u_mean = qU_mean
        m_gpy.q_u_chol = GPy.util.choleskies.triang_to_flat(qU_chol)

        dtype = 'float64'
        m = Model()
        m.N = Variable()
        m.X = Variable(shape=(m.N, 3))
        m.Z = Variable(shape=(3, 3), initial_value=mx.nd.array(Z, dtype=dtype))
        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 = SVGPRegression.define_variable(X=m.X, kernel=kernel, noise_var=m.noise_var, inducing_inputs=m.Z, shape=(m.N, 1), dtype=dtype)
        gp = m.Y.factor

        observed = [m.X, m.Y]
        infr = Inference(MAP(model=m, observed=observed), dtype=dtype)
        infr.initialize(X=X.shape, Y=Y.shape)
        infr.params[gp._extra_graphs[0].qU_mean] = mx.nd.array(qU_mean, dtype=dtype)
        infr.params[gp._extra_graphs[0].qU_cov_W] = mx.nd.array(qU_cov_W, dtype=dtype)
        infr.params[gp._extra_graphs[0].qU_cov_diag] = mx.nd.array(qU_cov_diag, dtype=dtype)

        loss, _ = infr.run(X=mx.nd.array(X, dtype=dtype), Y=mx.nd.array(Y, dtype=dtype))

        # noise_free, diagonal
        mu_gpy, var_gpy = m_gpy.predict_noiseless(Xt)

        infr2 = TransferInference(ModulePredictionAlgorithm(m, observed=[m.X], target_variables=[m.Y]), infr_params=infr.params, dtype=np.float64)
        res = infr2.run(X=mx.nd.array(Xt, dtype=dtype))[0]
        mu_mf, var_mf = res[0].asnumpy()[0], res[1].asnumpy()[0]

        assert np.allclose(mu_gpy, mu_mf), (mu_gpy, mu_mf)
        assert np.allclose(var_gpy[:,0], var_mf), (var_gpy[:,0], var_mf)

        # noisy, diagonal
        mu_gpy, var_gpy = m_gpy.predict(Xt)

        infr2 = TransferInference(ModulePredictionAlgorithm(m, observed=[m.X], target_variables=[m.Y]), infr_params=infr.params, dtype=np.float64)
        infr2.inference_algorithm.model.Y.factor.svgp_predict.noise_free = False
        res = infr2.run(X=mx.nd.array(Xt, dtype=dtype))[0]
        mu_mf, var_mf = res[0].asnumpy()[0], res[1].asnumpy()[0]

        assert np.allclose(mu_gpy, mu_mf), (mu_gpy, mu_mf)
        assert np.allclose(var_gpy[:,0], var_mf), (var_gpy[:,0], var_mf)
Esempio n. 11
0
    def test_sampling_prediction(self):
        np.random.seed(0)
        X = np.random.rand(10, 3)
        Xt = np.random.rand(20, 3)
        Y = np.random.rand(10, 1)
        noise_var = np.random.rand(1)
        lengthscale = np.random.rand(3)
        variance = np.random.rand(1)

        m_gpy = GPy.models.GPRegression(X=X,
                                        Y=Y,
                                        kernel=GPy.kern.RBF(
                                            3,
                                            ARD=True,
                                            lengthscale=lengthscale,
                                            variance=variance),
                                        noise_var=noise_var)

        dtype = 'float64'
        m = Model()
        m.N = Variable()
        m.X = Variable(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, 1),
                                           dtype=dtype)

        observed = [m.X, m.Y]
        infr = Inference(MAP(model=m, observed=observed), dtype=dtype)

        loss, _ = infr.run(X=mx.nd.array(X, dtype=dtype),
                           Y=mx.nd.array(Y, dtype=dtype))

        infr_pred = TransferInference(ModulePredictionAlgorithm(
            model=m, observed=[m.X], target_variables=[m.Y], num_samples=5),
                                      infr_params=infr.params)
        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

        y_samples = infr_pred.run(X=mx.nd.array(Xt, dtype=dtype))[0].asnumpy()
Esempio n. 12
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
Esempio n. 13
0
    def test_sampling_prediction(self):
        np.random.seed(0)
        np.random.seed(0)
        X = np.random.rand(10, 3)
        Y = np.random.rand(10, 1)
        Z = np.random.rand(3, 3)
        qU_mean = np.random.rand(3, 1)
        qU_cov_W = np.random.rand(3, 3)
        qU_cov_diag = np.random.rand(3,)
        noise_var = np.random.rand(1)
        lengthscale = np.random.rand(3)
        variance = np.random.rand(1)
        qU_chol = np.linalg.cholesky(qU_cov_W.dot(qU_cov_W.T)+np.diag(qU_cov_diag))[None,:,:]
        Xt = np.random.rand(5, 3)

        m_gpy = GPy.core.SVGP(X=X, Y=Y, Z=Z, kernel=GPy.kern.RBF(3, ARD=True, lengthscale=lengthscale, variance=variance), likelihood=GPy.likelihoods.Gaussian(variance=noise_var))
        m_gpy.q_u_mean = qU_mean
        m_gpy.q_u_chol = GPy.util.choleskies.triang_to_flat(qU_chol)

        dtype = 'float64'
        m = Model()
        m.N = Variable()
        m.X = Variable(shape=(m.N, 3))
        m.Z = Variable(shape=(3, 3), initial_value=mx.nd.array(Z, dtype=dtype))
        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 = SVGPRegression.define_variable(X=m.X, kernel=kernel, noise_var=m.noise_var, inducing_inputs=m.Z, shape=(m.N, 1), dtype=dtype)
        gp = m.Y.factor

        observed = [m.X, m.Y]
        infr = Inference(MAP(model=m, observed=observed), dtype=dtype)
        infr.initialize(X=X.shape, Y=Y.shape)
        infr.params[gp._extra_graphs[0].qU_mean] = mx.nd.array(qU_mean, dtype=dtype)
        infr.params[gp._extra_graphs[0].qU_cov_W] = mx.nd.array(qU_cov_W, dtype=dtype)
        infr.params[gp._extra_graphs[0].qU_cov_diag] = mx.nd.array(qU_cov_diag, dtype=dtype)

        loss, _ = infr.run(X=mx.nd.array(X, dtype=dtype), Y=mx.nd.array(Y, dtype=dtype))

        # noise_free, diagonal
        infr_pred = TransferInference(ModulePredictionAlgorithm(model=m, observed=[m.X], target_variables=[m.Y], num_samples=5),
                                      infr_params=infr.params)
        gp = m.Y.factor
        gp.attach_prediction_algorithms(
            targets=gp.output_names, conditionals=gp.input_names,
            algorithm=SVGPRegressionSamplingPrediction(
                gp._module_graph, gp._extra_graphs[0], [gp._module_graph.X]),
            alg_name='svgp_predict')
        gp.svgp_predict.diagonal_variance = False
        gp.svgp_predict.jitter = 1e-6

        y_samples = infr_pred.run(X=mx.nd.array(Xt, dtype=dtype))[0].asnumpy()
Esempio n. 14
0
    def test_sampling_prediction(self):
        D, X, Y, Z, noise_var, lengthscale, variance, qU_mean, \
            qU_cov_W, qU_cov_diag, qU_chol = self.gen_data()
        Xt = np.random.rand(5, 3)

        m_gpy = GPy.core.SVGP(
            X=X,
            Y=Y,
            Z=Z,
            kernel=GPy.kern.RBF(3,
                                ARD=True,
                                lengthscale=lengthscale,
                                variance=variance),
            likelihood=GPy.likelihoods.Gaussian(variance=noise_var))
        m_gpy.q_u_mean = qU_mean
        m_gpy.q_u_chol = GPy.util.choleskies.triang_to_flat(qU_chol)

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

        observed = [m.X, m.Y]
        infr = Inference(MAP(model=m, observed=observed), dtype=dtype)
        infr.initialize(X=X.shape, Y=Y.shape)
        infr.params[gp._extra_graphs[0].qU_mean] = mx.nd.array(qU_mean,
                                                               dtype=dtype)
        infr.params[gp._extra_graphs[0].qU_cov_W] = mx.nd.array(qU_cov_W,
                                                                dtype=dtype)
        infr.params[gp._extra_graphs[0].qU_cov_diag] = mx.nd.array(qU_cov_diag,
                                                                   dtype=dtype)

        loss, _ = infr.run(X=mx.nd.array(X, dtype=dtype),
                           Y=mx.nd.array(Y, dtype=dtype))

        # noise_free, diagonal
        infr_pred = TransferInference(ModulePredictionAlgorithm(
            model=m, observed=[m.X], target_variables=[m.Y], num_samples=5),
                                      infr_params=infr.params)
        gp = m.Y.factor
        gp.attach_prediction_algorithms(
            targets=gp.output_names,
            conditionals=gp.input_names,
            algorithm=SVGPRegressionSamplingPrediction(gp._module_graph,
                                                       gp._extra_graphs[0],
                                                       [gp._module_graph.X]),
            alg_name='svgp_predict')
        gp.svgp_predict.diagonal_variance = False
        gp.svgp_predict.noise_free = False
        gp.svgp_predict.jitter = 1e-6

        y_samples = infr_pred.run(X=mx.nd.array(Xt, dtype=dtype))[0].asnumpy()
Esempio n. 15
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)
Esempio n. 16
0
    def test_prediction_w_mean(self):
        D, X, Y, Z, noise_var, lengthscale, variance, qU_mean, \
            qU_cov_W, qU_cov_diag, qU_chol = self.gen_data()
        Xt = np.random.rand(5, 3)
        dtype = 'float64'
        m, gp, net = self.gen_mxfusion_model_w_mean(dtype, D, Z, noise_var,
                                                    lengthscale, variance)
        mean = net(mx.nd.array(X, dtype=dtype)).asnumpy()
        mean_t = net(mx.nd.array(Xt, dtype=dtype)).asnumpy()

        m_gpy = GPy.core.SVGP(
            X=X,
            Y=Y - mean,
            Z=Z,
            kernel=GPy.kern.RBF(3,
                                ARD=True,
                                lengthscale=lengthscale,
                                variance=variance),
            likelihood=GPy.likelihoods.Gaussian(variance=noise_var))
        m_gpy.q_u_mean = qU_mean
        m_gpy.q_u_chol = GPy.util.choleskies.triang_to_flat(qU_chol)

        observed = [m.X, m.Y]
        infr = Inference(MAP(model=m, observed=observed), dtype=dtype)
        infr.initialize(X=X.shape, Y=Y.shape)
        infr.params[gp._extra_graphs[0].qU_mean] = mx.nd.array(qU_mean,
                                                               dtype=dtype)
        infr.params[gp._extra_graphs[0].qU_cov_W] = mx.nd.array(qU_cov_W,
                                                                dtype=dtype)
        infr.params[gp._extra_graphs[0].qU_cov_diag] = mx.nd.array(qU_cov_diag,
                                                                   dtype=dtype)

        loss, _ = infr.run(X=mx.nd.array(X, dtype=dtype),
                           Y=mx.nd.array(Y, dtype=dtype))

        # noise_free, diagonal
        mu_gpy, var_gpy = m_gpy.predict_noiseless(Xt)
        mu_gpy += mean_t

        infr2 = TransferInference(ModulePredictionAlgorithm(
            m, observed=[m.X], target_variables=[m.Y]),
                                  infr_params=infr.params,
                                  dtype=np.float64)
        res = infr2.run(X=mx.nd.array(Xt, dtype=dtype))[0]
        mu_mf, var_mf = res[0].asnumpy()[0], res[1].asnumpy()[0]

        assert np.allclose(mu_gpy, mu_mf, rtol=1e-04,
                           atol=1e-05), (mu_gpy, mu_mf)
        assert np.allclose(var_gpy, var_mf, rtol=1e-04,
                           atol=1e-05), (var_gpy, var_mf)
Esempio n. 17
0
    def test_log_pdf(self):
        np.random.seed(0)
        D = 2
        X = np.random.rand(10, 3)
        Y = np.random.rand(10, D)
        noise_var = np.random.rand(1)
        lengthscale = np.random.rand(3)
        variance = np.random.rand(1)

        m_gpy = GPy.models.GPRegression(X=X,
                                        Y=Y,
                                        kernel=GPy.kern.RBF(
                                            3,
                                            ARD=True,
                                            lengthscale=lengthscale,
                                            variance=variance),
                                        noise_var=noise_var)

        l_gpy = m_gpy.log_likelihood()

        dtype = 'float64'
        m = Model()
        m.N = Variable()
        m.X = Variable(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),
                                           dtype=dtype)

        observed = [m.X, m.Y]
        infr = Inference(MAP(model=m, observed=observed), dtype=dtype)

        loss, _ = infr.run(X=mx.nd.array(X, dtype=dtype),
                           Y=mx.nd.array(Y, dtype=dtype))
        l_mf = -loss

        assert np.allclose(l_mf.asnumpy(), l_gpy)
Esempio n. 18
0
    def test_log_pdf(self):
        D, X, Y, Z, noise_var, lengthscale, variance = self.gen_data()

        m_gpy = GPy.models.SparseGPRegression(X=X, Y=Y, Z=Z, kernel=GPy.kern.RBF(3, ARD=True, lengthscale=lengthscale, variance=variance), num_inducing=3)
        m_gpy.likelihood.variance = noise_var

        l_gpy = m_gpy.log_likelihood()

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

        observed = [m.X, m.Y]
        infr = Inference(MAP(model=m, observed=observed), dtype=dtype)

        loss, _ = infr.run(X=mx.nd.array(X, dtype=dtype), Y=mx.nd.array(Y, dtype=dtype))
        l_mf = -loss

        assert np.allclose(l_mf.asnumpy(), l_gpy)
Esempio n. 19
0
    def test_log_pdf(self):
        np.random.seed(0)
        D = 2
        X = np.random.rand(10, 3)
        Y = np.random.rand(10, D)
        Z = np.random.rand(3, 3)
        qU_mean = np.random.rand(3, D)
        qU_cov_W = np.random.rand(3, 3)
        qU_cov_diag = np.random.rand(3,)
        noise_var = np.random.rand(1)
        lengthscale = np.random.rand(3)
        variance = np.random.rand(1)
        qU_chol = np.linalg.cholesky(qU_cov_W.dot(qU_cov_W.T)+np.diag(qU_cov_diag))[None,:,:]

        m_gpy = GPy.core.SVGP(X=X, Y=Y, Z=Z, kernel=GPy.kern.RBF(3, ARD=True, lengthscale=lengthscale, variance=variance), likelihood=GPy.likelihoods.Gaussian(variance=noise_var))
        m_gpy.q_u_mean = qU_mean
        m_gpy.q_u_chol = GPy.util.choleskies.triang_to_flat(qU_chol)

        l_gpy = m_gpy.log_likelihood()

        dtype = 'float64'
        m = Model()
        m.N = Variable()
        m.X = Variable(shape=(m.N, 3))
        m.Z = Variable(shape=(3, 3), initial_value=mx.nd.array(Z, dtype=dtype))
        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 = SVGPRegression.define_variable(X=m.X, kernel=kernel, noise_var=m.noise_var, inducing_inputs=m.Z, shape=(m.N, D), dtype=dtype)
        gp = m.Y.factor

        observed = [m.X, m.Y]
        infr = Inference(MAP(model=m, observed=observed), dtype=dtype)
        infr.initialize(X=X.shape, Y=Y.shape)
        infr.params[gp._extra_graphs[0].qU_mean] = mx.nd.array(qU_mean, dtype=dtype)
        infr.params[gp._extra_graphs[0].qU_cov_W] = mx.nd.array(qU_cov_W, dtype=dtype)
        infr.params[gp._extra_graphs[0].qU_cov_diag] = mx.nd.array(qU_cov_diag, dtype=dtype)

        loss, _ = infr.run(X=mx.nd.array(X, dtype=dtype), Y=mx.nd.array(Y, dtype=dtype))
        l_mf = -loss

        assert np.allclose(l_mf.asnumpy(), l_gpy)
Esempio n. 20
0
    def test_log_pdf_w_samples_of_noise_var(self):
        D, X, Y, Z, noise_var, lengthscale, variance, qU_mean, \
            qU_cov_W, qU_cov_diag, qU_chol = self.gen_data()
        dtype = 'float64'
        D = 2
        Y = np.random.rand(10, D)
        qU_mean = np.random.rand(3, D)

        m = Model()
        m.N = Variable()
        m.X = Variable(shape=(m.N, 3))
        m.Z = Variable(shape=(3, 3), initial_value=mx.nd.array(Z, dtype=dtype))
        m.noise_var = Variable(transformation=PositiveTransformation(),
                               shape=(m.N, D))
        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 = SVGPRegression.define_variable(X=m.X,
                                             kernel=kernel,
                                             noise_var=m.noise_var,
                                             inducing_inputs=m.Z,
                                             shape=(m.N, D),
                                             dtype=dtype)
        gp = m.Y.factor
        m.Y.factor.svgp_log_pdf.jitter = 1e-8

        observed = [m.X, m.Y]
        infr = Inference(MAP(model=m, observed=observed), dtype=dtype)
        infr.initialize(X=X.shape, Y=Y.shape)
        infr.params[gp._extra_graphs[0].qU_mean] = mx.nd.array(qU_mean,
                                                               dtype=dtype)
        infr.params[gp._extra_graphs[0].qU_cov_W] = mx.nd.array(qU_cov_W,
                                                                dtype=dtype)
        infr.params[gp._extra_graphs[0].qU_cov_diag] = mx.nd.array(qU_cov_diag,
                                                                   dtype=dtype)

        loss, _ = infr.run(X=mx.nd.array(X, dtype=dtype),
                           Y=mx.nd.array(Y, dtype=dtype),
                           max_iter=1)
Esempio n. 21
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)
Esempio n. 22
0
    def test_prediction_print(self):
        D, X, Y, noise_var, lengthscale, variance = self.gen_data()
        Xt = np.random.rand(20, 3)

        m_gpy = GPy.models.GPRegression(X=X,
                                        Y=Y,
                                        kernel=GPy.kern.RBF(
                                            3,
                                            ARD=True,
                                            lengthscale=lengthscale,
                                            variance=variance),
                                        noise_var=noise_var)

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

        observed = [m.X, m.Y]
        infr = Inference(MAP(model=m, observed=observed), dtype=dtype)

        loss, _ = infr.run(X=mx.nd.array(X, dtype=dtype),
                           Y=mx.nd.array(Y, dtype=dtype))
        print = infr.print_params()
        assert (len(print) > 1)
Esempio n. 23
0
    def test_log_pdf_w_mean(self):
        D, X, Y, Z, noise_var, lengthscale, variance, qU_mean, \
            qU_cov_W, qU_cov_diag, qU_chol = self.gen_data()
        dtype = 'float64'
        m, gp, net = self.gen_mxfusion_model_w_mean(dtype, D, Z, noise_var,
                                                    lengthscale, variance)
        mean = net(mx.nd.array(X, dtype=dtype)).asnumpy()

        m_gpy = GPy.core.SVGP(
            X=X,
            Y=Y - mean,
            Z=Z,
            kernel=GPy.kern.RBF(3,
                                ARD=True,
                                lengthscale=lengthscale,
                                variance=variance),
            likelihood=GPy.likelihoods.Gaussian(variance=noise_var))
        m_gpy.q_u_mean = qU_mean
        m_gpy.q_u_chol = GPy.util.choleskies.triang_to_flat(qU_chol)

        l_gpy = m_gpy.log_likelihood()

        observed = [m.X, m.Y]
        infr = Inference(MAP(model=m, observed=observed), dtype=dtype)
        infr.initialize(X=X.shape, Y=Y.shape)
        infr.params[gp._extra_graphs[0].qU_mean] = mx.nd.array(qU_mean,
                                                               dtype=dtype)
        infr.params[gp._extra_graphs[0].qU_cov_W] = mx.nd.array(qU_cov_W,
                                                                dtype=dtype)
        infr.params[gp._extra_graphs[0].qU_cov_diag] = mx.nd.array(qU_cov_diag,
                                                                   dtype=dtype)

        loss, _ = infr.run(X=mx.nd.array(X, dtype=dtype),
                           Y=mx.nd.array(Y, dtype=dtype))
        l_mf = -loss

        assert np.allclose(l_mf.asnumpy(), l_gpy)
Esempio n. 24
0
    def test_prediction(self):
        D, X, Y, Z, noise_var, lengthscale, variance, qU_mean, \
            qU_cov_W, qU_cov_diag, qU_chol = self.gen_data()
        Xt = np.random.rand(5, 3)

        m_gpy = GPy.core.SVGP(
            X=X,
            Y=Y,
            Z=Z,
            kernel=GPy.kern.RBF(3,
                                ARD=True,
                                lengthscale=lengthscale,
                                variance=variance),
            likelihood=GPy.likelihoods.Gaussian(variance=noise_var))
        m_gpy.q_u_mean = qU_mean
        m_gpy.q_u_chol = GPy.util.choleskies.triang_to_flat(qU_chol)

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

        observed = [m.X, m.Y]
        infr = Inference(MAP(model=m, observed=observed), dtype=dtype)
        infr.initialize(X=X.shape, Y=Y.shape)
        infr.params[gp._extra_graphs[0].qU_mean] = mx.nd.array(qU_mean,
                                                               dtype=dtype)
        infr.params[gp._extra_graphs[0].qU_cov_W] = mx.nd.array(qU_cov_W,
                                                                dtype=dtype)
        infr.params[gp._extra_graphs[0].qU_cov_diag] = mx.nd.array(qU_cov_diag,
                                                                   dtype=dtype)

        loss, _ = infr.run(X=mx.nd.array(X, dtype=dtype),
                           Y=mx.nd.array(Y, dtype=dtype))

        # noise_free, diagonal
        mu_gpy, var_gpy = m_gpy.predict_noiseless(Xt)

        infr2 = TransferInference(ModulePredictionAlgorithm(
            m, observed=[m.X], target_variables=[m.Y]),
                                  infr_params=infr.params,
                                  dtype=np.float64)
        res = infr2.run(X=mx.nd.array(Xt, dtype=dtype))[0]
        mu_mf, var_mf = res[0].asnumpy()[0], res[1].asnumpy()[0]

        assert np.allclose(mu_gpy, mu_mf), (mu_gpy, mu_mf)
        assert np.allclose(var_gpy[:, 0], var_mf), (var_gpy[:, 0], var_mf)

        # noisy, diagonal
        mu_gpy, var_gpy = m_gpy.predict(Xt)

        infr2 = TransferInference(ModulePredictionAlgorithm(
            m, observed=[m.X], target_variables=[m.Y]),
                                  infr_params=infr.params,
                                  dtype=np.float64)
        infr2.inference_algorithm.model.Y.factor.svgp_predict.noise_free = False
        res = infr2.run(X=mx.nd.array(Xt, dtype=dtype))[0]
        mu_mf, var_mf = res[0].asnumpy()[0], res[1].asnumpy()[0]

        assert np.allclose(mu_gpy, mu_mf), (mu_gpy, mu_mf)
        assert np.allclose(var_gpy[:, 0], var_mf), (var_gpy[:, 0], var_mf)

        # TODO: The full covariance matrix prediction with SVGP in GPy may not be correct. Need further investigation.

        # noise_free, full_cov
        mu_gpy, var_gpy = m_gpy.predict_noiseless(Xt, full_cov=True)

        infr2 = TransferInference(ModulePredictionAlgorithm(
            m, observed=[m.X], target_variables=[m.Y]),
                                  infr_params=infr.params,
                                  dtype=np.float64)
        infr2.inference_algorithm.model.Y.factor.svgp_predict.diagonal_variance = False
        infr2.inference_algorithm.model.Y.factor.svgp_predict.noise_free = True
        res = infr2.run(X=mx.nd.array(Xt, dtype=dtype))[0]
        mu_mf, var_mf = res[0].asnumpy()[0], res[1].asnumpy()[0]

        print(var_gpy.shape, var_mf.shape)

        assert np.allclose(mu_gpy, mu_mf), (mu_gpy, mu_mf)
        assert np.allclose(var_gpy[:, :, 0], var_mf), (var_gpy[:, :,
                                                               0], var_mf)

        # noisy, full_cov
        mu_gpy, var_gpy = m_gpy.predict(Xt, full_cov=True)

        infr2 = TransferInference(ModulePredictionAlgorithm(
            m, observed=[m.X], target_variables=[m.Y]),
                                  infr_params=infr.params,
                                  dtype=np.float64)
        infr2.inference_algorithm.model.Y.factor.svgp_predict.diagonal_variance = False
        infr2.inference_algorithm.model.Y.factor.svgp_predict.noise_free = False
        res = infr2.run(X=mx.nd.array(Xt, dtype=dtype))[0]
        mu_mf, var_mf = res[0].asnumpy()[0], res[1].asnumpy()[0]

        assert np.allclose(mu_gpy, mu_mf), (mu_gpy, mu_mf)
        assert np.allclose(var_gpy[:, :, 0], var_mf), (var_gpy[:, :,
                                                               0], var_mf)
Esempio n. 25
0
    def test_prediction(self):
        np.random.seed(0)
        X = np.random.rand(10, 3)
        Xt = np.random.rand(20, 3)
        Y = np.random.rand(10, 1)
        noise_var = np.random.rand(1)
        lengthscale = np.random.rand(3)
        variance = np.random.rand(1)

        m_gpy = GPy.models.GPRegression(X=X,
                                        Y=Y,
                                        kernel=GPy.kern.RBF(
                                            3,
                                            ARD=True,
                                            lengthscale=lengthscale,
                                            variance=variance),
                                        noise_var=noise_var)

        dtype = 'float64'
        m = Model()
        m.N = Variable()
        m.X = Variable(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, 1),
                                           dtype=dtype)

        observed = [m.X, m.Y]
        infr = Inference(MAP(model=m, observed=observed), dtype=dtype)

        loss, _ = infr.run(X=mx.nd.array(X, dtype=dtype),
                           Y=mx.nd.array(Y, dtype=dtype))

        # noise_free, diagonal
        mu_gpy, var_gpy = m_gpy.predict_noiseless(Xt)

        infr2 = TransferInference(ModulePredictionAlgorithm(
            m, observed=[m.X], target_variables=[m.Y]),
                                  infr_params=infr.params,
                                  dtype=np.float64)
        res = infr2.run(X=mx.nd.array(Xt, dtype=dtype))[0]
        mu_mf, var_mf = res[0].asnumpy()[0], res[1].asnumpy()[0]

        assert np.allclose(mu_gpy, mu_mf), (mu_gpy, mu_mf)
        assert np.allclose(var_gpy[:, 0], var_mf), (var_gpy[:, 0], var_mf)

        # noisy, diagonal
        mu_gpy, var_gpy = m_gpy.predict(Xt)

        infr2 = TransferInference(ModulePredictionAlgorithm(
            m, observed=[m.X], target_variables=[m.Y]),
                                  infr_params=infr.params,
                                  dtype=np.float64)
        infr2.inference_algorithm.model.Y.factor.gp_predict.noise_free = False
        res = infr2.run(X=mx.nd.array(Xt, dtype=dtype))[0]
        mu_mf, var_mf = res[0].asnumpy()[0], res[1].asnumpy()[0]

        assert np.allclose(mu_gpy, mu_mf), (mu_gpy, mu_mf)
        assert np.allclose(var_gpy[:, 0], var_mf), (var_gpy[:, 0], var_mf)

        # noise_free, full_cov
        mu_gpy, var_gpy = m_gpy.predict_noiseless(Xt, full_cov=True)

        infr2 = TransferInference(ModulePredictionAlgorithm(
            m, observed=[m.X], target_variables=[m.Y]),
                                  infr_params=infr.params,
                                  dtype=np.float64)
        infr2.inference_algorithm.model.Y.factor.gp_predict.diagonal_variance = False
        infr2.inference_algorithm.model.Y.factor.gp_predict.noise_free = True
        res = infr2.run(X=mx.nd.array(Xt, dtype=dtype))[0]
        mu_mf, var_mf = res[0].asnumpy()[0], res[1].asnumpy()[0]

        assert np.allclose(mu_gpy, mu_mf), (mu_gpy, mu_mf)
        assert np.allclose(var_gpy, var_mf), (var_gpy, var_mf)

        # noisy, full_cov
        mu_gpy, var_gpy = m_gpy.predict(Xt, full_cov=True)

        infr2 = TransferInference(ModulePredictionAlgorithm(
            m, observed=[m.X], target_variables=[m.Y]),
                                  infr_params=infr.params,
                                  dtype=np.float64)
        infr2.inference_algorithm.model.Y.factor.gp_predict.diagonal_variance = False
        infr2.inference_algorithm.model.Y.factor.gp_predict.noise_free = False
        res = infr2.run(X=mx.nd.array(Xt, dtype=dtype))[0]
        mu_mf, var_mf = res[0].asnumpy()[0], res[1].asnumpy()[0]
        print((var_gpy, var_mf))

        assert np.allclose(mu_gpy, mu_mf), (mu_gpy, mu_mf)
        assert np.allclose(var_gpy, var_mf), (var_gpy, var_mf)
Esempio n. 26
0
    def test_draw_samples(self):
        np.random.seed(0)
        X = np.random.rand(10, 3)
        Y = np.random.rand(10, 1)
        noise_var = np.random.rand(1)
        lengthscale = np.random.rand(3)
        variance = np.random.rand(1)
        dtype = 'float64'

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

        m = Model()
        m.N = Variable()
        m.X = Variable(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, 1),
                                           dtype=dtype,
                                           rand_gen=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))
        gp = GaussianProcess.define_variable(X=X_var,
                                             kernel=kern,
                                             shape=(10, 1),
                                             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)
        }
        samples_2 = gp.draw_samples(F=mx.nd,
                                    variables=variables,
                                    num_samples=2).asnumpy()

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