MLP as used in Vision Transformer, MLP-Mixer and related networks
| 43 | |
| 44 | |
| 45 | class Mlp(nn.Module): |
| 46 | """ MLP as used in Vision Transformer, MLP-Mixer and related networks""" |
| 47 | def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, bias=True, dtype=None, device=None): |
| 48 | super().__init__() |
| 49 | out_features = out_features or in_features |
| 50 | hidden_features = hidden_features or in_features |
| 51 | |
| 52 | self.fc1 = nn.Linear(in_features, hidden_features, bias=bias, dtype=dtype, device=device) |
| 53 | self.act = act_layer |
| 54 | self.fc2 = nn.Linear(hidden_features, out_features, bias=bias, dtype=dtype, device=device) |
| 55 | |
| 56 | def forward(self, x): |
| 57 | x = self.fc1(x) |
| 58 | x = self.act(x) |
| 59 | x = self.fc2(x) |
| 60 | return x |
| 61 | |
| 62 | |
| 63 | ################################################################################################# |