Upload src/model/mlp.py with huggingface_hub
Browse files- src/model/mlp.py +25 -0
src/model/mlp.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from torch import nn
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class MultiLayerPerceptron(nn.Module):
|
| 6 |
+
"""Multi-Layer Perceptron with residual links."""
|
| 7 |
+
def __init__(self, input_dim, hidden_dim) -> None:
|
| 8 |
+
super().__init__()
|
| 9 |
+
self.fc1 = nn.Conv2d(
|
| 10 |
+
in_channels=input_dim, out_channels=hidden_dim, kernel_size=(1, 1), bias=True)
|
| 11 |
+
self.fc2 = nn.Conv2d(
|
| 12 |
+
in_channels=hidden_dim, out_channels=hidden_dim, kernel_size=(1, 1), bias=True)
|
| 13 |
+
self.act = nn.ReLU()
|
| 14 |
+
self.drop = nn.Dropout(p=0.15)
|
| 15 |
+
|
| 16 |
+
def forward(self, input_data: torch.Tensor) -> torch.Tensor:
|
| 17 |
+
"""Feed forward of MLP.
|
| 18 |
+
Args:
|
| 19 |
+
input_data (torch.Tensor): input data with shape [B, D, N]
|
| 20 |
+
Returns:
|
| 21 |
+
torch.Tensor: latent repr
|
| 22 |
+
"""
|
| 23 |
+
hidden = self.fc2(self.drop(self.act(self.fc1(input_data)))) # MLP
|
| 24 |
+
hidden = hidden + input_data # residual
|
| 25 |
+
return hidden
|