def model(): latent = named.Object("latent") latent.list = named.List() loc = latent.list.add().loc.param_(torch.zeros(1)) latent.dict = named.Dict() foo = latent.dict["foo"].foo.sample_(dist.Normal(loc, torch.ones(1))) latent.object.bar.sample_(dist.Normal(loc, torch.ones(1)), obs=foo)
def model(): latent = named.Object("latent") latent.list = named.List() mu = latent.list.add().mu.param_(Variable(torch.zeros(1))) latent.dict = named.Dict() foo = latent.dict["foo"].foo.sample_(dist.normal, mu, ng_ones(1)) latent.object.bar.observe_(dist.normal, foo, mu, ng_ones(1))
def test_eval_str(): state = named.Object("state") state.x = 0 state.ys = named.List() state.ys.add().foo = 1 state.zs = named.Dict() state.zs[42].bar = 2 assert state is eval(str(state)) assert state.x is eval(str(state.x)) assert state.ys is eval(str(state.ys)) assert state.ys[0] is eval(str(state.ys[0])) assert state.ys[0].foo is eval(str(state.ys[0].foo)) assert state.zs is eval(str(state.zs)) assert state.zs[42] is eval(str(state.zs[42])) assert state.zs[42].bar is eval(str(state.zs[42].bar))
def guide_recurse(data, latent): latent.post_mu.param_(Variable(torch.zeros(1), requires_grad=True)) latent.post_sigma.param_(Variable(torch.ones(1), requires_grad=True)) latent.z.sample_(dist.normal, latent.post_mu, latent.post_sigma) if isinstance(data, Variable): pass elif isinstance(data, list): latent.list = named.List() for datum in data: guide_recurse(datum, latent.list.add()) elif isinstance(data, dict): latent.dict = named.Dict() for key, value in data.items(): guide_recurse(value, latent.dict[key]) else: raise TypeError("Unsupported type {}".format(type(data)))
def guide_recurse(data, latent): latent.post_loc.param_(torch.tensor(0.0)) latent.post_scale.param_(torch.tensor(1.0), constraint=constraints.positive) latent.z.sample_(dist.Normal(latent.post_loc, latent.post_scale)) if torch.is_tensor(data): pass elif isinstance(data, list): latent.list = named.List() for datum in data: guide_recurse(datum, latent.list.add()) elif isinstance(data, dict): latent.dict = named.Dict() for key, value in data.items(): guide_recurse(value, latent.dict[key]) else: raise TypeError("Unsupported type {}".format(type(data)))
def model_recurse(data, latent): if isinstance(data, Variable): latent.x.observe_(dist.normal, data, latent.z, ng_ones(1)) elif isinstance(data, list): latent.prior_sigma.param_(Variable(torch.ones(1), requires_grad=True)) latent.list = named.List() for data_i in data: latent_i = latent.list.add() latent_i.z.sample_(dist.normal, latent.z, latent.prior_sigma) model_recurse(data_i, latent_i) elif isinstance(data, dict): latent.prior_sigma.param_(Variable(torch.ones(1), requires_grad=True)) latent.dict = named.Dict() for key, value in data.items(): latent.dict[key].z.sample_(dist.normal, latent.z, latent.prior_sigma) model_recurse(value, latent.dict[key]) else: raise TypeError("Unsupported type {}".format(type(data)))
def model_recurse(data, latent): if torch.is_tensor(data): latent.x.sample_(dist.Normal(latent.z, 1.0), obs=data) elif isinstance(data, list): latent.prior_scale.param_(torch.tensor(1.0), constraint=constraints.positive) latent.list = named.List() for data_i in data: latent_i = latent.list.add() latent_i.z.sample_(dist.Normal(latent.z, latent.prior_scale)) model_recurse(data_i, latent_i) elif isinstance(data, dict): latent.prior_scale.param_(torch.tensor(1.0), constraint=constraints.positive) latent.dict = named.Dict() for key, value in data.items(): latent.dict[key].z.sample_( dist.Normal(latent.z, latent.prior_scale)) model_recurse(value, latent.dict[key]) else: raise TypeError("Unsupported type {}".format(type(data)))
def model(): latent = named.Dict("latent") loc = latent["loc"].param_(torch.zeros(1)) foo = latent["foo"].sample_(dist.Normal(loc, torch.ones(1))) latent["bar"].sample_(dist.Normal(loc, torch.ones(1)), obs=foo) latent["x"].z.sample_(dist.Normal(loc, torch.ones(1)))
def model(): latent = named.Dict("latent") mu = latent["mu"].param_(Variable(torch.zeros(1))) foo = latent["foo"].sample_(dist.normal, mu, ng_ones(1)) latent["bar"].observe_(dist.normal, foo, mu, ng_ones(1)) latent["x"].z.sample_(dist.normal, mu, ng_ones(1))