當前位置: 首頁>>代碼示例>>Python>>正文


Python torch.isnan方法代碼示例

本文整理匯總了Python中torch.isnan方法的典型用法代碼示例。如果您正苦於以下問題:Python torch.isnan方法的具體用法?Python torch.isnan怎麽用?Python torch.isnan使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在torch的用法示例。


在下文中一共展示了torch.isnan方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: centerness_target

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import isnan [as 別名]
def centerness_target(self, anchors, bbox_targets):
        # only calculate pos centerness targets, otherwise there may be nan
        gts = self.bbox_coder.decode(anchors, bbox_targets)
        anchors_cx = (anchors[:, 2] + anchors[:, 0]) / 2
        anchors_cy = (anchors[:, 3] + anchors[:, 1]) / 2
        l_ = anchors_cx - gts[:, 0]
        t_ = anchors_cy - gts[:, 1]
        r_ = gts[:, 2] - anchors_cx
        b_ = gts[:, 3] - anchors_cy

        left_right = torch.stack([l_, r_], dim=1)
        top_bottom = torch.stack([t_, b_], dim=1)
        centerness = torch.sqrt(
            (left_right.min(dim=-1)[0] / left_right.max(dim=-1)[0]) *
            (top_bottom.min(dim=-1)[0] / top_bottom.max(dim=-1)[0]))
        assert not torch.isnan(centerness).any()
        return centerness 
開發者ID:open-mmlab,項目名稱:mmdetection,代碼行數:19,代碼來源:atss_head.py

示例2: set_optimizer_params_grad

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import isnan [as 別名]
def set_optimizer_params_grad(named_params_optimizer, named_params_model, test_nan=False):
    """
        Utility function for optimize_on_cpu and 16-bits training.
        Copy the gradient of the GPU parameters to the CPU/RAMM copy of the model
    """
    is_nan = False
    for (name_opti, param_opti), (name_model, param_model) in zip(named_params_optimizer, named_params_model):
        if name_opti != name_model:
            logger.error("name_opti != name_model: {} {}".format(name_opti, name_model))
            raise ValueError
        if param_model.grad is not None:
            if test_nan and torch.isnan(param_model.grad).sum() > 0:
                is_nan = True
            if param_opti.grad is None:
                param_opti.grad = torch.nn.Parameter(param_opti.data.new().resize_(*param_opti.data.size()))
            param_opti.grad.data.copy_(param_model.grad.data)
        else:
            param_opti.grad = None
    return is_nan 
開發者ID:dolphin-zs,項目名稱:Doc2EDAG,代碼行數:21,代碼來源:base_task.py

示例3: forward

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import isnan [as 別名]
def forward(self, inputs, target, size_average=True):

        n = torch.abs(inputs -target)
        with torch.no_grad():
            if torch.isnan(n.var(dim=0)).sum().item() == 0:
                self.running_mean = self.running_mean.to(n.device)
                self.running_mean *= (1 - self.momentum)
                self.running_mean += (self.momentum * n.mean(dim=0))
                self.running_var = self.running_var.to(n.device)
                self.running_var *= (1 - self.momentum)
                self.running_var += (self.momentum * n.var(dim=0))


        beta = (self.running_mean - self.running_var)
        beta = beta.clamp(max=self.beta, min=1e-3)

        beta = beta.view(-1, self.num_features).to(n.device)
        cond = n < beta.expand_as(n)
        loss = torch.where(cond, 0.5 * n ** 2 / beta, n - 0.5 * beta)
        if size_average:
            return loss.mean()
        return loss.sum() 
開發者ID:soeaver,項目名稱:Parsing-R-CNN,代碼行數:24,代碼來源:adjust_smooth_l1_loss.py

示例4: test_forward

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import isnan [as 別名]
def test_forward(self):
        batch_size = 10
        in_shape = [2, 3, 4]
        out_shape = [5, 6]
        inputs = torch.randn(batch_size, *in_shape)

        for hidden_sizes in [[20], [20, 30], [20, 30, 40]]:
            with self.subTest(hidden_sizes=hidden_sizes):
                model = mlp.MLP(
                    in_shape=in_shape,
                    out_shape=out_shape,
                    hidden_sizes=hidden_sizes,
                )
                outputs = model(inputs)
                self.assertIsInstance(outputs, torch.Tensor)
                self.assertEqual(outputs.shape, torch.Size([batch_size] + out_shape))
                self.assertFalse(torch.isnan(outputs).any())
                self.assertFalse(torch.isinf(outputs).any())

        with self.assertRaises(Exception):
            mlp.MLP(
                in_shape=in_shape,
                out_shape=out_shape,
                hidden_sizes=[],
            ) 
開發者ID:bayesiains,項目名稱:nsf,代碼行數:27,代碼來源:mlp_test.py

