-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim.py
51 lines (41 loc) · 1.71 KB
/
sim.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import torch
import torch.nn as nn
from typing import Optional
class ExponentialSimilarity(nn.Module):
def __init__(
self, starting_value: Optional[float] = None, requires_grad: bool = True
) -> None:
"""The exponential similarity function: e^(-k*|x|). Given that the k is an
optional learnable parameter.
Args:
starting_value (Optional[float], optional): The starting value of k.
If None defaults to 1.
requires_grad (bool, optional): Whether k should be a
learnable parameter. Defaults to True.
"""
super().__init__()
self.param = nn.Parameter(
torch.Tensor([1] if starting_value is None else [starting_value]),
requires_grad=requires_grad,
)
def forward(self, x):
return torch.exp(-self.param * x.abs())
class AbsoluteInverseSimilarity(nn.Module):
def __init__(
self, starting_value: Optional[float] = None, requires_grad: bool = False
) -> None:
"""The absolute inverse similarity function: 1 / (1 + k*|x|). Given that
the k is an optional learnable parameter.
Args:
starting_value (Optional[float], optional): The starting value of k.
If None, defaults to 1.
requires_grad (bool, optional): Whether k should be a
learnable parameter. Defaults to False.
"""
super().__init__()
self.param = nn.Parameter(
torch.Tensor([1] if starting_value is None else [starting_value]),
requires_grad=requires_grad,
)
def forward(self, x):
return 1 / (1 + self.param * x.abs())