本文整理匯總了Python中torch.norm方法的典型用法代碼示例。如果您正苦於以下問題:Python torch.norm方法的具體用法?Python torch.norm怎麽用?Python torch.norm使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torch
的用法示例。
在下文中一共展示了torch.norm方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: complex_norm
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import norm [as 別名]
def complex_norm(
complex_tensor: Tensor,
power: float = 1.0
) -> Tensor:
r"""Compute the norm of complex tensor input.
Args:
complex_tensor (Tensor): Tensor shape of `(..., complex=2)`
power (float): Power of the norm. (Default: `1.0`).
Returns:
Tensor: Power of the normed input tensor. Shape of `(..., )`
"""
# Replace by torch.norm once issue is fixed
# https://github.com/pytorch/pytorch/issues/34279
return complex_tensor.pow(2.).sum(-1).pow(0.5 * power)
示例2: magphase
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import norm [as 別名]
def magphase(
complex_tensor: Tensor,
power: float = 1.0
) -> Tuple[Tensor, Tensor]:
r"""Separate a complex-valued spectrogram with shape `(..., 2)` into its magnitude and phase.
Args:
complex_tensor (Tensor): Tensor shape of `(..., complex=2)`
power (float): Power of the norm. (Default: `1.0`)
Returns:
(Tensor, Tensor): The magnitude and phase of the complex tensor
"""
mag = complex_norm(complex_tensor, power)
phase = angle(complex_tensor)
return mag, phase
示例3: AccelerationMatchingError
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import norm [as 別名]
def AccelerationMatchingError(input, target):
global lossfunc
"""
Takes input as (N,C,D,3) 3D coordinates and similiar targets (Here C is number of channels equivalent to number of joints)
"""
assert input.shape == target.shape
assert len(input.shape) == 4
#print('\n')
#print(input[0,:8,0,:])
#print(target[0,:8,0,:])
input = input.cuda()
inputdistances = input[:,:,1:,:] - input[:,:,:-1,:]
inputdistances = torch.norm(inputdistances, dim=3)
inputaccn = inputdistances[:,:,2:] + inputdistances[:,:,:-2] - 2*inputdistances[:,:,1:-1]
targetdistances = target[:,:,1:,:] - target[:,:,:-1,:]
targetdistances = torch.norm(targetdistances, dim=3)
targetaccn = targetdistances[:,:,2:] + targetdistances[:,:,:-2] - 2*targetdistances[:,:,1:-1]
return lossfunc(inputaccn, targetaccn)
示例4: poincare_case
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import norm [as 別名]
def poincare_case():
torch.manual_seed(42)
shape = manifold_shapes[geoopt.manifolds.PoincareBall]
ex = torch.randn(*shape, dtype=torch.float64) / 3
ev = torch.randn(*shape, dtype=torch.float64) / 3
x = torch.tanh(torch.norm(ex)) * ex / torch.norm(ex)
ex = x.clone()
v = ev.clone()
manifold = geoopt.PoincareBall().to(dtype=torch.float64)
x = geoopt.ManifoldTensor(x, manifold=manifold)
case = UnaryCase(shape, x, ex, v, ev, manifold)
yield case
manifold = geoopt.PoincareBallExact().to(dtype=torch.float64)
x = geoopt.ManifoldTensor(x, manifold=manifold)
case = UnaryCase(shape, x, ex, v, ev, manifold)
yield case
示例5: sphere_subspace_case
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import norm [as 別名]
def sphere_subspace_case():
torch.manual_seed(42)
shape = manifold_shapes[geoopt.manifolds.Sphere]
subspace = torch.rand(shape[-1], 2, dtype=torch.float64)
Q, _ = geoopt.linalg.batch_linalg.qr(subspace)
P = Q @ Q.t()
ex = torch.randn(*shape, dtype=torch.float64)
ev = torch.randn(*shape, dtype=torch.float64)
x = (ex @ P.t()) / torch.norm(ex @ P.t())
v = (ev - (x @ ev) * x) @ P.t()
manifold = geoopt.Sphere(intersection=subspace)
x = geoopt.ManifoldTensor(x, manifold=manifold)
case = UnaryCase(shape, x, ex, v, ev, manifold)
yield case
manifold = geoopt.SphereExact(intersection=subspace)
x = geoopt.ManifoldTensor(x, manifold=manifold)
case = UnaryCase(shape, x, ex, v, ev, manifold)
yield case
示例6: sphere_compliment_case
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import norm [as 別名]
def sphere_compliment_case():
torch.manual_seed(42)
shape = manifold_shapes[geoopt.manifolds.Sphere]
complement = torch.rand(shape[-1], 1, dtype=torch.float64)
Q, _ = geoopt.linalg.batch_linalg.qr(complement)
P = -Q @ Q.transpose(-1, -2)
P[..., torch.arange(P.shape[-2]), torch.arange(P.shape[-2])] += 1
ex = torch.randn(*shape, dtype=torch.float64)
ev = torch.randn(*shape, dtype=torch.float64)
x = (ex @ P.t()) / torch.norm(ex @ P.t())
v = (ev - (x @ ev) * x) @ P.t()
manifold = geoopt.Sphere(complement=complement)
x = geoopt.ManifoldTensor(x, manifold=manifold)
case = UnaryCase(shape, x, ex, v, ev, manifold)
yield case
manifold = geoopt.SphereExact(complement=complement)
x = geoopt.ManifoldTensor(x, manifold=manifold)
case = UnaryCase(shape, x, ex, v, ev, manifold)
yield case
示例7: sphere_case
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import norm [as 別名]
def sphere_case():
torch.manual_seed(42)
shape = manifold_shapes[geoopt.manifolds.Sphere]
ex = torch.randn(*shape, dtype=torch.float64)
ev = torch.randn(*shape, dtype=torch.float64)
x = ex / torch.norm(ex)
v = ev - (x @ ev) * x
manifold = geoopt.Sphere()
x = geoopt.ManifoldTensor(x, manifold=manifold)
case = UnaryCase(shape, x, ex, v, ev, manifold)
yield case
manifold = geoopt.SphereExact()
x = geoopt.ManifoldTensor(x, manifold=manifold)
case = UnaryCase(shape, x, ex, v, ev, manifold)
yield case
示例8: calculate_positive_embedding_loss
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import norm [as 別名]
def calculate_positive_embedding_loss(self, z, positive_edges):
"""
Calculating the loss on the positive edge embedding distances
:param z: Hidden vertex representation.
:param positive_edges: Positive training edges.
:return loss_term: Loss value on positive edge embedding.
"""
self.positive_surrogates = [random.choice(self.nodes) for node in range(positive_edges.shape[1])]
self.positive_surrogates = torch.from_numpy(np.array(self.positive_surrogates, dtype=np.int64).T)
self.positive_surrogates = self.positive_surrogates.type(torch.long).to(self.device)
positive_edges = torch.t(positive_edges)
self.positive_z_i = z[positive_edges[:, 0], :]
self.positive_z_j = z[positive_edges[:, 1], :]
self.positive_z_k = z[self.positive_surrogates, :]
norm_i_j = torch.norm(self.positive_z_i-self.positive_z_j, 2, 1, True).pow(2)
norm_i_k = torch.norm(self.positive_z_i-self.positive_z_k, 2, 1, True).pow(2)
term = norm_i_j-norm_i_k
term[term < 0] = 0
loss_term = term.mean()
return loss_term
示例9: calculate_negative_embedding_loss
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import norm [as 別名]
def calculate_negative_embedding_loss(self, z, negative_edges):
"""
Calculating the loss on the negative edge embedding distances
:param z: Hidden vertex representation.
:param negative_edges: Negative training edges.
:return loss_term: Loss value on negative edge embedding.
"""
self.negative_surrogates = [random.choice(self.nodes) for node in range(negative_edges.shape[1])]
self.negative_surrogates = torch.from_numpy(np.array(self.negative_surrogates, dtype=np.int64).T)
self.negative_surrogates = self.negative_surrogates.type(torch.long).to(self.device)
negative_edges = torch.t(negative_edges)
self.negative_z_i = z[negative_edges[:, 0], :]
self.negative_z_j = z[negative_edges[:, 1], :]
self.negative_z_k = z[self.negative_surrogates, :]
norm_i_j = torch.norm(self.negative_z_i-self.negative_z_j, 2, 1, True).pow(2)
norm_i_k = torch.norm(self.negative_z_i-self.negative_z_k, 2, 1, True).pow(2)
term = norm_i_k-norm_i_j
term[term < 0] = 0
loss_term = term.mean()
return loss_term
示例10: operator_norm_settings
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import norm [as 別名]
def operator_norm_settings(domain, codomain):
if domain == 1 and codomain == 1:
# maximum l1-norm of column
max_across_input_dims = True
norm_type = 1
elif domain == 1 and codomain == 2:
# maximum l2-norm of column
max_across_input_dims = True
norm_type = 2
elif domain == 1 and codomain == float("inf"):
# maximum l-inf norm of column
max_across_input_dims = True
norm_type = float("inf")
elif domain == 2 and codomain == float("inf"):
# maximum l2-norm of row
max_across_input_dims = False
norm_type = 2
elif domain == float("inf") and codomain == float("inf"):
# maximum l1-norm of row
max_across_input_dims = False
norm_type = 1
else:
raise ValueError('Unknown combination of domain "{}" and codomain "{}"'.format(domain, codomain))
return max_across_input_dims, norm_type
示例11: _power_iteration
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import norm [as 別名]
def _power_iteration(self, A, num_simulations=30):
# Ideally choose a random vector
# To decrease the chance that our vector
# Is orthogonal to the eigenvector
b_k = torch.rand(A.shape[1]).unsqueeze(dim=1) * 0.5 - 1
for _ in range(num_simulations):
# calculate the matrix-by-vector product Ab
b_k1 = torch.mm(A, b_k)
# calculate the norm
b_k1_norm = torch.norm(b_k1)
# re normalize the vector
b_k = b_k1 / b_k1_norm
return b_k
示例12: __call__
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import norm [as 別名]
def __call__(self, data):
(row, col), pos, pseudo = data.edge_index, data.pos, data.edge_attr
assert pos.dim() == 2 and pos.size(1) == 2
cart = pos[col] - pos[row]
rho = torch.norm(cart, p=2, dim=-1).view(-1, 1)
theta = torch.atan2(cart[..., 1], cart[..., 0]).view(-1, 1)
theta = theta + (theta < 0).type_as(theta) * (2 * PI)
if self.norm:
rho = rho / (rho.max() if self.max is None else self.max)
theta = theta / (2 * PI)
polar = torch.cat([rho, theta], dim=-1)
if pseudo is not None and self.cat:
pseudo = pseudo.view(-1, 1) if pseudo.dim() == 1 else pseudo
data.edge_attr = torch.cat([pseudo, polar.type_as(pos)], dim=-1)
else:
data.edge_attr = polar
return data
示例13: loss_l2
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import norm [as 別名]
def loss_l2(self, l2=0):
"""L2 loss centered around mu_init, scaled optionally per-source.
In other words, diagonal Tikhonov regularization,
||D(\mu-\mu_{init})||_2^2
where D is diagonal.
Args:
- l2: A float or np.array representing the per-source regularization
strengths to use
"""
if isinstance(l2, (int, float)):
D = l2 * torch.eye(self.d)
else:
D = torch.diag(torch.from_numpy(l2))
# Note that mu is a matrix and this is the *Frobenius norm*
return torch.norm(D @ (self.mu - self.mu_init)) ** 2
示例14: get_grad_norms
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import norm [as 別名]
def get_grad_norms(model, keys=[]):
grads = {}
for i, (k, param) in enumerate(dict(model.named_parameters()).items()):
accept = False
for key in keys:
# match substring in collection of model keys
if key in k:
accept = True
break
if not accept:
continue
if param.grad is None:
print('WARNING getting grads: {} param grad is None'.format(k))
continue
grads[k] = torch.norm(param.grad).cpu().item()
return grads
示例15: __init__
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import norm [as 別名]
def __init__(self,
in_feats,
out_feats,
aggregator_type='mean',
feat_drop=0.,
bias=True,
norm=None,
activation=None,
G=None):
super(AdaptSAGEConv, self).__init__()
self._in_feats = in_feats
self._out_feats = out_feats
self.norm = norm
self.feat_drop = nn.Dropout(feat_drop)
self.activation = activation
# self.fc_self = nn.Linear(in_feats, out_feats, bias=bias).double()
self.fc_neigh = nn.Linear(in_feats, out_feats, bias=bias)
self.reset_parameters()
self.G = G