Exemplo n.º 1
0
def test_elbo():
    mu0 = 1.5
    sigma = 1.0
    y_obs = np.array([1.6, 1.4])

    post_mu = np.array([1.88], dtype=theano.config.floatX)
    post_sd = np.array([1], dtype=theano.config.floatX)
    # Create a model for test
    with Model() as model:
        mu = Normal('mu', mu=mu0, sd=sigma)
        Normal('y', mu=mu, sd=1, observed=y_obs)

    # Create variational gradient tensor
    mean_field = MeanField(model=model)
    elbo = -KL(mean_field)()(10000)

    mean_field.shared_params['mu'].set_value(post_mu)
    mean_field.shared_params['rho'].set_value(np.log(np.exp(post_sd) - 1))

    f = theano.function([], elbo)
    elbo_mc = f()

    # Exact value
    elbo_true = (-0.5 * (3 + 3 * post_mu**2 - 2 *
                         (y_obs[0] + y_obs[1] + mu0) * post_mu + y_obs[0]**2 +
                         y_obs[1]**2 + mu0**2 + 3 * np.log(2 * np.pi)) + 0.5 *
                 (np.log(2 * np.pi) + 1))
    np.testing.assert_allclose(elbo_mc, elbo_true, rtol=0, atol=1e-1)
Exemplo n.º 2
0
    def from_full_rank(cls, full_rank):
        """
        Construct FullRankADVI from FullRank approximation

        Parameters
        ----------
        full_rank : :class:`FullRank`
            approximation to start with

        Returns
        -------
        :class:`FullRankADVI`
        """
        if not isinstance(full_rank, FullRank):
            raise TypeError('Expected MeanField, got %r' % full_rank)
        inference = object.__new__(cls)
        objective = KL(full_rank)(None)
        inference.hist = np.asarray(())
        inference.objective = objective
        return inference
Exemplo n.º 3
0
    def from_mean_field(cls, mean_field):
        """
        Construct ADVI from MeanField approximation

        Parameters
        ----------
        mean_field : :class:`MeanField`
            approximation to start with

        Returns
        -------
        :class:`ADVI`
        """
        if not isinstance(mean_field, MeanField):
            raise TypeError('Expected MeanField, got %r' % mean_field)
        inference = object.__new__(cls)
        objective = KL(mean_field)(None)
        inference.hist = np.asarray(())
        inference.objective = objective
        return inference
Exemplo n.º 4
0
    def from_mean_field(cls, mean_field, gpu_compat=False):
        """
        Construct FullRankADVI from MeanField approximation

        Parameters
        ----------
        mean_field : MeanField
            approximation to start with

        Flags
        -----
        gpu_compat : bool
            use GPU compatible version or not

        Returns
        -------
        FullRankADVI
        """
        full_rank = FullRank.from_mean_field(mean_field, gpu_compat)
        inference = object.__new__(cls)
        objective = KL(full_rank)(None)
        inference.objective = objective
        inference.hist = np.asarray(())
        return inference