• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python pyro.iarange函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中pyro.iarange函数的典型用法代码示例。如果您正苦于以下问题:Python iarange函数的具体用法?Python iarange怎么用?Python iarange使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了iarange函数的16个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: model

 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)
开发者ID:lewisKit,项目名称:pyro,代码行数:7,代码来源:test_valid_models.py


示例2: guide

 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))
开发者ID:lewisKit,项目名称:pyro,代码行数:7,代码来源:test_gradient.py


示例3: model

 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]))
开发者ID:lewisKit,项目名称:pyro,代码行数:7,代码来源:test_enum.py


示例4: guide

 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})
开发者ID:lewisKit,项目名称:pyro,代码行数:8,代码来源:test_enum.py


示例5: model

 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)
开发者ID:lewisKit,项目名称:pyro,代码行数:9,代码来源:test_multi.py


示例6: model

    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))
开发者ID:lewisKit,项目名称:pyro,代码行数:11,代码来源:test_gradient.py


示例7: xy_model

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]))
开发者ID:lewisKit,项目名称:pyro,代码行数:11,代码来源:test_util.py


示例8: iarange_reuse_model_guide

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()))
开发者ID:lewisKit,项目名称:pyro,代码行数:15,代码来源:test_compute_downstream_costs.py


示例9: gmm_batch_guide

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)
开发者ID:lewisKit,项目名称:pyro,代码行数:7,代码来源:test_enum.py


示例10: gmm_batch_guide

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)
开发者ID:Magica-Chen,项目名称:pyro,代码行数:7,代码来源:test_enum.py


示例11: guide

    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
开发者ID:Magica-Chen,项目名称:pyro,代码行数:26,代码来源:ss_vae_M2.py


示例12: guide

 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))
开发者ID:Magica-Chen,项目名称:pyro,代码行数:7,代码来源:test_gradient.py


示例13: iarange_model

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
开发者ID:lewisKit,项目名称:pyro,代码行数:7,代码来源:test_mapdata.py


示例14: guide

 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))
开发者ID:lewisKit,项目名称:pyro,代码行数:8,代码来源:vae.py


示例15: model

 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))
开发者ID:lewisKit,项目名称:pyro,代码行数:9,代码来源:vae_comparison.py


示例16: model

    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)
开发者ID:lewisKit,项目名称:pyro,代码行数:9,代码来源:test_advi.py



注:本文中的pyro.iarange函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python pyro.param函数代码示例发布时间:2022-05-27
下一篇:
Python pyro.clear_param_store函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap