def model(self, x): x_size = x.size(0) # sample the global weights with pyro.iarange("w_top_iarange", self.top_width * self.mid_width): w_top = pyro.sample("w_top", Gamma(self.alpha_w, self.beta_w)) with pyro.iarange("w_mid_iarange", self.mid_width * self.bottom_width): w_mid = pyro.sample("w_mid", Gamma(self.alpha_w, self.beta_w)) with pyro.iarange("w_bottom_iarange", self.bottom_width * self.image_size): w_bottom = pyro.sample("w_bottom", Gamma(self.alpha_w, self.beta_w)) # sample the local latent random variables # (the iarange encodes the fact that the z's for different datapoints are conditionally independent) with pyro.iarange("data", x_size): z_top = pyro.sample( "z_top", Gamma(self.alpha_z, self.beta_z).expand([self.top_width]).independent(1)) mean_mid = torch.mm(z_top, w_top.reshape(self.top_width, self.mid_width)) z_mid = pyro.sample( "z_mid", Gamma(self.alpha_z, self.beta_z / mean_mid).independent(1)) mean_bottom = torch.mm( z_mid, w_mid.view(self.mid_width, self.bottom_width)) z_bottom = pyro.sample( "z_bottom", Gamma(self.alpha_z, self.beta_z / mean_bottom).independent(1)) mean_obs = torch.mm( z_bottom, w_bottom.view(self.bottom_width, self.image_size)) # observe the data using a poisson likelihood pyro.sample('obs', Poisson(mean_obs).independent(1), obs=x)
def guide(subsample): loc = pyro.param("loc", lambda: torch.zeros(len(data), requires_grad=True)) scale = pyro.param("scale", lambda: torch.tensor([1.0], requires_grad=True)) with pyro.iarange("particles", num_particles): with pyro.iarange("data", len(data), subsample_size, subsample) as ind: loc_ind = loc[ind].unsqueeze(-1).expand(-1, num_particles) pyro.sample("z", Normal(loc_ind, scale))
def model(): with pyro.iarange("particles", num_particles): pyro.sample("x", dist.Bernoulli(p).expand_by([num_particles])) with pyro.iarange("outer", outer_dim): pyro.sample("y", dist.Bernoulli(p).expand_by([outer_dim, num_particles])) for i in pyro.irange("inner", inner_dim): pyro.sample("z_{}".format(i), dist.Bernoulli(p).expand_by([outer_dim, num_particles]))
def model(self): # register PyTorch module `decoder` with Pyro pyro.module("decoder", self.decoder) # Setup hyperparameters for prior p(z) z_mu = ng_zeros([self.n_samples, self.n_latent]) z_sigma = ng_ones([self.n_samples, self.n_latent]) # sample from prior z = pyro.sample("latent", dist.normal, z_mu, z_sigma) # decode the latent code z z_adj = self.decoder(z) # Subsampling if self.subsampling: with pyro.iarange("data", self.n_subsample, subsample=self.sample()) as ind: pyro.observe('obs', dist.bernoulli, self.adj_labels.view(1, -1)[0][ind], z_adj.view(1, -1)[0][ind]) # Reweighting else: with pyro.iarange("data"): pyro.observe('obs', weighted_bernoulli, self.adj_labels.view(1, -1), z_adj.view(1, -1), weight=self.pos_weight)
def model(y,x,N): w = pyro.sample('w'.format(''), dist.Beta(Variable(26.072914040168385*torch.ones([amb(1)])),Variable((42.3120851154)*torch.ones([amb(1)])))) with pyro.iarange('b_range_'.format(''), N): b = pyro.sample('b'.format(''), dist.Gamma(Variable((5.63887222899)*torch.ones([amb(N)])),Variable((40.1978121928)*torch.ones([amb(N)])))) with pyro.iarange('p_range_'.format(''), N): p = pyro.sample('p'.format(''), dist.Beta(Variable((52.1419233118)*torch.ones([amb(N)])),Variable((83.6618285099)*torch.ones([amb(N)])))) pyro.sample('obs__100'.format(), dist.Beta(w*x+b,p), obs=y)
def model(): p = torch.tensor([0.5], requires_grad=True) with pyro.iarange("iarange_outer", 10, 5) as ind_outer: pyro.sample("x", dist.Bernoulli(p).expand_by([len(ind_outer), 1])) with pyro.iarange("iarange_inner", 11, 6) as ind_inner: pyro.sample("y", dist.Bernoulli(p).expand_by([len(ind_inner)])) pyro.sample("z", dist.Bernoulli(p).expand_by([len(ind_outer), len(ind_outer)])) # error here
def model(): with pyro.iarange("num_particles", 10, dim=-3): with pyro.iarange("components", 2, dim=-1): p = pyro.sample("p", dist.Beta(torch.tensor(1.1), torch.tensor(1.1))) assert p.shape == torch.Size((10, 1, 2)) with pyro.iarange("data", data.shape[0], dim=-2): pyro.sample("obs", dist.Bernoulli(p), obs=data)
def guide(): q = pyro.param("q") with pyro.iarange("particles", num_particles): pyro.sample("y", dist.Bernoulli(q).expand_by([num_particles]), infer={"enumerate": enumerate1}) with pyro.iarange("iarange", iarange_dim): pyro.sample("z", dist.Bernoulli(q).expand_by([iarange_dim, num_particles]), infer={"enumerate": enumerate2})
def guide(): p = pyro.param("p", Variable(torch.Tensor([0.5]), requires_grad=True)) with pyro.iarange("iarange_0", 10, 5) as ind1: with pyro.iarange("iarange_1", 10, 5) as ind2: pyro.sample("x", dist.bernoulli, p, batch_size=len(ind1) * len(ind2))
def model(): p = Variable(torch.Tensor([0.5])) with pyro.iarange("iarange_0", 10, 5) as ind1: with pyro.iarange("iarange_1", 10, 5) as ind2: pyro.sample("x", dist.bernoulli, p, batch_size=len(ind1) * len(ind2))
def model(): with pyro.iarange("num_particles", 10, dim=-3): with pyro.iarange("components", 2, dim=-1): p = pyro.sample( "p", dist.Beta(torch.tensor(1.1), torch.tensor(1.1))) assert p.shape == torch.Size((10, 1, 2)) with pyro.iarange("data", data.shape[0], dim=-2): pyro.sample("obs", dist.Bernoulli(p), obs=data)
def model(loc, cov): x = pyro.param("x", torch.randn(2)) y = pyro.param("y", torch.randn(3, 2)) z = pyro.param("z", torch.randn(4, 2).abs(), constraint=constraints.greater_than(-1)) pyro.sample("obs_x", dist.MultivariateNormal(loc, cov), obs=x) with pyro.iarange("y_iarange", 3): pyro.sample("obs_y", dist.MultivariateNormal(loc, cov), obs=y) with pyro.iarange("z_iarange", 4): pyro.sample("obs_z", dist.MultivariateNormal(loc, cov), obs=z)
def model(): p = torch.tensor([0.5], requires_grad=True) with pyro.iarange("iarange_outer", 10, 5) as ind_outer: pyro.sample("x", dist.Bernoulli(p).expand_by([len(ind_outer), 1])) with pyro.iarange("iarange_inner", 11, 6) as ind_inner: pyro.sample("y", dist.Bernoulli(p).expand_by([len(ind_inner)])) pyro.sample("z", dist.Bernoulli(p).expand_by([len(ind_inner), 1])) # error here
def guide(): loc = pyro.param("loc", torch.zeros(len(data))) scale = pyro.param("scale", torch.tensor([1.])) pyro.sample("nuisance_c", Normal(4, 5)) with pyro.iarange("particles", num_particles, dim=-2): with pyro.iarange("data", len(data), dim=-1): pyro.sample("z", Normal(loc, scale)) pyro.sample("nuisance_b", Normal(2, 3)) pyro.sample("nuisance_a", Normal(0, 1))
def model(): p = torch.tensor(0.5, requires_grad=True) iarange_outer = pyro.iarange("iarange_outer", 10, 5, dim=-1) iarange_inner = pyro.iarange("iarange_inner", 11, 6, dim=-2) with iarange_outer as ind_outer: pyro.sample("x", dist.Bernoulli(p).expand_by([len(ind_outer)])) with iarange_inner as ind_inner: pyro.sample("y", dist.Bernoulli(p).expand_by([len(ind_inner), 1])) with iarange_outer as ind_outer, iarange_inner as ind_inner: pyro.sample("z", dist.Bernoulli(p).expand_by([len(ind_inner), len(ind_outer)]))
def guide(): p = pyro.param("p", Variable(torch.Tensor([0.5]), requires_grad=True)) for i in pyro.irange("irange0", 2): pyro.sample("x_%d" % i, dist.bernoulli, p) if i == 0: for j in pyro.irange("irange1", 2): with pyro.iarange("iarange1", 10, 5) as ind: pyro.sample("y_%d" % j, dist.bernoulli, p, batch_size=len(ind)) elif i == 1: with pyro.iarange("iarange1", 10, 5) as ind: pyro.sample("z", dist.bernoulli, p, batch_size=len(ind))
def xy_model(): d = dist.Bernoulli(0.5) x_axis = pyro.iarange('x_axis', 2, dim=-1) y_axis = pyro.iarange('y_axis', 3, dim=-2) pyro.sample('b', d) with x_axis: pyro.sample('bx', d.expand_by([2])) with y_axis: pyro.sample('by', d.expand_by([3, 1])) with x_axis, y_axis: pyro.sample('bxy', d.expand_by([3, 2]))
def model(): particles_iarange = pyro.iarange("particles", num_particles, dim=-2) data_iarange = pyro.iarange("data", len(data), dim=-1) pyro.sample("nuisance_a", Normal(0, 1)) with particles_iarange, data_iarange: z = pyro.sample("z", Normal(0, 1)) pyro.sample("nuisance_b", Normal(2, 3)) with data_iarange, particles_iarange: pyro.sample("x", Normal(z, 1), obs=data) pyro.sample("nuisance_c", Normal(4, 5))
def guide(): q = pyro.param("q") with pyro.iarange("particles", num_particles): pyro.sample("x", dist.Bernoulli(q).expand_by([num_particles]), infer={"enumerate": enumerate1}) with pyro.iarange("outer", outer_dim): pyro.sample("y", dist.Bernoulli(q).expand_by([outer_dim, num_particles]), infer={"enumerate": enumerate2}) for i in pyro.irange("inner", inner_dim): pyro.sample("z_{}".format(i), dist.Bernoulli(q).expand_by([outer_dim, num_particles]), infer={"enumerate": enumerate3})
def model(): p = Variable(torch.Tensor([0.5])) for i in pyro.irange("irange0", 2): pyro.sample("x_%d" % i, dist.bernoulli, p) if i == 0: for j in pyro.irange("irange1", 2): with pyro.iarange("iarange1", 10, 5) as ind: pyro.sample("y_%d" % j, dist.bernoulli, p, batch_size=len(ind)) elif i == 1: with pyro.iarange("iarange1", 10, 5) as ind: pyro.sample("z", dist.bernoulli, p, batch_size=len(ind))
def model(): pyro.sample("w", dist.Bernoulli(0.5), infer={'enumerate': 'parallel'}) x_iarange = pyro.iarange("x_iarange", 10, 5, dim=-1) y_iarange = pyro.iarange("y_iarange", 11, 6, dim=-2) pyro.sample("a", dist.Bernoulli(0.5)) with x_iarange: pyro.sample("b", dist.Bernoulli(0.5).expand_by([5])) with y_iarange: # Note that it is difficult to check that c does not depend on b. pyro.sample("c", dist.Bernoulli(0.5).expand_by([6, 1])) with x_iarange, y_iarange: pyro.sample("d", dist.Bernoulli(0.5).expand_by([6, 5]))
def model(): d = dist.Bernoulli(p) with pyro.iarange("particles", num_particles): context1 = pyro.iarange("outer", outer_dim, dim=-2) context2 = pyro.iarange("inner", inner_dim, dim=-3) pyro.sample("w", d.expand_by([num_particles])) with context1: pyro.sample("x", d.expand_by([outer_dim, num_particles])) with context2: pyro.sample("y", d.expand_by([inner_dim, 1, num_particles])) with context1, context2: pyro.sample("z", d.expand_by([inner_dim, outer_dim, num_particles]))
def guide(): d = dist.Bernoulli(pyro.param("q")) with pyro.iarange("particles", num_particles): context1 = pyro.iarange("outer", outer_dim, dim=-2) context2 = pyro.iarange("inner", inner_dim, dim=-3) pyro.sample("w", d.expand_by([num_particles]), infer={"enumerate": enumerate1}) with context1: pyro.sample("x", d.expand_by([outer_dim, num_particles]), infer={"enumerate": enumerate2}) with context2: pyro.sample("y", d.expand_by([inner_dim, 1, num_particles]), infer={"enumerate": enumerate3}) with context1, context2: pyro.sample("z", d.expand_by([inner_dim, outer_dim, num_particles]), infer={"enumerate": enumerate4})
def model(): pyro.sample("w", dist.Bernoulli(0.5), infer={'enumerate': 'parallel'}) with pyro.iarange("non_enum", 2): a = pyro.sample("a", dist.Bernoulli(0.5).expand_by([2]), infer={'enumerate': None}) p = (1.0 + a.sum(-1)) / (2.0 + a.size(0)) # introduce dependency of b on a with pyro.iarange("enum_1", 3): pyro.sample("b", dist.Bernoulli(p).expand_by([3]), infer={'enumerate': enumerate_})
def model(): p = torch.tensor(0.5, requires_grad=True) iarange_outer = pyro.iarange("iarange_outer", 10, 5, dim=-1) iarange_inner = pyro.iarange("iarange_inner", 11, 6, dim=-2) with iarange_outer as ind_outer: pyro.sample("x", dist.Bernoulli(p).expand_by([len(ind_outer)])) with iarange_inner as ind_inner: pyro.sample("y", dist.Bernoulli(p).expand_by([len(ind_inner), 1])) with iarange_outer as ind_outer, iarange_inner as ind_inner: pyro.sample( "z", dist.Bernoulli(p).expand_by([len(ind_inner), len(ind_outer)]))
def guide(y,x,N): arg_1 = torch.nn.Softplus()(pyro.param('arg_1', Variable(torch.ones((amb(1))), requires_grad=True))) arg_2 = torch.nn.Softplus()(pyro.param('arg_2', Variable(torch.ones((amb(1))), requires_grad=True))) w = pyro.sample('w'.format(''), dist.Beta(arg_1,arg_2)) arg_3 = torch.nn.Softplus()(pyro.param('arg_3', Variable(torch.ones((amb(N))), requires_grad=True))) arg_4 = torch.nn.Softplus()(pyro.param('arg_4', Variable(torch.ones((amb(N))), requires_grad=True))) with pyro.iarange('b_prange'): b = pyro.sample('b'.format(''), dist.Gamma(arg_3,arg_4)) arg_5 = torch.nn.Softplus()(pyro.param('arg_5', Variable(torch.ones((amb(N))), requires_grad=True))) arg_6 = torch.nn.Softplus()(pyro.param('arg_6', Variable(torch.ones((amb(N))), requires_grad=True))) with pyro.iarange('p_prange'): p = pyro.sample('p'.format(''), dist.Beta(arg_5,arg_6)) pass
def model(self, ob, ac, next_ob, reward, game_over): network_dist = pyro.random_module('dream_world', self.network, self.prior_dist) network = network_dist() next_ob_logits, reward_logits, game_over_logits = network(ob, ac) next_ob_batch = pyro.iarange('next_ob_batch', next_ob.shape[0], dim=-3) next_ob_height = pyro.iarange('next_ob_height', next_ob.shape[1], dim=-2) next_ob_width = pyro.iarange('next_ob_width', next_ob.shape[2], dim=-1) with next_ob_batch, next_ob_height, next_ob_width: next_ob = pyro.sample('next_ob', CustomCategorical(logits=next_ob_logits), obs=next_ob) with pyro.iarange('batch', len(ac)): reward = pyro.sample('reward', CustomCategorical(logits=reward_logits), obs=reward) game_over = pyro.sample('game_over', CustomCategorical(logits=game_over_logits), obs=game_over)
def model(): pyro.sample("w", dist.Bernoulli(0.5), infer={'enumerate': 'parallel'}) with pyro.iarange("non_enum", 2): a = pyro.sample("a", dist.Bernoulli(0.5).expand_by([2]), infer={'enumerate': None}) p = (1.0 + a.sum(-1)) / (2.0 + a.size(0) ) # introduce dependency of b on a with pyro.iarange("enum_1", 3): pyro.sample("b", dist.Bernoulli(p).expand_by([3]), infer={'enumerate': enumerate_})
def iarange_reuse_model_guide(include_obs=True, dim1=3, dim2=2): p0 = torch.tensor(math.exp(-0.40 - include_obs * 0.2), requires_grad=True) p1 = torch.tensor(math.exp(-0.33 - include_obs * 0.1), requires_grad=True) pyro.sample("a1", dist.Bernoulli(p0 * p1)) my_iarange1 = pyro.iarange("iarange1", dim1) my_iarange2 = pyro.iarange("iarange2", dim2) with my_iarange1 as ind1: with my_iarange2 as ind2: pyro.sample("c1", dist.Bernoulli(p1).expand_by([len(ind2), len(ind1)])) pyro.sample("b1", dist.Bernoulli(p0 * p1)) with my_iarange2 as ind2: with my_iarange1 as ind1: c2 = pyro.sample("c2", dist.Bernoulli(p1).expand_by([len(ind2), len(ind1)])) if include_obs: pyro.sample("obs", dist.Bernoulli(c2), obs=torch.ones(c2.size()))
def model(y, x, N): w = pyro.sample( 'w'.format(''), dist.Exponential(Variable((37.4790187832) * torch.ones([amb(1)])))) with pyro.iarange('b_range_'.format(''), N): b = pyro.sample( 'b'.format(''), dist.Exponential( Variable(31.494555467732837 * torch.ones([amb(N)])))) with pyro.iarange('p_range_'.format(''), N): p = pyro.sample( 'p'.format(''), dist.Normal(Variable(55.43075391668968 * torch.ones([amb(N)])), Variable((61.3521758404) * torch.ones([amb(N)])))) pyro.sample('obs__100'.format(), dist.LogNormal(w * x + b, p), obs=y)
def guide(nyear, C, nsite, year): arg_1 = pyro.param('arg_1', torch.ones((amb(1)))) arg_2 = pyro.param('arg_2', torch.ones((amb(1))), constraint=constraints.positive) sd_year = pyro.sample('sd_year'.format(''), dist.Normal(arg_1, arg_2)) arg_3 = pyro.param('arg_3', torch.ones((amb(1))), constraint=constraints.positive) arg_4 = pyro.param('arg_4', torch.ones((amb(1))), constraint=constraints.positive) sd_alpha = pyro.sample('sd_alpha'.format(''), dist.Gamma(arg_3, arg_4)) arg_5 = pyro.param('arg_5', torch.ones((amb(1))), constraint=constraints.positive) arg_6 = pyro.param('arg_6', torch.ones((amb(1))), constraint=constraints.positive) mu = pyro.sample('mu'.format(''), dist.Gamma(arg_5, arg_6)) arg_7 = pyro.param('arg_7', torch.ones((amb(nsite)))) arg_8 = pyro.param('arg_8', torch.ones((amb(nsite))), constraint=constraints.positive) with pyro.iarange('alpha_prange'): alpha = pyro.sample('alpha'.format(''), dist.Normal(arg_7, arg_8)) arg_9 = pyro.param('arg_9', torch.ones((amb(3))), constraint=constraints.positive) arg_10 = pyro.param('arg_10', torch.ones((amb(3))), constraint=constraints.positive) with pyro.iarange('beta_prange'): beta = pyro.sample('beta'.format(''), dist.Gamma(arg_9, arg_10)) arg_11 = pyro.param('arg_11', torch.ones((amb(nyear))), constraint=constraints.positive) arg_12 = pyro.param('arg_12', torch.ones((amb(nyear))), constraint=constraints.positive) with pyro.iarange('eps_prange'): eps = pyro.sample('eps'.format(''), dist.Gamma(arg_11, arg_12)) for i in range(1, nyear + 1): pass for i in range(1, nyear + 1): pass pass
def guide(num_particles): q1 = pyro.param("q1", torch.tensor(pi1, requires_grad=True)) q2 = pyro.param("q2", torch.tensor(pi2, requires_grad=True)) with pyro.iarange("particles", num_particles): z = pyro.sample("z", dist.Normal(q2, 1.0).expand_by([num_particles])) zz = torch.exp(z) / (1.0 + torch.exp(z)) pyro.sample("y", dist.Bernoulli(q1 * zz))
def model(num_particles): with pyro.iarange("particles", num_particles): q3 = pyro.param("q3", torch.tensor(pi3, requires_grad=True)) q4 = pyro.param("q4", torch.tensor(0.5 * (pi1 + pi2), requires_grad=True)) z = pyro.sample("z", dist.Normal(q3, 1.0).expand_by([num_particles])) zz = torch.exp(z) / (1.0 + torch.exp(z)) pyro.sample("y", dist.Bernoulli(q4 * zz))
def guide(): q1 = pyro.param("q1", torch.tensor(pi1, requires_grad=True)) q2 = pyro.param("q2", torch.tensor(pi2, requires_grad=True)) with pyro.iarange("particles", num_particles): y = pyro.sample("y", dist.Bernoulli(q1).expand_by([num_particles]), infer={"enumerate": enumerate1}) if include_z: pyro.sample("z", dist.Normal(q2 * y + 0.10, 1.0))
def gmm_batch_guide(data): with pyro.iarange("data", len(data)) as batch: n = len(batch) ps = pyro.param("ps", Variable(torch.ones(n, 1) * 0.6, requires_grad=True)) ps = torch.cat([ps, 1 - ps], dim=1) z = pyro.sample("z", dist.Categorical(ps)) assert z.size() == (n, 2)
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))
def gmm_batch_guide(data): with pyro.iarange("data", len(data)) as batch: n = len(batch) probs = pyro.param("probs", torch.tensor(torch.ones(n, 1) * 0.6, requires_grad=True)) probs = torch.cat([probs, 1 - probs], dim=1) z = pyro.sample("z", dist.OneHotCategorical(probs)) assert z.shape[-2:] == (n, 2)
def guide(self, y, m, l, x): ''' Define the guide (i.e. variational distribution), q(z| y, x) :param y: measurements :param x: conditioning variables :return: None ''' if self.use_cuda: y = y.cuda() x = x.cuda() m = m.cuda() l = l.cuda() # register PyTorch module `encoder` with Pyro pyro.module("encoder", self.encoder) # print(x.is_cuda, y.is_cuda, m) yFused = self.sim_measurements(x, 5) * (1 - m) + m * y #yFused = y with pyro.iarange("data", x.size(0)): # use the encoder to get the parameters used to define q(z|y,x) z_loc, z_scale = self.encoder.forward(yFused, x) # sample the latent code z pyro.sample("latent_posterior", dist.Normal(z_loc, z_scale).independent(1)) self.debug_qty += 1
def model(self, xs, ys=None): # can pass in ys as labels or not pass in anything for unlabelled # register PyTorch module `decoder` with Pyro pyro.module("decoder", self.decoder) # with pyro.plate("data"): with pyro.iarange("data", xs.shape[0]): # setup hyperparameters for prior p(z) z_loc = xs.new_zeros(torch.Size((xs.shape[0], self.z_dim))) z_scale = xs.new_ones(torch.Size((xs.shape[0], self.z_dim))) # sample from prior (value will be sampled by guide when computing the ELBO) zs = pyro.sample("z", dist.Normal(z_loc, z_scale).independent(1)) # if there is a label y, sample from the constant prior # otherwise, observe the value (i.e. score against constant prior) # alpha_prior = xs.new_ones(torch.Size((xs.shape[0], self.output_size))) / (1.0*self.output_size) # prior is the actual train data label distribution (not uniform) alpha_prior = self.prior_probs.repeat(xs.shape[0], 1) ys = pyro.sample("y", dist.OneHotCategorical(alpha_prior), obs=ys) # decoder outputs mean and sqroot cov, sample from normal recon_loc, recon_scale = self.decoder.forward([zs, ys]) pyro.sample("x", dist.Normal(recon_loc, recon_scale).independent(1), obs=xs.reshape(-1, 1335))
def iarange_model(subsample_size): loc = torch.zeros(20) scale = torch.ones(20) with pyro.iarange('iarange', 20, subsample_size) as batch: pyro.sample("x", dist.Normal(loc[batch], scale[batch])) result = list(batch.data) return result
def pyromodel(x, y): priors = {} for name, par in model.named_parameters(): priors[name] = dist.Normal(torch.zeros(*par.shape), 50 * torch.ones(*par.shape)).independent( par.dim()) #print("batch shape:", priors[name].batch_shape) #print("event shape:", priors[name].event_shape) #print("event dim:", priors[name].event_dim) bayesian_model = pyro.random_module('bayesian_model', model, priors) sampled_model = bayesian_model() sigma = pyro.sample('sigma', Uniform(0, 50)) with pyro.iarange("map", len(x)): prediction_mean = sampled_model(x) logging.debug(f"prediction_mean: {prediction_mean.shape}") if y is not None: logging.debug(f"y_data: {y.shape}") d_dist = Normal(prediction_mean, sigma).to_event(1) if y is not None: logging.debug(f"y_data: {y.shape}") logging.debug(f"batch shape: {d_dist.batch_shape}") logging.debug(f"event shape: {d_dist.event_shape}") logging.debug(f"event dim: {d_dist.event_dim}") pyro.sample("obs", d_dist, obs=y) return prediction_mean
def guide(self, xs, ys=None): """ The guide corresponds to the following: q(y|x) = categorical(alpha(x)) # infer digit from an image q(z|x,y) = normal(mu(x,y),sigma(x,y)) # infer handwriting style from an image and the digit mu, sigma are given by a neural network `encoder_z` alpha is given by a neural network `encoder_y` :param xs: a batch of scaled vectors of pixels from an image :param ys: (optional) a batch of the class labels i.e. the digit corresponding to the image(s) :return: None """ # inform Pyro that the variables in the batch of xs, ys are conditionally independent with pyro.iarange("independent"): # if the class label (the digit) is not supervised, sample # (and score) the digit with the variational distribution # q(y|x) = categorical(alpha(x)) if ys is None: alpha = self.encoder_y.forward(xs) ys = pyro.sample("y", dist.categorical, alpha) # sample (and score) the latent handwriting-style with the variational # distribution q(z|x,y) = normal(mu(x,y),sigma(x,y)) mu, sigma = self.encoder_z.forward([xs, ys]) zs = pyro.sample("z", dist.normal, mu, sigma) # noqa: F841
def guide(self, x): # register PyTorch module `encoder` with Pyro pyro.module("encoder", self.encoder) with pyro.iarange("data", x.size(0)): # use the encoder to get the parameters used to define q(z|x) z_loc, z_scale = self.encoder.forward(x) # sample the latent code z pyro.sample("latent", dist.Normal(z_loc, z_scale).independent(1))
def model(): pyro.sample("w", dist.Bernoulli(0.5), infer={'enumerate': 'parallel'}) inner_iarange = pyro.iarange("iarange", 10, 5) for i in pyro.irange("irange", 3): pyro.sample("y_{}".format(i), dist.Bernoulli(0.5)) with inner_iarange: pyro.sample("x_{}".format(i), dist.Bernoulli(0.5).expand_by([5]), infer={'enumerate': enumerate_})
def model(data): weights = pyro.sample('weights', dist.Dirichlet(0.5 * torch.ones(K))) locs = pyro.sample('locs', dist.Normal(0, 10).expand_by([K]).independent(1)) scale = pyro.sample('scale', dist.LogNormal(0, 1)) with pyro.iarange('data', len(data)): weights = weights.expand(torch.Size((len(data),)) + weights.shape) assignment = pyro.sample('assignment', dist.Categorical(weights)) pyro.sample('obs', dist.Normal(locs[assignment], scale), obs=data)
def model(self, data): decoder = pyro.module('decoder', self.vae_decoder) z_mean, z_std = torch.zeros([data.size(0), 20]), torch.ones([data.size(0), 20]) with pyro.iarange('data', data.size(0)): z = pyro.sample('latent', Normal(z_mean, z_std).independent(1)) img = decoder.forward(z) pyro.sample('obs', Bernoulli(img).independent(1), obs=data.reshape(-1, 784))
def model(): x_iarange = pyro.iarange("x_iarange", 10, 5, dim=-1) y_iarange = pyro.iarange("y_iarange", 11, 6, dim=-2) with pyro.iarange("num_particles", 50, dim=-3): with x_iarange: b = pyro.sample("b", dist.Beta(torch.tensor(1.1), torch.tensor(1.1))) assert b.shape == torch.Size((50, 1, 5)) with y_iarange: c = pyro.sample("c", dist.Bernoulli(0.5)) if enumerate_ == "parallel": assert c.shape == torch.Size((2, 50, 6, 1)) else: assert c.shape == torch.Size((50, 6, 1)) with x_iarange, y_iarange: d = pyro.sample("d", dist.Bernoulli(b)) if enumerate_ == "parallel": assert d.shape == torch.Size((2, 1, 50, 6, 5)) else: assert d.shape == torch.Size((50, 6, 5))
def model(): p = pyro.param("p", torch.tensor([0.05, 0.15])) probs = pyro.param("probs", torch.tensor([[0.1, 0.2, 0.3, 0.4], [0.4, 0.3, 0.2, 0.1]])) with pyro.iarange("iarange", 2): x = pyro.sample("x", dist.Bernoulli(p)) y = pyro.sample("y", dist.Categorical(probs)) assert x.size() == (2,) assert y.size() == (2,) return dict(x=x, y=y)