{"id":843,"date":"2025-04-27T20:09:08","date_gmt":"2025-04-28T00:09:08","guid":{"rendered":"https:\/\/brian.digitalmaddox.com\/blog\/?p=843"},"modified":"2025-04-27T20:09:08","modified_gmt":"2025-04-28T00:09:08","slug":"a-short-guide-to-nlp-part-3-how-we-do-nlp-today","status":"publish","type":"post","link":"https:\/\/brian.digitalmaddox.com\/blog\/?p=843","title":{"rendered":"A Short Guide to NLP \u2013 Part 3: How We Do NLP Today"},"content":{"rendered":"<p>OK I will admit it took me longer than I had planned to finish this up. \u00a0Life got in the way. \u00a0But now I think is a good time to finish up the series and move on to another.<\/p>\n<p>In the first parts of this series, we looked at why English is such a challenging language for natural language processing (NLP), and how early methods like rules-based systems and Bag of Words models approached the problem.<span class=\"Apple-converted-space\">\u00a0 <\/span>But like everything else, access to power GPU\u2019s and AI has had a big impact on modern NLP techniques.<\/p>\n<p>Today, NLP has evolved dramatically, powered by new methods that are far more powerful \u2014 and much better at handling the complexity and ambiguity of human language.<\/p>\n<p>Let us look at some of the modern techniques that have reshaped NLP in recent years.<\/p>\n<h1>Word Embeddings: Giving Words Meaning<\/h1>\n<p>One major leap forward came with <a href=\"https:\/\/www.turing.com\/kb\/guide-on-word-embeddings-in-nlp\">word embeddings<\/a> \u2014 ways of representing words as vectors in a multi-dimensional space, where words with similar meanings are close together.<\/p>\n<p>Unlike older methods that treated words as isolated tokens, embeddings like <a href=\"https:\/\/en.wikipedia.org\/wiki\/Word2vec\">Word2Vec<\/a>, <a href=\"https:\/\/nlp.stanford.edu\/projects\/glove\/\">GloVe<\/a>, and <a href=\"https:\/\/www.analyticsvidhya.com\/blog\/2017\/07\/word-representations-text-classification-using-fasttext-nlp-facebook\/\">FastText<\/a> actually learned that words like \u201cking\u201d and \u201cqueen\u201d are related, or that \u201cParis\u201d and \u201cFrance\u201d have a strong connection.<\/p>\n<p>These embeddings helped NLP systems recognize relationships and similarities between words \u2014 even when they were not identical \u2014 which made downstream tasks like translation, search, and classification much more accurate.<\/p>\n<p>Let us dig a little deeper into how these things work by taking a look at Word2Vec.<span class=\"Apple-converted-space\">\u00a0 <\/span>Word2Vec generates a vector representation of words in a multi-dimensional space.<span class=\"Apple-converted-space\">\u00a0 <\/span>In this space, similar words end up being \u201ccloser\u201d to one another.<span class=\"Apple-converted-space\">\u00a0 <\/span>The idea behind it is that words that appear in similar contexts tend to have similar meanings.<\/p>\n<p>Word2Vec actually generates a shallow neural network to learn the relationships between words.<span class=\"Apple-converted-space\">\u00a0 <\/span>It typically has a dense hidden layer that takes generated embeddings from the inputs to predict the outputs.<span class=\"Apple-converted-space\">\u00a0 <\/span>It does this in two different ways.<\/p>\n<p>The first way is the <a href=\"https:\/\/leshem-ido.medium.com\/skip-gram-word2vec-algorithm-explained-85cd67a45ffa\"><strong>Skip-Gram Model<\/strong><\/a> and is the most common method of generating the network. \u00a0We select a word in the middle of the sentence and train the model to predict the nearby words within a certain window size. \u00a0These word pairs (such as cat and furry) are fed into the network for training.<span class=\"Apple-converted-space\">\u00a0 The model learns to predict context words given an input word. \u00a0<\/span>A very simplified example of the network is given below.<\/p>\n<pre>Input Layer (one-hot encoding) : (cat, furry)\r\n          |\r\nHidden Layer (dense layer)\r\n          |\r\nOutput Layer (Softmax layer that outputs probabilities for words being a nearby word)<\/pre>\n<p>The second method is the <a href=\"https:\/\/www.geeksforgeeks.org\/continuous-bag-of-words-cbow-in-nlp\/\"><strong>Continuous Bag of Words<\/strong><\/a> model.<span class=\"Apple-converted-space\">\u00a0 <\/span>Here the model is trained by giving it context words and it then tries to predict the target word.<span class=\"Apple-converted-space\">\u00a0 <\/span>It can be thought of as a reverse of the previous technique. It uses a fixed-sized window of context words around the target word.<span class=\"Apple-converted-space\">\u00a0 <\/span>So if our target word in a sentence is cat, we could have context words of furry and purrs.<span class=\"Apple-converted-space\">\u00a0 <\/span>Another of my masterful graphic arts shows how this works below.<\/p>\n<pre>Input Layer (words are one-hot encoded, averaged into an input vector, and the vectors summed depending on the implementation)\r\n          |\r\nHidden Layer (dense layer)\r\n          |<span class=\"Apple-converted-space\">\u00a0<\/span>\r\n\r\nOutput Layer (Softmax layer that outputs probabilities of a single word being the target word)<\/pre>\n<p>But traditional embeddings had one big weakness: each word only had one vector, no matter how it was used. \u201cBank\u201d always meant the same thing, whether you were talking about rivers or money.<\/p>\n<p>That led to the next big innovation: contextual embeddings.<\/p>\n<h1>Contextual Embeddings: Understanding Words in Context<\/h1>\n<p>With models like <a href=\"https:\/\/paperswithcode.com\/method\/elmo\"><strong>ELMo<\/strong><\/a> and <a href=\"https:\/\/www.geeksforgeeks.org\/explanation-of-bert-model-nlp\/\"><strong>BERT<\/strong><\/a>, NLP systems moved beyond static word meanings.<span class=\"Apple-converted-space\">\u00a0 <\/span>Now, the context of a word \u2014 the words around it \u2014 could change its meaning. If you wrote \u201che deposited cash at the bank\u201d versus \u201cthey sat by the river bank,\u201d modern models could understand that bank means two very different things.<\/p>\n<p>This made a huge difference in tasks like question answering, translation, and search, where understanding the nuance of a sentence is critical.<\/p>\n<h2>Transformers: The Engine Behind Modern NLP<\/h2>\n<p>All of this was made possible by a groundbreaking architecture introduced in 2017: the Transformer.<\/p>\n<p>Transformers, introduced in the paper <a href=\"https:\/\/arxiv.org\/abs\/1706.03762\"><strong>Attention is All You Need<\/strong><\/a>, replaced older models like <a href=\"https:\/\/en.wikipedia.org\/wiki\/Recurrent_neural_network\"><strong>RNNs<\/strong><\/a> and <a href=\"https:\/\/en.wikipedia.org\/wiki\/Long_short-term_memory\"><strong>LSTMs<\/strong><\/a> by doing something surprisingly simple: instead of reading words one by one, they looked at the entire sentence (or even paragraph) all at once.<\/p>\n<p>At the heart of transformers is the <a href=\"https:\/\/machinelearningmastery.com\/the-transformer-attention-mechanism\/\"><strong>attention mechanism<\/strong><\/a>, which lets the model figure out which words in a sentence are most important when trying to understand a given word.<\/p>\n<p>This means a model can understand relationships across an entire sentence \u2014 or even multiple sentences \u2014 no matter how far apart the words are.<\/p>\n<p>For an example, we will look at how BERT (Bi-directional Encoder Representations from Transformers) works.<span class=\"Apple-converted-space\">\u00a0 <\/span>BERT is an encoder only transformer architecture that consists of four main modules:<\/p>\n<ol>\n<li>The <strong>Tokenizer Module<\/strong> converts the words in a sentence into a series of tokens.<\/li>\n<li>The <strong>Embeddings Module<\/strong> converts the tokens into embeddings.<\/li>\n<li>The <strong>Encoder Module<\/strong> is a stack of Transformer blocks that use self-attention without causal masking.<span class=\"Apple-converted-space\">\u00a0 <\/span>Self-attention here basically means the blocks determine the relative importance of component in a sequence relative to the other components of the sentence.<span class=\"Apple-converted-space\">\u00a0 <\/span>This lets the model learn the relationships between words no matter where they appear in a sentence.<\/li>\n<li>The <strong>Task-Head Module<\/strong> uses the final embeddings to predict outputs, such as masked words or next sentence predictions, typically through a classification layer.<\/li>\n<\/ol>\n<p>BERT processes the entire sentence at once, verses other methods that look at text sequentially.<span class=\"Apple-converted-space\">\u00a0 <\/span>As a whole model, BERT is trained and fine-tuned using two unsupervised-learning tasks. <span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<ol>\n<li><a href=\"https:\/\/medium.com\/data-science\/masked-language-modelling-with-bert-7d49793e5d2c\"><strong>Masked Language Modeling<\/strong><\/a> (MLM) masks random words in a sentence and the model learns how to predict the masked words based on the context provided by the other words in the sentence.<\/li>\n<li><a href=\"https:\/\/www.geeksforgeeks.org\/next-sentence-prediction-using-bert\/\"><strong>Next Sentence Prediction<\/strong><\/a> (NSP) works by giving the model pairs of sentences and it learns to predict whether or not the second sentence logically follows the first. <span class=\"Apple-converted-space\">\u00a0<\/span><\/li>\n<\/ol>\n<h1>Pre-trained Language Models: A Giant Leap<\/h1>\n<p>Once transformers became popular, researchers realized they could pre-train huge language models on massive amounts of text \u2014 and then fine-tune them for specific tasks.<\/p>\n<p>Instead of training a model from scratch every time you wanted to do translation, or summarization, or classification, you could just start with a giant model that already knew a lot about language, and tweak it slightly.<\/p>\n<p>Some of the most important pre-trained models today include:<\/p>\n<ul>\n<li><strong>BERT<\/strong>: A transformer-based model that reads text bidirectionally, learning to predict missing words.<\/li>\n<li><a href=\"https:\/\/aws.amazon.com\/what-is\/gpt\/\"><strong>GPT<\/strong><\/a> (Generative Pre-trained Transformer): A model that learns by predicting the next word in a sequence, leading to natural text generation (and later, chatbots).<\/li>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/T5_(language_model)\"><strong>T5<\/strong><\/a> (Text-to-Text Transfer Transformer): A model that frames everything as a text generation task, from translation to summarization.<\/li>\n<\/ul>\n<p>These models have powered major advances in search engines, customer support chatbots, translation apps, and even tools like ChatGPT.<span class=\"Apple-converted-space\">\u00a0 <\/span>Let us look at GPTs as an example of these models.<\/p>\n<p>GPTs are trained by a huge input corpus such as text books, Wikipedia, blogs, websites, and other sources.<span class=\"Apple-converted-space\">\u00a0 <\/span>With the word Transformers in the sentence, we know that they will use Transformers to process the training data.<span class=\"Apple-converted-space\">\u00a0 <\/span>As the input data is fed into the model, it uses attention and self-attention to consider the context of the word in a sentence based on all of the other words. \u00a0This self-supervised learning approach lets the model learn from vast amounts of unlabeled text, though the training data is often filtered for quality.<\/p>\n<p>When you give a GPT a prompt, it will first break the sentences you type in down and generate embeddings of the sentences.<span class=\"Apple-converted-space\">\u00a0 <\/span>It passes these embeddings through the model architecture to understand the relationships between the words for their meanings.<\/p>\n<p>The Generative part creates output text by computing probable sentences and their order to generate a coherent output.<span class=\"Apple-converted-space\">\u00a0 <\/span>The key here is that the output is entirely based on probabilities.<span class=\"Apple-converted-space\">\u00a0 <\/span>As your input text goes through the model, it predicts output that it thinks matches your input by generating information that is inside the model that is \u201cclose\u201d to the meaning of your input prompt.<span class=\"Apple-converted-space\">\u00a0 <\/span>It may seem like the model understands what you have said, but in the end everything is based on probability.<span class=\"Apple-converted-space\">\u00a0 <\/span>This is also why GPTs can hallucinate, where the model can incorrectly predict output that makes no sense or has incorrect information.<\/p>\n<h1>Challenges Still Remain<\/h1>\n<p>While modern NLP has come a long way, it\u2019s far from perfect. Some of the challenges include:<\/p>\n<ul>\n<li>Bias in training data: Large language models can reflect the biases of the data they were trained on.<\/li>\n<li>Understanding rare or low-resource languages: Most models are still strongest in English and other major languages.<\/li>\n<li>Cost and energy usage: Training massive models requires enormous amounts of computing power.<\/li>\n<\/ul>\n<p>Researchers are actively working on these issues, but they show that even with today\u2019s powerful tools, the complexity of human language is still a hard problem.<\/p>\n<h1>Wrapping Up<\/h1>\n<p>From handcrafted rules to massive transformers, NLP has evolved faster in the last five years than almost any other field in AI.<\/p>\n<p>While English \u2014 with its ambiguity, irregular grammar, and endless exceptions \u2014 remains a tough language for machines to master, the combination of contextual understanding, transformers, and pre-trained models has made it possible to do things that seemed like science fiction just a decade ago.<\/p>\n<p>The future of NLP is even more exciting, with research moving toward multilingual models, more efficient architectures, and even models that can understand images, sounds, and language together.<\/p>\n<p>Thanks for joining me on (and waiting on me to finish) this quick tour through the world of NLP. Until next time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>OK I will admit it took me longer than I had planned to finish this up. \u00a0Life got in the way. \u00a0But now I think is a good time to finish up the series and move on to another. In &hellip; <a href=\"https:\/\/brian.digitalmaddox.com\/blog\/?p=843\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-843","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/brian.digitalmaddox.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/843","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/brian.digitalmaddox.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/brian.digitalmaddox.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/brian.digitalmaddox.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/brian.digitalmaddox.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=843"}],"version-history":[{"count":3,"href":"https:\/\/brian.digitalmaddox.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/843\/revisions"}],"predecessor-version":[{"id":846,"href":"https:\/\/brian.digitalmaddox.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/843\/revisions\/846"}],"wp:attachment":[{"href":"https:\/\/brian.digitalmaddox.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/brian.digitalmaddox.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/brian.digitalmaddox.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}