-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResidualMLP.py
More file actions
28 lines (20 loc) · 951 Bytes
/
ResidualMLP.py
File metadata and controls
28 lines (20 loc) · 951 Bytes
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
import nn
class ResidualBlock(nn.Module):
def __init__(self, dim, hidden_dim):
super().__init__()
main_path = nn.Sequential(nn.Linear(dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, dim))
self.res = nn.Sequential(nn.Residual(main_path),
nn.ReLU())
def forward(self, x):
return self.res(x)
class ResidualMLP(nn.Module):
def __init__(self, dim, hidden_dim=128, num_blocks=3, num_classes=10):
super().__init__()
self.resnet = nn.Sequential(nn.Linear(dim, hidden_dim),
nn.ReLU(),
*[ResidualBlock(dim=hidden_dim, hidden_dim=hidden_dim//2) for _ in range(num_blocks)],
nn.Linear(hidden_dim, num_classes))
def forward(self, x):
return self.resnet(x)