示例#1
0
    def test_reconcile_model_and_posterior(self):
        x = np.random.rand(1000, 1)
        y = np.random.rand(1000, 1)
        x_nd, y_nd = mx.nd.array(y), mx.nd.array(x)

        net1 = self.make_net()
        net1(x_nd)
        net2 = self.make_net()
        net2(x_nd)
        m1, _ = self.make_bnn_model(net1)
        m2, _ = self.make_bnn_model(net2)

        from mxfusion.inference.meanfield import create_Gaussian_meanfield
        from mxfusion.inference import StochasticVariationalInference
        observed1 = [m1.y, m1.x]
        observed2 = [m2.y, m2.x]
        q1 = create_Gaussian_meanfield(model=m1, observed=observed1)
        alg1 = StochasticVariationalInference(num_samples=3,
                                              model=m1,
                                              posterior=q1,
                                              observed=observed1)
        q2 = create_Gaussian_meanfield(model=m2, observed=observed2)
        alg2 = StochasticVariationalInference(num_samples=3,
                                              model=m2,
                                              posterior=q2,
                                              observed=observed2)
        component_map = mf.models.FactorGraph.reconcile_graphs(
            alg1._graphs,
            primary_previous_graph=alg2._graphs[0],
            secondary_previous_graphs=[alg2._graphs[1]])
        assert len(set(component_map)) == \
            len(set(alg1.graphs[0].components.values()).union(
                set(alg1.graphs[1].components.values())))
