2023-03-05 14:59:24 +00:00
|
|
|
import logging
|
|
|
|
|
2023-03-08 14:03:36 +00:00
|
|
|
import torch.nn as nn
|
2023-03-19 15:45:30 +00:00
|
|
|
import torch
|
2023-03-08 14:10:25 +00:00
|
|
|
|
2023-03-05 14:59:24 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2023-03-06 14:16:45 +00:00
|
|
|
class PyTorchMLPModel(nn.Module):
|
2023-03-19 15:45:30 +00:00
|
|
|
"""
|
|
|
|
A multi-layer perceptron (MLP) model implemented using PyTorch.
|
|
|
|
|
|
|
|
:param input_dim: The number of input features.
|
|
|
|
:param output_dim: The number of output classes.
|
|
|
|
:param hidden_dim: The number of hidden units in each layer. Default: 256
|
|
|
|
:param dropout_percent: The dropout rate for regularization. Default: 0.2
|
|
|
|
:param n_layer: The number of layers in the MLP. Default: 1
|
|
|
|
|
|
|
|
:returns: The output of the MLP, with shape (batch_size, output_dim)
|
|
|
|
|
|
|
|
|
|
|
|
A neural network typically consists of input, output, and hidden layers, where the
|
|
|
|
information flows from the input layer through the hidden layers to the output layer.
|
|
|
|
In a feedforward neural network, also known as a multilayer perceptron (MLP), the
|
|
|
|
information flows in one direction only. Each hidden layer contains multiple units
|
|
|
|
or nodes that take input from the previous layer and produce output that goes to the
|
|
|
|
next layer.
|
|
|
|
|
|
|
|
The hidden_dim parameter in the FeedForward class refers to the number of units
|
|
|
|
(or nodes) in the hidden layer. This parameter controls the complexity of the neural
|
|
|
|
network and determines how many nonlinear relationships the network can represent.
|
|
|
|
A higher value of hidden_dim allows the network to represent more complex functions
|
|
|
|
but may also make the network more prone to overfitting, where the model memorizes
|
|
|
|
the training data instead of learning general patterns.
|
|
|
|
"""
|
|
|
|
|
2023-03-12 12:31:08 +00:00
|
|
|
def __init__(self, input_dim: int, output_dim: int, **kwargs):
|
2023-03-06 14:16:45 +00:00
|
|
|
super(PyTorchMLPModel, self).__init__()
|
2023-03-19 15:45:30 +00:00
|
|
|
hidden_dim: int = kwargs.get("hidden_dim", 256)
|
2023-03-12 12:31:08 +00:00
|
|
|
dropout_percent: int = kwargs.get("dropout_percent", 0.2)
|
|
|
|
n_layer: int = kwargs.get("n_layer", 1)
|
2023-03-05 14:59:24 +00:00
|
|
|
self.input_layer = nn.Linear(input_dim, hidden_dim)
|
2023-03-12 12:31:08 +00:00
|
|
|
self.blocks = nn.Sequential(*[Block(hidden_dim, dropout_percent) for _ in range(n_layer)])
|
2023-03-05 14:59:24 +00:00
|
|
|
self.output_layer = nn.Linear(hidden_dim, output_dim)
|
|
|
|
self.relu = nn.ReLU()
|
2023-03-12 12:31:08 +00:00
|
|
|
self.dropout = nn.Dropout(p=dropout_percent)
|
2023-03-05 14:59:24 +00:00
|
|
|
|
2023-03-19 15:45:30 +00:00
|
|
|
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
2023-03-05 14:59:24 +00:00
|
|
|
x = self.relu(self.input_layer(x))
|
|
|
|
x = self.dropout(x)
|
2023-03-19 15:03:36 +00:00
|
|
|
x = self.blocks(x)
|
2023-03-05 14:59:24 +00:00
|
|
|
logits = self.output_layer(x)
|
2023-03-06 14:16:45 +00:00
|
|
|
return logits
|
2023-03-12 12:31:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Block(nn.Module):
|
2023-03-19 15:45:30 +00:00
|
|
|
"""
|
|
|
|
A building block for a multi-layer perceptron (MLP) implemented using PyTorch.
|
|
|
|
|
|
|
|
:param hidden_dim: The number of hidden units in the feedforward network.
|
|
|
|
:param dropout_percent: The dropout rate for regularization.
|
|
|
|
|
|
|
|
:returns: torch.Tensor. with shape (batch_size, hidden_dim)
|
|
|
|
"""
|
|
|
|
|
2023-03-12 12:31:08 +00:00
|
|
|
def __init__(self, hidden_dim: int, dropout_percent: int):
|
|
|
|
super(Block, self).__init__()
|
|
|
|
self.ff = FeedForward(hidden_dim)
|
|
|
|
self.dropout = nn.Dropout(p=dropout_percent)
|
|
|
|
self.ln = nn.LayerNorm(hidden_dim)
|
|
|
|
|
2023-03-19 15:45:30 +00:00
|
|
|
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
2023-03-19 15:03:36 +00:00
|
|
|
x = self.ff(self.ln(x))
|
|
|
|
x = self.dropout(x)
|
2023-03-12 12:31:08 +00:00
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
|
|
class FeedForward(nn.Module):
|
2023-03-19 15:45:30 +00:00
|
|
|
"""
|
|
|
|
A fully-connected feedforward neural network block.
|
|
|
|
|
|
|
|
:param hidden_dim: The number of hidden units in the block.
|
|
|
|
:return: torch.Tensor. with shape (batch_size, hidden_dim)
|
|
|
|
"""
|
|
|
|
|
2023-03-12 12:31:08 +00:00
|
|
|
def __init__(self, hidden_dim: int):
|
|
|
|
super(FeedForward, self).__init__()
|
|
|
|
self.net = nn.Sequential(
|
|
|
|
nn.Linear(hidden_dim, hidden_dim),
|
|
|
|
nn.ReLU(),
|
|
|
|
)
|
|
|
|
|
2023-03-19 15:45:30 +00:00
|
|
|
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
2023-03-12 12:31:08 +00:00
|
|
|
return self.net(x)
|