Ejemplo n.º 1
0
vb.optimize_bmc_model(maxiter=100,verbose=False)
#%% Tracking devices
vb.save_distribution("%s/distrib%i"%(folder_name,0))
nplot = 201
delta_x = torch.linspace(-20,20,nplot)
delta_x_np = delta_x.flatten().numpy()
tp_np = (logjoint(delta_x.reshape(-1,1)).cpu()).flatten().numpy()
prediction_np = (vb.bmcmodel.prediction(delta_x.reshape(-1,1),cov="none")*vb.evals_std + vb.evals_mean).\
                numpy().astype(float)        
dmeans = [torch.norm(vb.currentq_mean.to("cpu")-true_mean).numpy()]
dcovs = [torch.norm(vb.currentq_mean.to("cpu")-true_cov,2).numpy()]
elbo_list = [vb.evidence_lower_bound(nsamples=10000).cpu().numpy()]
step_list = [0]
time_list = [0]
vbp_list = []
vbp = vb.current_logq(delta_x.reshape(-1,1)).cpu().flatten().numpy().astype(float)
vbp_list.append(vbp)

plt.plot(delta_x_np,np.exp(prediction_np))
plt.plot(delta_x_np,np.exp(tp_np))
raise KeyError
#%% Main loop
for i in range(50):
    tictoc.tic()
    _ = vb.update()
    if ((i+1)%10) == 0:
        vb.update_full()
    #%% Save trackings
    elapsed = tictoc.toc(printing=False)
    dmeans.append(np.linalg.norm(vb.currentq_mean.cpu()-true_mean,2))
    dcovs.append(np.linalg.norm(vb.currentq_cov.cpu()-true_cov,2))
Ejemplo n.º 2
0
elbo_np = np.array(elbo_list).astype(float)
step_list_np = np.array(step_list).astype(int)
times_np = np.array(time_list)
np.savez("%s/tracking" % folder_name,
         time=times_np,
         elbo=elbo_np,
         steps=step_list_np)
#%%
Nplot = 41
x, y = torch.linspace(-10, 10, Nplot), torch.linspace(-10, 10, Nplot)
X, Y = torch.meshgrid(x, y)
XY_ = torch.stack([X, Y], dim=-1).reshape(-1, 2)
Zgp_ = vb.scaled_prediction(XY_)
Zgp = Zgp_.reshape(*X.shape)
Zq_ = vb.current_logq(XY_)
Zq = Zq_.reshape(*X.shape)

fig1 = plt.figure()
ax1 = fig1.add_subplot(111, projection='3d')
ax1.plot_surface(X.numpy(), Y.numpy(), Zgp.numpy())
plt.show()

fig2 = plt.figure()
ax2 = fig2.add_subplot(111, projection='3d')
ax2.plot_surface(X.numpy(), Y.numpy(), Zq.numpy())
plt.show()
#%%
Nplot = 41
x = torch.linspace(-5, 5, Nplot).reshape(-1, 1)
xy_ = torch.cat([x, 1.0986122886681098 * torch.ones_like(x)], dim=-1)