 You have international friends talking smack behind your back. You use Google Translate You're looking upwards for a French class. You're good taking You use Google Translate You're in a foreign country and just want to ask the waiter for some extra cheese on your taco. You use Google Translate Google Translate has quite the eclectic of applications, but have you ever wondered how does it translate stuff? How is that all working online? We're going to answer these questions today tech. You're a non-techie. I'm gonna make sure you all follow along and learn something interesting in the end This is code Emporium and with that let's get started Language translation How do we translate a sentence in one language to another language to make things concrete? Let's say that we're translating from English to French Our first trial would be you take every word in the English sentence for every word you find the corresponding French translation Then spit it out and we repeat this for every word in the sentence It's a simple strategy and honestly, we don't need machine learning for this If we just have a curated database with English to French word translations, then we're all set For every English word look it up in the database get the corresponding French word and repeat this for every word That's great, but there's a problem with this if you're a bilingual or even if you just know English Then you know that language has two important components. That's tokens and grammar Tokens are the smallest units of language Grammar defines how these tokens should appear so that they make sense So in this context tokens are words every word is a token it's a beautiful day has five word tokens and Grammar is basically a guide or a set of rules that defines an ordering for these words If language was constructed from tokens and grammar didn't matter then language translation would be so much easier And our simple word translation system we came up with would actually be the state of the art translator However, that isn't the case grammar exists and we need to incorporate it in translator logic In order to incorporate grammar we have to ensure many things the first is syntax analysis Syntax is basic structure. It's basically asking the question does the structure of the sentence look correct? In English, we could have an adverb followed by an adjective followed by an noun like very big cloud And then we have semantic analysis Semantics is meaning and it asks the question does this sentence make sense in context? If we don't follow this then we're just outputting gibberish Language translation as to the chaos as we need to make sure the translated French sentence follows the similar rules Clearly language is more complex than simply an assortment of tokens Instead of trying to explicitly define our own grammar What if we let the machine's neural network do it for us? If you haven't heard of neural networks, don't worry about it too much Just think of it as a component that learns to solve problems by looking at hundreds of thousands of examples This allows the network to learn patterns and data and eventually it would be able to translate a given English sentence to French all on its own Now this sounds interesting, but what exactly is this network? Now we can actually derive the neural network architecture required based on the problem We are trying to solve in this case. We need a neural network that solves the problem of language translation Some English sentence is the input and it should spit out some French sentence The first thing you notice the input and outputs are both sentences or a sequence of words But computers don't understand sentences like humans do So we need to convert it into a form that they do understand and that's numbers More specifically vectors and matrices, which are just an assortment of numbers representing data And so we have the first part of our network a sentence to vector mapper This part of the network takes an English sentence and spits out a vector of numbers that the computer can understand Now this box here is a neural network and since we're dealing with sequences or sentences We use what's called a recurrent neural network Now again, if you haven't heard of a recurrent neural network think of it as a neural network that learns to solve Problems that involve sentences Since we're dealing with the problem of language translation and language translation requires sentences Well, we think recurrent neural network So we took our English sentence and with our current neural network. We converted it into a vector Now we need to convert this vector into a French sentence This vector to sentence mapping can be done with another network and once again since we're dealing with a sentence Transformation we use another recurrent neural network and Together these two recurrent neural nets make the bare-bone structure for our language translator What we've constructed here is a fundamental structure for the translation and it's called the encoder-decoder architecture The first network encodes the English sentence to computer data and the second decodes the computer data to the French sentence But what are these boxes these RNNs exactly? They are actually long short-term memory recurrent neural networks or LSTM RNNs We use LSTM cells specifically because they can deal with longer sentences fairly well It's a very interesting neural network that was conceived way back in the 90s as simple as it sounds this encoder-decoder network with LSTM cells is the basis of several papers and Was a state-of-the-art network in 2014 not too long ago This was the first time recurrent neural networks became wildly successful for language translation In fact, if we take a look at performance the x-axis here represents the number of words in the sentence and the y-axis is the blue score It's basically the accuracy of translation hire the blue score better as a performance So it looks like this encoder-decoder architecture works well for medium length sentences with about 15 to 20 words Let's see how this does with longer sentence translation with an example say we have an English sentence that we want to translate to French An admitting privilege is the right of a doctor to admit a patient to a hospital or a medical center to carry out a diagnosis or procedure based on his status as a healthcare worker at a hospital Now this is a long-winded sentence, but a valid one It's saying a doctor has the right to admit a patient for further testing If we were to pass this into the LSTM RNN encoder-decoder that we talked about we would get this French translation A privileged day Admission is the right day a medicine to recognize a patient a l'hôpital or a medical day a Diagnostic or to take a diagnostic in function de son état de santé Now I don't know French so I can't directly verify how correct this is But let's pop this into Google translate and see its English translation a Privilege of admission is the right of a physician to recognize a patient in the hospital or medical center of a diagnosis Or to make a diagnosis according to his state of health By comparing this with the original we can see that the meaning of the sentence breaks just after the term medical center The phrase medical center of a diagnosis doesn't make much sense But still it's not bad. It was able to keep up for about 20 words Now let's try another one Consider the English sentence this kind of experience is part of Disney's effort to extend the lifetime of its series and Build new relationships with audiences via digital platforms that are becoming ever more important. He added When popped into the RNN encoder-decoder we get this French translation Let's now once again pop this French translation into Google translate and see what it spits out in English This type of experience is part of Disney's initiatives to extend the life of its news and Develop links with digital players that are becoming more complex Now first off it didn't generate a closing quotation mark Instead of an audience with an online influence. They were addressed as digital players That's okay. I guess but then it says the links are becoming more complex But that isn't the case in the original sentence where it says the relationship is becoming more important Once again, though, not too bad But you can clearly see the quality of the model isn't quite optimal when translating much longer sentences So what can we do to improve this translation? Remember what I said before about language? It has two components tokens and grammar and it is this grammar that makes language so complex The problem with the current model is that it's not entirely addressing this complexity The thing with recurrent neural networks is it's using past information to make decisions about the present This means that while generating the tenth word of a translation in French sentence It looks at the first nine words in the English source sentence But we know that a word not only depends on the words that come before it in a sentence But also the words that come after it in a sentence all of this gives rise to the context of the word So in order to look in both directions forward and backward We replaced the normal recurrent neural network with a bidirectional recurrent neural network Interestingly these by RNNs were introduced way back in 1993 but gained popularity recently with the emergence of deep learning So if we're performing English to French translation while writing some word in the French translation We are looking at words that come before it and the words that come after it sweet But which words exactly should we focus on more in a large sentence? This could be difficult to figure out a Method to figure this out was devised in a 2016 paper learning to jointly align and translate I'll explain what this is. So don't worry consider an English sentence the agreement on European economic area was signed in August 1992 And this is the corresponding French translation la core sur l'espace économique européen a été signé en août 1992 Our translator will generate the translated French sentence one word at a time While generating some I th word like eté which words in the English sentence should be considered One solution would be for the I th French word consider the I th English word But then we get the old word word translator that we talked about in the beginning of the video and that's no fun Since it's more complicated than this it needs to be something the translator learns on its own So given the English sentence and its French translation our translator will try to align them in This example eté is lined up with the English words was and signed Really white means super aligned or more attention is focused on that English word while generating the French word While generating the French word European it looks like the only word it would consult is the English word European The same goes for ute The model learns to focus its attention only on the English word August while generating the French word ute In this way the model looks at thousands of other English sentences and their corresponding French Translations and it learns which English words to focus its attention on while generating the words of the French translation This alignment is learned by an extra unit called an attention mechanism and it sits between the encoder and decoder So during translation an English sentence is fed to the encoder It's encoded into some vector, which is just numbers the computer understands. It's basically the same English sentence in the computer's eyes Then we use an attention mechanism basically asking which French word will be generated by which English words The decoder will then generate the French translation one word at a time Focusing its attention on the words determined by the attention mechanism So that's sweet. This actually performs better than the original encoder-decoder architecture The sentence translation is now more closely aligned with the original Google Translates AI works exactly like this. The only difference is everything is scaled up By this I mean instead of using one LSTM for the encoder and decoder We use eight and we do this because deeper networks help better model complex problems So this network is more capable of understanding the semantics of language and grammar Just to recap on the final network. You want to translate English to French You pass the English text word by word to the encoder and it converts these words into a number of word vectors That's the numbers representing these words These are just numbers that represent the words themselves of the sentence these words are then just passed into an attention mechanism and This determines the English words to focus on while generating some French word This data is passed to the decoder which generates the translated French sentence one word at a time and That's it. So if you understood this you understood how Google Translates AI works So yay Just know that every time you use Google Translate from now on something not so magical is actually happening behind the scenes Thank you guys so much for watching and if you liked the video Show some love with a like and subscribe for more awesome content, and I'll see you in the next one. Bye. Bye