示例5: test_sample

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import isnan [as 別名]
def test_sample(self):
        num_samples = 10
        context_size = 20
        input_shape = [2, 3, 4]
        context_shape = [5, 6]
        dist = normal.StandardNormal(input_shape)
        maybe_context = torch.randn(context_size, *context_shape)
        for context in [None, maybe_context]:
            with self.subTest(context=context):
                samples = dist.sample(num_samples, context=context)
                self.assertIsInstance(samples, torch.Tensor)
                self.assertFalse(torch.isnan(samples).any())
                self.assertFalse(torch.isinf(samples).any())
                if context is None:
                    self.assertEqual(samples.shape, torch.Size([num_samples] + input_shape))
                else:
                    self.assertEqual(
                        samples.shape, torch.Size([context_size, num_samples] + input_shape)) 
開發者ID:bayesiains,項目名稱:nsf,代碼行數:20,代碼來源:normal_test.py

示例6: test_mean

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import isnan [as 別名]
def test_mean(self):
        context_size = 20
        input_shape = [2, 3, 4]
        context_shape = [5, 6]
        dist = normal.StandardNormal(input_shape)
        maybe_context = torch.randn(context_size, *context_shape)
        for context in [None, maybe_context]:
            with self.subTest(context=context):
                means = dist.mean(context=context)
                self.assertIsInstance(means, torch.Tensor)
                self.assertFalse(torch.isnan(means).any())
                self.assertFalse(torch.isinf(means).any())
                self.assertEqual(means, torch.zeros_like(means))
                if context is None:
                    self.assertEqual(means.shape, torch.Size(input_shape))
                else:
                    self.assertEqual(means.shape, torch.Size([context_size] + input_shape)) 
開發者ID:bayesiains,項目名稱:nsf,代碼行數:19,代碼來源:normal_test.py

示例7: test_sample_and_log_prob_with_context

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import isnan [as 別名]
def test_sample_and_log_prob_with_context(self):
        num_samples = 10
        context_size = 20
        input_shape = [2, 3, 4]
        context_shape = [2, 3, 4]

        dist = discrete.ConditionalIndependentBernoulli(input_shape)
        context = torch.randn(context_size, *context_shape)
        samples, log_prob = dist.sample_and_log_prob(num_samples, context=context)

        self.assertIsInstance(samples, torch.Tensor)
        self.assertIsInstance(log_prob, torch.Tensor)

        self.assertEqual(samples.shape, torch.Size([context_size, num_samples] + input_shape))
        self.assertEqual(log_prob.shape, torch.Size([context_size, num_samples]))

        self.assertFalse(torch.isnan(log_prob).any())
        self.assertFalse(torch.isinf(log_prob).any())
        self.assert_tensor_less_equal(log_prob, 0.0)

        self.assertFalse(torch.isnan(samples).any())
        self.assertFalse(torch.isinf(samples).any())
        binary = (samples == 1.0) | (samples == 0.0)
        self.assertEqual(binary, torch.ones_like(binary)) 
開發者ID:bayesiains,項目名稱:nsf,代碼行數:26,代碼來源:discrete_test.py

示例8: test_stochastic_elbo

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import isnan [as 別名]
def test_stochastic_elbo(self):
        batch_size = 10
        input_shape = [2, 3, 4]
        latent_shape = [5, 6]

        prior = distributions.StandardNormal(latent_shape)
        approximate_posterior = distributions.StandardNormal(latent_shape)
        likelihood = distributions.StandardNormal(input_shape)
        vae = base.VariationalAutoencoder(prior, approximate_posterior, likelihood)

        inputs = torch.randn(batch_size, *input_shape)
        for num_samples in [1, 10, 100]:
            with self.subTest(num_samples=num_samples):
                elbo = vae.stochastic_elbo(inputs, num_samples)
                self.assertIsInstance(elbo, torch.Tensor)
                self.assertFalse(torch.isnan(elbo).any())
                self.assertFalse(torch.isinf(elbo).any())
                self.assertEqual(elbo.shape, torch.Size([batch_size])) 
開發者ID:bayesiains,項目名稱:nsf,代碼行數:20,代碼來源:base_test.py

示例9: test_sample

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import isnan [as 別名]
def test_sample(self):
        num_samples = 10
        input_shape = [2, 3, 4]
        latent_shape = [5, 6]

        prior = distributions.StandardNormal(latent_shape)
        approximate_posterior = distributions.StandardNormal(latent_shape)
        likelihood = distributions.StandardNormal(input_shape)
        vae = base.VariationalAutoencoder(prior, approximate_posterior, likelihood)

        for mean in [True, False]:
            with self.subTest(mean=mean):
                samples = vae.sample(num_samples, mean=mean)
                self.assertIsInstance(samples, torch.Tensor)
                self.assertFalse(torch.isnan(samples).any())
                self.assertFalse(torch.isinf(samples).any())
                self.assertEqual(samples.shape, torch.Size([num_samples] + input_shape)) 
開發者ID:bayesiains,項目名稱:nsf,代碼行數:19,代碼來源:base_test.py

