def train_bayes(training_generator): svi, model, guide = get_pyro_model(return_all=True) loss_hist = [] for e in range(EPOCHS): losses = [] for x_data, y_data in tqdm(training_generator): losses.append(svi.step(x_data, y_data)) #test_model(model(x_data,y_data), model(x_data,y_data), Trace_ELBO()) loss_hist.append(np.mean(losses)) print(f"epoch {e}/{EPOCHS} :", loss_hist[-1]) trace = poutine.trace(model).get_trace(x_data, y_data) trace.compute_log_prob( ) # optional, but allows printing of log_prob shapes print(trace.format_shapes()) plt.plot(loss_hist) plt.yscale('log') plt.title("ELBO") plt.xlabel("step") plt.ylabel("Epoch loss") trace_summary(svi, model, x_data, y_data) for name, value in pyro.get_param_store().items(): print(name, pyro.param(name))
#test_model(model(x_data,y_data), model(x_data,y_data), Trace_ELBO()) loss_hist.append(np.mean(losses)) print(f"epoch {e}/{EPOCHS} :", loss_hist[-1]) trace = poutine.trace(model).get_trace(x_data, y_data) trace.compute_log_prob() # optional, but allows printing of log_prob shapes print(trace.format_shapes()) plt.plot(loss_hist) plt.yscale('log') plt.title("ELBO") plt.xlabel("step") plt.ylabel("Epoch loss") df = trace_summary(svi, model, x_data, y_data) for name, value in pyro.get_param_store().items(): print(name, pyro.param(name)) # plot the gaussian data # g = guide(None, None) # p = g(x_data) # plt.figure() # pp = p.cpu().detach().numpy()[:,0] # yy = y_data.cpu().detach().numpy()[:,0] # ss = y_data.cpu().detach().numpy()[:,1] # i = np.linspace(.1, 1, 30) # for x in x_data.cpu().detach().numpy(): # plt.plot(i, x) # plt.plot(pp, .25*np.ones_like(pp),'o')