-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
63 lines (46 loc) · 1.63 KB
/
models.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
52
53
54
55
56
57
58
59
60
61
62
63
"""
models.py
Mar 12 2023
Gabriel Moreira
"""
import torch
import torch.nn as nn
import torch.nn.functional as F
import hyperbolic.nn as hnn
import hyperbolic.functional as hf
from backbone import Convnet
def create_mlp(in_dim: int, mlp_dims: str):
"""
Create MLP with mlp_dims e.g., "128-128-128"
"""
mlp_spec = f"{in_dim}-{mlp_dims}"
layers = []
f = list(map(int, mlp_spec.split("-")))
for i in range(len(f) - 2):
layers.append(nn.Linear(f[i], f[i + 1]))
layers.append(nn.BatchNorm1d(f[i + 1]))
layers.append(nn.ReLU(True))
layers.append(nn.Linear(f[-2], f[-1], bias=False))
return nn.Sequential(*layers)
def create_manifold_encoder(backbone: str,
manifold: str,
dim: int,
k: float,
riemannian: bool,
clip: float=None):
if backbone == 'convnet':
conv = Convnet(out_dim=dim)
if manifold.lower() == 'poincare':
assert k < 0
to_manifold = hnn.PoincareExp0(k, riemannian, clip)
if manifold.lower() == 'lorentz':
assert k < 0
to_manifold = hnn.LorentzExp0(k)
elif manifold.lower() == 'spherical':
assert k > 0
to_manifold = hnn.SphericalProjection(k)
elif manifold.lower() == 'euclidean':
assert k == 0
to_manifold = nn.Identity()
model = nn.Sequential(conv, to_manifold)
return model