本文整理汇总了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
示例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
示例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()
示例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=[],
)
示例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))
示例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))
示例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))
示例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]))
示例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))
示例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))
示例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))
示例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
示例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
示例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
示例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