Embeddings
Embedding
¶
Bases:
Transformer embeddings layer.
This implementation uses a randomly initialized embedding lookup table with dimension [vocab_size, d_model].
There's the possibility of loading pretrained embeddings from GloVe.
This choice has been made to achieve acceptable performances with low resources training and limited time training.
Note
GloVe embeddings available for English only.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vocab_size
|
|
Number of tokens in vocabulary. |
required |
d_model
|
|
Model dimension. Defaults to None. |
None
|
from_pretrained
|
|
Load embeddings from pretrained. Defaults to False. |
False
|
pretrained_emb_type
|
|
Type of pretrained embeddings. Defaults to None. |
None
|
pretrained_emb_path
|
|
Path of pretrained embeddings. Defaults to None. |
None
|
Raises:
| Type | Description |
|---|---|
|
Raised when the provided embedding dimension does not match the expected size. |
|
Raised when the embedding type or file path is invalid or not found. |
|
Raised when a tokenizer is required but has not been supplied. |
|
Raised when an operation requiring a built vocabulary is attempted before the vocabulary is constructed. |
Source code in src/tfs_mt/embeddings.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |
forward(token_ids)
¶
Get token embeddings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token_ids
|
|
Input batch of token_ids. Where B is the batch size and S is the sequence length. |
required |
Returns:
| Type | Description |
|---|---|
|
Float[torch.Tensor, "B S D"]: Output batch of token embeddings. Where D is d_model. |
Source code in src/tfs_mt/embeddings.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |
load_pretrained(embeddings_path, emb_type='GloVe', **kwargs)
¶
Loads pretrained GloVe embedding into the embedding lookup table.
Source code in src/tfs_mt/embeddings.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | |
SinusoidalPositionalEncoding
¶
Bases:
Sinusoidal Positional Encoding implementation from the original paper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d_model
|
|
Model dimension. |
required |
dropout_prob
|
|
Dropout probability. Defaults to 0.1. |
0.1
|
max_sequence_length
|
|
Max sequence length. Defaults to 128. |
128
|
Source code in src/tfs_mt/embeddings.py
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | |
forward(token_embeddings)
¶
Get token embeddings with positional information.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token_embeddings
|
|
Input batch of token embeddings. |
required |
Raises:
| Type | Description |
|---|---|
|
Raised when the input embedding dimension is invalid. |
Returns:
| Type | Description |
|---|---|
|
Float[torch.Tensor, "B S D"]: Token embeddings with added positional information. |
Source code in src/tfs_mt/embeddings.py
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | |