def p_y_zt(self, z, t): # p(y|t,z) mu2_t0 = self.mu2_t0(z) mu2_t1 = self.mu2_t1(z) sig = Variable(torch.ones(mu2_t0.size())) if mu2_t0.is_cuda: sig = sig.cuda() if t: y = dist.normal(mu2_t1, sig) else: y = dist.normal(mu2_t0, sig) return y
def _action_selection(self, action_mean, action_std, exploration=True): if exploration: action = dist.normal(action_mean, action_std) else: action = dist.Normal(action_mean, action_std).analytic_mean() return action
def reconstruct_img(self, x): # encode image x z_mu, z_sigma = self.encoder(x) # sample in latent space z = dist.normal(z_mu, z_sigma) # decode the image (note we don't sample in image space) mu_img = self.decoder(z) return mu_img
def forward(self, x): x = F.leaky_relu(self.conv1(x)) x = F.leaky_relu(self.conv2(x)) x = F.leaky_relu(self.conv3(x)) x = F.leaky_relu(self.conv4(x)) x = x.view(x.size(0), -1) x = F.leaky_relu(self.linear1(x)) mean = 5e-4 * F.sigmoid(self.action_mean(x)) std = F.softplus(self.action_std(x)) action = dist.normal(mean, std) # remove it from the graph action = action.detach() # calculate the log probability log_p = dist.normal.log_pdf(action, mean, std) entropy = self.entropy(std) return action, log_p, entropy
def test_subsample_gradient(trace_graph, reparameterized): pyro.clear_param_store() data_size = 2 subsample_size = 1 num_particles = 1000 precision = 0.333 data = dist.normal(ng_zeros(data_size), ng_ones(data_size)) def model(subsample_size): with pyro.iarange("data", len(data), subsample_size) as ind: x = data[ind] z = pyro.sample("z", dist.Normal(ng_zeros(len(x)), ng_ones(len(x)), reparameterized=reparameterized)) pyro.observe("x", dist.Normal(z, ng_ones(len(x)), reparameterized=reparameterized), x) def guide(subsample_size): mu = pyro.param("mu", lambda: Variable(torch.zeros(len(data)), requires_grad=True)) sigma = pyro.param("sigma", lambda: Variable(torch.ones(1), requires_grad=True)) with pyro.iarange("data", len(data), subsample_size) as ind: mu = mu[ind] sigma = sigma.expand(subsample_size) pyro.sample("z", dist.Normal(mu, sigma, reparameterized=reparameterized)) optim = Adam({"lr": 0.1}) inference = SVI(model, guide, optim, loss="ELBO", trace_graph=trace_graph, num_particles=num_particles) # Compute gradients without subsampling. inference.loss_and_grads(model, guide, subsample_size=data_size) params = dict(pyro.get_param_store().named_parameters()) expected_grads = {name: param.grad.data.clone() for name, param in params.items()} zero_grads(params.values()) # Compute gradients with subsampling. inference.loss_and_grads(model, guide, subsample_size=subsample_size) actual_grads = {name: param.grad.data.clone() for name, param in params.items()} for name in sorted(params): print('\nexpected {} = {}'.format(name, expected_grads[name].cpu().numpy())) print('actual {} = {}'.format(name, actual_grads[name].cpu().numpy())) assert_equal(actual_grads, expected_grads, prec=precision)
def forward(self, x): # q(t|x) logits_t = self.logits_t.forward(x) qt = dist.bernoulli(logits_t) # q(y|x,t) hqy = self.hqy.forward(x) mu_qy_t0 = self.mu_qy_t0.forward(hqy) mu_qy_t1 = self.mu_qy_t1.forward(hqy) sig = Variable(torch.ones(mu_qy_t0.size())) if mu_qy_t0.is_cuda: sig = sig.cuda() qy = dist.normal(qt * mu_qy_t1 + (1. - qt) * mu_qy_t0, sig) # q(z|x,t,y) hqz = self.hqz.forward(torch.cat((x, qy), 1)) muq_t0, sigmaq_t0 = self.muq_t0_sigmaq_t0.forward(hqz) muq_t1, sigmaq_t1 = self.muq_t1_sigmaq_t1.forward(hqz) return muq_t0, sigmaq_t0, muq_t1, sigmaq_t1, qt
def model(x): pass def guide(x): pass opt = Adam({"lr": 0.0001}) loss = SVI(model, guide, opt, loss="ELBO") loss.step(1) for i in range(10): mu = Variable(torch.zeros(1)) # mean zero sigma = Variable(torch.ones(1)) # unit variance x = dist.normal(mu, sigma) # x is a sample from N(0,1) print(x) log_p_x = dist.normal.log_pdf(x, mu, sigma) print(log_p_x) x = pyro.sample("my_sample", dist.normal, mu, sigma) print(x) def weather(): cloudy = pyro.sample('cloudy', dist.bernoulli, Variable(torch.Tensor([0.3]))) cloudy = 'cloudy' if cloudy.data[0] == 1.0 else 'sunny' mean_temp = {'cloudy': [55.0], 'sunny': [75.0]}[cloudy] sigma_temp = {'cloudy': [10.0], 'sunny': [15.0]}[cloudy]
# http://pyro.ai/examples/intro_part_i.html import torch from torch.autograd import Variable import pyro import pyro.distributions as dist mu = Variable(torch.zeros(1)) sigma = Variable(torch.ones(1)) x = dist.normal(mu, sigma) print(x) log_p_x = dist.normal.log_pdf(x, mu, sigma) print(log_p_x) x = pyro.sample('my_sample', dist.normal, mu, sigma) print(x) def weather(): cloudy = pyro.sample('cloudy', dist.bernoulli, Variable(torch.Tensor([0.3]))) cloudy = 'cloudy' if cloudy.data[0] == 1.0 else 'sunny' mean_temp = {'cloudy': [55.0], 'sunny': [75.0]}[cloudy] sigma_temp = {'cloudy': [10.0], 'sunny': [15.0]}[cloudy] temp = pyro.sample('temp', dist.normal, Variable(torch.Tensor(mean_temp)), Variable(torch.Tensor(sigma_temp))) return cloudy, temp.data[0]
def reconstruct_img(self, x): z_mu, z_sigma = self.encoder(x) z = dist.normal(z_mu, z_sigma) img_mu = self.decoder(z) return img_mu
print(marginal(guess)) plt.hist([marginal(guess).data[0] for _ in range(100)], range=(5.0, 12.0)) plt.title("P(measurement | guess)") plt.xlabel("weight") plt.ylabel("#") plt.show() # ------------Normal distribution---------------- samples = [] for i in range(1000): mu = Variable(torch.zeros(1)) sigma = Variable(torch.ones(1)) samples.append((dist.normal(mu, sigma)).data[0]) plt.hist(samples, range=(-8.0, 8.0)) plt.title("P(sample)") plt.xlabel("value") plt.ylabel("#") plt.show() # --------------Exponential distribution-------------- samples = [] for i in range(1000): _lambda = Variable(torch.Tensor([0.5])) samples.append((dist.exponential(_lambda)).data[0])
def forward(self, x): mu, sigma = self.vae.encoder(x) x = dist.normal(mu, sigma) if self.exo_var is not None: x = torch.cat((x, self.exo_var), 1) return self.ff(x)
def reconstruct_ts(self, x): # encode, sample in latent space, and decode z_mu, z_sigma = self.encoder(x) z = dist.normal(z_mu, z_sigma) mu_ts = self.decoder(z) return mu_ts
# http://pyro.ai/examples/intro_part_i.html import torch from torch.autograd import Variable import pyro import pyro.distributions as dist mu = Variable(torch.zeros(1)) sigma = Variable(torch.ones(1)) x = dist.normal(mu, sigma) print(x) log_p_x = dist.normal.log_pdf(x, mu, sigma) print(log_p_x) x = pyro.sample('my_sample', dist.normal, mu, sigma) print(x) def weather(): cloudy = pyro.sample('cloudy', dist.bernoulli, Variable(torch.Tensor([0.3]))) cloudy = 'cloudy' if cloudy.data[0] == 1.0 else 'sunny' mean_temp = {'cloudy': [55.0], 'sunny': [75.0]}[cloudy] sigma_temp = {'cloudy': [10.0], 'sunny': [15.0]}[cloudy] temp = pyro.sample('temp', dist.normal, Variable(torch.Tensor(mean_temp)), Variable(torch.Tensor(sigma_temp))) return cloudy, temp.data[0] for _ in range(3):