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

Python torch.ones_like函数代码示例

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

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



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

示例1: forward

    def forward(self, X: Tensor) -> Tensor:
        r"""Evaluate Expected Improvement on the candidate set X.

        Args:
            X: A `b1 x ... bk x 1 x d`-dim batched tensor of `d`-dim design points.
                Expected Improvement is computed for each point individually,
                i.e., what is considered are the marginal posteriors, not the
                joint.

        Returns:
            A `b1 x ... bk`-dim tensor of Expected Improvement values at the
            given design points `X`.
        """
        self.best_f = self.best_f.to(X)
        posterior = self.model.posterior(X)
        self._validate_single_output_posterior(posterior)
        mean = posterior.mean
        # deal with batch evaluation and broadcasting
        view_shape = mean.shape[:-2] if mean.dim() >= X.dim() else X.shape[:-2]
        mean = mean.view(view_shape)
        sigma = posterior.variance.clamp_min(1e-9).sqrt().view(view_shape)
        u = (mean - self.best_f.expand_as(mean)) / sigma
        if not self.maximize:
            u = -u
        normal = Normal(torch.zeros_like(u), torch.ones_like(u))
        ucdf = normal.cdf(u)
        updf = torch.exp(normal.log_prob(u))
        ei = sigma * (updf + u * ucdf)
        return ei
开发者ID:saschwan,项目名称:botorch,代码行数:29,代码来源:analytic.py


示例2: forward

 def forward(self, inputs, targets):
     """
     Args:
     - inputs: feature matrix with shape (batch_size, feat_dim)
     - targets: ground truth labels with shape (num_classes)
     """
     n = inputs.size(0)
     
     # Compute pairwise distance, replace by the official when merged
     dist = torch.pow(inputs, 2).sum(dim=1, keepdim=True).expand(n, n)
     dist = dist + dist.t()
     dist.addmm_(1, -2, inputs, inputs.t())
     dist = dist.clamp(min=1e-12).sqrt()  # for numerical stability
     
     # For each anchor, find the hardest positive and negative
     mask = targets.expand(n, n).eq(targets.expand(n, n).t())
     dist_ap, dist_an = [], []
     for i in range(n):
         dist_ap.append(dist[i][mask[i]].max().unsqueeze(0))
         dist_an.append(dist[i][mask[i] == 0].min().unsqueeze(0))
     dist_ap = torch.cat(dist_ap)
     dist_an = torch.cat(dist_an)
     
     # Compute ranking hinge loss
     y = torch.ones_like(dist_an)
     loss = self.ranking_loss(dist_an, dist_ap, y)
     return loss
开发者ID:zysolanine,项目名称:deep-person-reid,代码行数:27,代码来源:losses.py


示例3: sample_conditional_a

    def sample_conditional_a(self, resid_image, var_so_far, pixel_1d):

        is_on = (pixel_1d < (self.n_discrete_latent - 1)).float()

        # pass through galaxy encoder
        pixel_2d = self.one_galaxy_vae.pixel_1d_to_2d(pixel_1d)
        z_mean, z_var = self.one_galaxy_vae.enc(resid_image, pixel_2d)

        # sample z
        q_z = Normal(z_mean, z_var.sqrt())
        z_sample = q_z.rsample()

        # kl term for continuous latent vars
        log_q_z = q_z.log_prob(z_sample).sum(1)
        p_z = Normal(torch.zeros_like(z_sample), torch.ones_like(z_sample))
        log_p_z = p_z.log_prob(z_sample).sum(1)
        kl_z = is_on * (log_q_z - log_p_z)

        # run through decoder
        recon_mean, recon_var = self.one_galaxy_vae.dec(is_on, pixel_2d, z_sample)

        # NOTE: we will have to the recon means once we do more detections
        # recon_means = recon_mean + image_so_far
        # recon_vars = recon_var + var_so_far

        return recon_mean, recon_var, is_on, kl_z
开发者ID:Runjing-Liu120,项目名称:discrete_vae_experimentation,代码行数:26,代码来源:galaxy_experiments_lib.py


示例4: mse

 def mse(self, prediction, target):
     if not hasattr(target, '__len__'):
         target = torch.ones_like(prediction)*target
         if prediction.is_cuda:
             target = target.cuda()
         target = Variable(target)
     return torch.nn.MSELoss()(prediction, target)
开发者ID:kazk1018,项目名称:manifold_mixup,代码行数:7,代码来源:base.py


