-
Notifications
You must be signed in to change notification settings - Fork 5
/
LModules.py
46 lines (35 loc) · 1.4 KB
/
LModules.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
import torch
import torch.nn as nn
from lib.lorentz.manifold import CustomLorentz
class LorentzAct(nn.Module):
""" Implementation of a general Lorentz Activation on space components.
"""
def __init__(self, activation, manifold: CustomLorentz):
super(LorentzAct, self).__init__()
self.manifold = manifold
self.activation = activation # e.g. torch.relu
def forward(self, x):
return self.manifold.lorentz_activation(x, self.activation)
class LorentzReLU(nn.Module):
""" Implementation of Lorentz ReLU Activation on space components.
"""
def __init__(self, manifold: CustomLorentz):
super(LorentzReLU, self).__init__()
self.manifold = manifold
def forward(self, x):
return self.manifold.lorentz_relu(x)
class LorentzGlobalAvgPool2d(torch.nn.Module):
""" Implementation of a Lorentz Global Average Pooling based on Lorentz centroid defintion.
"""
def __init__(self, manifold: CustomLorentz, keep_dim=False):
super(LorentzGlobalAvgPool2d, self).__init__()
self.manifold = manifold
self.keep_dim = keep_dim
def forward(self, x):
""" x has to be in channel-last representation -> Shape = bs x H x W x C """
bs, h, w, c = x.shape
x = x.view(bs, -1, c)
x = self.manifold.centroid(x)
if self.keep_dim:
x = x.view(bs, 1, 1, c)
return x