<?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 Omkiran Malepati on Medium]]></title>
        <description><![CDATA[Stories by Omkiran Malepati on Medium]]></description>
        <link>https://medium.com/@omkiran?source=rss-4359cd61ea43------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*zbxmGc4Qg9o2TH7DksFsZg.jpeg</url>
            <title>Stories by Omkiran Malepati on Medium</title>
            <link>https://medium.com/@omkiran?source=rss-4359cd61ea43------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 01:36:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@omkiran/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[Sentence Similarity with BERT]]></title>
            <link>https://medium.com/@omkiran/sentence-similarity-with-bert-49bcd250c1bc?source=rss-4359cd61ea43------2</link>
            <guid isPermaLink="false">https://medium.com/p/49bcd250c1bc</guid>
            <category><![CDATA[sentence-similarity]]></category>
            <category><![CDATA[streamlit-app-deployment]]></category>
            <category><![CDATA[bert]]></category>
            <dc:creator><![CDATA[Omkiran Malepati]]></dc:creator>
            <pubDate>Sun, 18 Jul 2021 03:27:19 GMT</pubDate>
            <atom:updated>2021-07-18T03:27:19.320Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/730/1*DBoJlmvHer2ToRaZ-mGjrg.png" /><figcaption>Source: <a href="https://scottdistillery.medium.com/reader-question-what-should-i-do-if-a-project-sells-in-hollywood-which-is-similar-to-a-script-im-a4ea5f80ce7e">https://scottdistillery.medium.com/reader-question-what-should-i-do-if-a-project-sells-in-hollywood-which-is-similar-to-a-script-im-a4ea5f80ce7e</a></figcaption></figure><p>BERT. Since the day of publication in 2018 it has been talk of the town. As there is much about it to explore and some or the other will be cooking while I am writing this, I just want to give an black-box intro about text sequence similarity measuring using BERT. This read just needs the reader to have basic understanding of NLP.</p><p>NLP often deals with high dimensional spaces. Typically NLP can be understood as taking text, transforming it to high dimensional numeric space for further processing.</p><blockquote><strong>What we do :</strong></blockquote><ol><li>Take a corpus and converting them into vectors (768 in our case).</li><li>Given a query sentence will be converted to same size vector (768).</li><li>Finding distances between the query vector and all other sentence vectors.</li><li>Listing top sentences with minimum distance from given query sentence.</li></ol><p>Sounds good?? But what happens internally is not that simple. Let’s have look at it.</p><h3><strong>Text to Vector :</strong></h3><ul><li>A sentence given will be divided into Tokens(128 in our case) based on pretrained vocab which is typically 30522.</li><li>These tokens will be further fed to encoder layers which are typically deep neural nets.</li><li>Internals of encoder layers are not discussed in this read.(To be frank I am still working on them)</li><li>Output of the model will be a single 768 dimension tensor. These 768 values contain numerical representation of our sentence which holds semantic sequence of the sentence.</li><li>We can then take our similarity metrics and calculate the respective similarity between different sequences.</li></ul><h3><strong>In code :</strong></h3><ul><li>Theory doesn’t excite us, Right? Let’s see how this works in reality.</li></ul><p>The easiest approach available to do this is through the sentence-transformers library, which covers most of the process that happens.</p><p>To use this, we need to install sentence-transformers using pip install sentence-transformers. We’ll be using bert-base-nli-mean-tokens model to implement what we discussed so far.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/465432282c9471e90bde5cf742d36b68/href">https://medium.com/media/465432282c9471e90bde5cf742d36b68/href</a></iframe><p>Here, sentence_embeddings is a tensor of shape (810650,768). Now we give a a sentence to find the similar sentences to it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/379abbc04f0abc56ac786d8d98f58a15/href">https://medium.com/media/379abbc04f0abc56ac786d8d98f58a15/href</a></iframe><p>In here, sentence_embeddings1 is a single tensor of length 768 , and now we measure similarity between the given sentence and all our sentences using cosine-_similarity and find the sentences with maximum similarity value which is 1.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6a050fe30519ee33a72b9a1fc140920b/href">https://medium.com/media/6a050fe30519ee33a72b9a1fc140920b/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f51009fe9ae8b01da8ff9919f14967be/href">https://medium.com/media/f51009fe9ae8b01da8ff9919f14967be/href</a></iframe><p>These are the top 10 sentences that are similar to given sentence ‘<em>What is the best course for Web development?’</em> in our corpus.</p><p>This is the easiest but abstract approach to compare our sentences with just few lines of code.</p><p>We can add simple UI to our code using StreamLit with a search option to view the results on a web page. Code can be found <a href="https://github.com/OmkiranMalepati/SentenceSimilarityUsingBERT/blob/main/Sentence_Similar_App.ipynb">here</a>.</p><p>Web page looks like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/925/1*B9sDUrt-zv3f4Qvq4VZu9g.png" /></figure><p>That’s all for this black-box view to measure the semantic similarity of sentences using BERT.</p><p>You can find the full notebook for this implementation <a href="https://github.com/OmkiranMalepati/SentenceSimilarityUsingBERT">here</a>.</p><p>This is my first article on Internet, I hope you’ve enjoyed this.</p><p>Thanks for reading!!</p><h3>References:</h3><ul><li><a href="https://www.analyticsvidhya.com/blog/2021/05/measuring-text-similarity-using-bert/">https://www.analyticsvidhya.com/blog/2021/05/measuring-text-similarity-using-bert/</a></li><li><a href="https://towardsdatascience.com/bert-for-measuring-text-similarity-eec91c6bf9e1">https://towardsdatascience.com/bert-for-measuring-text-similarity-eec91c6bf9e1</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=49bcd250c1bc" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>