Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
33e35326db | ||
|
d57776c45a | ||
|
a660434d21 | ||
|
00bfa63bc5 | ||
|
be40616920 |
@ -456,7 +456,7 @@ python ./tasks/argmax_task.py -cuda 0 -lr 0.0001 -rnn_type lstm -memory_type dnc
|
||||
|
||||
## General noteworthy stuff
|
||||
|
||||
1. SDNCs use the [FLANN approximate nearest neigbhour library](https://www.cs.ubc.ca/research/flann/), with its python binding [pyflann3](https://github.com/primetang/pyflann) and [FAISS](https://github.com/facebookresearch/faiss).
|
||||
1. SDNCs use the [FLANN approximate nearest neigbhour library](https://github.com/mariusmuja/flann), with its python binding [pyflann3](https://github.com/primetang/pyflann) and [FAISS](https://github.com/facebookresearch/faiss).
|
||||
|
||||
FLANN can be installed either from pip (automatically as a dependency), or from source (e.g. for multithreading via OpenMP):
|
||||
|
||||
|
24
dnc/util.py
24
dnc/util.py
@ -56,29 +56,23 @@ def cudalong(x, grad=False, gpu_id=-1):
|
||||
return t
|
||||
|
||||
|
||||
def θ(a, b, dimA=2, dimB=2, normBy=2):
|
||||
"""Batchwise Cosine distance
|
||||
def θ(a, b, normBy=2):
|
||||
"""Batchwise Cosine similarity
|
||||
|
||||
Cosine distance
|
||||
Cosine similarity
|
||||
|
||||
Arguments:
|
||||
a {Tensor} -- A 3D Tensor (b * m * w)
|
||||
b {Tensor} -- A 3D Tensor (b * r * w)
|
||||
|
||||
Keyword Arguments:
|
||||
dimA {number} -- exponent value of the norm for `a` (default: {2})
|
||||
dimB {number} -- exponent value of the norm for `b` (default: {1})
|
||||
|
||||
Returns:
|
||||
Tensor -- Batchwise cosine distance (b * r * m)
|
||||
Tensor -- Batchwise cosine similarity (b * r * m)
|
||||
"""
|
||||
a_norm = T.norm(a, normBy, dimA, keepdim=True).expand_as(a) + δ
|
||||
b_norm = T.norm(b, normBy, dimB, keepdim=True).expand_as(b) + δ
|
||||
|
||||
x = T.bmm(a, b.transpose(1, 2)).transpose(1, 2) / (
|
||||
T.bmm(a_norm, b_norm.transpose(1, 2)).transpose(1, 2) + δ)
|
||||
# apply_dict(locals())
|
||||
return x
|
||||
dot = T.bmm(a, b.transpose(1,2))
|
||||
a_norm = T.norm(a, normBy, dim=2).unsqueeze(2)
|
||||
b_norm = T.norm(b, normBy, dim=2).unsqueeze(1)
|
||||
cos = dot / (a_norm * b_norm + δ)
|
||||
return cos.transpose(1,2).contiguous()
|
||||
|
||||
|
||||
def σ(input, axis=1):
|
||||
|
Loading…
Reference in New Issue
Block a user