<?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 Karan Malhotra on Medium]]></title>
        <description><![CDATA[Stories by Karan Malhotra on Medium]]></description>
        <link>https://medium.com/@kmkaran212?source=rss-3603b9553f67------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*0x92y_Gu6xVDtEy2K6LhMA.jpeg</url>
            <title>Stories by Karan Malhotra on Medium</title>
            <link>https://medium.com/@kmkaran212?source=rss-3603b9553f67------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 10:02:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kmkaran212/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[Language Agnostic Sentence Embeddings]]></title>
            <link>https://medium.com/@kmkaran212/language-agnostic-sentence-embeddings-57445eba02a?source=rss-3603b9553f67------2</link>
            <guid isPermaLink="false">https://medium.com/p/57445eba02a</guid>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[towards-data-science]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Karan Malhotra]]></dc:creator>
            <pubDate>Wed, 22 Dec 2021 08:05:33 GMT</pubDate>
            <atom:updated>2021-12-22T08:05:33.871Z</atom:updated>
            <content:encoded><![CDATA[<h3>What are Language agnostic sentence embeddings?</h3><p>The vector representations of the sentences where the semantically similar sentences (even in different languages) are closer in the embedding space compared to the dissimilar ones[1]. This means that the vector representation of a given English sentence would be closer to the vector representation of its Hindi translation than the representation of any other dissimilar English/Hindi sentence.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6irKn16qDYdCROMlyKdq3A.gif" /><figcaption>Figure 1: Language Agnostic Representations Credits: <a href="https://github.com/yannvgn/laserembeddings/blob/master/laserembeddings.gif">laserembeddings</a></figcaption></figure><h3>Applications of language agnostic embeddings</h3><p>Such kinds of embeddings help in zero-shot or few-shot cross-lingual transfer (generally from a high resource to a low resource language), and can also be helpful in code-switching scenarios[1]. So basically one can train a classifier with English train data with such embeddings, and directly use it for another language like German, as these embeddings are trained in such a way that languages with limited resource benefits from the joint training over multiple languages.</p><p>Now let us discuss two of the recent techniques to get such language independent sentence embeddings.</p><h3>LASER [2]</h3><p>Language Agnostic Sentence Representations i.e. LASER was released by Facebook with support for 93 languages belonging to more than 30 different families and written in 28 different scripts [1]. A language agnostic Bi-LSTM encoder for building the sentence embeddings is coupled with a decoder and trained on a parallel corpus.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r4wmRDmDNYXbPWQT9v0fpQ.png" /><figcaption>Figure2: Architecture of the system used to learn embeddings. Credits: [1]</figcaption></figure><p>The sentence embeddings are generated by encoder and used in decoder at each time-step. There is no connection between encoder and decoder other than that through the sentence embedding, so the embedding capture full information of the input sequence. Also, no input language information is given at the encoder level which helps to get language independent representations.</p><h3>LaBSE [3]</h3><p>Language Agnostic BERT Sentence Embeddings i.e. LaBSE was released by Google AI and it has support for 109 languages. The multilingual BERT is adapted to produce language agnostic sentence embeddings. The model combines masked language model (MLM) and translation language model (TLM) pre-training with a translation ranking task using bi-directional dual encoders [3].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/616/0*YfHWN_qJzKv54g-x.png" /><figcaption>Figure2: Dual encoder architecture. Credits: [4]</figcaption></figure><p>The source and target are encoded using a shared transformer network, and the translation ranking task helps to get similar representations for the source and target text which have similar meaning.</p><p>Let’s try to analyze the embeddings discussed above for a few sentences and see how much language independence does they offer. Following are two English sentences with their several translations-</p><blockquote>Sent1_english = Can you please help me here?<br>Sent1_hindi = क्या आप कृपया यहाँ मेरी मदद कर सकते हैं?<br>Sent1_tamil = தயவுசெய்து எனக்கு இங்கு உதவ முடியுமா?<br>Sent1_marathi = तुम्ही मला इथे मदत करू शकता का?</blockquote><blockquote>Sent2_english = Sure, what help do you need?<br>Sent2_hindi = ज़रूर, आपको क्या मदद चाहिए?<br>Sent2_tamil = ிச்சயமாக, உங்களுக்கு என்ன உதவி தேவை?<br>Sent2_marathi = नक्कीच, तुम्हाला कोणती मदत हवी आहे?</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RUu99MKneM9TQmfviv_A5Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*giZrrtPGM7k2r6Z7YcOncw.png" /><figcaption>Figure3 : Pairwise cosine similarity between the sentence embeddings, left is for LaBSE and right is for LASER.</figcaption></figure><p>I loaded the LASER and LaBSE embeddings for the above sentences and plotted the similarity matrix by taking pairwise cosine similarity. We can see here that by using LaBSE embeddings we get more similarity among the sentence and its translations compared to LASER.</p><p>Overall both the pre-trained sentence embedding seems to represent a good amount of language independence as pair of sentences with same meaning and different languages are more similar when compared to pair of sentences with same language and different meaning. Check this <a href="https://github.com/karanMal/Notebooks/blob/main/language-agnostic-embeddingsz-details.ipynb">link</a> for python notebook used to plot Figure3.</p><p>So, If you want to train a machine learning model that should be language agnostic, do try these embeddings. Also, if you are looking for a cross-lingual transfer from a high resource to a low resource language for a task these embeddings could be quite helpful.</p><h3>References</h3><ol><li><a href="https://arxiv.org/pdf/1812.10464.pdf">Massively Multilingual Sentence Embeddings for Zero-Shot Cross-Lingual Transfer and Beyond by M Artetxe 2018</a>.</li><li><a href="https://github.com/facebookresearch/LASER">LASER Git Repo</a>.</li><li><a href="https://arxiv.org/abs/2007.01852">Language-agnostic BERT Sentence Embedding by F Feng 2020.</a></li><li><a href="https://ai.googleblog.com/2020/08/language-agnostic-bert-sentence.html">Language-Agnostic BERT Sentence Embeddings Google AI blog.</a></li><li><a href="https://huggingface.co/sentence-transformers/LaBSE">Sentence transformers — LaBSE</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=57445eba02a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Word Embeddings for Indian Languages]]></title>
            <link>https://medium.com/@kmkaran212/word-embeddings-for-indian-languages-d69ed933aecd?source=rss-3603b9553f67------2</link>
            <guid isPermaLink="false">https://medium.com/p/d69ed933aecd</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[towards-data-science]]></category>
            <dc:creator><![CDATA[Karan Malhotra]]></dc:creator>
            <pubDate>Sat, 20 Nov 2021 16:34:03 GMT</pubDate>
            <atom:updated>2021-11-20T16:34:03.114Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>NLP for Indian Languages</strong></h3><p>It is known that around 10% of the Indian population speak English [1], and the rest 90% speaks languages like Hindi, Marathi, Gujarati, Punjabi etc. So, the multilingual society in India would not be able to fully leverage the benefits of AI without the state of the art NLP systems for the regional languages.</p><p>In this article, I will discuss various resources of Word Embeddings specifically created for Indian languages. Before that, let’s check out what actually a word embedding is and some of its applications.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*51ULHBtW1f1ehrfE.jpg" /><figcaption>Figure 1: Different Languages Spoken in India. Credits <a href="https://commons.wikimedia.org/wiki/File:Indian-languages-map.jpg">Wikimedia</a></figcaption></figure><h3>Word Embeddings</h3><p><strong>Word embedding</strong> is a term used for the representation of words for text analysis, typically in the form of a real-valued vector that encodes the meaning of the word such that the words that are closer in the vector space are expected to be similar in meaning [2].</p><p>Nowadays it is common to use pre-trained word embeddings like <strong>word2vec, gloVe</strong> in solving several NLP problems rather than to train embeddings from scratch. Let’s now discuss what are the possible options if one is looking for such embeddings for Indian languages.</p><h3>fastText [3]</h3><p>fastText is a library for pre-trained word embeddings and text classification created by Facebook AI. The models were trained with words being represented as bag of character n-grams and the character n-grams have vectors associated to them. The words are then represented by sum of these vector representations [4]. fastText pre-trained word vectors are available for 294 languages trained on Wiki data and can be downloaded from <a href="https://fasttext.cc/docs/en/pretrained-vectors.html">here</a>. Below is a code snippet that shows loading fastText word embeddings for Hindi.</p><pre>import fasttext<br>import fasttext.util<br>ft = fasttext.load_model(&#39;wiki.hi.bin&#39;)<br>word = &quot;नृत्य&quot;<br>print(&quot;Embedding Shape is {}&quot;.format(ft.get_word_vector(word).shape))<br>print(&quot;Nearest Neighbors to {} are:&quot;.format(word))<br>ft.get_nearest_neighbors(word) </pre><blockquote>Embedding Shape is (300,)</blockquote><blockquote>Nearest Neighbors to नृत्य are:</blockquote><blockquote>[(0.8913929462432861, &#39;नृत्य।&#39;),<br> (0.8440190553665161, &#39;नृत्यगान&#39;),<br> (0.8374733924865723, &#39;<strong>नृत्यगीत</strong>&#39;),<br> (0.8336297869682312, &#39;नृत्यों&#39;),<br> (0.8265783190727234, &#39;नृत्यरत&#39;),<br> (0.7971948385238647, &#39;नृत्यकला&#39;),<br> (0.7879464626312256, &#39;नृत्त&#39;),<br> (0.7682990431785583, &#39;नृतक&#39;),<br> (0.7622954845428467, &#39;नृत्यरचना&#39;),<br> (0.7602956295013428, &#39;नृत्यग्राम&#39;)]</blockquote><p>The word “नृत्य” means dance in English, and the nearest words to it in the embedding space seems to be similar in meaning ( like नृत्यगीत means dance song ). Most of the Indian languages like Hindi, Marathi, Tamil, Telugu are supported by fastText.</p><h3>iNLTK [5]</h3><p>iNLTK is an open-source library consisting of various resources like pre-trained language models, word embeddings, sentence embeddings and a few more for 13 Indian languages [6]. The word embeddings are obtained from the embedding layer of the pre-trained language model. Below code snippet shows how to load the embeddings:</p><pre>from inltk.inltk import setup<br>setup(&#39;hi&#39;) #### Needed only once<br>from inltk.inltk import get_embedding_vectors<br>word = &quot;नृत्य&quot;<br>vectors = get_embedding_vectors(word, &#39;hi&#39;)<br>print(&quot;Embedding Shape is {}&quot;.format(vectors.shape))<br><br>from inltk.inltk import get_sentence_encoding<br>text =&quot;मैं भाग रहा हूँ&quot;<br>encoding = get_sentence_encoding(text, &#39;hi&#39;)<br>print(&quot;Sentence Embedding Shape is {}&quot;.format(encoding.shape))</pre><blockquote>Embedding Shape is (400,)<br><br>Sentence Embedding Shape is (400,)</blockquote><p>The sentence embeddings are obtained from encoder outputs of pre-trained language models and can be loaded in a similar way. As the embeddings are obtained from encoder outputs, these are contextual in nature. For more details, you can refer to this <a href="https://inltk.readthedocs.io/en/latest/api_docs.html#get-embedding-vectors">link</a>.</p><h3>IndicFT [7]</h3><p>These are the fastText models trained on IndicNLP [8] corpus and are available for 11 Indian languages. You will need to download the model binaries from <a href="https://indicnlp.ai4bharat.org/indicft/">here</a> and use them easily with the fastText library.</p><pre>import fasttext<br>import fasttext.util<br>ft = fasttext.load_model(&#39;indicnlp.ft.hi.300.bin&#39;)<br>word = &quot;नृत्य&quot;<br>print(&quot;Embedding Shape is {}&quot;.format(ft.get_word_vector(word).shape))<br>print(&quot;Nearest Neighbors to {} are:&quot;.format(word))<br>ft.get_nearest_neighbors(word)</pre><blockquote>Embedding Shape is (300,)<br>Nearest Neighbors to नृत्य are:</blockquote><blockquote>[(0.8393551111221313, &#39;नृत्यों&#39;),<br> (0.8289133906364441, &#39;लोकनृत्य&#39;),<br> (0.7881444096565247, &#39;लोकनृत्यों&#39;),<br> (0.7832040786743164, &#39;लोकनृत्य,&#39;),<br> (0.7764051556587219, &#39;नृत्य,&#39;),<br> (0.7572897672653198, &#39;नृत्य।&#39;),<br> (0.7543818354606628, &#39;नृत्य’&#39;),<br> (0.7490015625953674, &#39;नृत्यांगनाओं&#39;),<br> (0.7466280460357666, &#39;कत्थक&#39;),<br> (0.7412323355674744, &#39;गायन&#39;)]</blockquote><p>If we compare the nearest neighbors obtained from this technique to the default fastText one, then here various other words which don’t contain the string “नृत्य” but are related to it came as output, so it seems relationships other than meaning among words are better captured in IndicFT models. For more training details please refer to [6].</p><h3>MuRIL [9]</h3><p>MuRIL is a pre-trained BERT based language model released by Google and is trained on 17 Indian languages [10]. The training strategy is similar to that of multi-lingual BERT with few modifications. The model can be easily downloaded from <a href="https://huggingface.co/google/muril-base-cased">here</a> and can be loaded by a few lines with the HuggingFace library.</p><pre>path = &#39;google/muril-base-cased&#39;</pre><pre>## Loading the model<br>from transformers import AutoModel, AutoTokenizer<br>import torch<br>tokenizer = AutoTokenizer.from_pretrained(path)<br>model = AutoModel.from_pretrained(path,output_hidden_states=True)</pre><pre>## Embeddings <br>text =&quot;कोई अच्छी सी फिल्म लगायो&quot;<br>print(tokenizer.convert_ids_to_tokens(tokenizer.encode(text)))<br>input_encoded = tokenizer.encode_plus(text, return_tensors=&quot;pt&quot;)<br>with torch.no_grad():<br>         states = model(**input_encoded).hidden_states<br>output = torch.stack([states[i] for i in range(len(states))])<br>output = output.squeeze()<br>print(&quot;Output shape is {}&quot;.format(output.shape))</pre><blockquote>[&#39;[CLS]&#39;, &#39;कोई&#39;, &#39;अच्छी&#39;, &#39;सी&#39;, &#39;फिल्म&#39;, &#39;लगा&#39;, &#39;##यो&#39;, &#39;[SEP]&#39;]<br>Output shape is torch.Size([13, 8, 768])</blockquote><p>The first dimension of output represents the number of layers (12 layers + embedding layer), the second represents the number of tokens, and the third is the hidden size. The sentence embedding can be extracted by averaging over the layers and tokens ( <strong>usually the last four layers are considered,</strong> but one can take the average over all the layers as well ). The contextual word embeddings can be extracted by the sum over the corresponding token outputs (as input tokens here are sub-word units and not the words) with an average over the layers.</p><h3>IndicBERT [11]</h3><p>IndicBERT is released by AI4Bharat under <strong>IndicNLPSuite</strong> [8]. IndicBERT is a multi-lingual ALBERT based model pre-trained on 12 major Indian languages. The model can be easily downloaded from <a href="https://huggingface.co/ai4bharat/indic-bert">here</a> and can be loaded by a few lines with the HuggingFace library similarly as is done for the MuRIL model. The sentence and word embeddings can also be extracted using the above code by just changing the <em>path</em> variable from <em>‘google/muril-base-cased’</em> to ‘ai4bharat/indic-bert’.</p><h3>References</h3><ol><li><a href="https://en.wikipedia.org/wiki/Multilingualism_in_India">https://en.wikipedia.org/wiki/Multilingualism_in_India</a></li><li><a href="https://en.wikipedia.org/wiki/Word_embedding">https://en.wikipedia.org/wiki/Word_embedding</a></li><li><a href="https://fasttext.cc/docs/en/crawl-vectors.html">https://fasttext.cc/docs/en/crawl-vectors.html</a></li><li><a href="http://Enriching Word Vectors with Subword Information">Enriching Word Vectors with Subword Information</a></li><li><a href="https://inltk.readthedocs.io/en/latest/index.html">https://inltk.readthedocs.io/en/latest/index.html</a></li><li><a href="https://arxiv.org/pdf/2009.12534.pdf">iNLTK: Natural Language Toolkit for Indic Languages</a></li><li><a href="https://indicnlp.ai4bharat.org/indicft/">https://indicnlp.ai4bharat.org/indicft/</a></li><li><a href="https://indicnlp.ai4bharat.org/home/">https://indicnlp.ai4bharat.org/home/</a></li><li><a href="https://huggingface.co/google/muril-base-cased">https://huggingface.co/google/muril-base-cased</a></li><li><a href="https://arxiv.org/pdf/2103.10730.pdf">MuRIL: Multilingual Representations for Indian Languages</a></li><li><a href="https://indicnlp.ai4bharat.org/indic-bert/">https://indicnlp.ai4bharat.org/indic-bert/</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d69ed933aecd" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>