示例5: test_probability_of_improvement

    def test_probability_of_improvement(self, cuda=False):
        device = torch.device("cuda") if cuda else torch.device("cpu")
        for dtype in (torch.float, torch.double):
            mean = torch.tensor([0.0], device=device, dtype=dtype).view(1, 1)
            variance = torch.ones(1, 1, device=device, dtype=dtype)
            mm = MockModel(MockPosterior(mean=mean, variance=variance))

            module = ProbabilityOfImprovement(model=mm, best_f=1.96)
            X = torch.zeros(1, 1, device=device, dtype=dtype)
            pi = module(X)
            pi_expected = torch.tensor(0.0250, device=device, dtype=dtype)
            self.assertTrue(torch.allclose(pi, pi_expected, atol=1e-4))

            module = ProbabilityOfImprovement(model=mm, best_f=1.96, maximize=False)
            X = torch.zeros(1, 1, device=device, dtype=dtype)
            pi = module(X)
            pi_expected = torch.tensor(0.9750, device=device, dtype=dtype)
            self.assertTrue(torch.allclose(pi, pi_expected, atol=1e-4))

            # check for proper error if multi-output model
            mean2 = torch.rand(1, 2, device=device, dtype=dtype)
            variance2 = torch.ones_like(mean2)
            mm2 = MockModel(MockPosterior(mean=mean2, variance=variance2))
            module2 = ProbabilityOfImprovement(model=mm2, best_f=0.0)
            with self.assertRaises(UnsupportedError):
                module2(X)
开发者ID:saschwan,项目名称:botorch,代码行数:26,代码来源:test_analytic.py


示例6: forward

    def forward(self, input_ids, token_type_ids=None, attention_mask=None):
        if attention_mask is None:
            attention_mask = torch.ones_like(input_ids)
        if token_type_ids is None:
            token_type_ids = torch.zeros_like(input_ids)

        # We create a 3D attention mask from a 2D tensor mask.
        # Sizes are [batch_size, 1, 1, to_seq_length]
        # So we can broadcast to [batch_size, num_heads, from_seq_length, to_seq_length]
        # this attention mask is more simple than the triangular masking of causal attention
        # used in OpenAI GPT, we just need to prepare the broadcast dimension here.
        extended_attention_mask = attention_mask.unsqueeze(1).unsqueeze(2)

        # Since attention_mask is 1.0 for positions we want to attend and 0.0 for
        # masked positions, this operation will create a tensor which is 0.0 for
        # positions we want to attend and -10000.0 for masked positions.
        # Since we are adding it to the raw scores before the softmax, this is
        # effectively the same as removing these entirely.
        extended_attention_mask = extended_attention_mask.to(dtype=next(self.parameters()).dtype) # fp16 compatibility
        extended_attention_mask = (1.0 - extended_attention_mask) * -10000.0

        embedding_output = self.embeddings(input_ids, token_type_ids)
        all_encoder_layers = self.encoder(embedding_output, extended_attention_mask)
        sequence_output = all_encoder_layers[-1]
        pooled_output = self.pooler(sequence_output)
        return all_encoder_layers, pooled_output
开发者ID:zhouleidcc,项目名称:bert-Chinese-classification-task,代码行数:26,代码来源:modeling.py


示例7: test_index_setitem_bools_slices

    def test_index_setitem_bools_slices(self):
        true = torch.tensor(1, dtype=torch.uint8)
        false = torch.tensor(0, dtype=torch.uint8)

        tensors = [Variable(torch.randn(2, 3)), torch.tensor(3)]

        for a in tensors:
            # prefix with a 1,1, to ensure we are compatible with numpy which cuts off prefix 1s
            # (some of these ops already prefix a 1 to the size)
            neg_ones = torch.ones_like(a) * -1
            neg_ones_expanded = neg_ones.unsqueeze(0).unsqueeze(0)
            a[True] = neg_ones_expanded
            self.assertEqual(a, neg_ones)
            a[False] = 5
            self.assertEqual(a, neg_ones)
            a[true] = neg_ones_expanded * 2
            self.assertEqual(a, neg_ones * 2)
            a[false] = 5
            self.assertEqual(a, neg_ones * 2)
            a[None] = neg_ones_expanded * 3
            self.assertEqual(a, neg_ones * 3)
            a[...] = neg_ones_expanded * 4
            self.assertEqual(a, neg_ones * 4)
            if a.dim() == 0:
                with self.assertRaises(RuntimeError):
                    a[:] = neg_ones_expanded * 5
开发者ID:MaheshBhosale,项目名称:pytorch,代码行数:26,代码来源:test_indexing.py


示例8: test_index_setitem_bools_slices

    def test_index_setitem_bools_slices(self):
        true = variable(1).byte()
        false = variable(0).byte()

        tensors = [Variable(torch.randn(2, 3))]
        if torch._C._with_scalars():
            tensors.append(variable(3))

        for a in tensors:
            a_clone = a.clone()
            # prefix with a 1,1, to ensure we are compatible with numpy which cuts off prefix 1s
            # (some of these ops already prefix a 1 to the size)
            neg_ones = torch.ones_like(a) * -1
            neg_ones_expanded = neg_ones.unsqueeze(0).unsqueeze(0)
            a[True] = neg_ones_expanded
            self.assertEqual(a, neg_ones)
            a[False] = 5
            self.assertEqual(a, neg_ones)
            if torch._C._with_scalars():
                a[true] = neg_ones_expanded * 2
                self.assertEqual(a, neg_ones * 2)
                a[false] = 5
                self.assertEqual(a, neg_ones * 2)
            a[None] = neg_ones_expanded * 3
            self.assertEqual(a, neg_ones * 3)
            a[...] = neg_ones_expanded * 4
            self.assertEqual(a, neg_ones * 4)
            if a.dim() == 0:
                with self.assertRaises(RuntimeError):
                    a[:] = neg_ones_expanded * 5
开发者ID:bhuWenDongchao,项目名称:pytorch,代码行数:30,代码来源:test_indexing.py


示例9: dummy_mask

def dummy_mask(seq):
    '''
    create dummy mask (all 1)
    '''
    if isinstance(seq, tuple):
        seq = seq[0]
    assert len(seq.size()) == 1 or (len(seq.size()) == 2 and seq.size(1) == 1)
    return torch.ones_like(seq, dtype=torch.float)
开发者ID:UriSha,项目名称:sigmorphon,代码行数:8,代码来源:model.py


示例10: decode_step

 def decode_step(self, enc_hs, enc_mask, input_, hidden):
     trans, emiss, hidden = super().decode_step(enc_hs, enc_mask, input_,
                                                hidden)
     trans_mask = torch.ones_like(trans[0]).triu().unsqueeze(0)
     trans_mask = (trans_mask - 1) * -np.log(EPSILON)
     trans = trans + trans_mask
     trans = trans - trans.logsumexp(-1, keepdim=True)
     return trans, emiss, hidden
开发者ID:UriSha,项目名称:sigmorphon,代码行数:8,代码来源:model.py


示例11: topk_demo

def topk_demo(wl_dist: Tensor, ncubes: int):
    """ torch.topk() only returns the chosen index, sometimes I want to split the tensor,
        i.e., derive the other indices as well.
    """
    batch_dist, topk_idxs = wl_dist.topk(ncubes, sorted=False)  # topk_idxs: size <K>
    other_idxs = torch.arange(len(wl_dist))  # <Batch>
    other_idxs = torch.ones_like(other_idxs).byte().scatter_(-1, topk_idxs, 0)  # topk_idxs are 0, others are 1
    other_idxs = other_idxs.nonzero().squeeze(dim=-1)  # <Batch-K>
    return topk_idxs, other_idxs
开发者ID:AndriyLin,项目名称:Utils,代码行数:9,代码来源:pytorch.py


示例12: propose_log_prob

 def propose_log_prob(self, value):
     v = value / self._d
     result = -self._d.log()
     y = v.pow(1 / 3)
     result -= torch.log(3 * y ** 2)
     x = (y - 1) / self._c
     result -= self._c.log()
     result += Normal(torch.zeros_like(self.concentration), torch.ones_like(self.concentration)).log_prob(x)
     return result
开发者ID:lewisKit,项目名称:pyro,代码行数:9,代码来源:rejection_gamma.py


示例13: penalty

 def penalty(self, dis, real_data, fake_data):
     probe = self.get_probe(real_data.detach(), fake_data.detach())
     probe.requires_grad = True
     probe_logit, _ = dis(probe)
     gradients = autograd.grad(outputs=F.sigmoid(probe_logit),
                               inputs=probe,
                               grad_outputs=torch.ones_like(probe_logit))[0]
     grad_norm = gradients.view(gradients.shape[0], -1).norm(2, dim=1)
     penalty = ((grad_norm - self.target) ** 2).mean()
     return self.weight * penalty, grad_norm.mean()
