def guide(): mu_q = pyro.param("mu_q", Variable(self.analytic_mu_n.data + 0.094 * torch.ones(2), requires_grad=True)) log_sig_q = pyro.param("log_sig_q", Variable( self.analytic_log_sig_n.data - 0.11 * torch.ones(2), requires_grad=True)) sig_q = torch.exp(log_sig_q) trivial_baseline = pyro.module("mu_baseline", pt_mu_baseline, tags="baseline") baseline_value = trivial_baseline(ng_ones(1)) mu_latent = pyro.sample("mu_latent", dist.Normal(mu_q, sig_q, reparameterized=False), baseline=dict(baseline_value=baseline_value)) def obs_inner(i, _i, _x): for k in range(n_superfluous_top + n_superfluous_bottom): z_baseline = pyro.module("z_baseline_%d_%d" % (i, k), pt_superfluous_baselines[3 * k + i], tags="baseline") baseline_value = z_baseline(mu_latent.detach()).unsqueeze(-1) mean_i = pyro.param("mean_%d_%d" % (i, k), Variable(0.5 * torch.ones(4 - i, 1), requires_grad=True)) pyro.sample("z_%d_%d" % (i, k), dist.Normal(mean_i, ng_ones(4 - i, 1), reparameterized=False), baseline=dict(baseline_value=baseline_value)) def obs_outer(i, x): pyro.map_data("map_obs_inner_%d" % i, x, lambda _i, _x: obs_inner(i, _i, _x), batch_size=4 - i) pyro.map_data("map_obs_outer", [self.data_tensor[0:4, :], self.data_tensor[4:7, :], self.data_tensor[7:9, :]], lambda i, x: obs_outer(i, x), batch_size=3) return mu_latent
def guide(): mu_q = pyro.param( "mu_q", Variable(self.analytic_mu_n.data + 0.234 * torch.ones(2), requires_grad=True)) log_sig_q = pyro.param( "log_sig_q", Variable(self.analytic_log_sig_n.data - 0.27 * torch.ones(2), requires_grad=True)) sig_q = torch.exp(log_sig_q) mu_latent = pyro.sample( "mu_latent", fakes.nonreparameterized_normal, mu_q, sig_q, baseline=dict(use_decaying_avg_baseline=True)) def obs_outer(i, x): pyro.map_data("map_obs_inner_%d" % i, x, lambda _i, _x: None, batch_size=3) pyro.map_data("map_obs_outer", self.data, lambda i, x: obs_outer(i, x), batch_size=3) return mu_latent
def guide(): mu_q = pyro.param( "mu_q", Variable(analytic_mu_n.data + torch.Tensor([-0.18, 0.23]), requires_grad=True)) log_sig_q = pyro.param( "log_sig_q", Variable(analytic_log_sig_n.data - torch.Tensor([-0.18, 0.23]), requires_grad=True)) sig_q = torch.exp(log_sig_q) pyro.sample("mu_latent", dist.normal, mu_q, sig_q) if map_type == "list" or map_type is None: pyro.map_data("aaa", data, lambda i, x: None, batch_size=batch_size) elif map_type == "tensor": tdata = torch.cat([xi.view(1, -1) for xi in data], 0) # dummy map_data to do subsampling for observe pyro.map_data("aaa", tdata, lambda i, x: None, batch_size=batch_size) else: pass
def model(): mu_latent = pyro.sample("mu_latent", fakes.nonreparameterized_normal, self.mu0, torch.pow(self.lam0, -0.5)) def obs_inner(i, _i, _x): for k in range(n_superfluous_top): pyro.sample("z_%d_%d" % (i, k), fakes.nonreparameterized_normal, ng_zeros(4 - i, 1), ng_ones(4 - i, 1)) pyro.observe("obs_%d" % i, dist.normal, _x, mu_latent, torch.pow(self.lam, -0.5)) for k in range(n_superfluous_top, n_superfluous_top + n_superfluous_bottom): pyro.sample("z_%d_%d" % (i, k), fakes.nonreparameterized_normal, ng_zeros(4 - i, 1), ng_ones(4 - i, 1)) def obs_outer(i, x): pyro.map_data("map_obs_inner_%d" % i, x, lambda _i, _x: obs_inner(i, _i, _x), batch_size=4 - i) pyro.map_data("map_obs_outer", [ self.data_tensor[0:4, :], self.data_tensor[4:7, :], self.data_tensor[7:9, :] ], lambda i, x: obs_outer(i, x), batch_size=3) return mu_latent
def model(): p_latent = pyro.sample("p_latent", dist.beta, self.alpha0, self.beta0) pyro.map_data("aaa", self.data, lambda i, x: pyro.observe( "obs_{}".format(i), dist.bernoulli, x, p_latent), batch_size=self.batch_size) return p_latent
def model(): p_latent = pyro.sample("p_latent", Beta(self.alpha0, self.beta0)) x_dist = Bernoulli(p_latent) pyro.map_data(self.data, lambda i, x: pyro.observe("obs", x_dist, x), batch_size=2) return p_latent
def map_data_vector_cuda_model(subsample_size): mu = Variable(torch.zeros(20).cuda()) sigma = Variable(torch.ones(20).cuda()) pyro.map_data("data", mu, lambda i, mu: pyro.sample("x", dist.normal, mu, sigma[i]), batch_size=subsample_size, use_cuda=True)
def guide(): alpha_q_log = pyro.param("alpha_q_log", Variable(self.log_alpha_n.data + 0.17, requires_grad=True)) beta_q_log = pyro.param("beta_q_log", Variable(self.log_beta_n.data - 0.143, requires_grad=True)) alpha_q, beta_q = torch.exp(alpha_q_log), torch.exp(beta_q_log) pyro.sample("p_latent", dist.beta, alpha_q, beta_q) pyro.map_data("aaa", self.data, lambda i, x: None, batch_size=self.batch_size)
def map_data_iter_cuda_model(subsample_size): mu = Variable(torch.zeros(20).cuda()) sigma = Variable(torch.ones(20).cuda()) pyro.map_data( "data", list(mu), lambda i, mu: pyro.sample("x_{}".format(i), dist.normal, mu, sigma[i]), batch_size=subsample_size, use_cuda=True)
def model(): lambda_latent = pyro.sample("lambda_latent", Gamma(self.alpha0, self.beta0)) x_dist = Poisson(lambda_latent) # x0 = pyro.observe("obs0", x_dist, self.data[0]) pyro.map_data(self.data, lambda i, x: pyro.observe("obs", x_dist, x), batch_size=3) return lambda_latent
def model(): mu_latent = pyro.sample("mu_latent", dist.normal, self.mu0, torch.pow(self.lam0, -0.5)) pyro.map_data("aaa", self.data, lambda i, x: pyro.observe( "obs_%d" % i, dist.normal, x, mu_latent, torch.pow(self.lam, -0.5)), batch_size=self.batch_size) return mu_latent
def model(): prior_dist = DiagNormal(self.mu0, torch.pow(self.lam0, -0.5)) mu_latent = pyro.sample("mu_latent", prior_dist) x_dist = DiagNormal(mu_latent, torch.pow(self.lam, -0.5)) # x = pyro.observe("obs", x_dist, self.data) pyro.map_data(self.data, lambda i, x: pyro.observe("obs_%d" % i, x_dist, x), batch_size=1) return mu_latent
def model(means, stds): return pyro.map_data("a", means, lambda i, x: pyro.map_data("a_{}".format(i), stds, lambda j, y: pyro.sample("x_{}{}".format(i, j), dist.normal, x, y), batch_size=std_batch_size), batch_size=mean_batch_size)
def guide(): mu_q = pyro.param("mu_q", Variable(self.analytic_mu_n.data + 0.134 * torch.ones(2), requires_grad=True)) log_sig_q = pyro.param("log_sig_q", Variable( self.analytic_log_sig_n.data - 0.14 * torch.ones(2), requires_grad=True)) sig_q = torch.exp(log_sig_q) pyro.sample("mu_latent", dist.Normal(mu_q, sig_q, reparameterized=reparameterized)) pyro.map_data("aaa", self.data, lambda i, x: None, batch_size=self.batch_size)
def model(batch_size_outer=2, batch_size_inner=2): mu_latent = pyro.sample("mu_latent", dist.normal, ng_zeros(1), ng_ones(1)) def outer(i, x): pyro.map_data("map_inner_%d" % i, x, lambda _i, _x: inner(i, _i, _x), batch_size=batch_size_inner) def inner(i, _i, _x): pyro.sample("z_%d_%d" % (i, _i), dist.normal, mu_latent + _x, ng_ones(1)) pyro.map_data("map_outer", [[ng_ones(1)] * 2] * 2, lambda i, x: outer(i, x), batch_size=batch_size_outer) return mu_latent
def guide(): mu_q = pyro.param( "mu_q", Variable(self.analytic_mu_n.data + 0.134 * torch.ones(2), requires_grad=True)) log_sig_q = pyro.param( "log_sig_q", Variable(self.analytic_log_sig_n.data - 0.09 * torch.ones(2), requires_grad=True)) sig_q = torch.exp(log_sig_q) q_dist = DiagNormal(mu_q, sig_q) q_dist.reparametrized = reparametrized pyro.sample("mu_latent", q_dist) pyro.map_data(self.data, lambda i, x: None, batch_size=1)
def model(): mu_latent = pyro.sample( "mu_latent", dist.Normal(self.mu0, torch.pow(self.lam0, -0.5), reparameterized=False)) def obs_outer(i, x): pyro.map_data("map_obs_inner_%d" % i, x, lambda _i, _x: obs_inner(i, _i, _x), batch_size=3) def obs_inner(i, _i, _x): pyro.observe("obs_%d_%d" % (i, _i), dist.normal, _x, mu_latent, torch.pow(self.lam, -0.5)) pyro.map_data("map_obs_outer", self.data, lambda i, x: obs_outer(i, x), batch_size=3) return mu_latent
def map_data_iter_model(subsample_size): mu = Variable(torch.zeros(20)) sigma = Variable(torch.ones(20)) def local_model(i, unused): pyro.sample("x_{}".format(i), dist.normal, mu[i], sigma[i]) return i return pyro.map_data('mapdata', range(20), local_model, batch_size=subsample_size)
def guide(): mu_q = pyro.param("mu_q", Variable(self.analytic_mu_n.data + 0.234 * torch.ones(2), requires_grad=True)) log_sig_q = pyro.param("log_sig_q", Variable( self.analytic_log_sig_n.data - 0.27 * torch.ones(2), requires_grad=True)) sig_q = torch.exp(log_sig_q) mu_latent = pyro.sample( "mu_latent", dist.Normal(mu_q, sig_q, reparameterized=False), baseline=dict(use_decaying_avg_baseline=True)) def obs_outer(i, x): pyro.map_data("map_obs_inner_%d" % i, x, lambda _i, _x: None, batch_size=3) pyro.map_data("map_obs_outer", self.data, lambda i, x: obs_outer(i, x), batch_size=3) return mu_latent
def model(): mu_latent = pyro.sample("mu_latent", dist.normal, mu0, torch.pow(lam0, -0.5)) if map_type == "list": pyro.map_data("aaa", data, lambda i, x: pyro.observe( "obs_%d" % i, dist.normal, x, mu_latent, torch.pow(lam, -0.5)), batch_size=batch_size) elif map_type == "tensor": tdata = torch.cat([xi.view(1, -1) for xi in data], 0) pyro.map_data("aaa", tdata, # XXX get batch size args to dist right lambda i, x: pyro.observe("obs", dist.normal, x, mu_latent, torch.pow(lam, -0.5)), batch_size=batch_size) else: for i, x in enumerate(data): pyro.observe('obs_%d' % i, dist.normal, x, mu_latent, torch.pow(lam, -0.5)) return mu_latent
def map_data_vector_model(subsample_size): mu = Variable(torch.zeros(20)) sigma = Variable(torch.ones(20)) def local_model(batch, unused): pyro.sample("x", dist.normal, mu[batch], sigma[batch]) return batch LongTensor = torch.cuda.LongTensor if torch.Tensor.is_cuda else torch.LongTensor ind = Variable(LongTensor(range(20))) batch = pyro.map_data('mapdata', ind, local_model, batch_size=subsample_size) return list(batch.data)
def model(): mu_latent = pyro.sample( "mu_latent", dist.Normal(self.mu0, torch.pow(self.lam0, -0.5), reparameterized=False)) def obs_inner(i, _i, _x): for k in range(n_superfluous_top): pyro.sample("z_%d_%d" % (i, k), dist.Normal(ng_zeros(4 - i, 1), ng_ones(4 - i, 1), reparameterized=False)) pyro.observe("obs_%d" % i, dist.normal, _x, mu_latent, torch.pow(self.lam, -0.5)) for k in range(n_superfluous_top, n_superfluous_top + n_superfluous_bottom): pyro.sample("z_%d_%d" % (i, k), dist.Normal(ng_zeros(4 - i, 1), ng_ones(4 - i, 1), reparameterized=False)) def obs_outer(i, x): pyro.map_data("map_obs_inner_%d" % i, x, lambda _i, _x: obs_inner(i, _i, _x), batch_size=4 - i) pyro.map_data("map_obs_outer", [self.data_tensor[0:4, :], self.data_tensor[4:7, :], self.data_tensor[7:9, :]], lambda i, x: obs_outer(i, x), batch_size=3) return mu_latent
def outer(i, x): pyro.map_data("map_inner_%d" % i, x, lambda _i, _x: inner(i, _i, _x), batch_size=batch_size_inner)
def model(): lambda_latent = pyro.sample("lambda_latent", dist.gamma, self.alpha0, self.beta0) pyro.map_data("aaa", self.data, lambda i, x: pyro.observe( "obs_{}".format(i), dist.poisson, x, lambda_latent), batch_size=3) return lambda_latent
def obs_outer(i, x): pyro.map_data("map_obs_inner_%d" % i, x, lambda _i, _x: None, batch_size=3)
def model(): return pyro.map_data("md", data, local_model, batch_size=1, batch_dim=batch_dim)
def obs_outer(i, x): pyro.map_data("map_obs_inner_%d" % i, x, lambda _i, _x: obs_inner(i, _i, _x), batch_size=4 - i)