Transformer Based Models
PositionalEncoding
Bases: Module
Source code in engines/contentFilterEngine/nn_based_algorithms/transformer.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
|
__init__(embed_dim, dropout=0.1, max_len=5000)
Initialize the positional encoding.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
embed_dim
|
int
|
Embedding dimension. |
required |
dropout
|
float
|
Dropout rate. |
0.1
|
max_len
|
int
|
Maximum length of input sequences. |
5000
|
Source code in engines/contentFilterEngine/nn_based_algorithms/transformer.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
|
forward(x)
Apply positional encoding to the input tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor of shape (seq_length, batch_size, embed_dim). |
required |
Returns:
Type | Description |
---|---|
torch.Tensor: Positionally encoded tensor. |
Source code in engines/contentFilterEngine/nn_based_algorithms/transformer.py
71 72 73 74 75 76 77 78 79 80 81 82 |
|
TransformerModel
Bases: Module
Source code in engines/contentFilterEngine/nn_based_algorithms/transformer.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
|
__init__(input_dim, embed_dim, num_heads, hidden_dim, num_layers, dropout=0.1, num_classes=2)
Initialize the Transformer model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_dim
|
int
|
Dimension of the input features. |
required |
embed_dim
|
int
|
Embedding dimension. |
required |
num_heads
|
int
|
Number of attention heads. |
required |
hidden_dim
|
int
|
Dimension of the feedforward network. |
required |
num_layers
|
int
|
Number of Transformer encoder layers. |
required |
dropout
|
float
|
Dropout rate. |
0.1
|
num_classes
|
int
|
Number of output classes. |
2
|
Source code in engines/contentFilterEngine/nn_based_algorithms/transformer.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
forward(src)
Forward pass of the Transformer model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
src
|
Tensor
|
Input tensor of shape (batch_size, seq_length, input_dim). |
required |
Returns:
Type | Description |
---|---|
torch.Tensor: Output logits of shape (batch_size, num_classes). |
Source code in engines/contentFilterEngine/nn_based_algorithms/transformer.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
|