示例#2
0
    def test_meanfield_save_and_load(self):
        from mxfusion.inference.meanfield import create_Gaussian_meanfield
        from mxfusion.inference import StochasticVariationalInference
        from mxfusion.inference.grad_based_inference import GradBasedInference
        from mxfusion.inference import BatchInferenceLoop

        x = np.random.rand(1000, 1)
        y = np.random.rand(1000, 1)
        x_nd, y_nd = mx.nd.array(y), mx.nd.array(x)

        net = self.make_net()
        net(x_nd)

        m = self.make_model(net)

        observed = [m.y, m.x]
        q = create_Gaussian_meanfield(model=m, observed=observed)
        alg = StochasticVariationalInference(num_samples=3, model=m, observed=observed, posterior=q)
        infr = GradBasedInference(inference_algorithm=alg, grad_loop=BatchInferenceLoop())
        infr.initialize(y=y_nd, x=x_nd)
        infr.run(max_iter=1, learning_rate=1e-2, y=y_nd, x=x_nd)

        infr.save(prefix=self.PREFIX)

        net2 = self.make_net()
        net2(x_nd)

        m2 = self.make_model(net2)

        observed2 = [m2.y, m2.x]
        q2 = create_Gaussian_meanfield(model=m2, observed=observed2)
        alg2 = StochasticVariationalInference(num_samples=3, model=m2, observed=observed2, posterior=q2)
        infr2 = GradBasedInference(inference_algorithm=alg2, grad_loop=BatchInferenceLoop())
        infr2.initialize(y=y_nd, x=x_nd)

        # Load previous parameters
        infr2.load(primary_model_file=self.PREFIX+'_graph_0.json',
                   secondary_graph_files=[self.PREFIX+'_graph_1.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))

        infr2.run(max_iter=1, learning_rate=1e-2, y=y_nd, x=x_nd)
        self.remove_saved_files(self.PREFIX)
示例#3
0
    def test_forward_sampling(self):
        x = np.random.rand(1000, 1)
        y = np.random.rand(1000, 1) > 0.5
        x_nd, y_nd = mx.nd.array(y), mx.nd.array(x)

        self.net = self.make_net()
        self.net(x_nd)

        m = self.make_model(self.net)

        from mxfusion.inference.meanfield import create_Gaussian_meanfield
        from mxfusion.inference import StochasticVariationalInference
        from mxfusion.inference.grad_based_inference import GradBasedInference
        from mxfusion.inference.batch_loop import BatchInferenceLoop
        observed = [m.y, m.x]
        q = create_Gaussian_meanfield(model=m, observed=observed)
        alg = StochasticVariationalInference(num_samples=3,
                                             model=m,
                                             posterior=q,
                                             observed=observed)
        infr = GradBasedInference(inference_algorithm=alg,
                                  grad_loop=BatchInferenceLoop())
        infr.initialize(y=y_nd, x=x_nd)
        infr._verbose = True
        infr.run(max_iter=2, learning_rate=1e-2, y=y_nd, x=x_nd)

        infr2 = VariationalPosteriorForwardSampling(10, [m.x], infr, [m.r])
        infr2.run(x=x_nd)
示例#4
0
    def test_softplus_in_params(self):

        m = make_basic_model()

        x = np.random.rand(1000, 1)
        y = np.random.rand(1000, 1)
        x_nd, y_nd = mx.nd.array(y), mx.nd.array(x)

        from mxfusion.inference.meanfield import create_Gaussian_meanfield
        from mxfusion.inference import StochasticVariationalInference
        from mxfusion.inference.grad_based_inference import GradBasedInference
        from mxfusion.inference import BatchInferenceLoop
        observed = [m.x]
        q = create_Gaussian_meanfield(model=m, observed=observed)
        alg = StochasticVariationalInference(num_samples=3,
                                             model=m,
                                             observed=observed,
                                             posterior=q)
        infr = GradBasedInference(inference_algorithm=alg,
                                  grad_loop=BatchInferenceLoop())

        infr.initialize(x=x_nd)
        infr.run(max_iter=1, learning_rate=1e-2, x=x_nd)

        uuid_of_pos_var = m.v.uuid
        infr.params._params[uuid_of_pos_var]._data = mx.nd.array([-10])
        raw_value = infr.params._params[uuid_of_pos_var].data()
        transformed_value = infr.params[m.v]
        assert raw_value.asnumpy()[0] < 0 and transformed_value.asnumpy(
        )[0] > 0
示例#5
0
    def test_meanfield_minibatch(self):
        x = np.random.rand(1000, 1)
        y = np.random.rand(1000, 1)
        x_nd, y_nd = mx.nd.array(y), mx.nd.array(x)

        self.net = self.make_net()
        self.net(x_nd)

        m = self.make_model(self.net)

        from mxfusion.inference.meanfield import create_Gaussian_meanfield
        from mxfusion.inference import StochasticVariationalInference
        from mxfusion.inference.grad_based_inference import GradBasedInference
        from mxfusion.inference import MinibatchInferenceLoop
        observed = [m.y, m.x]
        q = create_Gaussian_meanfield(model=m, observed=observed)
        alg = StochasticVariationalInference(num_samples=3,
                                             model=m,
                                             observed=observed,
                                             posterior=q)
        infr = GradBasedInference(inference_algorithm=alg,
                                  grad_loop=MinibatchInferenceLoop(
                                      batch_size=100, rv_scaling={m.y: 10}))

        infr.initialize(y=(100, 1), x=(100, 1))
        infr.run(max_iter=1, learning_rate=1e-2, y=y_nd, x=x_nd)
示例#6
0
    def test_meanfield_saving(self):
        dtype = get_default_dtype()
        x = np.random.rand(10, 1)
        y = np.random.rand(10, 1)
        x_nd, y_nd = mx.nd.array(y, dtype=dtype), mx.nd.array(x, dtype=dtype)

        self.net = self.make_net()
        self.net(x_nd)

        m = self.make_model(self.net)

        from mxfusion.inference.meanfield import create_Gaussian_meanfield
        from mxfusion.inference import StochasticVariationalInference
        from mxfusion.inference.grad_based_inference import GradBasedInference
        from mxfusion.inference import BatchInferenceLoop
        observed = [m.y, m.x]

        q = create_Gaussian_meanfield(model=m, observed=observed)
        alg = StochasticVariationalInference(num_samples=3,
                                             model=m,
                                             observed=observed,
                                             posterior=q)
        infr = GradBasedInference(inference_algorithm=alg,
                                  grad_loop=BatchInferenceLoop())
        infr.initialize(y=y_nd, x=x_nd)
        infr.run(max_iter=1, learning_rate=1e-2, y=y_nd, x=x_nd)

        infr.save(self.ZIPNAME)
        os.remove(self.ZIPNAME)
示例#7
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]
示例#8
0
    def test_meanfield_save_and_load(self):
        dtype = get_default_dtype()
        from mxfusion.inference.meanfield import create_Gaussian_meanfield
        from mxfusion.inference import StochasticVariationalInference
        from mxfusion.inference.grad_based_inference import GradBasedInference
        from mxfusion.inference import BatchInferenceLoop

        x = np.random.rand(1000, 1)
        y = np.random.rand(1000, 1)
        x_nd, y_nd = mx.nd.array(y, dtype=dtype), mx.nd.array(x, dtype=dtype)

        net = self.make_net()
        net(x_nd)

        m = self.make_model(net)

        observed = [m.y, m.x]
        q = create_Gaussian_meanfield(model=m, observed=observed)
        alg = StochasticVariationalInference(num_samples=3,
                                             model=m,
                                             observed=observed,
                                             posterior=q)
        infr = GradBasedInference(inference_algorithm=alg,
                                  grad_loop=BatchInferenceLoop())
        infr.initialize(y=y_nd, x=x_nd)
        infr.run(max_iter=1, learning_rate=1e-2, y=y_nd, x=x_nd)

        infr.save(self.ZIPNAME)

        net2 = self.make_net()
        net2(x_nd)

        m2 = self.make_model(net2)

        observed2 = [m2.y, m2.x]
        q2 = create_Gaussian_meanfield(model=m2, observed=observed2)
        alg2 = StochasticVariationalInference(num_samples=3,
                                              model=m2,
                                              observed=observed2,
                                              posterior=q2)
        infr2 = GradBasedInference(inference_algorithm=alg2,
                                   grad_loop=BatchInferenceLoop())
        infr2.initialize(y=y_nd, x=x_nd)

        # Load previous parameters
        infr2.load(self.ZIPNAME)

        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))

        infr2.run(max_iter=1, learning_rate=1e-2, y=y_nd, x=x_nd)
        os.remove(self.ZIPNAME)