Word Embedding and Word2Vec, Clearly Explained!!!
Words are great, but if we want to use them as input to a neural network, we have to convert them to numbers. One of the most popular methods for assigning numbers to words is to use a Neural Network to create Word Embeddings. In this StatQuest, we go through the steps required to create Word Embeddings, and show how we can visualize and validate them. We then talk about one of the most popular Word Embedding tools, word2vec. BAM!!!
Note, this StatQuest assumes that you are already familiar with...
The Basics of how Neural Networks Work: • The Essential Main Ide...
The Basics of how Backpropagation Works: • Neural Networks Pt. 2:...
How the Softmax function works: • Neural Networks Part 5...
How Cross Entropy works: • Neural Networks Part 6...
If you'd like to support StatQuest, please consider...
Patreon: / statquest
...or...
KZhead Membership: / @statquest
...buying my book, a study guide, a t-shirt or hoodie, or a song from the StatQuest store...
statquest.org/statquest-store/
...or just donating to StatQuest!
www.paypal.me/statquest
Lastly, if you want to keep up with me as I research and create new StatQuests, follow me on twitter:
/ joshuastarmer
0:00 Awesome song and introduction
4:25 Building a Neural Network to do Word Embedding
8:18 Visualizing and Validating the Word Embedding
10:42 Summary of Main Ideas
11:44 word2vec
13:36 Speeding up training with Negative Sampling
#StatQuest #word2vec
To learn more about Lightning: lightning.ai/ Support StatQuest by buying my book The StatQuest Illustrated Guide to Machine Learning or a Study Guide or Merch!!! statquest.org/statquest-store/
In simple words, word embeddings is the by-product of training a neural network to predict the next word. By focusing on that single objective, the weights themselves (embeddings) can be used to understand the relationships between the words. This is actually quite fantastic! As always, great video @statquest!
bam! :)
Probably the most important concept in NLP. Thank you explaining it so simply and rigorously. Your videos are a thing of beauty!
Wow, thank you!
Josh; this is the absolutely clearest and most concise explanation of embeddings on KZhead!
Thank you very much!
totally agree
When I watched this,I have only one question which is why all the others failed to explain this if they are fully understood the concept?
bam!
@@statquest Double Bam!
Bam the bam!
Can't believe this is free to watch, your quality content really helps people develop a good intuition about how things work!
Thanks!
this channel is the best resource of ML in the entire internet
Thank you!
This channel is literally the best thing happened to me on youtube! Way too excited for your upcoming video on transformers, attention and LLMs. You're the best Josh ❤
Wow, thanks!
Yes, please do a video on transformers. Great channel.
@@MiloLabradoodle I'm working on the transformers video right now.
@@statquest Can't wait to see it!
Statquest is by far the best machine learning Chanel on KZhead to learn the basic concepts. Nice job
Thank you!
So good!!! This is literally the best deep learning tutorial series I find… after a very long search on the web!
Thank you! :)
Thank you Josh, this is something I've been meaning to wrap my head around for a while and you explained it so clearly!
Glad it was helpful!
On of the best videos I've seen till now regarding Embeddings.
Thank you!
I was struggling to understand NLP and DL concepts, thinking of dropping my classes, and BAM!!! I found you, and now I'm writing a paper on neural program repair using DL techniques.
BAM! :)
That was the first time I actually understood embeddings - thanks!
bam! :)
Damn, when I first learned about this 4 years ago, it took me two days to wrap my head around to understand these weights and embeddings to implement in codes. Just now, I need to refreshe myself the concepts since I have not worked with it in a while and your videos illustrated what I learned (whole 2 days in the past) in just 16 minutes !! I wished this video existed earlier !!
Thanks!
This is the best explanation of word embedding I have come across.
Thank you very much! :)
Thank you so much for this playlist! Got to learn a lot of things in a very clear manner. TRIPLE BAM!!!
Thank you! :)
Keep up the amazing work (especially the songs) Josh, you're making live easy for thousands of people !
Wow! Thank you so much for supporting StatQuest! TRIPLE BAM!!!! :)
The phrase "similar words will have similar numbers" in the song will stick with me for a long time, thank you!
bam!
StatQuest is great! I learn a lot from your channel. Thank you very much!
Glad you enjoy it!
Absolutely the best explanation that I've found so far! Thanks!
Thank you! :)
Hey Josh. A great new series that I, and many others, would be excited to see is bayesian statistics. Would love to watch you explain the intricacies of that branch of stats. Thanks as always for the great content and keep up with the neural-network related videos. They are especially helpful.
That's definitely on the to-do list.
@@statquest looking forward to it.
BAM!! StatQuest never lie, it is indeed super clear!
Thank you! :)
haha I love your opening and your teaching style! when we think something is extremely difficult to learn, everything should begin with singing a song, that make a day more beautiful to begin with ( heheh actually I am not just teasing lol, I really like that ) thanks for sharing your thoughts with us
Thanks!
It's so nice to google and realize that there is a StatQuest about your question, when you are certain of that there hadn't been one some time before
BAM! :)
Wow!! This is the best definition I have ever heard or seen, of word embedding. Right at 09:35. Thanks for the clear and awesome video. You guy rock!!
Thanks! :)
I love all of your songs. You should record a CD!!! 🤣 Thank you very much again and again for the elucidating videos.
Thanks!
This is one of the best sources of information.... I always find videos a great source of visual stimulation... thank you.... infinite baaaam
BAM! :)
Thank you sir. Your explanation is great and your work is much appreciated.
Thanks!
Hopefully everyone following this channel has Josh's book. It is quite excellent!
Thanks for that!
This video explains the source of the multiple dimensions in a word embedding, in the most simple way. Awesome. :)
Thanks!
highly valuable video and book tutorial, thanks for putting this kind of special tuts out here .
Glad you liked it!
Thank you statquest!!! Finally I started to understand LSTM
Hooray! BAM!
your work is extremely amazing and so helpful for new learns who want to go into detail of working of Deep Learning models , instead of just knowing what they do!! Keep it up!
Thanks!
the best video I saw about this topic so far. Great Content! Congrats!!
Wow, thanks!
Great presentation, You saved my day after watching several videos, thank you!
Glad it helped!
Hey Josh! Loved seeing your talk at BU! Appreciate your videos :)
Thanks so much! :)
Thanks for enlightening us Master.
Any time!
I admire your work a lot. Salute from Brazil.
Muito obrigado! :)
Great video! One suggestion is that you could expand on the Negative Sampling discussion by explaining how it chooses purposely unrelated (non-context) words to increase the model's accuracy in predicting related (context) words of the target word.
It actually doesn't purposely select unrelated words. It just selects random words and hopes that the vocabulary is large enough that the probability that the words are unrelated will be relatively high.
Thank you Statquest!!!!
Any time!
Thank you so much Mr.Josh Starmer, you are the only one that makes ML concepts easy to understand Can you , please , explain Glove ?
I'll keep that in mind.
Way better than my University slides. Thanks
Thanks!
Hey Josh, i'm a brazilian student and i love to see your videos, it's such a good and fun to watch explanation of every one of the concepts, i just wanted to say thank you, cause in the last few months you made me smile beautiful in the middle of studying, so, thank you!!! (sorry for the bad english hahaha)
Muito obrigado!!! :)
BAM! Thanks for your video, I finally realize what the negative sampling means ~
Happy to help!
I love the way you teach!
Thanks!
You are a beautiful human! Thank you so much for this video! I was finally able to understand this concept! Thanks so much again!!!!!!!!!!!!! :)
Glad it was helpful!
Great video for explaining word2vec!
Thanks!
Bro , i have my master degree in ML but trust me you explain it better than my teachers ❤❤❤ Big thanks
Thank you very much! :)
Wow, Awesome. Thank you so much!
You're very welcome!
Awesome as always. Thank you!!
Thank you! :)
This is by far the best video on embeddings. A while university corse is broken down in 15minutes
Thanks!
the best channel ever.
Double bam! :)
This is an amazing video. Thank you!
Thanks!
Great Explanation. Please make a video on how do we connect the output of an Embedding Layer to an LSTM/GRU for doing classification for say Sentiment Analysis
I show how to connect it to an LSTM for language translation here: kzhead.info/sun/f5yBe9udkXuFoJ8/bejne.html
@@statquest Thank You Professor Josh !
Thank you so much for these videos. It really helps with the visuals because I am dyslexic… Quadruple BAM!!!! lol 😊
Happy to help!
Hi, I love your videos! They're really well explained. Could you please make a video on partial least squares (PLS)
I'll keep that in mind.
It would also be nice to have a video about the difference between LM (linear regression models) and GLM (Generalized Linear Models). I know they're different but don't quite understand thAT when interpreting them or programming them in R. THAAANKS!
Linear models are just models based on linear regression and I describe them here in this playlist: kzhead.info/channel/PLblh5JKOoLUIzaEkCLIUxQFjPIlapw8nU.html Generalized Linear Models is more "generalized" and includes Logistic Regression kzhead.info/channel/PLblh5JKOoLUKxzEP5HA2d-Li7IJkHfXSe.html and a few other methods that I don't talk about like Poisson Regression.
@@statquest Thanks Josh!! I'll watch them all 🤗
Absolutely mind blowing and amazing presentation! For the Word2Vec's strategy for increasing context, does it employ the 2 strategies in "addition" to the 1-Output-For-1-Input basic method we talked about in the whole video or are they replacements? Basically, are we still training the model on predicting "is" for "Gymkata" in the same neural network along with predicting "is" for a combination of "Gymkata" and "great"?
Word2Vec uses one of the two strategies presented at the end of the video.
For those of you who find it hard to understand this video, my recommendation is to watch it at a slower pace and make notes of the same. It will really make things much more clear.
0.5 speed bam!!! :)
Great video as always!
Thanks again!
Extremamente didático! Parabéns.
Muito obrigado! :)
thanks for your tutorial!!!
You're welcome!
Love this channel.
Glad to hear it!
Keep going statquest!!
That's the plan!
Hi Josh, thank you for your excellent work! Just discovered your videos and consuming like a pack of crisps. I was wondering about the desired output when using the skip-gram model. When we have a word as input, the desired output is to have all the words found within the window size on any sentence of the corpus activate to 1 at the same time on the output layer, right? It is not said explicitly but I guess it is the only way it can be.
The outputs from a softmax function are all between 0 and 1 and add up to 1. In other words, softmax function does not allow more than one output to have a value of 1. See 12:16 for an example of outputs for the skipgram method.
@@statquest, thanks for your prompt reply! You are right, I didn't look carefully enough. I guess I got confused because after watching the video, I read other sources which seem to consider every skip-gram pair as a separate training example, which confused me.
Great vid. So your going to do a vid on transformer architectures? That would be incredible if so. Btw bought your book. Finished it in like 2 weeks. Great work on it!
Thank you! My video on Encoder-Decoders will come out soon, then Attention, then Transformers.
@@statquest When the universe needs you most, you provide
Thank you so much for the video! I have one question, at 15:09, why we only need to optimize 300 steps? For one word with 100 * 2 weights? not sure how to understand the '2' as well.
At 15:09 there are 100 weights going from the word "aardvark" to the 100 activation functions in the hidden layer. There are then 100 weights going from the activation functions to the sum for the word "A" and 100 weights going from the activation functions to the sum for the word "abandon". Thus, 100 + 100 + 100 = 300.
Thank you!@@statquest
you da bessssst, saved me alota time and confusion :..)
Thanks!
Awesome explanation..
Thanks!
Just incredible!
Thank you!
I struggled with this video series and its only been with 3 blue 1 brown's incredibly comprehensive and clear videos on deep learning that I've been able to understand gradient descent, back propagation and basic feed forward networks. Just different learning and training styles I guess.
That make sense to me. I made these videos because 3blue1brown's video's didn't help me understand any of these topics. So if 3blue1brown's works for you, bam!
Please make a video about the metrics for prediction performance: RMSE, MAE and R SQUARED. 🙏🏼🙏🏼🙏🏼 YOURE THE BEST!
The first video I ever made is on R-squared: kzhead.info/sun/ZaWKe9GvaGaje4U/bejne.html NOTE: Back then I didn't know about machine learning, so I only talk about R-squared in the context of fitting a straight line to data. In that context, R-squared can't be negative. However, with other machine learning algorithms, it is possible.
That was quite informative
BAM! Thank you so much for supporting StatQuest!!! :)
Machine learning explained like Sesame Street is exactly what I need right now.
bam!
Thank you Josh for this great video. I have a quick question about the Negative Sampling: If we only want to predict A, why do we need to keep the weights for "abandon" instead of just ignoring all the weights except for "A"?
If we only focused on the weights for "A" and nothing else, then training would cause all of the weights to make every output = 1. In contrast, by adding some outputs that we want to be 0, training is forced to make sure that not every single output gets a 1.
great stuff as usual ..BAM * 600 million
Thank you so much! :)
Can you do GloVe? i really enjoyed Word2Vec it will be great to see how GloVe works...how factorization based method works. Thank you for this amazing content!
I'll keep that in mind.
watched this video multiple times but unable to understand a thing. I'm sure I am dumb and the Josh is great!
Maybe you should start with the basics for neural networks: kzhead.info/sun/dtWIls1saH6cd68/bejne.html
I appreciate the knowledge you've just shared. It explains many things to me about neural networks. I have a question though, If you are randomly assigning a Value to a word, why not try something easier? For example, In Hebrew, each of the letters of the Alef - Bet is assigned a value. these values are added together to form a sum of a word. It is the context of the word, in a sentence that forms the block. Sabe? Take a look at Gamatra, Hewbew has been doing this for thousands of years. Just a thought.
Would that method result in words used in similar contexts to have similar numbers? Does it apply to other languages? Other symbols? And can we end up with multiple numbers per symbol to reflect how it can be used or modified in different contexts?
I wish I could answer that question better than to tell you context is EVERYTHING in Hebrew, a language that has but doesn't use vowels, since all who use the language understand the consonant-based word structures. Not only that, but in the late 1890s Rabbis from Ukraine and Azerbaijan developed a mathematical code that was used to predict word structures from the Torah that were accurate to a value of 0.001%. Others have tried to apply it to other books like Alice in Wonderland and could not duplicate the result. You can find more information on the subject through a book called, The Bible Code, which gives much more information as well as the formuli the Jewish Mathameticians created. While it is a poor citation, I have included this Wikipedia link: en.wikipedia.org/wiki/Bible_code#:~:text=The%20Bible%20code%20(Hebrew%3A%20%D7%94%D7%A6%D7%95%D7%A4%D7%9F,has%20predicted%20significant%20historical%20events. The book is available on Amazon if you find it peaks your interest. Please let me know if this helps. @@statquest
@starquest, I had not heard from you about the Wiki?
You ARE the Batman and Superman of machine learning!
:)
I have a question? Are the number of outputs softmax generating at the end of word 2 Vec varying between 2 to 20? Thats why the numbers of params calculated as 3M × 100 × 2? If it were to predict probs for all 3M words, would it have been 3M × 100 × 3M?
We get 3M * 100 * 2 because 1) there are 3 million inputs 2) 100 activation functions 3) and 3 million outputs = 3M * 100 * 2
@@statquest Thank you very much, Josh. My bad, instead of adding the number of parameters on either side of the hidden layer, I was taking their product for some reason. Thank you for the awesome videos.
My favourite topic its magic. Bam!!
:)
Thanks!
BAM!!! Thank you so much for supporting StatQuest!!! :)
Thank you very much for your excellent tutorials! Josh. Here I have a question, at around 13:30 of this video tutorial, you mentioned to multiply by 2. I am not sure why 2? I mean if there are more than 2 outputs, will we multiply the number of output nodes, instead of 2? Thank you for your clarification in advance.
If we have 3,000,000 words and phrases as inputs, and each input is connected to 100 activation functions, then we have 300,000,000 weights going from the inputs to the activation function. Then from those 100 activation function, we have 3,000,000 outputs (one per word or phrase), each with a weight. So we have 300,000,000 weights on the input side, and 300,000,000 weights on the output side, or a total of 600,000,000 weights. However, since we always have the same number of weights on the input and output sides, we only need to calculate the number of weights on one side and then just multiply that number by 2.
@@statquest Thanks for explaining! I also had the same question.
Ohhhhhhhhh! I missed that the first time around! BTW: (Stat)Squatch and Norm are right: StatQuest is awesome!!
Thank you so much for this video. Could you do something like this for audio embedding as well? or how could we merge (do fusion) audio and text embedding? I really appreciate it.
Unfortunately, I'm not familiar with audio embedding.
How are models that map text (i.e. not just a word, but say up to 256 words) to vector trained? (such as the popular `sentence-transformers/all-MiniLM-L6-v2` model at HuggingFace)... Are similar or different principles used?
"sentence-transformers" is a type of transformer. Word embedding is just small part of how transformers work. To learn more about basic transformers, see: kzhead.info/sun/rdyKqbiDb6OrrJE/bejne.html
@@statquest What other videos should I watch before I can fully grasp the video on transformers?
@@meirgoldenberg5638 I would recommend just watching the Transformer video first. If you are confused, have a lot of recommend videos in the video's description. Alternatively, you can just watch the whole neural network playlist: kzhead.info/sun/dtWIls1saH6cd68/bejne.html
Awesome video! This time, I feel I miss one step through. Namely, how do you train this network? I mean, I get that we want the network as such that similar words have similar embeddings. But what is the 'Actual' we use in our loss function to measure the difference from and use backpropagation with?
Yes
@@statquest haha I feel like I didn't ask the question well :D How would the network know, without human input, that Troll 2 and Gymkata is very similar and so it should optimize itself so that ultimately they have similar embeddings? (What "Actual" value do we use in the loss function to calculate the residual?)
@@balintnk We just use the context that the words are used in. Normal backpropagation plus the cross entropy loss function where we use neighboring words to predict "troll 2" and "gymkata" is all you need to use to get similar embedding values for those. That's what I used to create this video.
Hello. Thank you very much. Great, great video. I have a question. In the negative sampling procedure we never use A = 1 as input at any step in the training process. I am wondering about the time the embeddings for A are trained. I can see how the weights for A at the right of the activation functions are trained, but not for the weights at the left. I can see that because we use a lot of training steps, in some moment A will be a word we don't want to predict at the input; therefore the embeddings for A will change, however, the prediction won't be A for those steps.
Why would we never use "A = 1" in training?
so goooood! Thank alot!
Glad you like it!
Hi josh firstly thank you for all your videos. I had one doubt , in skip gram what will be the loss function on which the network is been optimized, in CBOW i can see that cross entropy is enough
I believe it's cross entropy in both.
Amazing lecture, congrats. The audio was also made from an NPL (Natural Language Processing), right?
The translated overdubs were.
Fantastic video! How do you apply in powerpoint the style of "pencil-written" boxes?
I use Keynote, and it's one of the default line types.
@@statquest Thanks!
Thanks ❤
:)
al fin alguien me explica como se convierte en si, todos me dicen "usa red que te lo ejecuta automaticamente" pero yo quiero saber que esta haciendo esa red internamente... al fiiiin
Muchas grasias!!! BAM!
Great video! Was just wondering why the output of the softmax activation at 10:10 are just 1 and 0s. Wouldn't that only be the case if we applied ArgMax here not SoftMax?
In this example the data set is very small and, for example, the word "is" is always followed by "great", every single time. In contrast, if we had a much larger dataset, then the word "is" would be followed by a bunch of words (like "great", or "awesome" or "horrible", etc) and not followed by a bunch of other words (like "ate", or "stand", etc). In that case, the soft max would tells which words had the highest probability of following is and we wouldn't just get 1.0 for a single word that could follow the word 'is'.
@@statquest Ohh ok, that clears it up. Thanks!!
Hello Josh, thanks for your video.May I know if we could use 3 neuron network to predict the next words?
Sure
funny and very nicely explained.
Thanks! 😃
Does this mean the neural net to get the embeddings can only have a single layer? I mean: 1. Say total 100 words of corpus 2. First hidden layer (with say I put the embedding size of 256) 3. Then another layer to predict the next word which will be 100 words again. Here, to plot the graph, or say to use the cosine similarity to get how close two words are, I will simply have to use the 256 weights of both words from the first hidden layer, right? So does that mean we can only have a single layer to optimise? Can't we add 2, 3, 50 layers? And if we can, then weights of which layer should we take as the embeddings to compare the words? Will you please guide? Thanks! You are a gem as always 🙌
There are no rules in neural networks, just guidelines. Most of the advancements in the field have come from people doing things differently and new. So feel free to try "multilayer word embedding" if you would like. See what happens! You might invent the next transformer.
@@statquest Haha, yes but... then weights of which layer should be used? 🤔😅 Yeah, I can use any as there are no strict rules, may take mean or something... but if there are any embedding models... may I know what is the standard? Thanks 🙏👍
@@enchanted_swiftie The standard is to use a single set of weights that go to activation functions.
@@statquest Oops, okay... 😅
Hi Josh, great video. I have one question, how are the 2-20 words selected for being dropped while doing negative sampling
This is answered at 13:44. We can pick a random set because the assumption is that when the vocabulary is large, the chances of selecting a similar word are small. And I believe you select a different subset each iteration, so even if you do pick a similar word, the long term effects will not be huge.
@@statquest Got it. Thanks