开发者ID:dccastro,项目名称:Morpho-MNIST,代码行数:10,代码来源:gan_loss.py


示例14: test_cuda_extension

    def test_cuda_extension(self):
        import torch_test_cpp_extension.cuda as cuda_extension

        x = torch.zeros(100, device='cuda', dtype=torch.float32)
        y = torch.zeros(100, device='cuda', dtype=torch.float32)

        z = cuda_extension.sigmoid_add(x, y).cpu()

        # 2 * sigmoid(0) = 2 * 0.5 = 1
        self.assertEqual(z, torch.ones_like(z))
开发者ID:xiongyw,项目名称:pytorch,代码行数:10,代码来源:test_cpp_extensions.py


示例15: test_cuda_extension

    def test_cuda_extension(self):
        import torch_test_cuda_extension as cuda_extension

        x = torch.FloatTensor(100).zero_().cuda()
        y = torch.FloatTensor(100).zero_().cuda()

        z = cuda_extension.sigmoid_add(x, y).cpu()

        # 2 * sigmoid(0) = 2 * 0.5 = 1
        self.assertEqual(z, torch.ones_like(z))
开发者ID:MaheshBhosale,项目名称:pytorch,代码行数:10,代码来源:test_cpp_extensions.py


示例16: bce

def bce(prediction, target):
    if not hasattr(target, '__len__'):
        target = torch.ones_like(prediction)*target
        if prediction.is_cuda:
            target = target.cuda()
        target = Variable(target)
    loss = torch.nn.BCELoss()
    if prediction.is_cuda:
        loss = loss.cuda()
    return loss(prediction, target)
开发者ID:kazk1018,项目名称:manifold_mixup,代码行数:10,代码来源:mugan.py


示例17: forward

    def forward(self, prob, targets, infos, wt=None):
        prob = prob.clamp(min=1e-7, max=1-1e-7)
        if wt is None:
            wt1 = torch.ones_like(prob)
        if config.TRAIN.CE_LOSS_WEIGHTED and self.pos_wt is not None:
            wt1 = wt * (targets.detach() * self.pos_wt + (1-targets.detach()) * self.neg_wt)

        loss = -torch.mean(wt1 * (torch.log(prob) * targets + torch.log(1-prob) * (1-targets)))

        return loss
开发者ID:rsummers11,项目名称:CADLab,代码行数:10,代码来源:my_loss.py


示例18: valid_lb_ub

def valid_lb_ub(lb: Tensor, ub: Tensor) -> bool:
    """ To be valid:
        (1) Size ==
        (2) LB <= UB
    """
    if lb.size() != ub.size():
        return False

    # '<=' will return a uint8 tensor of 1 or 0 for each element, it should have all 1s.
    rel = lb <= ub
    return torch.equal(rel, torch.ones_like(rel))
开发者ID:AndriyLin,项目名称:Utils,代码行数:11,代码来源:pytorch.py


示例19: sample

 def sample(self, sample_shape=torch.Size()):
     sample_shape = torch.Size(sample_shape)
     samples = self._categorical.sample(torch.Size((self.total_count,)) + sample_shape)
     # samples.shape is (total_count, sample_shape, batch_shape), need to change it to
     # (sample_shape, batch_shape, total_count)
     shifted_idx = list(range(samples.dim()))
     shifted_idx.append(shifted_idx.pop(0))
     samples = samples.permute(*shifted_idx)
     counts = samples.new(self._extended_shape(sample_shape)).zero_()
     counts.scatter_add_(-1, samples, torch.ones_like(samples))
     return counts.type_as(self.probs)
开发者ID:inkawhich,项目名称:pytorch,代码行数:11,代码来源:multinomial.py


示例20: torch_ones_like

def torch_ones_like(x):
    """
    Polyfill for `torch.ones_like()`.
    """
    # Work around https://github.com/pytorch/pytorch/issues/2906
    if isinstance(x, Variable):
        return Variable(torch_ones_like(x.data))
    # Support Pytorch before https://github.com/pytorch/pytorch/pull/2489
    try:
        return torch.ones_like(x)
    except AttributeError:
        return torch.ones(x.size()).type_as(x)
开发者ID:Magica-Chen,项目名称:pyro,代码行数:12,代码来源:util.py



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Python torch.pow函数代码示例发布时间:2022-05-27
下一篇:
Python torch.ones函数代码示例发布时间: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