<?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 Manohar Sai Alapati on Medium]]></title>
        <description><![CDATA[Stories by Manohar Sai Alapati on Medium]]></description>
        <link>https://medium.com/@alapatimanoharsai12?source=rss-110a8ed9967d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*1mCohnPYRbhdY3fPwE9lgQ.jpeg</url>
            <title>Stories by Manohar Sai Alapati on Medium</title>
            <link>https://medium.com/@alapatimanoharsai12?source=rss-110a8ed9967d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 14:24:17 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@alapatimanoharsai12/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[One-Shot Signature Forgery Detection]]></title>
            <link>https://medium.com/@alapatimanoharsai12/one-shot-signature-forgery-detection-eb0ac523f2bb?source=rss-110a8ed9967d------2</link>
            <guid isPermaLink="false">https://medium.com/p/eb0ac523f2bb</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[cnn-model]]></category>
            <category><![CDATA[siamese-networks]]></category>
            <category><![CDATA[forgery-detection]]></category>
            <dc:creator><![CDATA[Manohar Sai Alapati]]></dc:creator>
            <pubDate>Tue, 29 Nov 2022 13:51:48 GMT</pubDate>
            <atom:updated>2022-12-09T08:46:05.185Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Signature forgery</strong> refers to the act of falsely replicating another person’s signature. Humans can identify, when a signature is forged based on very few experiences. But as the citizens of the Automation country, can we remove humans in the loop?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*cHZY_PT8TTbiwqUh.jpg" /><figcaption>source : <a href="https://www.knowledgecity.com/">https://www.knowledgecity.com/</a></figcaption></figure><h3>Introduction</h3><p>A number of characteristics can suggest to an examiner that a signature has been forged, mostly stemming from the forger focusing on accuracy rather than fluency. These include</p><ul><li>Shaky handwriting</li><li>Pen lifts</li><li>Signs of retouching</li><li>Letter proportions</li><li>Very close similarity between two or more signatures</li></ul><p>While these features makes absolute sense, capturing them would require multiple modalities that is ridiculously impossible to obtain. Modelling the relationship between the features is not going to be easy too. So, we aim to rely on only image information and the power of the modern Machine Learning systems to detect the forgery.</p><h3>Abstractly…</h3><p>The goal is to train a Machine Learning model that takes two images as an input, the original signature and the current signature that we want to test. The expectation from the model is a score that represents the confidence, learned by the model, that both the signatures belong to the same person.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/727/1*91EhkyDsPXvfDeisq2Yh_Q.png" /><figcaption>source : <a href="http://www.fortanix.com/">www.fortanix.com</a></figcaption></figure><p>To teach the model to identify the legitness of the signature pair, we use data that contains several samples of positive (original, original) pairs and several samples of negative (original, sampled | forged) pairs of signatures.</p><p>So, we our experiment does the following journey:</p><ol><li>Dataset</li><li>Model Building</li><li>Objective Formulation</li><li>Evaluation</li></ol><h3>Dataset</h3><p>The dataset consists of signature samples of different person. Each directory for the person consists of his/hers original signature samples and some forged samples.</p><pre>--train<br>        ---person-01<br>                     ---original<br>                                  ---001.jpg<br>                                  ---002.jpg<br>                                  ---003.jpg<br>                     ---forged<br>                                  ---001.jpg<br>                                  ---002.jpg<br>                                  ---003.jpg<br>        ---person-02<br>                      ---original<br>                                    ---<br>                                    ---<br>                                    ---<br>                                    ---<br>---test  <br>        ---<br>        ---<br>        ---</pre><p>We prepare the dataset for training and testing by aggregating the positive samples — pairing two original images from the same person and the negative samples — pairing two images from the original and forged category respectively.</p><pre>train_path = &#39;/path/to/data/&#39;<br>dataset = []<br>targets = []<br><br>real_count =0<br>forged_count = 0<br><br>persons = os.listdir(train_path)<br>for person in persons:<br>  path = os.path.join(train_path,person)<br>  real = os.path.join(path,&#39;Real/&#39;)<br>  real_files = os.listdir(real)<br>  fraud = os.path.join(path,&#39;Forged/&#39;)<br>  fraud_files = os.listdir(fraud)<br><br>  for j in range(len(real_files)):<br>        for k in range(len(real_files)):<br>            if j==k:<br>                continue<br>            real_count +=1<br>            img1 = load_img(os.path.join(real,real_files[j]))<br>            img2 = load_img(os.path.join(real,real_files[k]))<br>            dataset.append([img1,img2])<br>            targets.append(0.)<br>    <br>  for j in range(len(real_files)):<br>        for k in range(len(fraud_files)):<br>            if j==k:<br>                continue<br>            forged_count+=1<br>            img1 = load_img(os.path.join(real,real_files[j]))<br>            img2 = load_img(os.path.join(fraud,fraud_files[k]))<br>            dataset.append([img1,img2])<br>            targets.append(1.)</pre><p>We can also use pre-trained model to sample hard negatives. This is called hard-negative mining and improves the performance of the model by increasing its robustness. You can learn more about it <a href="https://arxiv.org/abs/2010.04592">here</a>.</p><h3>Modeling</h3><p>Siamese networks is one of the widely used Neural Network Architecture for Deep Metric Learning and Few-shot learning.</p><ul><li>It contains two identical branches, typically, modelled using a CNN, where each input is given a branch.</li><li>The objective of each branch is to produce an embedding for the given input image that semantically encodes all the required features (can be the above ones) required to perform the classification.</li><li>The final similarity score is produced by taking the cosine similarity or a normalised euclidean distance between the embeddings produced by the respective branches.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/845/0*f5cFxMdZ9MFWPTyD.png" /><figcaption>source : <a href="https://towardsdatascience.com/a-friendly-introduction-to-siamese-networks-85ab17522942">https://towardsdatascience.com</a></figcaption></figure><p>While training, rather than computing the similarity score, we pass the two embeddings into an objective function that drives the model to forcefully learn important distictions between the signatures.</p><pre>def Siamese_Model(input_shape=(width,height,3)):<br>  <br>  input_one = tf.keras.layers.Input(shape=input_shape)<br>  input_two = tf.keras.layers.Input(shape=input_shape)<br><br>  cnn = tf.keras.models.Sequential()<br>  cnn.add(tf.keras.layers.Conv2D(32,(3,3),activation=&#39;relu&#39;,padding=&#39;same&#39;))<br>  cnn.add(tf.keras.layers.AveragePooling2D((2,2)))<br>  cnn.add(tf.keras.layers.Conv2D(64,(3,3),activation=&#39;relu&#39;,padding=&#39;same&#39;))<br>  cnn.add(tf.keras.layers.AveragePooling2D((2,2)))<br>  cnn.add(tf.keras.layers.Conv2D(64,(3,3),activation=&#39;relu&#39;,padding=&#39;same&#39;))<br>  cnn.add(tf.keras.layers.AveragePooling2D((2,2)))<br>  cnn.add(tf.keras.layers.Flatten())<br>  cnn.add(tf.keras.layers.Dropout(0.3))<br>  cnn.add(tf.keras.layers.Dense(128))<br><br>  distance_layer = tf.keras.layers.Lambda(lambda tensor: abs(tensor[0]-tensor[1]))<br>  out1 = cnn(input_one)<br>  out2 = cnn(input_two)<br>  l1_distance = distance_layer([out1,out2])<br>  final_out = tf.keras.layers.Dense(1,activation=&#39;sigmoid&#39;)(l1_distance)<br><br>  model = tf.keras.Model([input_one,input_two],final_out)<br>  return model</pre><p>You can clearly see that the two branches of the network are implemented using a single network with serial forwarding of both the inputs.</p><h3>Objective Choice</h3><p>Siamese networks enjoy the boon of the availability of multiple loss functions. But, selecting the right one can at times be critical and effortful. Some of the important objective functions include:</p><h4>Triplet Loss</h4><p>Triplet loss deals with three different embeddings and try to maximize or minimise the distance between the two pairs of embeddings based on the classes of the pairs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*WbomUmOJqsJKo2Mm.png" /><figcaption>source : stackoverflow.com</figcaption></figure><h4>Contrastive/Discriminative Loss</h4><p>Contrastive Loss is similar to the pair distinction, where the distance between the embeddings is penalized or encouraged depedning on the class of the input pair.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*Zx9JiegMhpnN4LJu" /><figcaption>source : pyimageseach.com</figcaption></figure><h4>Quadruplet Loss</h4><p>Quadruplet loss is similar to triplet loss except that, now we have four entities to aggregate losses from. Specifically, in addition to the triplet loss, we sample a new negative object that is different from all the three objects. In our case, it is a signature sample from a different person.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Ie4_5rA9a_wfuNhH.png" /><figcaption>source : synrise.com</figcaption></figure><p>You can learn about different objectives in depth <a href="https://towardsdatascience.com/how-to-choose-your-loss-when-designing-a-siamese-neural-net-contrastive-triplet-or-quadruplet-ecba11944ec">here</a>.</p><h3>Evaluation</h3><p>The evaluation of the task can be done in a similar fashion as that of a classification task. So the classic metrics used are accuracy, f1-score. If the test samples are less, we can perform cross-validation as required with the appropriate number of splits.</p><p><strong>Accuracy</strong> : 0.961 and <strong>F1-Score</strong> : 0.953</p><h3>Key Points</h3><ul><li>There is memory limit in dealing with images especially in Siamese networks.</li><li>Training is done on 30 samples(true and fraud) from each person in the train set.</li><li>The batch Size is set to automatic by default due to the limit on memory.</li><li>If more memory is available, the overall accuracy can be greatly improved by taking sampling ore data pairs from the dataset.</li></ul><p>The code notebook is present at <a href="https://github.com/ManoharSai2000/DeepForge">https://github.com/ManoharSai2000/DeepForge</a></p><h3>References</h3><ul><li><a href="https://arxiv.org/abs/2010.04592">Contrastive Learning with Hard Negative Samples</a></li><li><a href="https://paperswithcode.com/paper/siamese-neural-networks-for-one-shot-image">Papers with Code - Siamese neural networks for one-shot image recognition</a></li><li><a href="https://towardsdatascience.com/how-to-choose-your-loss-when-designing-a-siamese-neural-net-contrastive-triplet-or-quadruplet-ecba11944ec">How to choose your loss when designing a Siamese Neural Net : Contrastive, Triplet or Quadruplet ?</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eb0ac523f2bb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Bypassing Anti-Malware agents using Generative Adversarial Networks]]></title>
            <link>https://medium.com/@alapatimanoharsai12/bypassing-malware-detection-systems-using-generative-adversarial-networks-7e509e95c273?source=rss-110a8ed9967d------2</link>
            <guid isPermaLink="false">https://medium.com/p/7e509e95c273</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[tensorflow]]></category>
            <category><![CDATA[gans]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[neural-networks]]></category>
            <dc:creator><![CDATA[Manohar Sai Alapati]]></dc:creator>
            <pubDate>Thu, 29 Sep 2022 12:08:50 GMT</pubDate>
            <atom:updated>2022-09-30T06:34:33.082Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>One of the most critical applications of Machine Learning is in the field of cyber security. A lot of malware detection systems that are rule based, now rely on Machine Learning to detect and isolate malware apps. While such systems are posed to be accurate, the question of robustness has no proper answer. In that line of thought, Adversarial Machine Learning — a study of attacks on machine learning algorithms, and of the defenses against such attacks, has gained a lot of attention recently.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*XLeEvM3sBGW3v797.jpg" /><figcaption>image source : wired.com</figcaption></figure><p>While the intent of such attacks is always destructive, training the models on the simulation data of the attacks not only allows us to re-think the possible modes of failure but also increase the robustness of our models. Adversarial Learning can be found in many other domains including Computer Vision, Natural Language Understanding , etc.</p><h3>Application</h3><p>The primary goal of this article is to first fool a remote malware detection system by identifying its deficiencies over different distributions of input features and then if given access, use this data to re-train the model to make it more powerful and robust. An important thing to keep in mind is that the anti-malware system we typically consider is a black-box model — we do not have any information on the model like parameters, architecture, its family, etc. But we do have the classification labels of the data with respect to the model.</p><p>For the scope of this blog, we consider Google’s PlayStore — a platform containing billions of apps with high possibility of malware applications hiding around. As a defensive move, PlayStore has a malware detection system to identify such harmful apps that are uploaded to the platform. It tags the apps as harmful based on its features like the read/write permissions it require, different API calls it makes at install and runtimes, etc. Most of these attributes are binary or categorical in nature that indicates the binary requirement of a permission or a API call that is needed by the app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/259/0*hc2ifyTdNd9gLH_h" /><figcaption>image source : latesthackingnews.com</figcaption></figure><p>So ultimately, we need a model/functions that takes in the features of the malware app and modifies the features to make it undetectable by the black box detector. We choose <a href="https://arxiv.org/abs/1406.2661"><strong>GAN</strong></a><strong>(Generative Adversarial Networks)</strong> for this task.</p><h3>Why GAN?</h3><p>GAN’s are well known in the field of image generation for their ability to produce high quality images. They first learn the approximate distribution of the data through an adversarial game play and then generate samples from that distribution.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*rhKtWopJUkDAzX_-.png" /><figcaption>source : <a href="https://lilianweng.github.io/">lilianweng.github.io</a></figcaption></figure><p>Our task can be precisely viewed as a simple alteration of the image generation task, where instead of generating images, we generate app’s features and instead of the need for images to be similar to real ones, we need the features to be similar to that of the features of a non-malware app. One can see the pointwise correspondence between the two tasks. Moreover, GANs have matured over time and offer the flexibility to use different architectures to approximate different distributions without having any strong assumptions. So, unlike other likelihood based methods like VAE or Diffusion models, we can generalize a GAN to our application without thinking much on the change in dynamics and implementation.</p><h3>Dataset</h3><p>The dataset for the task can be downloaded from the this <a href="https://data.mendeley.com/datasets/b4mxg7ydb7/3">link</a>. In overview, the dataset contains features of more than half a million android apps hosted on various channels with around 160 categorical features extracted at the time of installation and execution. There is also a label malware or benign that is tagged with each sample as identified by the black box malware detection agent.</p><p>So, each sample can be seen as vector of 0’s and 1’s [0 1 0 0 0 1 1 …. 1 0] where each feature representing a presence or requirement of a specific attribute.</p><h3>Modeling</h3><ul><li>The strategy is to train a generator to take in the features of the malware app that is to be disguised, and output modified features of the app that is ideally recognized as benign by the black box system.</li><li>To do this we need a loss signal that guides the generator to produce the appropriate output. This is achieved by a discriminator that separately learns the true malware nature of the data while simultaneously teaching the discriminator on how to generate convincing benign samples.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/499/1*LiyikCkoG9vN7ypV_Lw0lQ.png" /><figcaption>Architecture of the Adversarial System (source : <a href="https://arxiv.org/pdf/1702.05983.pdf">https://arxiv.org/pdf/1702.05983.pdf</a>)</figcaption></figure><ul><li>Specifically, we take alternate turns where</li></ul><ol><li>In one turn, we train the discriminator on malware and benign samples to correctly classify them.</li><li>On the other turn, we input the malware app’s features to the generator along with some noise resulting in a modified feature set which is then piped to the discriminator to classify. The discriminator is freezed and the labels are inverted for loss calculation as done in the standard GAN training(for details, look <a href="https://arxiv.org/abs/1406.2661">here</a>).</li></ol><p>The end result is a trained generator that can produce a set of features for a given malware app that fools the trained discriminator and hopefully, the black box detector to a decent extent.</p><h3>Code</h3><p>The entire system is implemented in Tensorflow, Keras framework.</p><blockquote>Discriminator :</blockquote><pre><strong>def</strong> discriminator():<br>    <br>    input <strong>=</strong> tf<strong>.</strong>keras<strong>.</strong>layers<strong>.</strong>Input(shape<strong>=</strong>(no_features))<br>    d1 <strong>=</strong> tf<strong>.</strong>keras<strong>.</strong>layers<strong>.</strong>Dense(256)(input)<br>    output <strong>=</strong> tf<strong>.</strong>keras<strong>.</strong>layers<strong>.</strong>Dense(1,&#39;sigmoid&#39;)(d1)<br>    <br>    model <strong>=</strong> tf<strong>.</strong>keras<strong>.</strong>Model(input,output)<br>    <strong>return</strong> model</pre><blockquote>Generator :</blockquote><pre><strong>def</strong> generator():<br>    <br>    input <strong>=</strong> tf<strong>.</strong>keras<strong>.</strong>layers<strong>.</strong>Input(shape<strong>=</strong>(no_features))<br>    noise <strong>=</strong> tf<strong>.</strong>keras<strong>.</strong>layers<strong>.</strong>Input(shape<strong>=</strong>(noise_dim))<br>    full_input <strong>=</strong> tf<strong>.</strong>keras<strong>.</strong>layers<strong>.</strong>Concatenate(axis<strong>=</strong>1)([input,noise])<br>    d1 <strong>=</strong> tf<strong>.</strong>keras<strong>.</strong>layers<strong>.</strong>Dense(512)(full_input)<br>    d2 <strong>=</strong> tf<strong>.</strong>keras<strong>.</strong>layers<strong>.</strong>Dense(no_features,activation<strong>=</strong>&#39;sigmoid&#39;)(d1)<br>    # To preserve the features the malware app needs<br>    output <strong>=</strong> tf<strong>.</strong>keras<strong>.</strong>layers<strong>.</strong>Maximum()([input,d2])<br>    <br>    model <strong>=</strong> tf<strong>.</strong>keras<strong>.</strong>Model([input,noise],output)<br>    <strong>return</strong> model</pre><blockquote>Training</blockquote><pre><strong>def</strong> train_step():<br>    <br>        idx <strong>=</strong> np<strong>.</strong>random<strong>.</strong>randint(0, fin_mal<strong>.</strong>shape[0], batch_size)<br>        mal_samples <strong>=</strong> fin_mal[idx]<br>        noise <strong>=</strong> np<strong>.</strong>random<strong>.</strong>uniform(0, 1, (batch_size, noise_dim))<br>    <br>        idx <strong>=</strong> np<strong>.</strong>random<strong>.</strong>randint(0, fin_benign<strong>.</strong>shape[0], batch_size)<br>        ben_samples <strong>=</strong> fin_benign[idx]<br>        ben_labels <strong>=</strong> np<strong>.</strong>zeros((len(ben_samples),1))<br>    <br>        gen_samples <strong>=</strong> gen<strong>.</strong>predict([mal_samples, noise])<br>        mal_labels <strong>=</strong> clf<strong>.</strong>predict(np<strong>.</strong>ones(gen_samples<strong>.</strong>shape)<strong>*</strong>(gen_samples <strong>&gt;</strong> 0.5))<br>    <br>        d_loss_real <strong>=</strong> disc<strong>.</strong>train_on_batch(gen_samples, mal_labels)<br>        d_loss_fake <strong>=</strong> disc<strong>.</strong>train_on_batch(ben_samples, ben_labels)<br>        d_loss <strong>=</strong> (np<strong>.</strong>mean(d_loss_real)<strong>+</strong>np<strong>.</strong>mean(d_loss_fake))<strong>/</strong>2<br>    <br>        idx <strong>=</strong> np<strong>.</strong>random<strong>.</strong>randint(0, fin_mal<strong>.</strong>shape[0], batch_size)<br>        mal_samples <strong>=</strong> fin_mal[idx]<br>        noise <strong>=</strong> np<strong>.</strong>random<strong>.</strong>uniform(0, 1, (batch_size, noise_dim))<br><br>        g_loss <strong>=</strong> gan<strong>.</strong>train_on_batch([mal_samples, noise], np<strong>.</strong>zeros((batch_size, 1)))<br>    <br>        noise <strong>=</strong> np<strong>.</strong>random<strong>.</strong>uniform(0, 1, (fin_mal<strong>.</strong>shape[0], noise_dim))<br>        gen_samples <strong>=</strong> gen<strong>.</strong>predict([fin_mal, noise])<br>        TPR <strong>=</strong> clf<strong>.</strong>score(np<strong>.</strong>ones(gen_samples<strong>.</strong>shape) <strong>*</strong> (gen_samples <strong>&gt;</strong> 0.5), np<strong>.</strong>ones((len(fin_mal),1)))<br>    <br>        <strong>return</strong> d_loss,g_loss,TPR</pre><p>The full code can be found <a href="https://github.com/ManoharSai2000/GANdroid"><strong>here</strong></a>.</p><h3>Metrics</h3><p>We use the <strong>True Positive Rate — TPR </strong>as the metric, the percentage of samples, the black box software correctly identifies (should be low in our case).</p><p>If the actual anti-malware system is inaccessible to calculate the TPR, then we can use a strong classifier like RandomForests or XGBoost to model the black box detector and see the true positive rate after each epoch. Specifically, we pre-train a classifier on the entire data and assume that this classifier is the actual black box detector. This is then used as a substitute to evaluate the generator’s ability to fool the actual black box detector.</p><p>The TPR I have achieved after training is 22%, meaning the generator is able to fool the system 78% of the time.</p><h3>Conclusion</h3><p>If allowed access, we can re-train the original detector model on the data generated by the generator to make it better. This kind of adversarial modeling of the data provides a way to make the models confident and trustable. While there are different data and model centric ways to achieve model robustness and generalizability, adversarial learning is one of the unique and powerful ways available.</p><h3>References</h3><p>Dataset : <a href="https://data.mendeley.com/datasets/b4mxg7ydb7/3">https://data.mendeley.com/datasets/b4mxg7ydb7/3</a></p><p>Paper : <a href="https://arxiv.org/abs/1702.05983">https://arxiv.org/abs/1702.05983</a></p><p>GAN : <a href="https://arxiv.org/abs/1406.2661">https://arxiv.org/abs/1406.2661</a></p><p>Code Ideas : <a href="https://github.com/yanminglai/Malware-GAN">https://github.com/yanminglai/Malware-GAN</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7e509e95c273" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Item2Vec with Metadata: incorporating side-information in item embeddings]]></title>
            <link>https://medium.com/swiggy-bytes/item2vec-with-metadata-incorporating-side-information-in-item-embeddings-167fb8d3f404?source=rss-110a8ed9967d------2</link>
            <guid isPermaLink="false">https://medium.com/p/167fb8d3f404</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[word-embeddings]]></category>
            <category><![CDATA[recommender-systems]]></category>
            <category><![CDATA[multitask-learning]]></category>
            <category><![CDATA[swiggy-data-science]]></category>
            <dc:creator><![CDATA[Manohar Sai Alapati]]></dc:creator>
            <pubDate>Fri, 24 Dec 2021 04:59:05 GMT</pubDate>
            <atom:updated>2021-12-24T04:59:05.067Z</atom:updated>
            <content:encoded><![CDATA[<p>With inputs from <a href="https://www.linkedin.com/in/rutvik-vijjali-338410155/">Rutvik Vijjali</a>, <a href="https://www.linkedin.com/in/mithuntm/">Mithun TM</a></p><h3>Introduction</h3><p>Most people have a sense of knowing the similarity between items, i.e, given an item say “Veg Burger’’, we can relate it to a list of similar items, starting from burgers, to pizzas, fries and so on. Can machines do this? Possibly. This can be achieved by learning a distributed representation for each item which forms the basis of many of the recommendation systems we see and use. Learning these embeddings is not just limited to recommendation systems but they are useful for several other tasks. For instance, at Swiggy, the item embeddings are used to derive restaurant embeddings and customer embeddings as both restaurants and customers are defined by the items they serve and order respectively. Combo meals rankings and other advanced applications make use of item embeddings as inputs. Therefore ensuring quality and reliability of the embeddings is critical and must.</p><h3>Vanilla Item2Vec</h3><p>Among the different candidate approaches including simple word comparisons, statistical methods which consider co-occurring frequencies and matrix factorisation techniques, word2vec was chosen because of its performance and flexibility. Word2vec can handle large vocabulary sizes efficiently compared to the other approaches. For learning the item embeddings using word2vec, we can consider past purchase sessions as sentences and the item names in those purchases as words. This formulation is based on the foundational intuition of learning word embeddings from sentences which is “You shall know a word by the company it keeps”. The same intuition applied to a purchase dataset would be “Similar items are purchased along with their similar counterparts”.</p><p>The main thing to note here is that unlike other word2vec formulations which have their object relationships to be learned as atomic entities, here we have our item names as a combination of words which are atomic in themselves. If we ignore this and consider each item name as a unique entity, we will be losing a lot of similarity information in the input itself. For example, “Chicken Biryani” and “Fish Biryani” when considered as two unique atomic entities, we are excluding the fact that these two items belong to the “Biryani” category. So the idea is to add to the sequence, the individual words of the item name and the item name itself as entities. So the item name “Chicken Biryani” is added to the sentence as [“chicken”, “biryani”, “chicken biryani”] and the window length is taken to be long enough to capture this relation.</p><p>So, a sentence in word2vec domain for order [“mexican pizza”, “chicken burger”] looks like [“mexican”,“pizza”, “mexican pizza”, “chicken”, “burger”, “chicken burger”]. As a vanilla model, previous three month’s’ purchases were used and a skip-gram model is trained with window length of 5 with the above mentioned configuration.</p><h3>That’s not it!</h3><p>Though this approach works reasonably well in constructing the item embeddings, it has several limitations.</p><ol><li>It does not address the <strong>cold start</strong> problem. When a new item which is not present in the training set is given, we are not able to construct an embedding for it, at least a reasonable one.</li><li>An item is not alone defined by its name and its neighbouring items in an order. For instance two same items from different restaurants can vary a lot in price which is an important consideration for recommendation. Another example is that a simple sandwich can be from a veg or non-veg category.</li></ol><p>These limitations are crucial in application and when removed, can greatly affect the quality of embeddings. So, this problem is handed over to “Meta Item2Vec”.</p><h3>Evaluation</h3><p>Before moving to our topic of interest, we need to evaluate our embeddings on some reliable quantitative metric to see the effectiveness of each approach. For this, we first roll our item embeddings to restaurant level by averaging across the past ordered items in that restaurant say for 84 days. From this, we get restaurant embeddings and then evaluate these embeddings on two metrics:</p><ol><li>NDCG (<strong>Normalized Discounted Cumulative Gain). </strong>You can learn more about it on<a href="https://towardsdatascience.com/evaluate-your-recommendation-engine-using-ndcg-759a851452d1"> this</a> post. In short, it evaluates the embeddings based on the ranking quality which in this case is the ranking of restaurants. We have the data of restaurants the users searched for and the restaurants they clicked. We want our embedding of the query restaurant to be as close as the clicked restaurant, i.e the clicked restaurant embedding similarity with the query restaurant embedding should be high as compared to any other restaurant.</li><li>MRR <strong>(Mean Reciprocal Rank)</strong>. MRR is almost similar to NDCG as it is evaluated on ranking but the way of calculating it slightly differs. It takes average of the reciprocal of the ranks given to the clicked instances. You can learn more about it<a href="https://towardsdatascience.com/ranking-evaluation-metrics-for-recommender-systems-263d0a66ef54"> here</a>.</li></ol><p>Both these metrics range from 0 to 1 and more the score on these metrics, the better the embeddings. The scores for the above vanilla model for NDCG and MRR metrics are respectively 0.609 and 0.399.</p><h3>MetaItem2Vec</h3><p>Metadata in general refers to data about data. In our context, it means the features/information about the item apart from its name which can include, price, vegness, dish category etc. Fusing this information into the item embeddings can greatly enhance their quality and also solve the cold start problem, since these features provide a certain default point for creating embeddings of new items.</p><p>There is some existing work regarding the inclusion of meta information — <a href="https://arxiv.org/abs/1607.07326">meta prod2vec</a>. The work focuses on including the meta features at the train time but it does not explicitly integrate the information into the embedding i.e while inferencing to get the final item embeddings, the method does not take meta features into account. Unlike this method, meta item2vec directly integrates meta information into the embeddings.</p><h3>Framework and data</h3><p>The first step to take in migrating to the meta item2vec embeddings is to select a set of relevant features to integrate into the embeddings. Throughout our experiments, we take the price and vegness of an item as its meta features. The price of each item is taken as a categorical variable representing Low, Medium and High prices. This is done by creating 3 quantiles from the price of all items belonging to a category and then taking the first quantile as LowP, next as MediumP and the rest as HighP. The “vegness” is also a categorical variable with its classes belonging to Veg, Non-Veg and Egg categories.</p><p>The next step is to formulate the task to train the network. We use a bigram model with multitasking. Specifically, given a pair of items, we propose the task as predicting the later item’s name and its meta features given the former item name and its meta features. This implies that we use a bigram model and the network is a multi-tasking network.</p><p>The dataset pairs are generated by taking the past one month’s orders and forming item pairs by flattening the order data. A final sample of data contains the pair of items i.e, their names, price categories and vegness categories. The total dataset size is approximately nine million which means we have nine million pairs of items and their metadata.</p><p>There are several details and design choices to take into consideration before training the model. We will slowly evolve our modeling from a simple approach to a decent one.</p><h3>Evolution — 1</h3><p>This is the first migration step towards meta item2vec. The main design choice to make here is how do we handle the entities i.e, should we consider the entire item name as a unique entity or consider them at a word level. The former approach increases the tokens size and also explicitly removes word level relationships between different items of the same dish family. So, we carefully model the later approach.</p><p>Now, we have to decide how we model the inputs and outputs of the network. The meta information is easy to deal with. At the input side, they are represented as one hot encoded vector and at the output side, they are predicted as multinomial distribution via softmax. The input item name is processed using the embedding layer followed by an averaging layer which finally gives out a fixed dimensional embedding representing the input name. The critical issue is the prediction of the next item’s name which is of variable length. To start simple, we sample a single word out of the output’s name and make the model predict this word via softmax.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/702/0*xIfkh5TLD-4gRd__" /><figcaption>Architecture of Model 1</figcaption></figure><p>As you can see, the network has three tasks, ergo a multitasking network. The main challenge in the multitasking network is that one task dominates the other resulting in sub-optimality in the overall optimization. The most direct way of solving this is to scale/balance the losses which in turn scales the gradients across the network. The best work in this direction is shown by the<a href="https://arxiv.org/abs/1705.07115"> Multitasking with Bayesian Task uncertainty.</a> This approach weighs the losses based on the variance in the loss of the task. Intuitively, a difficult task or a loss with high variance is allowed to learn slowly by decreasing its weight and an easy task or a task with less variance has its weighting increased, so that it’s learning is accelerated.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/311/0*zuree4HBVpHpd8l0" /><figcaption>Structure of the Multitasking loss function with two tasks</figcaption></figure><p>L1 and L2 are losses from two loss functions conditioned on parameters W. These loss functions can hold outputs from Gaussian Distribution for a regression task, and multinomial distribution for a classification task. If the loss is consistently high, implying that the outputs are deviating more from their mean (ground truth) values, the variance of the represented distribution for that output is high. The trainable parameters represented by the (1/σ^2) balance this variance which from the model’s perspective reduces the learning rate for that task.</p><p>This gives us a base model that can integrate meta information into the embeddings. At inference, we pass the item’s name, price and vegness into the model and get the embedding from the dense layer.</p><p>The scores obtained on NDCG and MRR are respectively 0.587and 0.376. It can be observed that the meta model performs worse than the vanilla one. For one thing, the meta model is hand constructed and is trained on only one month’s data due to time and compute constraints while the vanilla model is trained on three month’s data beforehand. Also, the interactions between the entities in the vanilla model are dense as the window size is high and we consider the flattened version of the item name enabling it to learn much denser representations. No to mention that this model has several flaws. The main thing is that we are only predicting one word of the output item’s name. To make the task standard, we have to find a way to predict the entire name of the item. This brings us to evolution 2.</p><h3>Evolution — 2</h3><p>In this evolution, we modify the model to predict the entire name of the output item by the task of multi-label classification. Unlike multi-class classification where we output a multinomial distribution that supports a single class output, in multi-label classification, the prediction of each class is treated as an independent binomial distribution. So the active classes will be the words which are present in the name of the item and are set to 1 and the remaining classes are set to zero.</p><p>One of the challenges is that, out of the large vocabulary of words, only few (upto three of them on average) are 1’s and remaining are 0’s. This leads to a high class imbalance which intuitively can hack the network into predicting zeros for every class irrespective of the input. To solve this problem, we deploy the class weighting strategy where we weigh the classes set to 1 with the ratio of the number of negative classes to the number of positive classes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/702/0*1CBDngzEOYj9KPJG" /><figcaption>Architecture of Model 2 (the difference is in the task of the Item name prediction)</figcaption></figure><p>All the other configurations are set constant as in the previous evolution such as using the dynamic weighting, averaging, etc. The scores on the NDCG and MRR metrics for this method are 0.593 and 0.381 respectively. Though this is an improvement over the previous evolution, still the vanilla model performs better. So, now we go on to tackle the problem of lacking the dense interactions in our methods as opposed to the vanilla approach.</p><h3>Evolution — 3</h3><p>In the vanilla approach, we are flattening the item name at a word level and using a skip-gram model with window length of 5. This enables more interactions between the word entities and as a result the embeddings relate more with each other. This can be called dense interactions in the network. There is no such opportunity in our architecture as the same idea is not applicable to the meta features.</p><p>So, we solve this issue using a Sub-word Tokenizer specifically Word Piece Tokenizer. You can read more about this<a href="https://towardsdatascience.com/wordpiece-subword-based-tokenization-algorithm-1fbd14394ed7"> here</a>. Basically, the word piece tokenizer splits up rare words and retains the frequently occurring words as they were.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/744/0*vqf1Jv4WUBWgNuWZ" /><figcaption>Architecture of Model -3 (Difference comes in the addition of Word Piece Tokenization Module)</figcaption></figure><p>This has a few advantages:</p><ol><li>Reduce vocabulary Size</li><li>Better handling of OOV’s.</li><li>More interactions of the subwords, which is relatively better than words.</li></ol><p>This along with multi-label classification solves the problem of the dense interactions to a great extent. The NDCG and MRR scores for this evolution are 0.603 and 0.392 which is a great improvement.</p><h3>Evolution — 4</h3><p>To be fair, the vanilla model has bias in terms of data used and the dense interactions gained due to skip-grams formulation giving it an upper hand. We also want to prove that the meta information is valuable but due to less data and lack of dense relations, the scores are little less than the vanilla model.</p><p>The best way to increase the performance of these embeddings and at the same time visualize the value of the meta information is using ensembling. While the meta embeddings have the advantage of having meta information, the vanilla embeddings are trained on more dense signals. Combining them by bagging, specifically averaging the embeddings should give the best of both worlds.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/281/0*HokhEJlBb9p36Nb7" /><figcaption>Architecture of Model 4 — Ensembling by Bagging</figcaption></figure><p>The NDCG and the MRR scores for this ensemble approach are 0.6167 and 0.4086. This approach performs better than the vanilla approach on the same subset of data, which means that the metadata played a role in improving the embeddings.</p><h3>Conclusion</h3><p>The evaluation metrics for different approaches are:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xZT3CGJQEpeTWoTD" /><figcaption>Metrics for each of the approaches</figcaption></figure><p>In conclusion, the meta information for any product or entity is important because customers consider their similarity not only on the product itself but on various other factors like price, brand, etc. We expect the recommendation systems to capture the same notion of similarity which is dependent on all these factors. It is also important to note that an embedding of a given size can hold limited information before it forgets/misses other relationships when there are a lot of meta features. Another approach we can think of is using a sequence to sequence architecture to take input names and get the other item’s name as output. LSTMs were tried with different settings but failed as these layers hindered the direct learning of the embeddings. So, adding continuous or other time series features to these architectures is still challenging.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=167fb8d3f404" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swiggy-bytes/item2vec-with-metadata-incorporating-side-information-in-item-embeddings-167fb8d3f404">Item2Vec with Metadata: incorporating side-information in item embeddings</a> was originally published in <a href="https://medium.com/swiggy-bytes">Swiggy Bytes — Tech Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>