|
| 1 | +import torch |
| 2 | +import torch.nn as nn |
| 3 | +from torch.nn import functional as F |
| 4 | + |
| 5 | +# ---------- Shallow small net ---------- |
| 6 | +class ShallowNet(nn.Module): |
| 7 | + def __init__(self, input_size): |
| 8 | + super().__init__() |
| 9 | + self.net = nn.Sequential( |
| 10 | + nn.Linear(input_size, 32), |
| 11 | + nn.ReLU(), |
| 12 | + nn.Linear(32, 1), |
| 13 | + nn.Sigmoid() |
| 14 | + ) |
| 15 | + def forward(self, x): |
| 16 | + return self.net(x) |
| 17 | + |
| 18 | +# ---------- Wide fully-connected net ---------- |
| 19 | +class WideNet(nn.Module): |
| 20 | + def __init__(self, input_size): |
| 21 | + super().__init__() |
| 22 | + self.net = nn.Sequential( |
| 23 | + nn.Linear(input_size, 256), |
| 24 | + nn.ReLU(), |
| 25 | + nn.Linear(256, 256), |
| 26 | + nn.ReLU(), |
| 27 | + nn.Linear(256, 1), |
| 28 | + nn.Sigmoid() |
| 29 | + ) |
| 30 | + def forward(self, x): |
| 31 | + return self.net(x) |
| 32 | + |
| 33 | +# ---------- Residual MLP ---------- |
| 34 | +class ResidualBlock(nn.Module): |
| 35 | + def __init__(self, width): |
| 36 | + super().__init__() |
| 37 | + self.fc = nn.Linear(width, width) |
| 38 | + self.bn = nn.BatchNorm1d(width) |
| 39 | + def forward(self, x): |
| 40 | + return F.relu(self.bn(self.fc(x)) + x) |
| 41 | + |
| 42 | +class ResidualNet(nn.Module): |
| 43 | + def __init__(self, input_size): |
| 44 | + super().__init__() |
| 45 | + self.input_layer = nn.Linear(input_size, 128) |
| 46 | + self.block1 = ResidualBlock(128) |
| 47 | + self.block2 = ResidualBlock(128) |
| 48 | + self.output = nn.Linear(128, 1) |
| 49 | + def forward(self, x): |
| 50 | + x = F.relu(self.input_layer(x)) |
| 51 | + x = self.block1(x) |
| 52 | + x = self.block2(x) |
| 53 | + return torch.sigmoid(self.output(x)) |
| 54 | + |
| 55 | +# ---------- GAIL-style discriminator ---------- |
| 56 | +class GAILDiscriminator(nn.Module): |
| 57 | + def __init__(self, input_size): |
| 58 | + super().__init__() |
| 59 | + self.net = nn.Sequential( |
| 60 | + nn.Linear(input_size, 256), |
| 61 | + nn.LeakyReLU(0.2), |
| 62 | + nn.Linear(256, 128), |
| 63 | + nn.LeakyReLU(0.2), |
| 64 | + nn.Linear(128, 1), |
| 65 | + nn.Sigmoid() |
| 66 | + ) |
| 67 | + def forward(self, x): |
| 68 | + return self.net(x) |
| 69 | + |
| 70 | +# ---------- TabTransformer ---------- |
| 71 | +class TabTransformer(nn.Module): |
| 72 | + def __init__(self, input_size, n_heads=4, depth=3): |
| 73 | + super().__init__() |
| 74 | + self.embedding = nn.Linear(input_size, 64) |
| 75 | + encoder_layer = nn.TransformerEncoderLayer( |
| 76 | + d_model=64, nhead=n_heads, batch_first=True |
| 77 | + ) |
| 78 | + self.transformer = nn.TransformerEncoder(encoder_layer, num_layers=depth) |
| 79 | + self.fc_out = nn.Linear(64, 1) |
| 80 | + |
| 81 | + def forward(self, x): |
| 82 | + x = self.embedding(x).unsqueeze(1) |
| 83 | + x = self.transformer(x) |
| 84 | + return torch.sigmoid(self.fc_out(x[:, 0])) |
0 commit comments