Code Transformer neural network components piece by piece.
ajhalthor / transformer-neural-network Goto Github PK
View Code? Open in Web Editor NEWCode Transformer neural network components piece by piece
License: MIT License
Code Transformer neural network components piece by piece
License: MIT License
Code Transformer neural network components piece by piece.
Hi and thanks for the great series about transformers!
I noticed that you initialize the nn.embeddings layer for the word embeddings with an input size that is equal to the vocabulary size.
As we would like to add the positional encodings with dimensions max_seq_length X 512
on top of the embeddings, the dimensions of the words embeddings should be the same as the ones for the positional embeddings (max_seq_length X 512
).
So the corrected code would look something like:
class SentenceEmbedding(nn.Module):
"For a given sentence, create an embedding"
def __init__(self, max_sequence_length, d_model, language_to_index, START_TOKEN, END_TOKEN, PADDING_TOKEN):
super().__init__()
self.vocab_size = len(language_to_index)
self.max_sequence_length = max_sequence_length
self.embedding = nn.Embedding(self.max_sequence_length, d_model) #CORRECTED LINE....
...
```
Regards,
M
Hello I am using a this repo to convert English to Italian and I keep running into this error
I kept all the code the same except for the vocab dictionaries which are shown below
english_vocabulary = [START_TOKEN, ' ', '!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', '<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\\\', ']', '^', '_', '
',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z',
'{', '|', '}', '~', '_', PADDING_TOKEN, END_TOKEN]
`
italian_vocabulary = [START_TOKEN, ' ', '!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', '<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'Z', 'À', 'È', 'É', 'Ì', 'Í', 'Ò', 'Ó', 'Ù', '[', '\\\\', ']', '^', '_', '
',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'z',
'à', 'è', 'é', 'ì', 'í', 'ò', 'ó', 'ù',
'{', '|', '}', '~', '_', PADDING_TOKEN, END_TOKEN]
`
Besides that, all the code is the same just updated from kn->it and the naming for Kannada -> italian
Epoch 0 Traceback (most recent call last): File "/Users/alessandrobongiorno/Desktop/singularity/research/transformers/gpt-3/train/train.py", line 213, in <module> it_predictions = transformer( File "/Users/alessandrobongiorno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1532, in _wrapped_call_impl return self._call_impl(*args, **kwargs) File "/Users/alessandrobongiorno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1541, in _call_impl return forward_call(*args, **kwargs) File "/Users/alessandrobongiorno/Desktop/singularity/research/transformers/gpt-3/train/../model/model.py", line 301, in forward x = self.encoder(x, encoder_self_attention_mask, start_token=enc_start_token, end_token=enc_end_token) File "/Users/alessandrobongiorno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1532, in _wrapped_call_impl return self._call_impl(*args, **kwargs) File "/Users/alessandrobongiorno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1541, in _call_impl return forward_call(*args, **kwargs) File "/Users/alessandrobongiorno/Desktop/singularity/research/transformers/gpt-3/train/../model/model.py", line 178, in forward x = self.sentence_embedding(x, start_token, end_token) File "/Users/alessandrobongiorno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1532, in _wrapped_call_impl return self._call_impl(*args, **kwargs) File "/Users/alessandrobongiorno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1541, in _call_impl return forward_call(*args, **kwargs) File "/Users/alessandrobongiorno/Desktop/singularity/research/transformers/gpt-3/train/../model/model.py", line 71, in forward x = self.embedding(x) File "/Users/alessandrobongiorno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1532, in _wrapped_call_impl return self._call_impl(*args, **kwargs) File "/Users/alessandrobongiorno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1541, in _call_impl return forward_call(*args, **kwargs) File "/Users/alessandrobongiorno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/torch/nn/modules/sparse.py", line 163, in forward return F.embedding( File "/Users/alessandrobongiorno/.pyenv/versions/3.9.19/lib/python3.9/site-packages/torch/nn/functional.py", line 2264, in embedding return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse) IndexError: index out of range in self (env) alessandrobongiorno@Alessandros-Air train %
I believe the error is coming from this line
self.embedding = nn.Embedding(self.vocab_size, d_model)
If anyone has any ideas I would really appreciate it
train.en and train.kn files are needed.
Please how can I implement this for word to word translation?
When got new values by scale_dot_product operation, it should permute values
like before, to exchange max_sequence_length and num_heads dimensions. And then reshape its size. Such that we can make sure for one row of values
, it comes from many heads.
values = values.permute(0, 2, 1, 3).reshape(batch_size, max_sequence_length, self.num_heads * self.head_dim)
hey i am getting these error where tensor dimension are not in place
when i am converting from english to roman urdu, it is giving error:
IndexError Traceback (most recent call last)
Cell In[21], line 14
12 encoder_self_attention_mask, decoder_self_attention_mask, decoder_cross_attention_mask = create_masks(eng_batch, ur_batch)
13 optim.zero_grad()
---> 14 ur_predictions = transformer(eng_batch,
15 ur_batch,
16 encoder_self_attention_mask.to(device),
17 decoder_self_attention_mask.to(device),
18 decoder_cross_attention_mask.to(device),
19 enc_start_token=False,
20 enc_end_token=False,
21 dec_start_token=True,
22 dec_end_token=True)
23 labels = transformer.decoder.sentence_embedding.batch_tokenize(ur_batch, start_token=False, end_token=True)
24 loss = criterian(
25 ur_predictions.view(-1, ur_vocab_size).to(device),
26 labels.view(-1).to(device)
27 ).to(device)
File c:\Users\AAHIL ALWANI\Desktop\FYP\Web App\Meme_AI\meme\Lib\site-packages\torch\nn\modules\module.py:1511, in Module._wrapped_call_impl(self, *args, **kwargs)
1509 return self._compiled_call_impl(*args, **kwargs) # type: ignore[misc]
1510 else:
-> 1511 return self._call_impl(*args, **kwargs)
File c:\Users\AAHIL ALWANI\Desktop\FYP\Web App\Meme_AI\meme\Lib\site-packages\torch\nn\modules\module.py:1520, in Module._call_impl(self, *args, **kwargs)
1515 # If we don't have any hooks, we want to skip the rest of the logic in
1516 # this function, and just call forward.
1517 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
1518 or _global_backward_pre_hooks or _global_backward_hooks
1519 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1520 return forward_call(*args, **kwargs)
1522 try:
1523 result = None
File ~\Desktop\FYP\Web App\Meme_AI\transformer model\transformer_model.py:302, in Transformer.forward(self, x, y, encoder_self_attention_mask, decoder_self_attention_mask, decoder_cross_attention_mask, enc_start_token, enc_end_token, dec_start_token, dec_end_token)
292 def forward(self,
293 x,
294 y,
(...)
300 dec_start_token=False, # We should make this true
301 dec_end_token=False): # x, y are batch of sentences
--> 302 x = self.encoder(x, encoder_self_attention_mask, start_token=enc_start_token, end_token=enc_end_token)
303 out = self.decoder(x, y, decoder_self_attention_mask, decoder_cross_attention_mask, start_token=dec_start_token, end_token=dec_end_token)
304 out = self.linear(out)
File c:\Users\AAHIL ALWANI\Desktop\FYP\Web App\Meme_AI\meme\Lib\site-packages\torch\nn\modules\module.py:1511, in Module._wrapped_call_impl(self, *args, **kwargs)
1509 return self._compiled_call_impl(*args, **kwargs) # type: ignore[misc]
1510 else:
-> 1511 return self._call_impl(*args, **kwargs)
File c:\Users\AAHIL ALWANI\Desktop\FYP\Web App\Meme_AI\meme\Lib\site-packages\torch\nn\modules\module.py:1520, in Module._call_impl(self, *args, **kwargs)
1515 # If we don't have any hooks, we want to skip the rest of the logic in
1516 # this function, and just call forward.
1517 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
1518 or _global_backward_pre_hooks or _global_backward_hooks
1519 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1520 return forward_call(*args, **kwargs)
1522 try:
1523 result = None
File ~\Desktop\FYP\Web App\Meme_AI\transformer model\transformer_model.py:179, in Encoder.forward(self, x, self_attention_mask, start_token, end_token)
178 def forward(self, x, self_attention_mask, start_token, end_token):
--> 179 x = self.sentence_embedding(x, start_token, end_token)
180 x = self.layers(x, self_attention_mask)
181 return x
File c:\Users\AAHIL ALWANI\Desktop\FYP\Web App\Meme_AI\meme\Lib\site-packages\torch\nn\modules\module.py:1511, in Module._wrapped_call_impl(self, *args, **kwargs)
1509 return self._compiled_call_impl(*args, **kwargs) # type: ignore[misc]
1510 else:
-> 1511 return self._call_impl(*args, **kwargs)
File c:\Users\AAHIL ALWANI\Desktop\FYP\Web App\Meme_AI\meme\Lib\site-packages\torch\nn\modules\module.py:1520, in Module._call_impl(self, *args, **kwargs)
1515 # If we don't have any hooks, we want to skip the rest of the logic in
1516 # this function, and just call forward.
1517 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
1518 or _global_backward_pre_hooks or _global_backward_hooks
1519 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1520 return forward_call(*args, **kwargs)
1522 try:
1523 result = None
File ~\Desktop\FYP\Web App\Meme_AI\transformer model\transformer_model.py:72, in SentenceEmbedding.forward(self, x, start_token, end_token)
70 def forward(self, x, start_token, end_token): # sentence
71 x = self.batch_tokenize(x, start_token, end_token)
---> 72 x = self.embedding(x)
73 pos = self.position_encoder().to(get_device())
74 x = self.dropout(x + pos)
File c:\Users\AAHIL ALWANI\Desktop\FYP\Web App\Meme_AI\meme\Lib\site-packages\torch\nn\modules\module.py:1511, in Module._wrapped_call_impl(self, *args, **kwargs)
1509 return self._compiled_call_impl(*args, **kwargs) # type: ignore[misc]
1510 else:
-> 1511 return self._call_impl(*args, **kwargs)
File c:\Users\AAHIL ALWANI\Desktop\FYP\Web App\Meme_AI\meme\Lib\site-packages\torch\nn\modules\module.py:1520, in Module._call_impl(self, *args, **kwargs)
1515 # If we don't have any hooks, we want to skip the rest of the logic in
1516 # this function, and just call forward.
1517 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
1518 or _global_backward_pre_hooks or _global_backward_hooks
1519 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1520 return forward_call(*args, **kwargs)
1522 try:
1523 result = None
File c:\Users\AAHIL ALWANI\Desktop\FYP\Web App\Meme_AI\meme\Lib\site-packages\torch\nn\modules\sparse.py:163, in Embedding.forward(self, input)
162 def forward(self, input: Tensor) -> Tensor:
--> 163 return F.embedding(
164 input, self.weight, self.padding_idx, self.max_norm,
165 self.norm_type, self.scale_grad_by_freq, self.sparse)
File c:\Users\AAHIL ALWANI\Desktop\FYP\Web App\Meme_AI\meme\Lib\site-packages\torch\nn\functional.py:2237, in embedding(input, weight, padding_idx, max_norm, norm_type, scale_grad_by_freq, sparse)
2231 # Note [embedding_renorm set_grad_enabled]
2232 # XXX: equivalent to
2233 # with torch.no_grad():
2234 # torch.embedding_renorm_
2235 # remove once script supports set_grad_enabled
2236 no_grad_embedding_renorm(weight, input, max_norm, norm_type)
-> 2237 return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
IndexError: index out of range in self
`
Hi Ajay,
In "Multi Head Attention " section, after scaled_dot_product, it needed to reshape, but I think before the reshape, it need to permute the head dimension and the sequence dimension.
Please let me know if I'm wrong.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.