/usr/local/lib64/python3.6/site-packages/torch/nn/modules/__pycache__
Edit: /usr/local/lib64/python3.6/site-packages/torch/nn/modules/__pycache__/distance.cpython-36.pyc (3684B)
3
Eg] @ sH d dl mZ ddlmZ ddlmZ G dd deZG dd d eZd
S ) )Module )
functional )Tensorc sX e Zd ZU dZdddgZeee deeedd fd
d
Z
eeedd
dZ Z
S )PairwiseDistancea
Computes the pairwise distance between vectors :math:`v_1`, :math:`v_2` using the p-norm:
.. math ::
\Vert x \Vert _p = \left( \sum_{i=1}^n \vert x_i \vert ^ p \right) ^ {1/p}.
Args:
p (real): the norm degree. Default: 2
eps (float, optional): Small value to avoid division by zero.
Default: 1e-6
keepdim (bool, optional): Determines whether or not to keep the vector dimension.
Default: False
Shape:
- Input1: :math:`(N, D)` or :math:`(D)` where `N = batch dimension` and `D = vector dimension`
- Input2: :math:`(N, D)` or :math:`(D)`, same shape as the Input1
- Output: :math:`(N)` or :math:`()` based on input dimension.
If :attr:`keepdim` is ``True``, then :math:`(N, 1)` or :math:`(1)` based on input dimension.
Examples::
>>> pdist = nn.PairwiseDistance(p=2)
>>> input1 = torch.randn(100, 128)
>>> input2 = torch.randn(100, 128)
>>> output = pdist(input1, input2)
normepskeepdim @ư>FN)pr r
returnc s$ t t| j || _|| _|| _d S )N)superr __init__r r r
)selfr
r r
) __class__ E/usr/local/lib64/python3.6/site-packages/torch/nn/modules/distance.pyr $ s zPairwiseDistance.__init__)x1x2r c C s t j||| j| j| jS )N)FZpairwise_distancer r r
)r r r r r r forward* s zPairwiseDistance.forward)r r F)__name__
__module____qualname____doc__
__constants__floatr r boolr
r r r
__classcell__r r )r r r s
r c sP e Zd ZU dZddgZeed
eedd fdd
Z e
e
e
d
ddZ ZS )CosineSimilaritya Returns cosine similarity between :math:`x_1` and :math:`x_2`, computed along `dim`.
.. math ::
\text{similarity} = \dfrac{x_1 \cdot x_2}{\max(\Vert x_1 \Vert _2 \cdot \Vert x_2 \Vert _2, \epsilon)}.
Args:
dim (int, optional): Dimension where cosine similarity is computed. Default: 1
eps (float, optional): Small value to avoid division by zero.
Default: 1e-8
Shape:
- Input1: :math:`(\ast_1, D, \ast_2)` where D is at position `dim`
- Input2: :math:`(\ast_1, D, \ast_2)`, same number of dimensions as x1, matching x1 size at dimension `dim`,
and broadcastable with x1 at other dimensions.
- Output: :math:`(\ast_1, \ast_2)`
Examples::
>>> input1 = torch.randn(100, 128)
>>> input2 = torch.randn(100, 128)
>>> cos = nn.CosineSimilarity(dim=1, eps=1e-6)
>>> output = cos(input1, input2)
dimr r :0yE>N)r" r r c s t t| j || _|| _d S )N)r r! r r" r )r r" r )r r r r G s zCosineSimilarity.__init__)r r r c C s t j||| j| jS )N)r Zcosine_similarityr" r )r r r r r r r L s zCosineSimilarity.forward)r r# )
r r r r r intr" r r r r r r r r )r r r! . s
r! N) moduler r r Ztorchr r r! r r r r
s '