| |
| import math |
| import torch.nn as nn |
| import torch |
|
|
| |
| class position_wide_feed_forward(nn.Module): |
| def __init__(self, dimension_for_model, dimension_for_network, dropout = 0.1): |
|
|
| ''' |
| A Constructor for the positional feed forward network |
| dimensin_for_model: the manually decided dimension that's used in the embeddings layer |
| dimension_for_network: the dimension needed to expand the embedded results into |
| dropout: optional dropout to wipe out specific columns and rows of the matrix to improve the model's abilities during training |
| ''' |
|
|
| super().__init__() |
| self.expansion = nn.Linear(dimension_for_model, dimension_for_network) |
| self.apply_dropout = nn.Dropout(dropout) |
| self.activation = nn.ReLU() |
| self.reverse_expansion = nn.Linear(dimension_for_network, dimension_for_model) |
| def forward(self, x): |
|
|
| ''' |
| Applying the process of the positional feed_forward function |
| x: the data which the positional feed forward is applied to |
| ''' |
| parsed = self.apply_dropout(self.activation(self.expansion(x))) |
| return self.reverse_expansion(parsed) |
| |
|
|
| class Residual_layer (nn.Module): |
| def __init__(self, dimension_for_model, dropout = 0.1): |
| ''' |
| A Constructor for the Residual and Normalization Layer |
| dropout: optional dropout to wipe out specific columns and rows of the matrix to improve the model's abilities during training |
| dimension_for_model: The desired dimension from the embeddings layer |
| ''' |
| super().__init__() |
| self.normalize = nn.LayerNorm(dimension_for_model) |
| self.apply_dropout = nn.Dropout(dropout) |
| def forward(self, input_tensor, sublayer_tensor): |
| ''' |
| input_tensor: the collection of tensor sum at the current stage |
| sublayer_tensor: the tensor from the specific sublayer and still needed to be added |
| ''' |
| result = self.apply_dropout(sublayer_tensor)+input_tensor |
| return self.normalize(result) |
| |
|
|
|
|
| if __name__ == '__main__': |
| inp = torch.tensor([[ |
| [1.0, 2.0, 3.0, 4.0], |
| [0.5, 1.5, 2.5, 3.5], |
| [4.0, 3.0, 2.0, 1.0] |
| ]]) |
|
|
| |
| ffn = position_wide_feed_forward(dimension_for_model=4, dimension_for_network=8, dropout=0.0) |
|
|
| |
| out = ffn(inp) |
|
|
| |
| print("Input:", inp) |
| print("Output:", out) |
| print("Output shape:", out.shape) |
| x = torch.tensor([[[1.0, 2.0, 3.0, 4.0], |
| [4.0, 3.0, 2.0, 1.0], |
| [0.5, 1.5, 2.5, 3.5]]]) |
| |
| |
| sub = torch.tensor([[[0.1, 0.1, 0.1, 0.1], |
| [0.2, 0.2, 0.2, 0.2], |
| [0.3, 0.3, 0.3, 0.3]]]) |
| |
| |
| layer = Residual_layer(dimension_for_model=4, dropout=0.0) |
| |
| |
| out = layer(x, sub) |
| |
| |
| print("Input X:\n", x) |
| print("\nSublayer output:\n", sub) |
| print("\nResidual+Norm output:\n", out) |
| |
|
|
|
|
|
|