Exemplo n.º 1
0
            #mean_grad_rms_analytical = log_p_grad_rms_analytical[:K]
            #log_sigma_grad_rms_analytical = log_p_grad_rms_analytical[K:]

            #mean_grad_clr2_analytical = log_p_grad_clr2_analytical[:K]
            #log_sigma_grad_clr2_analytical = log_p_grad_clr2_analytical[K:]

            #mean_grad_clr_analytical = log_p_grad_clr_analytical[:K]
            #log_sigma_grad_clr_analytical = log_p_grad_clr_analytical[K:]
            #log_p_grad = log_p_grad_analytical
            #log_p_grad_clr = log_p_grad_clr_analytical
            #log_p_grad_rms = log_p_grad_rms_analytical
            #log_p_grad_clr2 = log_p_grad_clr2_analytical

            log_p = np.array([fit_hmc.log_prob(s) for s in samples.T])
            elbo = np.mean(log_p) + compute_entropy(log_sigmas)
            #elbo_analytical = elbo_logit(zs, means, log_sigmas, X, Y)

            log_p_clr = np.array(
                [fit_hmc.grad_log_prob(s) for s in samples_clr.T])
            elbo_clr = np.mean(log_p_clr) + compute_entropy(log_sigmas_vb_clr)
            #elbo_clr_analytical = elbo_logit(zs, means_vb_clr, log_sigmas_vb_clr, X, Y)

            log_p_clr2 = np.array(
                [fit_hmc.grad_log_prob(s) for s in samples_clr2.T])
            elbo_clr2 = np.mean(log_p_clr2) + compute_entropy(
                log_sigmas_vb_clr2)
            #elbo_clr2_analytical = elbo_logit(zs, means_vb_clr2, log_sigmas_vb_clr2, X, Y)

            log_p_rms = np.array(
                [fit_hmc.grad_log_prob(s) for s in samples_rms.T])
                zs_t = np.random.standard_t(dof, size=(num_params, num_samples))
                zs_t_swa = np.random.standard_t(dof, size=(num_params, num_samples_swa))
                samples, grad_correction = reparametrize(zs, means, L)
                samples_rms, grad_correction_rms = reparametrize(zs, means_vb_rms, L_vb_rms)
                samples_clr, grad_correction_clr = reparametrize(zs, means_vb_clr, L_vb_clr)
                #samples_swa2, grad_correction_swa2 = reparametrize(zs, means_vb_clr2, log_sigmas_vb_clr2)
                #samples, grad_correction = reparametrize(zs_swa, means, log_sigmas)

                #log_p_grad1 = np.array([fit_hmc.grad_log_prob(s)])
                # evaluate gradient of log p (does grad_log_prob support vectorization??) and gradient of log q
                log_p_grad = np.array([fit_hmc.grad_log_prob(s) for s in samples.T])
                log_p_grad_rms = np.array([fit_hmc.grad_log_prob(s) for s in samples_rms.T])
                log_p_grad_clr = np.array([fit_hmc.grad_log_prob(s) for s in samples_clr.T])

                log_p = np.array([fit_hmc.log_prob(s) for s in samples.T])
                elbo= np.mean(log_p) + compute_entropy(L)

                log_p_clr = np.array([fit_hmc.grad_log_prob(s) for s in samples_clr.T])
                elbo_clr = np.mean(log_p_clr) + compute_entropy(L_vb_clr)

                log_p_rms = np.array([fit_hmc.grad_log_prob(s) for s in samples_rms.T])
                elbo_rms = np.mean(log_p_rms) + compute_entropy(L_vb_rms)
                entropy_grad_swa = np.diag(1./np.diag(L_vb_swa))
                entropy_grad_clr = np.diag(1./np.diag(L_vb_clr))
                entropy_grad_rms = np.diag(1./np.diag(L_vb_rms))
                #print(log_p_grad)
                #print( np.mean(log_p_grad*grad_correction, axis=0) + entropy_grad)
                #print(log_p_grad_analytical )
                # compute gradients wrt. mean and log_sigma
                mean_grad = np.mean(log_p_grad, axis=0)
                mean_grad_clr = np.mean(log_p_grad_clr, axis=0)