-
Notifications
You must be signed in to change notification settings - Fork 2
/
other_classes.py
45 lines (33 loc) · 1.02 KB
/
other_classes.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
import torch.nn as nn
class Residual(nn.Module):
def __init__(self, fn):
super().__init__()
self.fn = fn
def forward(self, x, **kwargs):
return self.fn(x, **kwargs) + x
class PreNorm(nn.Module):
def __init__(self, dim, fn):
super().__init__()
self.norm = nn.LayerNorm(dim)
self.fn = fn
def forward(self, x, **kwargs):
return self.fn(self.norm(x), **kwargs)
class FeedForward(nn.Module):
def __init__(self, dim, hidden_dim):
super().__init__()
self.net = nn.Sequential(
nn.Linear(dim, hidden_dim),
nn.GELU(),
nn.Linear(hidden_dim, dim)
)
def forward(self, x, **kwargs):
return self.net(x)
class QuickFix(nn.Module):
def __init__(self, dim, heads, fn):
super().__init__()
self.dim = dim
self.heads = heads
self.linear = nn.Linear(dim * heads, dim)
self.fn = fn
def forward(self, x, **kwargs):
return self.linear(self.fn(x, **kwargs))