示例10: test_encode

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import isnan [as 別名]
def test_encode(self):
        batch_size = 20
        input_shape = [2, 3, 4]
        latent_shape = [5, 6]
        inputs = torch.randn(batch_size, *input_shape)

        prior = distributions.StandardNormal(latent_shape)
        approximate_posterior = distributions.StandardNormal(latent_shape)
        likelihood = distributions.StandardNormal(input_shape)
        vae = base.VariationalAutoencoder(prior, approximate_posterior, likelihood)

        for num_samples in [None, 1, 10]:
            with self.subTest(num_samples=num_samples):
                encodings = vae.encode(inputs, num_samples)
                self.assertIsInstance(encodings, torch.Tensor)
                self.assertFalse(torch.isnan(encodings).any())
                self.assertFalse(torch.isinf(encodings).any())
                if num_samples is None:
                    self.assertEqual(encodings.shape, torch.Size([batch_size] + latent_shape))
                else:
                    self.assertEqual(
                        encodings.shape, torch.Size([batch_size, num_samples] + latent_shape)) 
開發者ID:bayesiains,項目名稱:nsf,代碼行數:24,代碼來源:base_test.py

示例11: test_reconstruct

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import isnan [as 別名]
def test_reconstruct(self):
        batch_size = 20
        input_shape = [2, 3, 4]
        latent_shape = [5, 6]
        inputs = torch.randn(batch_size, *input_shape)

        prior = distributions.StandardNormal(latent_shape)
        approximate_posterior = distributions.StandardNormal(latent_shape)
        likelihood = distributions.StandardNormal(input_shape)
        vae = base.VariationalAutoencoder(prior, approximate_posterior, likelihood)

        for mean in [True, False]:
            for num_samples in [None, 1, 10]:
                with self.subTest(mean=mean, num_samples=num_samples):
                    recons = vae.reconstruct(inputs, num_samples=num_samples, mean=mean)
                    self.assertIsInstance(recons, torch.Tensor)
                    self.assertFalse(torch.isnan(recons).any())
                    self.assertFalse(torch.isinf(recons).any())
                    if num_samples is None:
                        self.assertEqual(recons.shape, torch.Size([batch_size] + input_shape))
                    else:
                        self.assertEqual(
                            recons.shape, torch.Size([batch_size, num_samples] + input_shape)) 
開發者ID:bayesiains,項目名稱:nsf,代碼行數:25,代碼來源:base_test.py

示例12: normalize_u

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import isnan [as 別名]
def normalize_u(u, codomain, out=None):
    if not torch.is_tensor(codomain) and codomain == 2:
        u = F.normalize(u, p=2, dim=0, out=out)
    elif codomain == float('inf'):
        u = projmax_(u)
    else:
        uabs = torch.abs(u)
        uph = u / uabs
        uph[torch.isnan(uph)] = 1
        uabs = uabs / torch.max(uabs)
        uabs = uabs**(codomain - 1)
        if codomain == 1:
            u = uph * uabs / vector_norm(uabs, float('inf'))
        else:
            u = uph * uabs / vector_norm(uabs, codomain / (codomain - 1))
    return u 
開發者ID:rtqichen,項目名稱:residual-flows,代碼行數:18,代碼來源:mixed_lipschitz.py

示例13: precision

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import isnan [as 別名]
def precision(pred, target, num_classes):
    r"""Computes the precision
    :math:`\frac{\mathrm{TP}}{\mathrm{TP}+\mathrm{FP}}` of predictions.

    Args:
        pred (Tensor): The predictions.
        target (Tensor): The targets.
        num_classes (int): The number of classes.

    :rtype: :class:`Tensor`
    """
    tp = true_positive(pred, target, num_classes).to(torch.float)
    fp = false_positive(pred, target, num_classes).to(torch.float)

    out = tp / (tp + fp)
    out[torch.isnan(out)] = 0

    return out 
開發者ID:rusty1s,項目名稱:pytorch_geometric,代碼行數:20,代碼來源:metric.py

示例14: recall

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import isnan [as 別名]
def recall(pred, target, num_classes):
    r"""Computes the recall
    :math:`\frac{\mathrm{TP}}{\mathrm{TP}+\mathrm{FN}}` of predictions.

    Args:
        pred (Tensor): The predictions.
        target (Tensor): The targets.
        num_classes (int): The number of classes.

    :rtype: :class:`Tensor`
    """
    tp = true_positive(pred, target, num_classes).to(torch.float)
    fn = false_negative(pred, target, num_classes).to(torch.float)

    out = tp / (tp + fn)
    out[torch.isnan(out)] = 0

    return out 
開發者ID:rusty1s,項目名稱:pytorch_geometric,代碼行數:20,代碼來源:metric.py

示例15: f1_score

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import isnan [as 別名]
def f1_score(pred, target, num_classes):
    r"""Computes the :math:`F_1` score
    :math:`2 \cdot \frac{\mathrm{precision} \cdot \mathrm{recall}}
    {\mathrm{precision}+\mathrm{recall}}` of predictions.

    Args:
        pred (Tensor): The predictions.
        target (Tensor): The targets.
        num_classes (int): The number of classes.

    :rtype: :class:`Tensor`
    """
    prec = precision(pred, target, num_classes)
    rec = recall(pred, target, num_classes)

    score = 2 * (prec * rec) / (prec + rec)
    score[torch.isnan(score)] = 0

    return score 
開發者ID:rusty1s,項目名稱:pytorch_geometric,代碼行數:21,代碼來源:metric.py


注:本文中的torch.isnan方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。