本文整理汇总了Python中torch.Tensor.norm方法的典型用法代码示例。如果您正苦于以下问题:Python Tensor.norm方法的具体用法?Python Tensor.norm怎么用?Python Tensor.norm使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torch.Tensor
的用法示例。
在下文中一共展示了Tensor.norm方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: multi_perspective_match_pairwise
# 需要导入模块: from torch import Tensor [as 别名]
# 或者: from torch.Tensor import norm [as 别名]
def multi_perspective_match_pairwise(vector1: torch.Tensor,
vector2: torch.Tensor,
weight: torch.Tensor,
eps: float = 1e-8) -> torch.Tensor:
"""
Calculate multi-perspective cosine matching between each time step of
one vector and each time step of another vector.
Parameters
----------
vector1 : ``torch.Tensor``
A tensor of shape ``(batch, seq_len1, hidden_size)``
vector2 : ``torch.Tensor``
A tensor of shape ``(batch, seq_len2, hidden_size)``
weight : ``torch.Tensor``
A tensor of shape ``(num_perspectives, hidden_size)``
eps : ``float`` optional, (default = 1e-8)
A small value to avoid zero division problem
Returns
-------
A tensor of shape (batch, seq_len1, seq_len2, num_perspectives) consisting
multi-perspective matching results
"""
num_perspectives = weight.size(0)
# (1, num_perspectives, 1, hidden_size)
weight = weight.unsqueeze(0).unsqueeze(2)
# (batch, num_perspectives, seq_len*, hidden_size)
vector1 = weight * vector1.unsqueeze(1).expand(-1, num_perspectives, -1, -1)
vector2 = weight * vector2.unsqueeze(1).expand(-1, num_perspectives, -1, -1)
# (batch, num_perspectives, seq_len*, 1)
vector1_norm = vector1.norm(p=2, dim=3, keepdim=True)
vector2_norm = vector2.norm(p=2, dim=3, keepdim=True)
# (batch, num_perspectives, seq_len1, seq_len2)
mul_result = torch.matmul(vector1, vector2.transpose(2, 3))
norm_value = vector1_norm * vector2_norm.transpose(2, 3)
# (batch, seq_len1, seq_len2, num_perspectives)
return (mul_result / norm_value.clamp(min=eps)).permute(0, 2, 3, 1)
示例2: forward
# 需要导入模块: from torch import Tensor [as 别名]
# 或者: from torch.Tensor import norm [as 别名]
def forward(self, tensor_1: torch.Tensor, tensor_2: torch.Tensor) -> torch.Tensor:
normalized_tensor_1 = tensor_1 / tensor_1.norm(dim=-1, keepdim=True)
normalized_tensor_2 = tensor_2 / tensor_2.norm(dim=-1, keepdim=True)
return (normalized_tensor_1 * normalized_tensor_2).sum(dim=-1)
示例3: _forward_internal
# 需要导入模块: from torch import Tensor [as 别名]
# 或者: from torch.Tensor import norm [as 别名]
def _forward_internal(self, vector: torch.Tensor, matrix: torch.Tensor) -> torch.Tensor:
a_norm = vector / (vector.norm(p=2, dim=-1, keepdim=True) + 1e-13)
b_norm = matrix / (matrix.norm(p=2, dim=-1, keepdim=True) + 1e-13)
return torch.bmm(a_norm.unsqueeze(dim=1), b_norm.transpose(-1, -2)).squeeze(1)
示例4: forward
# 需要导入模块: from torch import Tensor [as 别名]
# 或者: from torch.Tensor import norm [as 别名]
def forward(self, matrix_1: torch.Tensor, matrix_2: torch.Tensor) -> torch.Tensor:
a_norm = matrix_1 / (matrix_1.norm(p=2, dim=-1, keepdim=True) + 1e-13)
b_norm = matrix_2 / (matrix_2.norm(p=2, dim=-1, keepdim=True) + 1e-13)
return torch.bmm(a_norm, b_norm.transpose(-1, -2))