本文整理匯總了Python中torch.isclose方法的典型用法代碼示例。如果您正苦於以下問題:Python torch.isclose方法的具體用法?Python torch.isclose怎麽用?Python torch.isclose使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torch
的用法示例。
在下文中一共展示了torch.isclose方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_torch_ref_match
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import isclose [as 別名]
def test_torch_ref_match():
# Verify if the torch implementation values match the original Numpy implementation.
num_teachers, num_examples, num_labels = (100, 50, 10)
preds = (np.random.rand(num_teachers, num_examples) * num_labels).astype(int) # fake preds
indices = (np.random.rand(num_examples) * num_labels).astype(int) # true answers
preds[:, 0:10] *= 0
data_dep_eps, data_ind_eps = pate.perform_analysis_torch(
preds, indices, noise_eps=0.1, delta=1e-5
)
data_dep_eps_ref, data_ind_eps_ref = pate.perform_analysis(
preds, indices, noise_eps=0.1, delta=1e-5
)
assert torch.isclose(data_dep_eps, torch.tensor(data_dep_eps_ref, dtype=torch.float32))
assert torch.isclose(data_ind_eps, torch.tensor(data_ind_eps_ref, dtype=torch.float32))
示例2: test_hinge_loss
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import isclose [as 別名]
def test_hinge_loss():
true_value = torch.Tensor([[1.2], [1], [1], [1]])
pred_value = torch.Tensor([[1.2], [0.1], [0], [-0.3]])
expected_loss = torch.Tensor([(0 + 1 - 0.3 + 0) / 2.0])
loss = losses.RankHingeLoss()(pred_value, true_value)
assert torch.isclose(expected_loss, loss)
expected_loss = torch.Tensor(
[(2 + 0.1 - 1.2 + 2 - 0.3 + 0) / 2.0])
loss = losses.RankHingeLoss(margin=2)(pred_value, true_value)
assert torch.isclose(expected_loss, loss)
true_value = torch.Tensor(
[[1.2], [1], [0.8], [1], [1], [0.8]])
pred_value = torch.Tensor(
[[1.2], [0.1], [-0.5], [0], [0], [-0.3]])
expected_loss = torch.Tensor(
[(0 + 1 - 0.15) / 2.0])
loss = losses.RankHingeLoss(num_neg=2, margin=1)(
pred_value, true_value)
assert torch.isclose(expected_loss, loss)
示例3: test_rank_crossentropy_loss
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import isclose [as 別名]
def test_rank_crossentropy_loss():
losses.neg_num = 1
def softmax(x):
return np.exp(x) / np.sum(np.exp(x), axis=0)
true_value = torch.Tensor([[1.], [0.], [0.], [1.]])
pred_value = torch.Tensor([[0.8], [0.1], [0.8], [0.1]])
expected_loss = torch.Tensor(
[(-np.log(softmax([0.8, 0.1])[0]) - np.log(
softmax([0.8, 0.1])[1])) / 2])
loss = losses.RankCrossEntropyLoss()(pred_value, true_value)
assert torch.isclose(expected_loss, loss)
true_value = torch.Tensor([[1.], [0.], [0.], [0.], [1.], [0.]])
pred_value = torch.Tensor([[0.8], [0.1], [0.1], [0.8], [0.1], [0.1]])
expected_loss = torch.Tensor(
[(-np.log(softmax([0.8, 0.1, 0.1])[0]) - np.log(
softmax([0.8, 0.1, 0.1])[1])) / 2])
loss = losses.RankCrossEntropyLoss(num_neg=2)(
pred_value, true_value)
assert torch.isclose(expected_loss, loss)
示例4: _components
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import isclose [as 別名]
def _components(self) -> Dict[Tuple[str, str, str], Tuple[Tensor, Tensor]]:
states_per_measure = defaultdict(list)
for state_belief in self.state_beliefs:
for m, measure in enumerate(self.design.measures):
H = state_belief.H[:, m, :].data
m = H * state_belief.means.data
std = H * torch.diagonal(state_belief.covs.data, dim1=-2, dim2=-1).sqrt()
states_per_measure[measure].append((m, std))
out = {}
for measure, means_and_stds in states_per_measure.items():
means, stds = zip(*means_and_stds)
means = torch.stack(means).permute(1, 0, 2)
stds = torch.stack(stds).permute(1, 0, 2)
for s, (process_name, state_element) in enumerate(self.design.state_elements):
if ~torch.isclose(means[:, :, s].abs().max(), torch.zeros(1)):
out[(measure, process_name, state_element)] = (means[:, :, s], stds[:, :, s])
return out
示例5: test_rand
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import isclose [as 別名]
def test_rand(self):
"""Tests uniform random variable generation on [0, 1)"""
for size in [(10,), (10, 10), (10, 10, 10)]:
randvec = crypten.rand(*size)
self.assertTrue(randvec.size() == size, "Incorrect size")
tensor = randvec.get_plain_text()
self.assertTrue(
(tensor >= 0).all() and (tensor < 1).all(), "Invalid values"
)
randvec = crypten.rand(int(1e6)).get_plain_text()
mean = torch.mean(randvec)
var = torch.var(randvec)
self.assertTrue(torch.isclose(mean, torch.Tensor([0.5]), rtol=1e-3, atol=1e-3))
self.assertTrue(
torch.isclose(var, torch.Tensor([1.0 / 12]), rtol=1e-3, atol=1e-3)
)
示例6: _run_act_layer_grad
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import isclose [as 別名]
def _run_act_layer_grad(act_type):
x = torch.rand(10, 1000) * 10
m = MLP(act_layer=act_type)
def _run(x, act_layer=''):
if act_layer:
# replace act layer if set
m.act = create_act_layer(act_layer, inplace=True)
out = m(x)
l = (out - 0).pow(2).sum()
return l
out_me = _run(x)
with set_layer_config(scriptable=True):
out_jit = _run(x, act_type)
assert torch.isclose(out_jit, out_me)
with set_layer_config(no_jit=True):
out_basic = _run(x, act_type)
assert torch.isclose(out_basic, out_jit)
示例7: test_arcface_loss
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import isclose [as 別名]
def test_arcface_loss(self):
margin = 30
scale = 64
loss_func = ArcFaceLoss(margin=margin, scale=scale, num_classes=10, embedding_size=2)
embedding_angles = torch.arange(0, 180)
embeddings = torch.tensor([c_f.angle_to_coord(a) for a in embedding_angles], requires_grad=True, dtype=torch.float) #2D embeddings
labels = torch.randint(low=0, high=10, size=(180,))
loss = loss_func(embeddings, labels)
loss.backward()
weights = torch.nn.functional.normalize(loss_func.W, p=2, dim=0)
logits = torch.matmul(embeddings, weights)
for i, c in enumerate(labels):
logits[i, c] = torch.cos(torch.acos(logits[i, c]) + torch.tensor(np.radians(margin)))
correct_loss = torch.nn.functional.cross_entropy(logits*scale, labels)
self.assertTrue(torch.isclose(loss, correct_loss))
示例8: test_triplet_margin_loss
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import isclose [as 別名]
def test_triplet_margin_loss(self):
margin = 0.2
loss_funcA = TripletMarginLoss(margin=margin)
loss_funcB = TripletMarginLoss(margin=margin, reducer=MeanReducer())
embedding_angles = [0, 20, 40, 60, 80]
embeddings = torch.tensor([c_f.angle_to_coord(a) for a in embedding_angles], requires_grad=True, dtype=torch.float) #2D embeddings
labels = torch.LongTensor([0, 0, 1, 1, 2])
lossA = loss_funcA(embeddings, labels)
lossB = loss_funcB(embeddings, labels)
lossA.backward()
lossB.backward()
triplets = [(0,1,2), (0,1,3), (0,1,4), (1,0,2), (1,0,3), (1,0,4), (2,3,0), (2,3,1), (2,3,4), (3,2,0), (3,2,1), (3,2,4)]
correct_loss = 0
num_non_zero_triplets = 0
for a, p, n in triplets:
anchor, positive, negative = embeddings[a], embeddings[p], embeddings[n]
curr_loss = torch.relu(torch.sqrt(torch.sum((anchor-positive)**2)) - torch.sqrt(torch.sum((anchor-negative)**2)) + margin)
if curr_loss > 0:
num_non_zero_triplets += 1
correct_loss += curr_loss
self.assertTrue(torch.isclose(lossA, correct_loss/num_non_zero_triplets))
self.assertTrue(torch.isclose(lossB, correct_loss/len(triplets)))
示例9: test_angular_loss
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import isclose [as 別名]
def test_angular_loss(self):
loss_func = AngularLoss(alpha=40)
embedding_angles = [0, 20, 40, 60, 80]
embeddings = torch.tensor([c_f.angle_to_coord(a) for a in embedding_angles], requires_grad=True, dtype=torch.float) #2D embeddings
labels = torch.LongTensor([0, 0, 1, 1, 2])
loss = loss_func(embeddings, labels)
loss.backward()
sq_tan_alpha = torch.tan(torch.tensor(np.radians(40)))**2
triplets = [(0,1,2), (0,1,3), (0,1,4), (1,0,2), (1,0,3), (1,0,4), (2,3,0), (2,3,1), (2,3,4), (3,2,0), (3,2,1), (3,2,4)]
correct_losses = [0,0,0,0]
for a, p, n in triplets:
anchor, positive, negative = embeddings[a], embeddings[p], embeddings[n]
exponent = 4*sq_tan_alpha*torch.matmul(anchor+positive,negative) - 2*(1+sq_tan_alpha)*torch.matmul(anchor, positive)
correct_losses[a] += torch.exp(exponent)
total_loss = 0
for c in correct_losses:
total_loss += torch.log(1+c)
total_loss /= len(correct_losses)
self.assertTrue(torch.isclose(loss, total_loss.to(torch.float32)))
示例10: test_cosface_loss
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import isclose [as 別名]
def test_cosface_loss(self):
margin = 0.5
scale = 64
loss_func = CosFaceLoss(margin=margin, scale=scale, num_classes=10, embedding_size=2)
embedding_angles = torch.arange(0, 180)
embeddings = torch.tensor([c_f.angle_to_coord(a) for a in embedding_angles], requires_grad=True, dtype=torch.float) #2D embeddings
labels = torch.randint(low=0, high=10, size=(180,))
loss = loss_func(embeddings, labels)
loss.backward()
weights = torch.nn.functional.normalize(loss_func.W, p=2, dim=0)
logits = torch.matmul(embeddings, weights)
for i, c in enumerate(labels):
logits[i, c] -= margin
correct_loss = torch.nn.functional.cross_entropy(logits*scale, labels)
self.assertTrue(torch.isclose(loss, correct_loss))
示例11: test_proxy_nca_loss
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import isclose [as 別名]
def test_proxy_nca_loss(self):
softmax_scale = 10
loss_func = ProxyNCALoss(softmax_scale=softmax_scale, num_classes=10, embedding_size=2)
embedding_angles = torch.arange(0, 180)
embeddings = torch.tensor([c_f.angle_to_coord(a) for a in embedding_angles], requires_grad=True, dtype=torch.float) #2D embeddings
labels = torch.randint(low=0, high=10, size=(180,))
loss = loss_func(embeddings, labels)
loss.backward()
proxies = torch.nn.functional.normalize(loss_func.proxies, p=2, dim=1)
correct_loss = 0
for i in range(len(embeddings)):
curr_emb, curr_label = embeddings[i], labels[i]
curr_proxy = proxies[curr_label]
denominator = torch.sum((curr_emb-proxies)**2, dim=1)
denominator = torch.sum(torch.exp(-denominator*softmax_scale))
numerator = torch.sum((curr_emb-curr_proxy)**2)
numerator = torch.exp(-numerator*softmax_scale)
correct_loss += -torch.log(numerator/denominator)
correct_loss /= len(embeddings)
self.assertTrue(torch.isclose(loss, correct_loss))
示例12: test_add_infinity_and_beyond
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import isclose [as 別名]
def test_add_infinity_and_beyond(a, b, c, negative, manifold, dtype):
_a = a
if torch.isclose(c, c.new_zeros(())).any():
pytest.skip("zero not checked")
infty = b * 10000000
for i in range(100):
z = manifold.expmap(a, infty, project=False)
z = manifold.projx(z)
assert not torch.isnan(z).any(), ("Found nans", i, z)
assert torch.isfinite(z).all(), ("Found Infs", i, z)
z = manifold.mobius_scalar_mul(
torch.tensor(1000.0, dtype=z.dtype), z, project=False
)
z = manifold.projx(z)
assert not torch.isnan(z).any(), ("Found nans", i, z)
assert torch.isfinite(z).all(), ("Found Infs", i, z)
infty = manifold.transp(a, z, infty)
assert torch.isfinite(infty).all(), (i, infty)
a = z
z = manifold.expmap(a, -infty)
# they just need to be very far, exact answer is not supposed
tolerance = {
torch.float32: dict(rtol=3e-1, atol=2e-1),
torch.float64: dict(rtol=1e-1, atol=1e-3),
}
if negative:
np.testing.assert_allclose(z.detach(), -a.detach(), **tolerance[dtype])
else:
assert not torch.isnan(z).any(), "Found nans"
assert not torch.isnan(a).any(), "Found nans"
示例13: test_weighted_midpoint
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import isclose [as 別名]
def test_weighted_midpoint(_k, lincomb):
manifold = stereographic.Stereographic(_k, learnable=True)
a = manifold.random(2, 3, 10).requires_grad_(True)
mid = manifold.weighted_midpoint(a, lincomb=lincomb)
assert torch.isfinite(mid).all()
assert mid.shape == (a.shape[-1],)
mid.sum().backward()
assert torch.isfinite(a.grad).all()
assert not torch.isclose(manifold.k.grad, manifold.k.new_zeros(()))
示例14: test_weighted_midpoint_reduce_dim
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import isclose [as 別名]
def test_weighted_midpoint_reduce_dim(_k, lincomb):
manifold = stereographic.Stereographic(_k, learnable=True)
a = manifold.random(2, 3, 10).requires_grad_(True)
mid = manifold.weighted_midpoint(a, reducedim=[0], lincomb=lincomb)
assert mid.shape == a.shape[-2:]
assert torch.isfinite(mid).all()
mid.sum().backward()
assert torch.isfinite(a.grad).all()
assert not torch.isclose(manifold.k.grad, manifold.k.new_zeros(()))
示例15: test_weighted_midpoint_weighted
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import isclose [as 別名]
def test_weighted_midpoint_weighted(_k, lincomb):
manifold = stereographic.Stereographic(_k, learnable=True)
a = manifold.random(2, 3, 10).requires_grad_(True)
mid = manifold.weighted_midpoint(
a, reducedim=[0], lincomb=lincomb, weights=torch.rand_like(a[..., 0])
)
assert mid.shape == a.shape[-2:]
assert torch.isfinite(mid).all()
mid.sum().backward()
assert torch.isfinite(a.grad).all()
assert not torch.isclose(manifold.k.grad, manifold.k.new_zeros(()))