sentence transformers are built on top of bert networks which are an extension of transformer networks which were built to replace recurrent networks let me explain these one at a time in the context of language but before doing so please do give this video a like that would be really helpful to me and also do join us on discord for some fun discussions links are all down in the description below so recurrent neural networks solve the problem of factor to sequence modeling sequence to vector modeling and sequence to sequence modeling using recurrent units sequences are an
ordered set of tokens but we'll only focus on the sequence to sequence part since it's pertinent to language sentences are an example of sequence of words so recurrent neural networks could be used for english to french translation clearly rnns are versatile but for language problems they have their disadvantages so rnns are slow in training and inference this is because the input words are processed one word at a time and longer sentences just take a longer time rnns also don't truly understand the context of a word that is they only learn about a word based on
the words that come before it but in reality the context of a word really depends on the sentence as a whole now bidirectional lstms try to address this but even here the left to right and right to left context are learnt separately and then they are concatenated and so some of the true context is lost now in 2017 transformers were introduced to address these concerns they're faster since they can now process words simultaneously this is because the architecture has no recurrent units and instead makes use of attention units and then they better understand the context
of words since they look at english words simultaneously they can learn bi-directional context in the true sense so the understanding of these words is so much better transformer networks have two parts an encoder and a decoder for english to french translation we pass in the entire english sentence into the encoder simultaneously and then we get the corresponding word vectors simultaneously now these word vectors encode the meaning of the word and they are better than recurrent networks because they understand bi-directional context through attention units now we pass these vectors into the decoder along with the previously
generated french word to generate the next french word in the sentence and we keep passing the french words that are generated back into the decoder until we hit the end of sentence transformers work well for sequence to sequence problems but for the specific natural language problems like question answering and text summarization even transformers have drawbacks related to one fact language problems are complex and the main drawbacks associated with this are we need a lot of data to train transformers from scratch and the architecture may not be complex enough to understand patterns to solve these language
problems after all transformers weren't designed to be language models so the word representations generated can still be improved address these concerns bert was introduced burt was built with the ideology that different nlp problems all rely on the same fundamental understanding of language now burp models can be built in two phases the first is the pre-training phase where we train the model to understand language and then the fine-tuning phase where we further train the model on a specific task just to tune the network parameters this addresses the data concerns of the transformers if we already have
a pre-trained model that understands language we only need data to fine-tune the model for our specific case the other concern was model complexity which bert also addresses bert models are a stack of transformer encoders and they are called a bi-directional encoder representation of transformers it's exactly what it is it's bi-directional since it understands the context of words looking both ways via attention encoder and transformer well these words arise since burt is essentially a stack of the encoder part of the transformer architecture and then representations now since bert is pre-trained to be a language model it
better understands word representations this means that this output word vector is better encapsulating the meaning of the word this will be really important later on so cool i hope these explanations give you a basic understanding of bert i dedicated an entire video to bert and explaining its architecture with pre-training and fine-tuning explained in detail so if you're looking for a lot more detail please do check that video out the big takeaway here is that bert can now solve a host of complex language specific problems except for one type you're a data scientist at cora which
is a question answer site and you want to design a system that finds related questions to the one that is currently being asked how would we solve this with bert so let's just diagnose this step by step we first take this question and another question that has been asked in the past pass both of these into bert bert generates word vectors we then pipe this into some fun feed forward layers such that the output would be a single neuron and this neuron would have the notion of similarity higher the number higher the similarity essentially and
then we'd need to pass in every question on the platform in this way to determine the similarity then you just like the questions that have the highest similarity and they are considered the most relevant or closest questions but there's a big problem here if there are 100 million questions on quora then we need to run the forward pass of bert 100 million times every single time a new question comes in this is not viable so how do we make bert viable let's think about this in a four pass explanation in the first pass is a
high-level intuition and solution and the future passes we will dive into implementation details so pass one let's think about an ideal solution here so step one would be we want to pass a new question into bert to get a single vector that represents the question then step two we compare it to all other questions using something like a cosine similarity metric and then the third step is that we return the nearest neighbors as the most related questions so for a question added we need to use the vert model only once and not 100 million times
as we suggested before this is good since computing simple cosine similarities between vectors is much cheaper than passing in all questions on the platform through the complex model every time you need to make a decision but clearly to implement this there are a lot of unanswered questions which we will take a look into now in past two so the first step was to pass a new question into birth to get a single vector that represents the question but burt only gives us word vectors so if you want a sentence vector instead you'll need to somehow
aggregate these word vectors by passing it through some unit and the most straightforward way of doing this is to just take the average of these vectors which is like mean pooling this here is the simplest sentence transformer but the output sentence vector here generated is extremely poor quality so poor that you might be better off just taking the average of glove embeddings and not even using bert so that's no good now in order to have burr to create sentence vectors that actually have meaning we need to further train it on sentence level tasks such as
natural language inferencing sentence text similarity and something called the triplet data set we'll talk about each of them in the next pass but just know that once we train bert on any or all of these tasks this sentence vector becomes a good representation of the sentence that is it encodes the meaning of the sentence very well this is important since it means that closer the vectors are in terms of distance the more similar is the meaning and so the question why does the earth revolve around the sun would have a higher similarity score with the
question what are kepler's laws of planetary motion than it would with the question why do cheeseburgers taste good since the first pair is just closer in meaning in our quora questioning setting we would just pass in every question through the sentence transformer once and store it somewhere for future use then when a new question comes in we pass only that question through the sentence transformer to get the sentence vector representation and then determine the questions with the highest cosine similarity and surface them as related questions we can find the nearest neighbors through some nearest neighbor
techniques this completes the past two of the explanation now let's get deeper with past three bert is good at word representations but we want to make a sentence transformer that's good with sentence representations to do this we find to invert on three sentence related tasks natural language inference sentence text similarity and the triplet data set let's take a look at each of these natural language inference is a task that takes in two sentences and determines if sentence one entails sentence two or sentence one contradicts sentence two or just neither this allows bert to understand sentence
meanings as a whole and is hence chosen for training for trained natural language inference we use a siamese network siamese means twins and so we have two of the exact same sentence transformer networks connected in this fashion if we want to compare two sentences we pass them through the different bert networks to get word representations these word vectors are then combined to create a sentence vector and then we concatenate the two sentence vectors and their difference the output is a softmax classification which can be one of three classes if sentence one entails sentence two then
class one if sentence one contradicts sentence to then class two otherwise it's neither and it's just class three the data set would just be sentence pairs with one of the three class labels so we minimize the cross entropy loss and hence train the network note the mean pulling and this concatenation look really arbitrary but they are chosen because they yielded the best results than any other strategy like for example choosing max pooling or just considering the absolute difference between the vectors instead of concatenating all three so i hope this makes sense during inference time we
only need the sentence transformer piece where we get a question and then we get the corresponding sentence vector this vector is the sentence representation that encodes the meaning very well this natural inference task isn't the only way though that we can train sentence transformers another task we can use to fine-tune burt to understand sentences is using sentence text similarity given two sentences output the rating of how similar they are so just like natural language inference this is also trained with a siamese network during training we pass the two sentences to compare through different sentence transformers
to get these sentence vectors and then we compute the cosine similarity between these sentences to get a number between negative 1 and positive 1. these are then compared to an actual labeled similarity rating so this could be on a scale of 1 to 5 which we normalize to be comparable we minimize the square difference between the two so that the model can be trained now a third type of task that we can train sentence transformers is using a data set that has triples of sentences so of these sentences the main sentence is called the anchor
then the next sentence is a related sentence and the third sentence is some unrelated sentence to the anchor we can quickly make this type of data set by taking a wikipedia page then choose a sentence as an anchor then the next sentence in the same paragraph can be chosen as the related sentence and then we can just choose a sentence from another paragraph as some unrelated sentence the network is a triplet of the exact same sentence transformer architectures so during training we pass each sentence through a sentence transformer to get three sentence vectors we want
to make sure that the difference between the anchor and the related sentence is low and that the difference between the anchor and the unrelated sentence is high this is so that the meanings are learned and we can combine it in some lost form that looks like the one that's shown on screen regardless of which of the methods you choose for training sentence transformers during inference time we should be able to pass in some sentences and get a sentence representation vector that encodes the meaning very well let's talk about this in the fourth and final pass
of the explanation so for this pass let's go back to our data science job at cora how do we recommend similar questions so first before additional questions are asked we want to pass every single sentence through the fine-tuned sentence transformer to get the sentence vectors for each these vectors are good sentence representations so we can put them in a space that kind of looks like this this space is sometimes called the embedding space by the way next when a sentence comes in we pass it into the sentence transformer to get the sentence representation or sentence
embedding next we determine the cosign similarity between this question and other candidate questions and then we'll return the closest question list for small data sets we can determine the cosine similarity for this question with every other but it becomes increasingly hard to do when there is hundreds of millions of questions on quora so to do this now here there are a couple of algorithms we can use spotify uses an approximate nearest neighbor algorithm called annoy to recommend music to you in this case songs are encoded into vectors and i kind of explained this entire process
in detail in another video it's an open source github repository so you can go and check it out another way that we can quickly find nearest neighbors is using aws which has an implementation of the k-nearest neighbors which is super fast so it's also worth trying out i'll leave all the links to these two in the description down below for more details right so this is a long video so let's summarize what we've discussed we started our discussion with recurrent neural networks and how they deal with sequence to sequence problems however they are slow to
train and don't truly understand context much because of recurrent units transformers replace the recurrent units with attention units addressing both concerns but transformers were designed to better solve sequence to sequence problems and not necessarily be complex enough to train a language model bert addresses this by stacking transformer encoders its increased complexity makes this network better at understanding word representations and so we can use burke to solve a host of natural language problems but it isn't good for sentence similarity tasks and this is where sentence transformers comes in sentence transformers address this by adding a pooling
layer to bert and then fine-tuning it on sentence related tasks like natural language inference sentence text similarity or a triplet data set and also we did a case study as a core data scientist where we find the most relevant questions to a given question using sentence transformers and nearest neighbor algorithms hope you all enjoyed this video additional resources are down in the references below so please do give this a thumbs up join us on discord and i will see you very soon bye-bye you