Skip to content
This repository has been archived by the owner on Jun 14, 2024. It is now read-only.

Deferred initialization error #127

Open
tdiethe opened this issue Nov 16, 2018 · 0 comments
Open

Deferred initialization error #127

tdiethe opened this issue Nov 16, 2018 · 0 comments

Comments

@tdiethe
Copy link
Contributor

tdiethe commented Nov 16, 2018

When using gluon blocks, it's common to not specify the shape of e.g. the inputs to begin with, say:

        # Create net
        self.net = mx.gluon.nn.HybridSequential(prefix=self.prefix)
        with self.net.name_scope():
            for layer in self.network_shape[1:-1]:
                self.net.add(mx.gluon.nn.Dense(layer, activation="relu"))
            #  Last layer for classification
            self.net.add(mx.gluon.nn.Dense(self.network_shape[-1], flatten=True, in_units=self.network_shape[-2]))
        self.net.hybridize()
        self.net.initialize(mx.init.Xavier(magnitude=2.34), ctx=self.ctx)

then I create my MXFusion model as follows:

        self.model = Model()
        self.model.N = Variable()
        self.model.D = Variable()
        self.model.f = MXFusionGluonFunction(self.net, num_outputs=1, broadcastable=False)
        self.model.x = Variable(shape=(self.model.N, self.model.D))
        self.model.r = self.model.f(self.model.x)
        for v in self.model.r.factor.parameters.values():
            v.set_prior(Normal(mean=mx.nd.array([0]), variance=mx.nd.array([3.])))
            self.model.y = Categorical.define_variable(log_prob=self.model.r, shape=(self.model.N, 1), num_classes=2)

I then define my posterior and initialize:

            data = mx.nd.flatten(batch.data[0]).as_in_context(self.ctx)
            labels = batch.label[0].as_in_context(self.ctx)
            N, D = data.shape

            # pass some data to initialise the net
            self.net.forward(data[0, :])

            observed = [self.model.x, self.model.y]
            q = create_Gaussian_meanfield(model=self.model, observed=observed)
            alg = StochasticVariationalInference(num_samples=5, model=self.model, posterior=q, observed=observed)
            inference = GradBasedInference(inference_algorithm=alg, grad_loop=BatchInferenceLoop())
            inference.initialize(N=N, D=D, y=mx.nd.array(labels), x=mx.nd.array(data))

Now I'd like to set the variances to be some small value:

            for v_name, v in self.model.r.factor.parameters.items():
                qv_mean = q[v.uuid].factor.mean
                qv_variance = q[v.uuid].factor.variance
                a = inference.params.param_dict[qv_variance.uuid].data().asnumpy()
                a[:] = 1e-8

But here I get mxnet.gluon.parameter.DeferredInitializationError, since the shape of q_variance is still (256, 0).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant