<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Anjani Kumar on Medium]]></title>
        <description><![CDATA[Stories by Anjani Kumar on Medium]]></description>
        <link>https://medium.com/@anjanimca2007?source=rss-87f9fa6d075f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*IXChERESEI_m48TqNU2-iQ.jpeg</url>
            <title>Stories by Anjani Kumar on Medium</title>
            <link>https://medium.com/@anjanimca2007?source=rss-87f9fa6d075f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 14:49:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@anjanimca2007/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Recurrent Neural Network(RNN) in NLP]]></title>
            <link>https://anjanimca2007.medium.com/recurrent-neural-network-rnn-in-nlp-784310ea4939?source=rss-87f9fa6d075f------2</link>
            <guid isPermaLink="false">https://medium.com/p/784310ea4939</guid>
            <dc:creator><![CDATA[Anjani Kumar]]></dc:creator>
            <pubDate>Thu, 19 Sep 2024 13:59:01 GMT</pubDate>
            <atom:updated>2024-09-19T16:36:07.221Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/819/1*vDP7j9wNfnfc9r9qVvbDrA.png" /><figcaption>RNN Source: Internet</figcaption></figure><p>As a human we use text extensively in verbal and written way to communicate as its easy to remember and use words then numbers in our day to day communication. But unfortunately machines does not understand it directly. To solve this issue scientist&#39;s were doing research from many years .RNNs were first conceptualized by David Rumelhart in 1986 whereas a similar network by the name of Hopfield networks was discovered earlier by John Hopfield in 1982.Lets deep dive for further details.</p><h4><strong>What is RNN?</strong></h4><p>RNN or Recurrent Neural Network is a type of neural network designed to recognize patterns in sequential data, like time series, speech, text or even video data frames. RNNs are unique as they have ability to “remember” previous inputs because they have loops that pass information from one time step to the next. <br>Basically output of first time stamps is passed as input to next time steps and so on. This is different from traditional neural networks that treat each input independently.</p><p><strong>Diagram of RNN</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4Wg8J_0WG03vTp5dnWhIRg.png" /><figcaption>image :internet</figcaption></figure><p>Here Xt-1 is input at time t-1,U is weight assigned,Ot-1 is output at time t-1</p><h4><strong>Why RNN ?</strong></h4><p>RNNs are ideal for tasks where the order or sequence of data matters like language translations ,next word prediction in sentence or time series etc.</p><p><strong>RNNs help in:</strong></p><p><strong>Language modeling</strong>: Predicting the next word in a sentence or understanding the structure of a sentence.<br><strong>Speech recognition</strong>: Understanding sequences in spoken language.<br><strong>Time series prediction</strong>: Predicting future values in a stock market or weather forecast based on past data.</p><p><strong>The “recurrent” nature of RNNs helps them perform well in tasks where the sequence of information is key.</strong></p><h4><strong>What are Types of RNNs ?</strong></h4><p><strong>Simple RNN:</strong> The simplest form, where the output depends on the current input and the hidden state from the previous step.</p><p><strong>LSTM (Long Short-Term Memory):</strong> LSTMs are an advanced type of RNN designed to combat issues like short-term memory. They remember things for longer durations, making them great for tasks requiring long-term dependencies, like understanding long sentences.</p><p><strong>GRU (Gated Recurrent Unit):</strong> A simplified version of LSTM. It also handles long-term dependencies but with fewer parameters, making it faster to train.</p><p><strong>Bidirectional RNN: </strong>Processes the data in both directions (forward and backward), improving the model’s understanding by considering future context in addition to the past.</p><p><strong>Deep RNN:</strong> Stacks multiple RNN layers to capture more complex patterns in data, much like deep neural networks.</p><h4><strong>Working of RNN ?</strong></h4><p>Imagine you’re reading a story. Each sentence depends on the one before it. Similarly, in an RNN, information flows step-by-step, with each step (or cell) remembering what came before.</p><p><strong>Input:</strong> RNN takes an input at each time step. Let’s say you input a word.</p><p><strong>Hidden State: </strong>The RNN cell has a hidden state, which stores past information. When you input the next word, the hidden state helps provide context by “remembering” the previous words.</p><p><strong>Loop:</strong> This process continues, looping through all inputs in the sequence. The output at each step is influenced by both the current input and the hidden state (past inputs).</p><p><strong>In simple terms, RNN works like a chain of linked memories, where each one depends on the previous one to make sense of the current input.</strong></p><h4>Issue with RNNs &amp; Solution</h4><p><strong>The Problem:</strong></p><p><strong>Vanishing Gradient</strong><br>One of the major issues with simple RNNs is something called the vanishing gradient problem. When training an RNN, especially on long sequences, the gradient (used to update the network’s weights) gets smaller and smaller as it moves backward through time. This makes it hard for the RNN to learn long-term dependencies. Essentially, the network “forgets” earlier information when processing long sequences.</p><p><strong>The Solution: LSTMs and GRUs</strong><br>LSTM and GRU networks were developed to solve this problem. They use special mechanisms called gates to control the flow of information, deciding which data to keep or discard. These gates allow LSTMs and GRUs to retain important information for longer periods, making them much more effective at learning long-term dependencies without losing past information.</p><h3>Conclusion:</h3><p>RNNs has done great job in dealing sequence of data but as soon as the sentence size increases ,RNNs tends to forget the context and does not perform good. To solve this problem, <strong>LSTM </strong>were introduced .Please find the link below .</p><p><a href="https://anjanimca2007.medium.com/simple-explanation-of-long-short-term-memory-lstm-in-nlp-5e31a320d28c">Simple Explanation of Long Short Term Memory (LSTM) in NLP</a></p><p>I hope you like my article on RNNs. Please click on <strong>Clap</strong> if you liked and want me to motivate to write more .Share with your friends too.</p><p>Want to connect :</p><p>Linked In : <a href="https://www.linkedin.com/in/anjani-kumar-9b969a39/">https://www.linkedin.com/in/anjani-kumar-9b969a39/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=784310ea4939" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Simple Explanation of Long Short Term Memory (LSTM) in NLP]]></title>
            <link>https://anjanimca2007.medium.com/simple-explanation-of-long-short-term-memory-lstm-in-nlp-5e31a320d28c?source=rss-87f9fa6d075f------2</link>
            <guid isPermaLink="false">https://medium.com/p/5e31a320d28c</guid>
            <dc:creator><![CDATA[Anjani Kumar]]></dc:creator>
            <pubDate>Tue, 17 Sep 2024 01:06:29 GMT</pubDate>
            <atom:updated>2024-09-17T05:32:22.374Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/738/1*R30B6-mcPMoc_r0Z9GHvdg.jpeg" /><figcaption>source:internet</figcaption></figure><p>To understand the LSTM better ,lets understand the little bit about the RNNs and its drawback.</p><p><strong>Recurrent Neural Networks (RNNs)</strong> are a type of neural network designed to handle sequential data by retaining information from previous inputs in a hidden state. They are commonly used for tasks like time series prediction, natural language processing, and speech recognition.</p><p>In an RNN, the output at each time step depends not only on the current input but also on the hidden state from the previous time step. This structure allows the model to maintain a form of memory over sequences, making it suitable for tasks where past information is important for predicting future outputs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/870/1*uGl21vs00_VlhtrQMQI7Vw.png" /><figcaption>rnn image: colah blog</figcaption></figure><h4>Drawbacks of RNNs:</h4><ol><li><strong>Vanishing and Exploding Gradient Problem:</strong></li><li><strong>Difficulty with Long-Term Dependencies</strong></li><li><strong>Sequential Processing(No Parallel Processing)</strong></li><li><strong>Short-Term Memory</strong></li></ol><p>To come over the above short coming LSTM came into existence in 1995.</p><h3>What is LSTM ?</h3><blockquote>LSTM stands for <strong>Long Short-Term Memory (LSTM)</strong> .A <strong>Long Short-Term Memory </strong>is a type of recurrent neural network (RNN) designed to overcome the limitations of standard RNNs, particularly in retaining information over long sequences. Unlike traditional RNNs, which struggle to remember information for extended periods due to the <strong><em>vanishing gradient problem</em> </strong>, LSTMs have a built-in memory system that enables them to store, update, and retrieve relevant information more effectively.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tniGNC8jblUad_iJ7T-r0g.png" /><figcaption>lstm image :colah blog</figcaption></figure><h4>Key components:</h4><ul><li><strong>Memory Cell(Ct):</strong> The core of the LSTM, responsible for retaining information over time. It helps the model “remember” important details over long sequences.</li><li><strong>Forget Gate:</strong> Decides what information from the memory cell to forget or discard based on the current input and previous hidden state. It helps the model forget irrelevant or outdated information.</li><li><strong>Input Gate:</strong> Determines what new information to store in the memory cell. This gate updates the memory cell with new, relevant data.</li><li><strong>Output Gate:</strong> Controls what information from the memory cell should be used to produce the current output. It determines how much of the memory should influence the next prediction.</li></ul><p>Let’s break down the functioning of each gate in a simple LSTM:</p><p><strong>1.Forget Gate:</strong></p><ul><li><strong>Purpose:</strong> Decides what information to discard from the cell state.</li><li><strong>How it works:</strong> It looks at the previous hidden state and the current input and passes them through a sigmoid function. This generates values between 0 and 1, where 0 means “forget everything” and 1 means “keep everything.”</li></ul><p><strong>Example:</strong> If the current subject changes in a language model, we might want to forget information about the previous subject (like gender).</p><p><strong>Equation:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/976/1*QE6BMwXjvSEmXT8yfMwNaw.png" /><figcaption>image:colah blog</figcaption></figure><p>where σ — is the sigmoid function which converts values between 0 to 1,</p><p>Wf — weights associated with hidden state and current state​</p><p><strong>h</strong>t-1 — Output from previous time stamp also called hidden state passed as input.</p><p>Xt — current time stamp input</p><p>bf — bias value</p><p><strong>2. Input Gate:</strong></p><ul><li><strong>Purpose:</strong> Decides what new information to store in the cell state.</li><li><strong>How it works:</strong> Two steps happen here:</li></ul><ol><li>A sigmoid layer decides which parts of the new information to update.</li><li>A tanh layer creates new candidate values ​ to be added to the cell state.</li></ol><p><strong>Example:</strong> In a language model, if we encounter a new subject, the input gate would add the gender of the new subject to the cell state.</p><p><strong>Equation:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1014/1*_xtzUxLWf9_GngAVyDun9A.png" /><figcaption>image:colah blog</figcaption></figure><p>Where Wi and Wc is weights and bi and bc is biased values.</p><p>Others ,same as forget gate.</p><p><strong>3.Cell State Update:</strong></p><ul><li><strong>Purpose:</strong> Updates the cell state by combining the forget and input gates.</li><li><strong>How it works:</strong> The old cell state is multiplied by the forget gate output​ (to forget irrelevant information), and the result is added to the new candidate values ​ (to store new relevant information).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/909/1*a4K9FsDRIVjPcrULuC860g.png" /><figcaption>image:colah blog</figcaption></figure><p><strong>4.Output Gate:</strong></p><ul><li><strong>Purpose:</strong> Determines what information will be output from the current time step.</li><li><strong>How it works:</strong> A sigmoid layer determines what parts of the cell state will be output, and the cell state is passed through a tanh function to scale the values between −1 and 1. The final output is a filtered version of the cell state.</li></ul><p><strong>Equation:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/994/1*tl42Lyvf1mgvGkxuGVXkCg.png" /></figure><h4>Benefits of Using LSTM:</h4><ol><li><strong>Handles Long-Term Dependencies:</strong> LSTMs excel at capturing long-range patterns in sequential data.</li><li><strong>Mitigates Vanishing Gradient Problem:</strong> LSTMs solve the vanishing gradient issue common in traditional RNNs.</li><li><strong>Selective Memory:</strong> LSTMs selectively keep or discard information using forget, input, and output gates.</li><li><strong>Effective for Sequential Data:</strong> Ideal for tasks like time series forecasting, speech recognition etc.</li><li><strong>Versatility:</strong> LSTMs are used for various sequence-based tasks such as classification, regression, text generation.</li></ol><h4>Shortcomings of LSTM:</h4><ol><li><strong>High Computational Cost:</strong> LSTMs are resource-intensive and slower to train due to their complex structure.</li><li><strong>Memory Consumption:</strong> They consume more memory, especially when handling long sequences or large datasets.</li><li><strong>Difficulty in Parallelization:</strong> LSTMs process data sequentially, making parallelization difficult and slowing training.</li><li><strong>Overfitting with Small Data:</strong> LSTMs tend to overfit on small datasets without proper regularization.</li><li><strong>Architecture Complexity:</strong> LSTMs are more complex and harder to tune compared to simpler recurrent models.</li></ol><h4>Conclusion:</h4><p>LSTMs has done great job in dealing with long sequences but takes lots of time to run(still data was passed sequentially). To solve this problem, <strong>Transformers</strong> were introduced .Please find the link below .</p><p><a href="https://anjanimca2007.medium.com/understanding-transformers-in-simple-way-827cdaad6f49">Understanding Transformers in Simple Way</a></p><p>I hope you like my article on LSTMs. Please click on <strong>Clap</strong> if you liked and want me to motivate to write more .Share with your friends too.</p><p>Want to connect :</p><p>Linked In : <a href="https://www.linkedin.com/in/anjani-kumar-9b969a39/">https://www.linkedin.com/in/anjani-kumar-9b969a39/</a></p><p>Thanks to colah blog for details:</p><p><a href="https://colah.github.io/posts/2015-08-Understanding-LSTMs/">Understanding LSTM Networks</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5e31a320d28c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Transformers in Simple Way]]></title>
            <link>https://anjanimca2007.medium.com/understanding-transformers-in-simple-way-827cdaad6f49?source=rss-87f9fa6d075f------2</link>
            <guid isPermaLink="false">https://medium.com/p/827cdaad6f49</guid>
            <dc:creator><![CDATA[Anjani Kumar]]></dc:creator>
            <pubDate>Tue, 10 Sep 2024 11:56:00 GMT</pubDate>
            <atom:updated>2024-09-17T04:48:28.633Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qtgskRbj0dMNyQR7__tI1A.png" /><figcaption>Transformers</figcaption></figure><p>To understand transformers, lets first understand the problems with previous Models/Architectures.</p><p><strong>1.Recurrent Neural Network (RNN):</strong></p><p>A Recurrent Neural Network (RNN) is a type of neural network designed for sequential data, such as time series or language. It maintains a hidden state that captures information from previous inputs, allowing it to learn temporal patterns. RNNs use shared weights across time steps, making them effective for tasks where the order of data matters, like speech recognition or text generation.</p><p><strong>Issues with RNNs</strong></p><p>1a) They can not remember long sentences<br>1b) They can not be parallelized.</p><p>To solve this LSTM was introduced.</p><p><strong>2.Long Short-Term Memory (LSTM):</strong></p><p>Long Short-Term Memory (LSTM) is a specialized type of Recurrent Neural Network (RNN) designed to better capture long-term dependencies in sequential data. It achieves this by using a memory cell and three gates (input, output, and forget gates) to control the flow of information. These gates regulate which information is kept, updated, or discarded, making LSTMs effective for tasks like time series prediction, language modeling, and speech recognition where long-range dependencies are important.</p><p>LSTMs were able to remember information ,little bit longer time then RNNs, but even LSTMs having below Issues</p><p>2a) They are more complex than traditional RNNs and need larger amounts of training data to learn effectively <br>2b) They are unsuited for online learning tasks like prediction or classification with non-sequential data.<br>2c) LSTM takes longer time to train.</p><p>To solve these issues here comes Transformers.</p><p><strong>3. Transformers :</strong><br>Transformers are type of Neural Network Architecture which transforms a input sequence into an output sequence. They does this by understanding the context and tracking relationship between data.</p><p>Basically they can learn long-term dependencies between words given into a sentence, which make them powerful for task like<br>machine translation, text summarization and questions answering systems</p><p>Transformers works on <strong>attention</strong> mechanism to remember information.<br>They do not have any recurrence as in RNNs. Due to this they are faster to train and can be parallelized.(We will discuss about attention in details below)</p><p>Now lets discuss in details about Transformer Architecture.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*M3_5plpKADnrYqP9fPg3SQ.jpeg" /><figcaption>Transformer Architecture(source: internet)</figcaption></figure><p>Lets breakdown the this architecture to understand it fully.</p><p>Transformer is composed on Encoders and Decoders or Its also called Encoder-Decoder Architecture .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/601/1*lqAtp6UoOT5uabPmm45OtQ.png" /></figure><p>But as per the Original Paper <strong>Attention is All You Need</strong> <a href="https://proceedings.neurips.cc/paper_files/paper/2017/file/3f5ee243547dee91fbd053c1c4a845aa-Paper.pdf">https://proceedings.neurips.cc/paper_files/paper/2017/file/3f5ee243547dee91fbd053c1c4a845aa-Paper.pdf</a></p><p>we have pairs of 6 Encoders and 6 Decoders.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*8OhtQkuGDyP1sV1HTvd95Q.png" /></figure><p>Each Encoder contains <strong>one multi head attention layer</strong> and <strong>one feed forward neural network layer and two Add &amp; Norm layer.</strong></p><p>Each Decoder contains <strong>two</strong> <strong>multi head attention layer</strong> and <strong>one feed forward neural network layer and three Add &amp; Norm layer.</strong></p><p><strong>Working Steps as follows.</strong></p><ol><li>We feed all the words of the sentence to the multi-head attention layer together. Here all the words of the sentence is compared with all the other words of the sentence</li><li>Result of multi-head attention layer(step 1) is passed to the feed forward neural network separately .So they do not have any information exchanged.</li><li>Result of feed forward neural network (from step 2) is passed to next encoder and so on. Remember we have 6 encoders.(In each encoder, neural network used is same but since we have 6 encoders we have six different neural network)</li><li>Result of final Encoder is passed as input to Decoder’s multi-head attention layer.</li><li>Result obtained from Decoder’s multi-head attention layer is passed to feed forward neural network.</li><li>Linear and Softmax layer work is ued to predict the class of an input sequence.Example in language translation its the probability of target word.Basically probability of each word is assigned and word with highest probability is returned as output.</li></ol><h4>Attention Mechanisms in Transformers:</h4><p><strong>What is Attention ?</strong><br>Attention is an ability of model to pay attention to the important part of the sentence or an image or any input.</p><p>In the attention layer,internally SCALED-DOT-PRODUCT attention is used. This is done multiple times to create the effect of muti-head attention.</p><p><strong>Query, Key, Value (QKV):</strong></p><ul><li>In transformers, attention mechanisms use QKV vectors to manage information. Imagine it as a dictionary where you have a query (what you’re searching for), keys (where to find the relevant information), and values (the actual information). The query helps you locate the relevant keys, and each key has an associated value that provides the needed information.Please find details below.</li></ul><p><strong>Query Vector</strong>: The query vector represents the information you’re looking to discover or understand.It helps in finding connections between<br>this query and other elements.</p><p>Imagine you’re translating a sentence from English to French. The query vector focuses on a particular word you want to translate (like “apple”). The query helps figure out which other words (keys) in the sentence are important for understanding or translating the word “apple.”</p><p><strong>Key Vector</strong>: The key vector represents each word in the input. It is used to check how closely each word (key) is related to the query word.</p><p>For example, if you have the words [“cat”, “apple”, “tree”, “juice”], the key vector helps you determine how important each of these words is when trying to understand or translate the query word, like “apple.”</p><p><strong>Value Vector</strong>: The value vector holds the actual information you’ll use, like the translations of the words. The value vectors are weighted depending on how relevant the query and key vectors are to each other.</p><p>For example, if “apple” is closely related to its key, the value vector (its translation, “pomme” in French) will be given more importance in the final translation.</p><p>In this case, the values could be [“chat”, “pomme”, “arbre”, “jus”] — the <br>French translations of [“cat”, “apple”, “tree”, “juice”].</p><p>In short, the query asks for information, the keys help check relevance, and the values provide the answer based on the importance calculated.</p><p><strong>Scaled Dot-Product Attention:</strong></p><ul><li>At the core of attention is scaled dot-product attention, which measures how closely related the query is to each key-value pair. This involves calculating the dot product between the query and each key, scaling this by the square root of the key’s dimension, and then applying a softmax function to obtain attention weights. These weights determine how much each value contributes to the final output.</li></ul><p><strong>Weighted Output:</strong></p><ul><li>The final output of the attention mechanism is a combination of the values weighted by the attention scores. Higher scores mean that the corresponding values are more influential, enabling the model to focus on the most relevant parts of the input sequence for each step of processing.</li></ul><h4><strong>Inputs :</strong></h4><p>All the input which goes to the encoder is embedded. Embedding is technique to convert words to its n-length vectors representation(Here in transformers architecture they are using 512 Length vector representation). After this, Positional Encoding is applied to tell the model about the position of the word in the given sentence.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/427/1*ykYWbLu_-itCjoPj007BCA.png" /></figure><h4><strong>Multi-Head Attention:</strong></h4><p>In Multi-head attention layer, each word in the sentence is compared with all the other words of the sentence.</p><h4><strong>Masked Multi-head Attention:</strong></h4><p><strong>Masked multihead attention</strong> in transformers helps the model focus only on past and current words when predicting the next word, preventing it from seeing future words. This is crucial for generating text in the right order.Basically,all the words coming before the current word is compared.</p><p><strong>How It Works:</strong><br>1)During training, a mask is applied to the attention matrix, setting the attention weights for future tokens (the words that come later in the sequence) to zero.</p><p>2)This ensures that when the model is generating or predicting tokens step by step, it’s only relying on the words it has already seen.</p><h4><strong>Add &amp; Normalization Layer:</strong></h4><p>Add and Normalization layer is used to standardize the data to a common scale.</p><h4><strong>Outputs:</strong></h4><p>In the end of the decoder we have <strong>Linear Layer</strong> and <strong>Softmax Layer</strong></p><ol><li><strong>Linear Layer:</strong><br>Think of a linear layer as a simple math operation. It takes some input and multiplies it by numbers (called weights), then adds another number (called a bias). This changes the shape or size of the data.<br>In transformers, these layers help modify or process the information at different points, like after attention calculations or before generating the final output.</li><li><strong>Softmax Layer:</strong><br> The softmax layer takes the final output from the linear layer and converts it into probabilities. It ensures all the values add up to 1, which makes it easier to interpret as “which option is the most likely.”<br>For example, in a translation task, the softmax layer helps the model decide which word is the most likely next word.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/384/1*0o0U9Wjc9pgJ0KyKs-7YWg.png" /></figure><p><strong>Final Output:</strong> After softmax, the output is a set of probabilities. If you’re generating text, the word with the highest probability is chosen as the next word.</p><p>I hope you like my article on Transformers. Please click on <strong>Clap</strong> if you liked and want me to motivate to write more .Share with your friends too.</p><p>Want to connect :</p><p>Linked In : <a href="https://www.linkedin.com/in/anjani-kumar-9b969a39/">https://www.linkedin.com/in/anjani-kumar-9b969a39/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=827cdaad6f49" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Convolutional Neural Network(CNN) Simplified :]]></title>
            <link>https://anjanimca2007.medium.com/convolutional-neural-network-cnn-simplified-d61198bd9ba5?source=rss-87f9fa6d075f------2</link>
            <guid isPermaLink="false">https://medium.com/p/d61198bd9ba5</guid>
            <category><![CDATA[cnn]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[image-recognition]]></category>
            <category><![CDATA[convolutional-neural-net]]></category>
            <dc:creator><![CDATA[Anjani Kumar]]></dc:creator>
            <pubDate>Sat, 28 Oct 2023 11:16:30 GMT</pubDate>
            <atom:updated>2023-10-28T11:21:31.556Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/813/1*pINAswR1U92USwX3cNAZuA.png" /><figcaption>image courtesy educba</figcaption></figure><p><strong>Convolutional Neural Network(CNN) Simplified(Part 1) :</strong></p><p>A Convolutional Neural Network (CNN) is a class of deep learning neural networks primarily designed for processing grid-like data, <br>such as images and videos. They are very effective in tasks like image recognition, object detection, and even in some natural language<br>processing applications.</p><p>CNNs are particularly well-suited for these tasks because they can automatically and adaptively learn spatial hierarchies of features <br>from input data. They do this through a series of convolutional and pooling layers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bUu6v9h0c9FSEeJ8C0oANw.jpeg" /></figure><p>Now we know the definition of CNN, Before Moving to CNNs deeply, let’s understand <strong>why we should not use ANN for image</strong> recognition and other image and video processing tasks.</p><p>Reasons are below:</p><ol><li>High Computation Cost(due to fully connected layers)</li><li>Overfitting</li><li>Loss of important information like spatial arrangement of pixels of an image.</li></ol><p><strong>Let us understand working of CNNs</strong> :</p><p>An image is 2D grid of Pixels. Given an image, we apply filters(also called kernels) whose main role is to find the primitive features and <br>later more complex features in a given image.</p><p><strong>Types of images:</strong></p><p>We have two type of images</p><p>1 ) Black &amp; White image(also called Grey Scale Images)<br>2 ) Colored Image(also called RGB(Red Blue &amp; Green) images)</p><p>Just to re-iterate an image is made up of Pixels.Our main task is to detect edges in the given image using convolutional operation <br>Detecting edges means checking for intensity change in that given image.</p><p><strong>To Perform a convolutional operation we require filters/kernels. Below images will give an idea.</strong></p><p>We have <strong>greyscale (black &amp; white) image</strong> of 6*6 pixels. We have applied a horizontal filter of 3*3 on that image. As a result we got the feature map of size 4*4</p><p>Calculation to get the feature map of 4*4(performing for 1st row of 1st column of the feature map)</p><p>0*-1+0*-1+0*-1+0*0+0*0+0*0+0*1+0*1+0*1=<strong>0</strong></p><p>Similarly we will perform the convolution operation for all the cells.</p><p>Equation to get 4*4 feature map when the image size is 6*6.</p><p>Let Image is <strong>n</strong>,filter/kernel is <strong>m</strong> then feature map will be calculated using below formula</p><p><strong>Feature Map=(n-m+1)*(n-m+1) =(6–3+1)*(6–3+1)=4*4</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/970/1*d024XOWHpmQ22d4lxVLRLQ.png" /></figure><figure><img alt="Horizontal Edge detection Filter/Kernal" src="https://cdn-images-1.medium.com/max/1024/1*eTSsWoL4Z8efJR0rlQlVeA.png" /><figcaption>Horizontal Edge Detection Filter</figcaption></figure><p>Similarly we can apply a vertical edge Detection filter of 3*3 and we will be able to find the vertical edges.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*41KYtxNXs9BD3VW61kBfrg.jpeg" /></figure><p>In case of <strong>colored(RGB) images</strong> we have three channels of Red, Blue and Green and convolution operation will look like below. While performing convolution operation, it will look like a cuboid placed on top of the image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sctlvPgAe-9il4ais8arNA.jpeg" /></figure><p>Here 3 in 6*6*<strong><em>3</em></strong> &amp; 3*3*<strong><em>3</em></strong> denotes the Channel (red, blue &amp; green)</p><p><strong>The Complete CNN Operation looks like below</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/708/1*ERM2KbxLtbBg-1dpkJj5Bw.jpeg" /><figcaption>image courtesy internet</figcaption></figure><p>I hope you like my article on CNN(Part 1).Please click on <strong>Clap</strong> if you liked and want me to motivate to write more .Share with your friends too.</p><p>Want to connect :</p><p>Linked In : <a href="https://www.linkedin.com/in/anjani-kumar-9b969a39/">https://www.linkedin.com/in/anjani-kumar-9b969a39/</a></p><p>If you like my posts here on Medium and would wish for me to continue doing this work, consider supporting me on <a href="https://www.patreon.com/anjanikumar"><strong>patreon</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d61198bd9ba5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Multiclass Classification - Performance Metrics]]></title>
            <link>https://anjanimca2007.medium.com/multiclass-classification-performance-metrics-6cda2e11eae0?source=rss-87f9fa6d075f------2</link>
            <guid isPermaLink="false">https://medium.com/p/6cda2e11eae0</guid>
            <category><![CDATA[classification]]></category>
            <category><![CDATA[metrics]]></category>
            <dc:creator><![CDATA[Anjani Kumar]]></dc:creator>
            <pubDate>Tue, 23 Mar 2021 10:03:37 GMT</pubDate>
            <atom:updated>2021-03-23T12:36:31.666Z</atom:updated>
            <content:encoded><![CDATA[<p>We have seen in multiple scenarios where we have to deal with multiple classes in the target variable in any machine learning model.</p><p>Lets understand ,how to visualize and understand the model performance with more then 2 classes</p><blockquote>Lets assume we have below three classes in our target variable</blockquote><p>#Lets Assume three classes<br>D=’Dog’<br>C=’Cat’<br>R=’Rat’</p><p>#Since we are just testing lets have our actual and predicted values</p><p>y_test =[D,D,D,D,D,D,C,C,C,C,C,C,R,R,R,R,R,R]<br>y_predict=[D,D,D,C,D,D,C,C,C,R,D,C,R,R,R,D,R,R]</p><p><strong>#Python Implementation</strong></p><p>from sklearn import metrics</p><p>#Calculate Confusion Metrics<br>Conf_metrics=metrics.confusion_matrix(y_test,y_predict)<br>print(Conf_metrics)</p><p><strong>Picture 1: </strong>All Predicted values are shown vertical for a given classes and all the Actual values are in horizontal in the given classes</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/592/1*nsFoPwC10TeKn96A3cy2Eg.png" /></figure><p><strong>Picture 2:”</strong>Accuracy” of this model can be found by taking all the values in diagonal(red box) divided by total values</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/596/1*mY06hCHNOqZJj7O3yEo0qw.png" /></figure><p><strong>Picture 3:</strong></p><p><strong>Precision of this model can be found as below formula</strong></p><p><strong>Precision of a class ie say ‘D’ is</strong> = Number of correctly predicted ‘D’ class<strong>/</strong>All Predicted ‘D’ class</p><p>ie. 4/(4+1+0)=</p><p><strong>Recall</strong> <strong>of a class ie say ‘D’ is</strong>= Number of correctly predicted ‘D’ class/Number of Actual ‘D’ Class</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/598/1*IAp0IHV_EKE5Y_LnMnBzyg.png" /></figure><p>#Generate Classification Report</p><p>classf_report=metrics.classification_report(y_test,y_predict,digits=3)<br>print(classf_report)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yVPiy9q2qi5LBqDfzgESvQ.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6cda2e11eae0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Statistics : T-test & Chi-Square test]]></title>
            <link>https://anjanimca2007.medium.com/statistics-t-test-chi-square-test-38bb78e8c255?source=rss-87f9fa6d075f------2</link>
            <guid isPermaLink="false">https://medium.com/p/38bb78e8c255</guid>
            <category><![CDATA[statistics]]></category>
            <category><![CDATA[t-test]]></category>
            <category><![CDATA[chi-square-test]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Anjani Kumar]]></dc:creator>
            <pubDate>Wed, 05 Aug 2020 16:19:53 GMT</pubDate>
            <atom:updated>2020-08-05T16:19:53.328Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*eUKCbD-Iv8XHyQx2GDYLmw.jpeg" /><figcaption>image:pixels</figcaption></figure><h3><strong>Statistics : T-test &amp; Chi-Square test</strong></h3><h4>Introduction:</h4><p>In statistics we have various types of test to validate the a single group or multiple groups.Here we can say a group as a feature in the data set.These features can be either categorical or numerical features.</p><p>With the help of T-test and Chi-square test we can conclude that if the given group/feature’s are independent or dependent with each other.</p><h4><strong>T-test :</strong></h4><p>When we want to compare one or two numerical variable for independence we use T-test.<a href="https://gist.github.com/ANJANIGITHUB/8143e093e3c2bf698508fb0c8381c94c">Git Link</a></p><p><strong>It is of three types.</strong></p><p>1-Sample T-test<br>2-Sample T-Test or Independent T-test<br>3-Paired T-test</p><p><strong>1-Sample T-test</strong></p><p>To compare population mean and sample means if they are same or different we use 1-Sample T-test.</p><p>Below code to test in Python.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aLlBrk-C6jxXQQV-4AVLhg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fXVra2JMCIK8NDzaK_osSA.png" /></figure><p><strong>2-Sample T-Test or Independent T-test</strong></p><p>Sample T-test will compare two independent means of two features to validate if both are same or different</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vjVeXKYLpGLwhqoCXhGClA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U8YVeVWSYXOMfyr_WRkvVw.png" /></figure><p><strong>3-Paired T-test</strong></p><p>We go for paired t-test when we want to compare different means of same group taken at different time interval.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u9FH5KtsUcu_1bYGwnzYXQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YK9YzCG7WI26ubmDnheFQA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pOCxvalfhmjt8se0QInAAw.png" /></figure><h4>Chi-Square test:</h4><p>Chi-Square test is used to compare two categorical variables to see if they are same or different .<a href="https://gist.github.com/ANJANIGITHUB/ad10d3e85a728163aca51130c0293a3a">Git Link</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lPEJpfgX4QgrlgAitP77UQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gpA40OfHreWQaiwfBxV7Zg.png" /></figure><p><strong>Conclusion</strong>: These test are very important in data Preprocessing and feature engineering steps where we drop the columns which depict same information to avoid multicollinearity issue.</p><p>Hope you like my article.Please hit Clap 👏(50 times) to motivate me to write further.</p><p>Please write your queries &amp; comments and share your feedback.</p><p>Want to connect :</p><p>Linked In : <a href="https://www.linkedin.com/in/anjani-kumar-9b969a39/">https://www.linkedin.com/in/anjani-kumar-9b969a39/</a></p><p>If you like my posts here on Medium and would wish for me to continue doing this work, consider supporting me on <a href="https://www.patreon.com/anjanikumar"><strong>patreon</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=38bb78e8c255" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deep Learning : Artificial Neural Networks(ANN)]]></title>
            <link>https://medium.datadriveninvestor.com/deep-learning-artificial-neural-networks-ann-9eb56cf95be8?source=rss-87f9fa6d075f------2</link>
            <guid isPermaLink="false">https://medium.com/p/9eb56cf95be8</guid>
            <category><![CDATA[ann]]></category>
            <category><![CDATA[artificial-neural-network]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Anjani Kumar]]></dc:creator>
            <pubDate>Sun, 26 Jul 2020 17:53:52 GMT</pubDate>
            <atom:updated>2020-07-28T17:27:18.789Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/970/1*6kMy36ia12_xabVCUeOwNA.jpeg" /><figcaption>Image : TechRadar</figcaption></figure><h3><strong>Deep Learning : Artificial Neural Networks(ANN)</strong></h3><h3><strong>Deep Learning :</strong></h3><p>According to given definition.</p><blockquote><strong>Deep learning</strong> is a subset of <strong>machine learning</strong> in artificial intelligence (AI) that has networks capable of <strong>learning</strong> unsupervised from data that is unstructured or unlabeled. Also known as <strong>deep</strong> neural <strong>learning</strong> or <strong>deep neural network</strong></blockquote><h3>Categories in Deep Learning:</h3><p>Deep learning can be broadly divided into three major categories.</p><ol><li>Artificial Neural Network also know as ANN.</li><li>Convolutional neural network know as CNN</li><li>Recurrent neural network know as RNN.</li></ol><h3><strong>Artificial Neural Network (ANN) :</strong></h3><p>Artificial Neural Network widely know as ANN is an information processing system that is inspired by the way human biological nervous systems, such as the brain, process information.</p><p>In simple terms ANN works on the concept of Neurons in our human brain .A neuron, (also known as a neuron or nerve cell) is an electrically excitable cell that processes and transmits information through electrical and chemical signals.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rbvjJWxn7_DpP6hDsGoHuQ.png" /><figcaption>image:visiblebody</figcaption></figure><p><strong><em>In ANN information is passed from below mention layers</em></strong></p><ol><li>Input Layer — Which accepts inputs features from a dataset</li><li>Hidden Layers — This layer is responsible for performing various operations on input dataset.These (Circles in hidden layers)are called Neurons.</li><li>Output layer — This show the result of the operation performed by Hidden Layer.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/918/1*Ok8e8fjZ8YF7QqkcafDEMg.png" /></figure><p>As you can see in above picture we have three input features Input F1,Input F2 &amp; Input F3 which is passed from input layer to Hidden Layer.</p><p>W1,W2,W3,W4,W5,W6,W7,W8 are different weights assigned to each connection to hidden Neurons.These weights play vital role to optimize the model which we will discuss in detail below.</p><p>Each Input Feature is connected to all the Neurons of Hidden Layer and the output of hidden layer are connected to the final output layer.</p><p><a href="https://www.datadriveninvestor.com/2020/06/19/artificial-intelligence-and-the-new-frontiers-of-brainsourcing/">Artificial Intelligence and the new frontiers of &quot;brainsourcing&quot; | Data Driven Investor</a></p><h3><strong>Working of A Neuron in ANN :</strong></h3><p>Each Neuron perform two major operation.</p><p><strong>Operation 1</strong> : it contains summation of all the weights multiplied by input features and add bias on that.Example, in our linear regression, we use below formula to find the best fit line <strong>y=m * x+c</strong></p><p><strong>Here m is all our weights and x is all our input features and c is bias.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/451/1*_a52FX88wZ6P2YQ-NTKXTg.png" /></figure><p>Same will be applied to all the other Neurons.</p><p><strong>Operation 2 :</strong></p><p>Neuron applies activation function on the output of the <strong>Operation 1</strong> to convert values.</p><p>Few of the famous activation functions are <strong>Sigmoid</strong> and <strong>Relu</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/726/1*29VH_NiSdoLJ1jUMLrURCA.png" /><figcaption>Image :Towards Data Science</figcaption></figure><p>Once the operation under each neuron is complete ,output of the neuron along with the new weight is passed to the output layer.</p><p><strong>In Picture below ,</strong></p><p>Output of Hidden Neuron HL_1 is O1 which is passed to output layer along with Weight W7 and Output of Neuron HL_2 is O2 which is passed to output layer along with Weight W8.</p><p>This predicted output say OP1 is compared with Actual Output to compare.</p><p>This whole flow of information from steps from <strong>Input Layer to Hidden Layer to Output Layer is called 1 Forward Propagation.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/914/1*TSjrM4rEfQik5-OSLzRPBQ.png" /></figure><p><strong>Loss Function : </strong>When we have single predicted output we will compare with the actual output and calculate the loss</p><p><strong>Cost Function : </strong>When we have Multiple predicted output we will compare with the actual outputs and calculate the loss(by summing all the losses)</p><p><strong>Backward propagation</strong> is process to update the weights to reduce the loss (to reach global minima) when the predicted output is not equal to actual output by using Optimizers. Few of the famous optimizers are gradient descent,stochastic gradient descent,mini batch S.G.D. etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nAUzoWNPMkIYUeDlDRfdGw.png" /><figcaption>image gradient descent: medium</figcaption></figure><p><strong>Epoch</strong> : One complete forward propagation and backward propagation is called 1 epoch.</p><p><strong>Conclusion</strong>: Artificial Neural Network is extremely important to get good accuracy of models and generally used when we have millions of records in dataset. I will cover details about each layers along with all the Activation functions ,optimizers in my incoming articles.</p><p>Hope you like my article.Please hit Clap 👏(50 times) to motivate me to write further.</p><p>Please write your queries &amp; comments and share your feedback.</p><p>Want to connect :</p><p>Linked In : <a href="https://www.linkedin.com/in/anjani-kumar-9b969a39/">https://www.linkedin.com/in/anjani-kumar-9b969a39/</a></p><p>If you like my posts here on Medium and would wish for me to continue doing this work, consider supporting me on <a href="https://www.patreon.com/anjanikumar"><strong>patreon</strong></a></p><p><strong>Gain Access to Expert View — </strong><a href="https://datadriveninvestor.com/ddi-intel"><strong>Subscribe to DDI Intel</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9eb56cf95be8" width="1" height="1" alt=""><hr><p><a href="https://medium.datadriveninvestor.com/deep-learning-artificial-neural-networks-ann-9eb56cf95be8">Deep Learning : Artificial Neural Networks(ANN)</a> was originally published in <a href="https://medium.datadriveninvestor.com/">DataDrivenInvestor</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Word2Vec in Natural Language Processing]]></title>
            <link>https://medium.com/analytics-vidhya/word2vec-in-natural-language-processing-1379b050220?source=rss-87f9fa6d075f------2</link>
            <guid isPermaLink="false">https://medium.com/p/1379b050220</guid>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[word2vec]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[naturallanguageprocessing]]></category>
            <dc:creator><![CDATA[Anjani Kumar]]></dc:creator>
            <pubDate>Fri, 03 Jul 2020 12:35:39 GMT</pubDate>
            <atom:updated>2020-07-03T13:12:01.770Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9x46VEb3Ybw1lZ3YjQeszw.jpeg" /><figcaption>image:mortentolboll.weebly</figcaption></figure><h3><strong>Overview</strong></h3><p>In the previously discussed topics ,in Bag of Words (BOW)&amp; TF-IDF approach, semantic information is not stored. Here BOW give equal preference to each words in corpus where as TF-IDF gives importance to uncommon words.</p><p>Semantic means that in a sentence the order &amp; relation of words are important. Like if I have a sentence say “He is going to Collage” it’s important to have ordering between words in this sentence</p><p>There is also chance of over fitting with BOW &amp; TF-IDF</p><p>Solution for both of above issues is <strong>Word2Vec</strong></p><p><strong>Introduction &amp; Working of Word2Vec</strong></p><p>1.In word2vec,each word id basically represented as a vector of 32 or more dimension instead of single number</p><p>2.In Word2Vec,the semantic information and relation between different words is also preserved.</p><p><strong>Visual Representation of Word2Vec:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/635/1*GcaVMDzBemheqQ8e5_JRbg.png" /><figcaption>image:researchgate.net</figcaption></figure><blockquote>If you see in the picture above,we have tried to show words in 3 dimensions.Since words<strong> “newspaper”</strong> and <strong>“magazine”</strong> are related, they have nearby vector (distance between vectors for these two words are very close) representation where as the word <strong>“biking”</strong> which is not related to above two is having completely different vector representation.So we can say there is a semantic information and relation exists between words <strong>“newspaper”</strong> and <strong>“magazine”</strong></blockquote><p><strong>Steps to create Word2Vec</strong></p><p>Step1 : Tokenization of sentences in the corpus.</p><p>Step2: Create Histograms</p><p>Step3:Take most frequent words from the corpus</p><p>Step4: Create a matrix with all the unique words.It also shows the relation between the word based on occurrences</p><p>Lets do it with the Python,Gensim library.</p><p><strong>Import all the required Libraries</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1009/1*udJDPPUemUm77u024i3RIg.png" /></figure><p><strong>Corpus/Paragraph:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WSGsnlQyyHyL9hu5d-BbNA.png" /></figure><p><strong>Clean the Paragraph above:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F1mveaKOxfpn3z5qrxs5Cg.png" /></figure><p><strong>Sentences will look like this:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6n0QIEQzTEca8Ig9m1OpMA.png" /></figure><p><strong>Using Tokenizer to split sentences into words and using Stopwords to remove frequent occurring unwanted words(like the,he she,we,is,etc)</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gsXPDRJfS6yj1qvH4iimJQ.png" /></figure><p>Finally using <strong>Word2Vec</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*79DmcRhQ8qR3Cg40qmzEFA.png" /></figure><p><strong>Vectors representation of each word</strong>.Each word, here <strong>“freedom”</strong> is represented in 100 dimensions</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iABXrqgZXtt2sTKgZoAoIA.png" /></figure><p>Just to show you all the 100 dimension values of word <strong>“freedom”</strong></p><pre>array([ 1.1273683e-03, -3.3408357e-03,  3.1538198e-03,  1.6635142e-03,<br>       -1.5097707e-04, -2.3732651e-03, -5.5288838e-04,  4.2790128e-03,<br>       -1.6280514e-03, -2.5043052e-03, -1.2443076e-03,  3.7798416e-03,<br>        2.4867388e-03,  4.6275570e-03, -2.0362085e-03, -1.1484585e-03,<br>       -3.0532812e-03,  1.7743394e-03, -1.1969920e-03,  1.6191329e-03,<br>        3.2258648e-03, -1.5515186e-03, -3.7306850e-04,  4.0565613e-03,<br>       -4.5308433e-03,  2.7869337e-03, -2.6286333e-03, -1.4752239e-03,<br>       -3.0462523e-03, -7.1018201e-04,  4.0662824e-03,  2.4954581e-03,<br>       -4.1038552e-03, -2.8832494e-03, -2.1366167e-03, -4.3516876e-03,<br>       -1.2155144e-03,  4.9223285e-03,  1.2021879e-03,  1.9537236e-03,<br>        2.6177356e-03,  3.5373569e-03, -4.1266498e-03, -7.0183648e-04,<br>       -3.5120137e-03,  1.4333301e-03, -2.7147203e-03,  1.5479618e-03,<br>        3.6891426e-03,  3.7910854e-03, -1.3579437e-04, -3.6631080e-03,<br>        5.8001833e-04,  1.0410204e-03,  3.0223157e-03,  1.0503514e-03,<br>       -4.8348093e-03, -7.5404608e-04, -2.5279538e-03,  4.6469667e-03,<br>        3.5378032e-03,  4.7412640e-03, -2.0815984e-03, -4.1108266e-03,<br>       -4.5497515e-03, -2.0349291e-03,  4.8185606e-03, -3.5920267e-03,<br>        2.0674071e-03,  1.9790779e-03, -3.9039373e-03,  3.4050874e-03,<br>        3.8651349e-03,  3.6706368e-03,  4.2692507e-03, -3.9807847e-03,<br>        7.2977535e-05,  2.1913229e-03,  2.3057887e-03, -1.3587050e-04,<br>       -4.7944724e-03,  1.2130835e-03, -1.8126203e-03, -2.1072873e-03,<br>       -2.2353262e-03, -2.9427181e-03,  6.3250802e-04,  5.5979716e-04,<br>        3.3508011e-03, -9.0776308e-04, -4.8847585e-03,  1.9552025e-03,<br>       -2.2549990e-03, -4.4488683e-03,  1.2665773e-03, -1.4139886e-03,<br>        1.4697905e-03, -4.8091747e-03,  3.0768490e-03,  6.0989073e-04],<br>      dtype=float32)</pre><p>Finding Similar words like word <strong>“freedom”</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8RnOtYL-YNeGZcSxPutucw.png" /></figure><blockquote>So we can see here that we are getting semantic and relationship information here with word2vec and we are able to find the related words.In this example its “freedom”</blockquote><p><strong>Conclusion</strong>: Word2vec is very useful in finding semantic and relationship information between words and heavily used in AI &amp; NLP application like Survey responses, comment analysis, recommendation engines, and more.</p><p>Please write your queries &amp; comments and share your feedback.</p><p>Hope you like my article.Please hit Clap 👏(50 times) to motivate me to write further.</p><p>Want to connect :</p><p>Linked In : <a href="https://www.linkedin.com/in/anjani-kumar-9b969a39/">https://www.linkedin.com/in/anjani-kumar-9b969a39/</a></p><p>If you like my posts here on Medium and would wish for me to continue doing this work, consider supporting me on <a href="https://www.patreon.com/anjanikumar"><strong>patreon</strong></a></p><p><a href="https://medium.com/@anjanimca2007/tf-idf-in-natural-language-processing-8db8ef4a7736">TF-IDF Link</a></p><p><a href="https://medium.com/@anjanimca2007/bag-of-words-in-natural-language-processing-fe996f9ae9d2">Bag of Words Link</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1379b050220" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/word2vec-in-natural-language-processing-1379b050220">Word2Vec in Natural Language Processing</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[TF-IDF in Natural Language Processing]]></title>
            <link>https://medium.datadriveninvestor.com/tf-idf-in-natural-language-processing-8db8ef4a7736?source=rss-87f9fa6d075f------2</link>
            <guid isPermaLink="false">https://medium.com/p/8db8ef4a7736</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[tf-idf]]></category>
            <category><![CDATA[term-frequency]]></category>
            <category><![CDATA[naturallanguageprocessing]]></category>
            <category><![CDATA[tf-idf-explained]]></category>
            <dc:creator><![CDATA[Anjani Kumar]]></dc:creator>
            <pubDate>Fri, 03 Jul 2020 07:35:42 GMT</pubDate>
            <atom:updated>2020-07-03T14:17:00.631Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/714/1*wgKxsWlT3ifsZmNy-DihFQ.png" /><figcaption>image:Kaggle</figcaption></figure><h3><strong>Overview</strong></h3><p>As we know from my previous article of <a href="https://medium.com/@anjanimca2007/bag-of-words-in-natural-language-processing-fe996f9ae9d2">Bag of Words,</a>we convert sentences into vectors of words through BOW which converts words as either 0 (when word is not there in sentence) or 1 (when word is there).</p><p><strong>Bag of Words</strong> just creates a set of vectors containing the count of <strong>word</strong> occurrences in the sentences/corpus , but it does not contain information on important <strong>words.</strong></p><p>So we have another technique to achieve the words importance is called</p><blockquote><strong>TF-IDF </strong>which means<strong> Term Frequency and Inverse Document Frequency,</strong> is a scoring measure widely <strong>used</strong> in information retrieval (IR) or summarization. <strong>TF</strong>-<strong>IDF</strong> is intended to reflect how relevant a term is in a given document.</blockquote><h4><strong>Understanding TF-IDF in Details with example:</strong></h4><p>Let’s understand TF-IDF though the same example which we saw in “Bag of words”</p><p><strong>Let’s say we have sentences like below</strong></p><p>1.He is eating Veg</p><p>2. She is eating NonVeg</p><p>3.Both are eating Food</p><p>After Applying data cleansing technique and apply stop words we will get below sentences words and there count.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/190/1*fHXgR-q2Uy0wqCiiob5L1Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/239/1*2Y4LkwR94fMMNOLTvHlIXw.png" /></figure><p><strong>Lets see the formula to calculate Term Frequency (TF)</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/729/1*Sea7Nis9wSuRhtE0x1lnLA.png" /></figure><p><strong>Lets calculate Term Frequency for our sentences</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/533/1*j0eFud1jkbywLt7zoyBrew.png" /></figure><p><strong>Lets see the formula to calculate Inverse Document Frequency(IDF)</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/879/1*33rTbQmvUh5cENS-66ZJcA.png" /></figure><p><strong>Lets Calculate Inverse Document Frequency(IDF) for our words in sentences</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/474/1*qX8wOtNJB92xHlwdrjrEFQ.png" /></figure><p>Now to Calculate TF-IDF ,multiply TF table generated above with IDF</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/502/1*ktEXNSF6anRFiwIfGXTB0Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/797/1*_gjbGsHUcSJ_dEdXB0UEWw.png" /></figure><p>After Simplifying we get below table of TF * IDF Matrix</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/784/1*6dGHBvFnw71CXaUxEmn5EQ.png" /></figure><blockquote>Here we can see that in sentence1(eat veg),word “veg” is given importance similarly in sentence (eat nonveg),word “nonveg” is given importance and in sentence3(eat food),word “food” is given importance and its having some form of semantic meaning.</blockquote><blockquote>Word “eat” is not given that much importance as its frequently available in all the sentences and can have same meaning and so not useful.</blockquote><p>Now just think we have only three sentences and how helpful it will be when we have huge corpus of paragraphs and sentences.</p><p>All the input features will be given to the model along with O/P (output feature) and model will be trained and All the important words with value in the form of matrix can be obtained.</p><p><strong>Python code for reference.</strong>Package used is<strong> TfidfVectorizer </strong>from<strong> sklearn</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/850/1*dAObcdgudptT3c57Q7Q8dg.png" /></figure><p><strong>Conclusion </strong>: TF-IDF is technique in Natural Language Processing for converting words in Vectors and with some semantic information and it gives weighted to uncommon words , used in various NLP applications.There are other advance techniques <strong>Word2Vec</strong> which i will cover in my next articles which will have semantic meaning and associated relation among the words.</p><p>Please write your queries &amp; comments and share your feedback.</p><p>Hope you like my article.Please hit Clap 👏(50 times) to motivate me to write further.</p><p>Want to connect :</p><p>Linked In : <a href="https://www.linkedin.com/in/anjani-kumar-9b969a39/">https://www.linkedin.com/in/anjani-kumar-9b969a39/</a></p><p>If you like my posts here on Medium and would wish for me to continue doing this work, consider supporting me on <a href="https://www.patreon.com/anjanikumar"><strong>patreon</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8db8ef4a7736" width="1" height="1" alt=""><hr><p><a href="https://medium.datadriveninvestor.com/tf-idf-in-natural-language-processing-8db8ef4a7736">TF-IDF in Natural Language Processing</a> was originally published in <a href="https://medium.datadriveninvestor.com/">DataDrivenInvestor</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Bag of Words in Natural Language Processing]]></title>
            <link>https://medium.datadriveninvestor.com/bag-of-words-in-natural-language-processing-fe996f9ae9d2?source=rss-87f9fa6d075f------2</link>
            <guid isPermaLink="false">https://medium.com/p/fe996f9ae9d2</guid>
            <category><![CDATA[bag-of-words]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[bow]]></category>
            <category><![CDATA[naturallanguageprocessing]]></category>
            <dc:creator><![CDATA[Anjani Kumar]]></dc:creator>
            <pubDate>Thu, 02 Jul 2020 17:59:34 GMT</pubDate>
            <atom:updated>2020-07-03T15:02:49.912Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/821/1*JAr9n0cVtL7yr7o1YnI-iQ.png" /><figcaption>image:macmillandictionaryblog</figcaption></figure><h4><strong>Overview</strong></h4><p>When dealing with corpus we come across multiples words which we use in Natural Language Processing(NLP) applications to get meaningful insight .To do that we need to convert those word into something which model can understand. We are going to discuss here some thing know as <strong>“</strong>Bag of Words<strong>”</strong>.</p><p>In Simple terms</p><p>“Bag of words also called BOW” is representation of words into vectors of numbers 0 (zero) and 1 (one).These words can belong from various sentences and various paragraphs.</p><p><a href="https://www.datadriveninvestor.com/2020/02/19/five-data-science-and-machine-learning-trends-that-will-define-job-prospects-in-2020/">Five Data Science and Machine Learning Trends That Will Define Job Prospects in 2020 | DataDrivenInvestor</a></p><p><strong>Understanding Bag of Words in details</strong></p><p>Two Major things which we achieve from Bag of Words or BOW is</p><ol><li>Frequency of words in corpus</li><li>Vector representation of words into number 0 and 1</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D9mqenCbuEbToP9B_67FeA.png" /></figure><p>Lets understand with one example here</p><p><strong>Let’s say we have sentences like below</strong></p><p>1.He is eating Veg</p><p>2. She is eating NonVeg</p><p>3. Both are eating Food</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/937/1*YeWWNz040U9fOeWHSsqiuQ.png" /></figure><p><strong>Lets Perform below steps to generate Bag of Words</strong></p><p>Step 1: Split the sentences into words.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/838/1*TtjsvSgZk51pOEOEKR_F8Q.png" /></figure><p><strong>We can use this way also</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/802/1*IGT5C54l9LNKnJ6K0I78vw.png" /></figure><p>Step 2: Lower the sentences.</p><p>Step 3: Apply Stop Words to remove unwanted words like he,she,is,are etc.</p><p>Step 4: Find the frequency of each word.</p><p>Step 5: Perform Stemming &amp; Lamitization.</p><p>Step 2,3,4,5 performed in below code</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rRvzMqjuFJbtasBg5QRBhw.png" /></figure><p>Step 6: Find the Word Frequency &amp; Vector representation of each word</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/873/1*qxmW6qncJoiiAMz89NgscA.png" /></figure><p><strong>Word Frequency</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/267/1*PG8n52rMmpkbNFerWU4PPQ.png" /></figure><p><strong>Vector representation of each word</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/691/1*8ZvQAfyZR9gJAaknj1Py2A.png" /></figure><p>All the words are represented as vectors but major problem with Bag of words ,are not able to find which word is more important and which is less as all are given same weightage.</p><p>Solution for this is TF-IDF which is my next topic.</p><p><strong>Conclusion </strong>: Bag of Words is basic technique in Natural Language Processing for converting words in Vectors and use in some NLP applications.There are other advance techniques like TF-IDF and Word2Vec which i will cover in my next articles.</p><p>Please write your queries &amp; comments and share your feedback.</p><p>Hope you like my article.Please hit Clap 👏(50 times) to motivate me to write further.</p><p>Want to connect :</p><p>Linked In : <a href="https://www.linkedin.com/in/anjani-kumar-9b969a39/">https://www.linkedin.com/in/anjani-kumar-9b969a39/</a></p><p>If you like my posts here on Medium and would wish for me to continue doing this work, consider supporting me on <a href="https://www.patreon.com/anjanikumar"><strong>patreon</strong></a></p><p><strong>Gain Access to Expert View — </strong><a href="https://datadriveninvestor.com/ddi-intel"><strong>Subscribe to DDI Intel</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fe996f9ae9d2" width="1" height="1" alt=""><hr><p><a href="https://medium.datadriveninvestor.com/bag-of-words-in-natural-language-processing-fe996f9ae9d2">Bag of Words in Natural Language Processing</a> was originally published in <a href="https://medium.datadriveninvestor.com/">DataDrivenInvestor</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>