Yeah, and you see how these technologies works. It's insane, that in the end it looks easy that you can do something that companies of millions and billions of dollars do. In a small way but the same idea at the end.
The full code is available on GitHub: github.com/hkproj/pytorch-transformer It also includes a Colab Notebook so you can train the model directly on Colab. Of course nobody reinvents the wheel, so I have watched many resources about the transformer to learn how to code it. All of the code is written by me from zero except for the code to visualize the attention, which I have taken from the Harvard NLP group article about the Transformer. I highly recommend all of you to do the same: watch my video and try to code your own version of the Transformer... that's the best way to learn it. Another suggestion I can give is to download my git repo, run it on your computer while debugging the training and inference line by line, while trying to guess the tensor size at each step. This will make sure you understand all the operations. Plus, if some operation was not clear to you, you can just watch the variables in real time to understand the shapes involved. Have a wonderful day!
I have browsed UA-cam for the perfect set of videos on transformer, but your set of videos (the video explanation you did on the transformer architecture) and this one is by far the best !! Take a bow brother, you have really contributed to the viewers in amount you cant even imagine. Really appreciate this !!!
Hi Umar. I am a first year student at MIT who wants to do AI startups. Your explanation and comments during coding were really helpful. After spending about 10 hours on the video, I walk away with great learnings and great inspiration. Thank you so much, you are an amazing teacher!
I'm not sure if it is because I have study this content 1000000 times or not, but is the first time that I understood the code, and feel confident about it. Thanks!
Thank God, it's not one of those 'ML in 5 lines of Python code' or 'learn AI in 5 minutes'. Thank you. I can not imagine how much time you must have spent on making this tutorial. thank you so much. I have watched it three times already and wrote the code while watching the second time (with a lot of typos :D).
Hi Umar. Absolutely amazing 🤯. Your clear breakdown and explanation of the concepts and code is just next level. Until I watched your video I had a very tentative handle on transformers. After watching I have a much better fundamental grasp of EVERY component. I can't say thank you enough. Please keep doing what you are doing.
Keep doing what you are doing. I really appreciate you taking out so much time to spread such knowledge for free. Been studying transformers for a long time but never have I understood it so well. The theoretical explanation in the other video combined with this practical implementation, just splendid. Will be going through your other tutorials as well. I know how much time taking it is to produce such high level content and all I can really say is that I really am grateful for what you are doing and hope that you continue doing it. Wish you a great day!
I really appreciate your efforts. The explanations are very clear. This is a great service for people that wish to learn the future of AI! All the best from Spain!
Loving this video (only 13 minutes in), really like you using type hints, commenting, descriptive variable names, etc. Way better coding practices than most of the ML code I've looked at. At 13:00, for the 2nd arg of the array indexing, you could just do ":" and it would be identical.
Thank you for this comment! I'm coding along with this video and I wasn't sure if my understanding was correct. I'm glad someone else was thinking the same thing. Just to be clear, I am VERY THANKFUL for this video and am in no way complaining. I just wanted to make sure I understand because I want to fully internalize this information.
Thanks Bro. With your explanation, I am able to build the transformer model for my application. You explained so awesome. Please do what you are doing.
Thank you so much for taking the time to code and explain the transformer model in such detail. You are amazing and please do a series on how transformers can be used for time series anomaly detection and forecasting!
What a WONDERFUL example of transformer! I am Chinese and I am doing my PhD program in Korea. My research is also about AI. This video helps me a lot. Thank you! BTW, your Chinese is very good!😁😁
Thanks Umar for this comprehensive tutorial, after watching many videos I would say, this is AWESOME! It would be really nice if you can provide us with more tutorials on Transformers especially training them for longer sequences. :)
Dear Umar - thank you so much for this amazing and very clear explanation. It has deeply helped me and many others in understanding the theoretical and practical implementation of transformers! Take a bow!
ATTENTION: You don't need torchtext anymore, it's deprecated. Just remove the line "import torchtext.datasets as datasets" and install "pip install datasets"
It is really amazing video. I tried understanding the code of it from various other youtube channel; but was always getting confused. Thanks a lot :) . Can you make a series on BERT & GPT aswell; where you build these models and train on custom data?
Hi Phanindra! I'll definetely continue making more videos. It takes a lot of time and patience to make just one video, not considering the preparation time to study the model, write the code and test it. Please share the channel and subscribe, that's the biggest motivation to continue providing high quality content to you all.
Hi, I just happen to see your video. It's really amazing, your channel is so good with valuable information. Hope, you keep this up because I really love your contents.
Just want to say thank you!! This is easily one of my favorite video on UA-cam! I have watched a few videos on transformers but none explained it as clear as you, at first I was scared by the length of the video but you managed to have my attention for the full 3 hours! Following your instructions I am now able to train my very first transformer! Btw, I am using the tokenizer the way you are but looking at the tokenizer file it looks like my tokenizer didn’t split the sentences into words and it is using the whole sentence as token. Do you have any idea why? I am using mac if that matters.
Hi! Thanks for your kind words! Make sure your PreTokenizer is the "Whitespace" one and that the Tokenizer is the "WordLevel" tokenizer. As a last resort, you can clone the repository from my GitHub and compare my code with yours. Have a wonderful rest of the day!
I have PreTokenizer set as whitespace and using WordLevel tokenizer and trainer but it will still encode the sentence as a whole. I did a direct swap to use BPE tokenizer and that is correctly encoding the sentences, maybe there is bug in WordLevel tokenizer for macOS. Another question that I have is what determines the max context size for LLMs? Is it the d_model size?@@umarjamilai
Dear Umar, thank you so so much for the video! I don't have much experience in deep learning, but your explanations are so clear and detailed I understood almost everything 😄. It wil be a great help for me at my work. Wish you all the best! ❤
Hey there! I enjoyed watching that video, you did a wonderful job explaining everything, and I found it super easy to follow along. Overall, it was a really great experience!
This is excellent! Thank you for putting this together. I do have one point of confusion with how the final multihead attention concatenation takes place. I believe the concatenation takes place on line 110 where V' = (V1, V2,.. Vh) (sequenc_length, h*dk) This is intended to be multiplied by matrix W0 (h*dk, dmodel) to give something of shape (sequenc_length, dmodel ) as is required. However, here you implement a linear layer operation which takes the concat V' (sequence_length, d_model) and is fed into a linear layer constructed so that we do the following: W*V'+b where the dimension of W and b are chose to satisfy the output dimension. This is different from multiplying directly with a predefined trainable matrix of size W0. Now, I can see how these are nearly the same thing and in practice it may not matter, but it would be helpful to point out these tricks of the trade so folks like myself don't get bogged down with these subtleties. Thanks
This is the best one; we need to train a model; let the model observe your actions; and learn from you. With a physical structure, Tesla robot, could take classes based on your training.
This is an excellent video, your explanation is so clear and the live coding helps understanding! Can you give us tips to debug such an huge model? Because it is really hard to make sure the model works well. My tips on debugging is to print out the shape of the tensor in each step, but this only make sure the shape is correct, there may be some logical error I may miss out. Thank you!
Hi! I'd love to give a golden rule for debugging models, but unfortunately, it depends highly on the architecture/loss/data itself. One thing that you can do is, before training the model on a big dataset, it is recommended to train it on a very small dataset to make sure everything is working and the model should overfit on the small dataset. For example, if instead of training on many books, you train a LLM on a single book, hopefully it should be able to write sentences from that book, given a prompt. The second most important thing is to validate the model as the training is proceeding to verify that the quality is improving over time. Last but not least, use metrics to decide if the model is going in the right direction and make experiments on hyper parameters to verify assumptions, do not just make assumptions without validating them. When you have a model with billions of parameters, it is difficult to predict patterns, so every assumption must be verified experimentally. Have a nice day!
You are one of the coolest dude in this area. It'd be helpful if you provide a roadmap to reach your expertise. I'd really love to learn from you but i can't understand. Roadmap will help so many of your subscribers.
perfect video!! Thank you so much. I always wonder the detail code and its explanation and now I almost understand all of it. thanks:) you are the best for me!
Note: this implementation follows 'pre-LN' version of transformer -- which is slightly different from the original transformer in residual connection part. In the original block diagram, the layer normalization(LN) should be applied AFTER multi-head attention / feed-forward network. However, this code applies the LN BEFORE multi-head attention and feed-forward network. You can see the difference by comparing the ResidualConnection forward() code and section 3.2 of original "Attention Is All You Need" paper. This is a valid architecture too (proposed by the other papers), but it is not exactly as proposed in the original one.
Its really awesome video with clear explanations. And flow of code is very easy to understand. One question, how to implement this transformer architecture for Question-Answer based model ? (Q/A on very specific topic lets say a manual of instrument..) Thank you ! so much for this video !!!
sir, your explanation is just beyond awesome!!! Thank you so much for creating such content. Sir I didn't get the residual connections part. As I am from India, I was working on Indic Languages, so i had to make more code but that's just okay. I just want if you could please help in understanding beam search code, the one which you also gave in the GitHub File. Also, if you could give the code for evaluating the BLEU Score. I'll be really grateful to you. And again, thank you so much for such a comprehensive content. We'd love to see your more videos especially in Generative AI! P.S. : I didn't understand how you wrote it, what I've understood is that we have to take the input of the previous layer and then add with o/p of the same layer and then apply layer norm on that. Basically Add and then LayerNorm. Please help me correct mysefl!
Hi, thanks for the video, it was really helpful, I do have 1 question though. at 57:01, shouldn't the Q, K, V be encoder_output, encoder_output, x; instead of x, encoder_output, encoder_output? if we're calculating Q@K.T, I think of that somewhat similar to "capturing the essence of the input sentence", which would be the encoder output for both Q & K, and the V would build on top of the input sentence's "essence". Can you please elaborate why the order of inputs are what they are? Even in the attention paper, the first 2 inputs(Q and K) are coming from the encoder output, and the Last input V comes from the decoder's self attention output.
At 29:14, the part on multihead attention, we feed each Q,V, K multiply by Wq, Wv, Wk then split them into n heads then dot product and concat them again. But should we not split them first, then apply Wq_h where Wq_h is the weight matrix for the hth query matrix, same for V and K? Because it seems like we just split them, apply attention, then concat?
Thank you for your straight to the point no bs videos. Good code alongs and commentary. But it looks like positional encoding aren't correct (as per paper). There is power there in: denominator = np.power(10000, 2*i/d). I get it you decided to use exp+log pair for stability, but no mentions of the power gone. And extra layer norm after encoder. As in we "norm + add" (like you defined in the video, instead add & norm as per paper, but you said "lets stick with it", I understand) but than we norm again, after the last encoder block. Like so: layer norm + add (in last encoder block (inside residual connection ) + layer norm again (inside Encoder class) (I could be wrong but it looks like that)
Just finished watching. Thanks so much for the detailed video. I plan to spend this weekend on coding this model. How long did it take to train on your hardware?
Hi Mohsin! Good job! It took around 3 hours to train 30 epochs on my computer. You can train even for 20 epochs to see good results. Have a wonderful day!
@@NaofumiShinomiya Training time depends on the architecture of the network, on your hardware and the amount of data you use, plus other factors like learning rate, learning scheduler, optimizer, etc. So many conditions to factor in.
in the 13:13/2:59:23, when we build the PositionalEncoding function, this line x = x + (self.pe[:,:x.shape[1],:]).requires_grad_(False), the x.shape[1] looks like not be used in the transformer model, because when we build the dataset.py function, we pad all the sentences into the same length, and then we load the (batch, seq_len, input_embedding_dim) into the PositionalEncoding function, where all x.shape[1] in the batch is the seq_len, instead of varying by their original sentence length.
@umarjamilai. I have the same question. x.shape[1] in this case will alway equal seq_len. So every time this will just return the entire pe tensor. Wondering if this is unique to this use case example??
for determining the max len of tgt sentence, I believe you should point to tokenizer_tgt rather than tokenizer_src. tgt_ids = tokenizer_tgt.encode(item['transaltion'][config['ang_tgt']]).ids
Thank you umar jamil for this wonderfull content, to be honest i find it so hard to keep undertanding each part and what happens in each line of code for a beginner in pytorch. I wonder what i need to know before starting one of your videos. I think i need to read the paper multiple times till understand it?
Thanks so much such a great video. Really liked it a lot. I have a small query. For ResidualConnection, in the paper the equation is given by "LayerNorm(x + Sublayer(x))". In the code, we have: x + self.dropout(sublayer(self.norm(x))). Why it is not self.norm(self.dropout((x + sublayer(x))) ?
Hello Umar, really impressive work on Transformer. I have followed your step on this experiment. One small thing I am not sure is when you compute the loss you use the nn.CrossEntropyLoss() method, this method have already apply the softmax itself. As their document said:"The input is expected to contain the unnormalized logits for each class (which do not need to be positive or sum to 1, in general)". But in your project method in the built Transformer model, it has applied softmax. I wonder if we should only output the logits without this softmax to fit the nn.CrossEntropyLoss() method? Thank you anyway.
Great video! I'm wondering, is there any reason to save the positional encoding vector? I don't see why you would need to save it since it seems to always be the same value considering the init parameters don't change.
Thank you for such a great video. However, it seems that the softmax layer after the decoder is not included in your code. I tried implementing it myself, but after adding the final softmax, the loss function becomes extremely difficult to converge and decreases very slowly. How can this be resolved?
I believe I’m going to have to code and understand all the code to be able to replicate this for other use case! At the moment I am not able to follow the code as I am new to python! Im going to stick to this and understand this no matter how long it takes! I really did want to get into the visual transformer video, but I believe I should master this first!
This is definitely one of the best video to learn about Transformers. Is it normal for me(as a beginner in ai) to see this 4 or 5 times struggling for weeks to understand it fully? 😅 For example, I had to go study huggingface tutorial to understand the tokenizer part and I wonder if I should learn it 100% or just enough to implement it.
Hi! At the beginning it's normal to learn things "just to make things work". Most people who start coding, use libraries without knowing how they work internally. As time goes, you will naturally find yourself curious on how things work on a "lower level" and will explore and study them more deeply. The goal in life is not to understand everything today, but rather, to understand a little more compared to yesterday. Have a nice day!
For the Encoder/Decode code - why is the last step in these a normalization layer? We wrote the Residual Connection layers with a pre-normalization step (instead of post-normalization as was in the original Transorfer paper).
Hello Umar, Thank you very much for this video. This is one of the best. I took inspiration and tried to implement the paper "Attention is all you need" for English French translation. I used opus-book en-fr dataset to train the transformer. I ran for 40 epochs and my batch size was 64. In the end the loss converges to around 3.6. Between epoch 30 and epoch 40, for 10 epochs, for each mini batch the step loss was around 3.6. The model's performance after 40 epoch on test set was not good. I would be thankful to you if you share some details like, for how many epochs did you train and what was your step loss towards the end of your training. Probably you took en-it dataset, still it would be helpful for me to get an idea. Thank You very much.
personally, I find that seeing someone actually code something from scratch is the best way to get a basic understanding
indeed
indeed
i don't need to see someone typing... but you might also enjoy watching the gras grow or paint dry
Yeah, and you see how these technologies works.
It's insane, that in the end it looks easy that you can do something that companies of millions and billions of dollars do. In a small way but the same idea at the end.
Yeah kinda ironic how that works. The simplest stuff required the most complex explanations
The full code is available on GitHub: github.com/hkproj/pytorch-transformer
It also includes a Colab Notebook so you can train the model directly on Colab.
Of course nobody reinvents the wheel, so I have watched many resources about the transformer to learn how to code it. All of the code is written by me from zero except for the code to visualize the attention, which I have taken from the Harvard NLP group article about the Transformer.
I highly recommend all of you to do the same: watch my video and try to code your own version of the Transformer... that's the best way to learn it.
Another suggestion I can give is to download my git repo, run it on your computer while debugging the training and inference line by line, while trying to guess the tensor size at each step. This will make sure you understand all the operations. Plus, if some operation was not clear to you, you can just watch the variables in real time to understand the shapes involved.
Have a wonderful day!
The best video ever
Can you provide with the pretrained models?
🎉is this Bert architecture?
@@wilfredomartel7781 Its complete encoder- decoder based model, bert is the encoder part of this encoder-decoder model
I have browsed UA-cam for the perfect set of videos on transformer, but your set of videos (the video explanation you did on the transformer architecture) and this one is by far the best !! Take a bow brother, you have really contributed to the viewers in amount you cant even imagine. Really appreciate this !!!
这是我见过最详细的从零创建Transformer模型的视频,从代码实现到数据处理,再到可视化,up主真是嚼碎磨细了讲,感谢!
Nn entendi nada! Mas botei meu like.
@@decarteaoO cara da China e muito engracado con o video
Hi Umar. I am a first year student at MIT who wants to do AI startups. Your explanation and comments during coding were really helpful. After spending about 10 hours on the video, I walk away with great learnings and great inspiration. Thank you so much, you are an amazing teacher!
Best of luck with your studies and thank you for your support!
I am a 3rd semester student at IIT Roorkee. I am also interested in AI startups.
Greeting from China! I am PhD student focused on AI study. Your video really helped me a lot. Thank you so much and hope you enjoy your life in China.
谢谢你!我们在领英联系吧
I am also a Ph.D. student. This video is valuable. Many thanks!
I'm not sure if it is because I have study this content 1000000 times or not, but is the first time that I understood the code, and feel confident about it. Thanks!
Thank God, it's not one of those 'ML in 5 lines of Python code' or 'learn AI in 5 minutes'. Thank you. I can not imagine how much time you must have spent on making this tutorial. thank you so much. I have watched it three times already and wrote the code while watching the second time (with a lot of typos :D).
Hi Umar. Absolutely amazing 🤯. Your clear breakdown and explanation of the concepts and code is just next level. Until I watched your video I had a very tentative handle on transformers. After watching I have a much better fundamental grasp of EVERY component. I can't say thank you enough. Please keep doing what you are doing.
One of the best tutorial to understand and implement the Transformer model...Thank you for making such a wonderful video
Keep doing what you are doing. I really appreciate you taking out so much time to spread such knowledge for free. Been studying transformers for a long time but never have I understood it so well. The theoretical explanation in the other video combined with this practical implementation, just splendid. Will be going through your other tutorials as well. I know how much time taking it is to produce such high level content and all I can really say is that I really am grateful for what you are doing and hope that you continue doing it. Wish you a great day!
Thank you for your kind words. I wish you a wonderful day and success for your journey in deep learning!
Dear Umar, your video is full of knowledge; thanks for sharing.
I really appreciate your efforts. The explanations are very clear. This is a great service for people that wish to learn the future of AI! All the best from Spain!
Loving this video (only 13 minutes in), really like you using type hints, commenting, descriptive variable names, etc. Way better coding practices than most of the ML code I've looked at.
At 13:00, for the 2nd arg of the array indexing, you could just do ":" and it would be identical.
Thank you for this comment! I'm coding along with this video and I wasn't sure if my understanding was correct. I'm glad someone else was thinking the same thing. Just to be clear, I am VERY THANKFUL for this video and am in no way complaining. I just wanted to make sure I understand because I want to fully internalize this information.
老哥你救了我啊, 我是中科大的一名研究生,看你的视频,不仅学习了深度学习,还练习了我的英语听力 😁
不客气! 我最近会发新视频, stay tuned!
Thank you Umar for our extraordinary excellent work! Best transformer tutorial ever I have seen!
WOW WOW WOW, though it was a bit tough for me to understand it, I was able to understand around 80 % of the code, beautiful. Thank you soo much
This feels really fantastic when looking someone write a program from bottom up
Just to repeat what everyone else is saying here - many thanks for an amazing explanation! Looking forward to more of your videos.
best video I have ever seen on whole youtube eon transformer model. Thank you so much sir!
Thanks Bro. With your explanation, I am able to build the transformer model for my application. You explained so awesome. Please do what you are doing.
Thank you so much for taking the time to code and explain the transformer model in such detail. You are amazing and please do a series on how transformers can be used for time series anomaly detection and forecasting!
What a WONDERFUL example of transformer! I am Chinese and I am doing my PhD program in Korea. My research is also about AI. This video helps me a lot. Thank you!
BTW, your Chinese is very good!😁😁
Thanks Umar for this comprehensive tutorial, after watching many videos I would say, this is AWESOME! It would be really nice if you can provide us with more tutorials on Transformers especially training them for longer sequences. :)
Hi mohamednabil374, stay tuned for my next video on the LongNet, a new transformer architecture that can scale up to 1 billion tokens.
Thanks a ton for making this video and all your other videos. Incredibly useful.
Thanks for your support!
I learnt a lot from following the steps out of this video and create a transformer myself step by step!! Thank you!!
Thanks a lot for such a detailed video. Your videos on transformer are best.
This video is incredible, never understood it like this before. I will watch your next videos for sure, thank you so much!
Dear Umar - thank you so much for this amazing and very clear explanation. It has deeply helped me and many others in understanding the theoretical and practical implementation of transformers! Take a bow!
ATTENTION: You don't need torchtext anymore, it's deprecated. Just remove the line "import torchtext.datasets as datasets" and install "pip install datasets"
thank you so much
Umar, thank you for the amazing example and clear explanation of all your steps and actions.
Thank you for watching my video and your kind words! Subscribe for more videos coming soon!
@@umarjamilai , mission completed 😎.
Already subscribed.
All the best, Umar
It is really amazing video. I tried understanding the code of it from various other youtube channel; but was always getting confused. Thanks a lot :) . Can you make a series on BERT & GPT aswell; where you build these models and train on custom data?
Hi Phanindra! I'll definetely continue making more videos. It takes a lot of time and patience to make just one video, not considering the preparation time to study the model, write the code and test it. Please share the channel and subscribe, that's the biggest motivation to continue providing high quality content to you all.
A coding example for BERT would be great!@@umarjamilai
This is such a great work, I don't really know how to thank you but this is an amazing explanation of an advanced topic such as transformer.
Thanks for making it so easy to understand. I definitely learn a lot and gain much more confidence from this!
Best video I came across for transformer from scratch.
Hi, I just happen to see your video. It's really amazing, your channel is so good with valuable information. Hope, you keep this up because I really love your contents.
Hi Umar thank you for all the work you are doing, please consider making a video like this on vision transformers
Just want to say thank you!! This is easily one of my favorite video on UA-cam! I have watched a few videos on transformers but none explained it as clear as you, at first I was scared by the length of the video but you managed to have my attention for the full 3 hours! Following your instructions I am now able to train my very first transformer!
Btw, I am using the tokenizer the way you are but looking at the tokenizer file it looks like my tokenizer didn’t split the sentences into words and it is using the whole sentence as token. Do you have any idea why? I am using mac if that matters.
Hi! Thanks for your kind words! Make sure your PreTokenizer is the "Whitespace" one and that the Tokenizer is the "WordLevel" tokenizer. As a last resort, you can clone the repository from my GitHub and compare my code with yours. Have a wonderful rest of the day!
I have PreTokenizer set as whitespace and using WordLevel tokenizer and trainer but it will still encode the sentence as a whole. I did a direct swap to use BPE tokenizer and that is correctly encoding the sentences, maybe there is bug in WordLevel tokenizer for macOS.
Another question that I have is what determines the max context size for LLMs? Is it the d_model size?@@umarjamilai
Dear Umar, thank you so so much for the video! I don't have much experience in deep learning, but your explanations are so clear and detailed I understood almost everything 😄. It wil be a great help for me at my work. Wish you all the best! ❤
Thank you for your kind words, @angelinakoval8360!
Really great explanation to understand Transformer, many thanks to you.
Big thankyou for the video, makes transformer so easy to learn(also the explanation video)👍👍
Hey there! I enjoyed watching that video, you did a wonderful job explaining everything, and I found it super easy to follow along. Overall, it was a really great experience!
it was very useful to watch. Question: What books or learning sources you would suggest to learn pytorch deeply. Thanks
Im enjoying clear explanation of The Transformer Coding !
This is excellent! Thank you for putting this together. I do have one point of confusion with how the final multihead attention concatenation takes place. I believe the concatenation takes place on line 110 where V' = (V1, V2,.. Vh) (sequenc_length, h*dk) This is intended to be multiplied by matrix W0 (h*dk, dmodel) to give something of shape (sequenc_length, dmodel ) as is required. However, here you implement a linear layer operation which takes the concat V' (sequence_length, d_model) and is fed into a linear layer constructed so that we do the following: W*V'+b where the dimension of W and b are chose to satisfy the output dimension. This is different from multiplying directly with a predefined trainable matrix of size W0. Now, I can see how these are nearly the same thing and in practice it may not matter, but it would be helpful to point out these tricks of the trade so folks like myself don't get bogged down with these subtleties. Thanks
Wow super usefull! Coding really helps me understand the process better than visuals.
This is the best one; we need to train a model; let the model observe your actions; and learn from you. With a physical structure, Tesla robot, could take classes based on your training.
Thanks for your detailed tutorial. Learned a lot!
This is an excellent video, your explanation is so clear and the live coding helps understanding!
Can you give us tips to debug such an huge model? Because it is really hard to make sure the model works well.
My tips on debugging is to print out the shape of the tensor in each step, but this only make sure the shape is correct, there may be some logical error I may miss out. Thank you!
Hi! I'd love to give a golden rule for debugging models, but unfortunately, it depends highly on the architecture/loss/data itself.
One thing that you can do is, before training the model on a big dataset, it is recommended to train it on a very small dataset to make sure everything is working and the model should overfit on the small dataset. For example, if instead of training on many books, you train a LLM on a single book, hopefully it should be able to write sentences from that book, given a prompt.
The second most important thing is to validate the model as the training is proceeding to verify that the quality is improving over time.
Last but not least, use metrics to decide if the model is going in the right direction and make experiments on hyper parameters to verify assumptions, do not just make assumptions without validating them. When you have a model with billions of parameters, it is difficult to predict patterns, so every assumption must be verified experimentally.
Have a nice day!
You are one of the coolest dude in this area. It'd be helpful if you provide a roadmap to reach your expertise. I'd really love to learn from you but i can't understand. Roadmap will help so many of your subscribers.
I appreciate you for this explanation. Great video!
You are a great professional, thanks a ton for this
I can't possibly thank you enough for this incredibly informative video
Great video, you are insanely talented btw.
Great Explanation. Thanks👍
perfect video!! Thank you so much. I always wonder the detail code and its explanation and now I almost understand all of it. thanks:) you are the best for me!
You're welcome!
OMG. And you also note Matrix shapes in comments! Beautiful. I actually know the shapes without having to trace some variable backwards.
Note: this implementation follows 'pre-LN' version of transformer -- which is slightly different from the original transformer in residual connection part. In the original block diagram, the layer normalization(LN) should be applied AFTER multi-head attention / feed-forward network. However, this code applies the LN BEFORE multi-head attention and feed-forward network. You can see the difference by comparing the ResidualConnection forward() code and section 3.2 of original "Attention Is All You Need" paper. This is a valid architecture too (proposed by the other papers), but it is not exactly as proposed in the original one.
Amazingly useful video. Thank you.
This was really good. I understood multihead attention better with the code explanation.
At 22:39, it describes the essentials of self-attentions computation in very clear and easy to understand way.
Its really awesome video with clear explanations. And flow of code is very easy to understand. One question, how to implement this transformer architecture for Question-Answer based model ? (Q/A on very specific topic lets say a manual of instrument..)
Thank you ! so much for this video !!!
sir, your explanation is just beyond awesome!!! Thank you so much for creating such content. Sir I didn't get the residual connections part. As I am from India, I was working on Indic Languages, so i had to make more code but that's just okay. I just want if you could please help in understanding beam search code, the one which you also gave in the GitHub File. Also, if you could give the code for evaluating the BLEU Score. I'll be really grateful to you.
And again, thank you so much for such a comprehensive content. We'd love to see your more videos especially in Generative AI!
P.S. : I didn't understand how you wrote it, what I've understood is that we have to take the input of the previous layer and then add with o/p of the same layer and then apply layer norm on that. Basically Add and then LayerNorm. Please help me correct mysefl!
Hi, thanks for the video, it was really helpful, I do have 1 question though.
at 57:01, shouldn't the Q, K, V be encoder_output, encoder_output, x; instead of x, encoder_output, encoder_output?
if we're calculating Q@K.T, I think of that somewhat similar to "capturing the essence of the input sentence", which would be the encoder output for both Q & K, and the V would build on top of the input sentence's "essence". Can you please elaborate why the order of inputs are what they are? Even in the attention paper, the first 2 inputs(Q and K) are coming from the encoder output, and the Last input V comes from the decoder's self attention output.
Dottore...sei un grande!
At 29:14, the part on multihead attention, we feed each Q,V, K multiply by Wq, Wv, Wk then split them into n heads then dot product and concat them again. But should we not split them first, then apply Wq_h where Wq_h is the weight matrix for the hth query matrix, same for V and K? Because it seems like we just split them, apply attention, then concat?
Excellent lecture. What lib or method do you recommend to parallelise your code using more than 1 GPU?
what to say.. just WOW! thank you so much !!
Thank you for your straight to the point no bs videos. Good code alongs and commentary.
But it looks like positional encoding aren't correct (as per paper). There is power there in: denominator = np.power(10000, 2*i/d). I get it you decided to use exp+log pair for stability, but no mentions of the power gone.
And extra layer norm after encoder. As in we "norm + add" (like you defined in the video, instead add & norm as per paper, but you said "lets stick with it", I understand) but than we norm again, after the last encoder block. Like so:
layer norm + add (in last encoder block (inside residual connection ) + layer norm again (inside Encoder class)
(I could be wrong but it looks like that)
Just finished watching. Thanks so much for the detailed video. I plan to spend this weekend on coding this model. How long did it take to train on your hardware?
Hi Mohsin! Good job! It took around 3 hours to train 30 epochs on my computer. You can train even for 20 epochs to see good results.
Have a wonderful day!
@@umarjamilai what is your hardware? Just started studying deep learning few days ago and i didnt know transformers could take this long to train
@@NaofumiShinomiya Training time depends on the architecture of the network, on your hardware and the amount of data you use, plus other factors like learning rate, learning scheduler, optimizer, etc. So many conditions to factor in.
in the 13:13/2:59:23, when we build the PositionalEncoding function,
this line x = x + (self.pe[:,:x.shape[1],:]).requires_grad_(False), the x.shape[1] looks like not be used in the transformer model, because when we build the dataset.py function, we pad all the sentences into the same length, and then we load the (batch, seq_len, input_embedding_dim) into the PositionalEncoding function, where all x.shape[1] in the batch is the seq_len, instead of varying by their original sentence length.
@umarjamilai. I have the same question. x.shape[1] in this case will alway equal seq_len. So every time this will just return the entire pe tensor. Wondering if this is unique to this use case example??
Thank you admin. Your video is great. It helps me understand. Thank you very much.
I love your videos. Thank you for sharing your knowledge and i cant wait to learn more.
for determining the max len of tgt sentence, I believe you should point to tokenizer_tgt rather than tokenizer_src. tgt_ids = tokenizer_tgt.encode(item['transaltion'][config['ang_tgt']]).ids
Thanks for a great video.
Sincere congratulations for this fine and very useful tutorial ! Much appreciated 👏🏻
Very good video. Tysm for making this, you are making a difference
Great explanation! Thanks very much
Thank you umar jamil for this wonderfull content, to be honest i find it so hard to keep undertanding each part and what happens in each line of code for a beginner in pytorch.
I wonder what i need to know before starting one of your videos.
I think i need to read the paper multiple times till understand it?
Your video is truly amazing, thanks a lot for this. I want to train this model on Summarization Task so what changes I need to do?
Thanks so much such a great video. Really liked it a lot. I have a small query. For ResidualConnection, in the paper the equation is given by "LayerNorm(x + Sublayer(x))". In the code, we have: x + self.dropout(sublayer(self.norm(x))). Why it is not self.norm(self.dropout((x + sublayer(x))) ?
Hello Umar, really impressive work on Transformer. I have followed your step on this experiment. One small thing I am not sure is when you compute the loss you use the nn.CrossEntropyLoss() method, this method have already apply the softmax itself. As their document said:"The input is expected to contain the unnormalized logits for each class (which do not need to be positive or sum to 1, in general)". But in your project method in the built Transformer model, it has applied softmax. I wonder if we should only output the logits without this softmax to fit the nn.CrossEntropyLoss() method? Thank you anyway.
Amazing explanation
Thank you for this
Thank you mate. You are a godsend!
This video is great! But can you explain how you convert the formula of positional embeddings into log form?
Great video! I'm wondering, is there any reason to save the positional encoding vector? I don't see why you would need to save it since it seems to always be the same value considering the init parameters don't change.
seriously awesome
the contents are crazy !!!!
Why at 51:08 are you applying an extra normalization at the end of the whole encoder pass?
The tutorial has been amazing so far ;)
I have the same question ...
one of the best videos thanks a lot for the video.
Thank you for such a great video. However, it seems that the softmax layer after the decoder is not included in your code. I tried implementing it myself, but after adding the final softmax, the loss function becomes extremely difficult to converge and decreases very slowly. How can this be resolved?
Great video! Where the Residual connection is calculated?
I believe I’m going to have to code and understand all the code to be able to replicate this for other use case! At the moment I am not able to follow the code as I am new to python! Im going to stick to this and understand this no matter how long it takes! I really did want to get into the visual transformer video, but I believe I should master this first!
This is definitely one of the best video to learn about Transformers. Is it normal for me(as a beginner in ai) to see this 4 or 5 times struggling for weeks to understand it fully? 😅
For example, I had to go study huggingface tutorial to understand the tokenizer part and I wonder if I should learn it 100% or just enough to implement it.
Hi! At the beginning it's normal to learn things "just to make things work". Most people who start coding, use libraries without knowing how they work internally. As time goes, you will naturally find yourself curious on how things work on a "lower level" and will explore and study them more deeply.
The goal in life is not to understand everything today, but rather, to understand a little more compared to yesterday. Have a nice day!
Wow Your explanation amazing
For the Encoder/Decode code - why is the last step in these a normalization layer? We wrote the Residual Connection layers with a pre-normalization step (instead of post-normalization as was in the original Transorfer paper).
Really great video - learned a lot. Your inference notebook is using the dataset batching, but how can you build inference with user typed sentences?
Great video! Thanks :)
Awesome! Highly appreciate. 超級讚!非常的感謝。
Hello Umar, Thank you very much for this video. This is one of the best. I took inspiration and tried to implement the paper "Attention is all you need" for English French translation. I used opus-book en-fr dataset to train the transformer. I ran for 40 epochs and my batch size was 64. In the end the loss converges to around 3.6. Between epoch 30 and epoch 40, for 10 epochs, for each mini batch the step loss was around 3.6. The model's performance after 40 epoch on test set was not good. I would be thankful to you if you share some details like, for how many epochs did you train and what was your step loss towards the end of your training. Probably you took en-it dataset, still it would be helpful for me to get an idea. Thank You very much.