コード例 #1
0
def prep_first_order_network_inference(population):
    """ Initialize functions that compute the gradient and Hessian of
        the log probability with respect to the differentiable GLM
        parameters, e.g. the weight matrix if it exists.
    """
    network = population.network
    syms = population.get_variables()

    # Compute gradients of the log prob wrt the GLM parameters
    network_syms = differentiable(syms['net'])

    print "Computing gradient of the network prior w.r.t. the differentiable GLM parameters"
    g_network_logprior, _ = grad_wrt_list(network.log_p, _flatten(network_syms))

    # TODO: Replace this with a function that just gets the shapes?
    x0 = population.sample()
    nvars = population.extract_vars(x0, 0)
    dnvars = get_vars(network_syms, nvars['net'])
    _,network_shapes = packdict(dnvars)

    # Private function to compute the log probability and its gradient
    # with respect to a set of parameters
    def nlp(x_network_vec, x):
        """
        Helper function to compute the negative log posterior for a given set
        of GLM parameters. The parameters are passed in as a vector.
        """
        x_network = unpackdict(x_network_vec, network_shapes)
        set_vars(network_syms, x['net'], x_network)
        lp = seval(network.log_prior,
                   syms,
                   x)

        return -1.0 * lp

    def grad_nlp(x_glm_vec, x):
        """
        Helper function to compute the gradient of negative log posterior for
        a given set of GLM parameters. The parameters are passed in as a vector.
        """
        x_network = unpackdict(x_glm_vec, network_shapes)
        set_vars(network_syms, x['net'], x_network)
        glp = seval(g_network_logprior,
                   syms,
                   x)

        return -1.0 * glp

    return network_syms, nlp, grad_nlp
コード例 #2
0
def prep_first_order_glm_inference(population):
    """ Initialize functions that compute the gradient and Hessian of
        the log probability with respect to the differentiable GLM
        parameters, e.g. the weight matrix if it exists.
    """
    glm = population.glm
    syms = population.get_variables()

    # Compute gradients of the log prob wrt the GLM parameters
    glm_syms = differentiable(syms['glm'])

    print "Computing gradient of the prior w.r.t. the differentiable GLM parameters"
    g_glm_logprior, _ = grad_wrt_list(glm.log_prior, _flatten(glm_syms))

    print "Computing gradient of the GLM likelihood w.r.t. the differentiable GLM parameters"
    g_glm_ll, _ = grad_wrt_list(glm.ll, _flatten(glm_syms))

    # TODO: Replace this with a function that just gets the shapes?
    x0 = population.sample()
    nvars = population.extract_vars(x0, 0)
    dnvars = get_vars(glm_syms, nvars['glm'])
    _,glm_shapes = packdict(dnvars)

    # Private function to compute the log probability and its gradient
    # with respect to a set of parameters
    def nlp(x_glm_vec, x):
        """
        Helper function to compute the negative log posterior for a given set
        of GLM parameters. The parameters are passed in as a vector.
        """
        x_glm = unpackdict(x_glm_vec, glm_shapes)
        set_vars(glm_syms, x['glm'], x_glm)
        lp = seval(glm.log_prior,
                   syms,
                   x)

        # Add the likelihood of each data sequence
        for data in population.data_sequences:
            # Set the data
            population.set_data(data)
            lp += seval(glm.ll,
                        syms,
                        x)

        return -1.0 * lp

    def grad_nlp(x_glm_vec, x):
        """
        Helper function to compute the gradient of negative log posterior for
        a given set of GLM parameters. The parameters are passed in as a vector.
        """
        x_glm = unpackdict(x_glm_vec, glm_shapes)
        set_vars(glm_syms, x['glm'], x_glm)
        glp = seval(g_glm_logprior,
                   syms,
                   x)

        # Add the likelihood of each data sequence
        for data in population.data_sequences:
            # Set the data
            population.set_data(data)
            glp += seval(g_glm_ll,
                        syms,
                        x)

        return -1.0 * glp

    return glm_syms, nlp, grad_nlp