<?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 Aakanksha NS on Medium]]></title>
        <description><![CDATA[Stories by Aakanksha NS on Medium]]></description>
        <link>https://medium.com/@aakanksha.ns?source=rss-f30e60797a83------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*I2dp-wiFiD1M16yEJ-ixBA.jpeg</url>
            <title>Stories by Aakanksha NS on Medium</title>
            <link>https://medium.com/@aakanksha.ns?source=rss-f30e60797a83------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 16:33:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@aakanksha.ns/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[Machine Learning and Data Science Resources]]></title>
            <link>https://medium.com/@aakanksha.ns/machine-learning-and-data-science-resources-5d2860b5c8d3?source=rss-f30e60797a83------2</link>
            <guid isPermaLink="false">https://medium.com/p/5d2860b5c8d3</guid>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[interview]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Aakanksha NS]]></dc:creator>
            <pubDate>Sun, 07 Mar 2021 05:01:08 GMT</pubDate>
            <atom:updated>2021-03-07T05:07:26.829Z</atom:updated>
            <content:encoded><![CDATA[<p>A collection of data science related resources that helped me with my journey so far</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5rXejhcKLorK7JiXnQH-tg.jpeg" /><figcaption><a href="https://unsplash.com/photos/5fNmWej4tAA">Source</a></figcaption></figure><p>I transitioned into the field of data science from software development about 1.5 years ago. In this post, I aim to share a list of resources that I personally found helpful in my journey so far. If you’re someone who is just getting started, preparing for interviews, or looking for new material, I hope you find this useful. I’ve divided these resources into groups.</p><h3>Interview Preparation</h3><p>There’s a wide variety of roles available in this field — Data Scientist, Product Analyst, Data Analyst, Data Engineer, Machine Learning Engineer, Computer Vision Engineer etc., and each role has specific focus areas. However, here’s a general list of topics that I found to be covered to some extent in each role:</p><h4>Machine learning and Deep Learning Fundamentals</h4><ol><li>I found this great repo covering most topics related to Machine learning and Deep learning that might be relevant for interviews.</li></ol><p><a href="https://github.com/khangich/machine-learning-interview">GitHub - khangich/machine-learning-interview: Machine Learning Interviews from FAANG, Snapchat, LinkedIn. I have offers from Snapchat, Coupang, Stitchfix etc. Blog: mlengineer.io.</a></p><p>2. This is a comprehensive overview of different gradient descent algorithms and their evolution over the years. You can also check out more blogs from the author here: <a href="https://ruder.io/">https://ruder.io/</a></p><p><a href="https://ruder.io/optimizing-gradient-descent/">An overview of gradient descent optimization algorithms</a></p><h4>Programming</h4><ol><li><a href="https://leetcode.com/">Leetcode</a> is a great platform for coding practice and has curated lists of programming questions for a wide variety of topics. However, it can be quite overwhelming to go through each of them, so here’s a list of Leetcode questions I found covering most common patterns:</li></ol><p><a href="https://seanprashad.com/leetcode-patterns/">Leetcode Patterns</a></p><p>2. If you’re looking to really understand algorithm fundamentals and complexity analysis, I highly recommend reading this book I was introduced to in the first programming course I ever took in my undergrad. I’ve gone through the main sections (Sorting, Searching, Dynamic programming, Complexity Analysis, and Graph-based algorithms) multiple times.</p><p><a href="https://www.amazon.com/Introduction-Algorithms-3rd-MIT-Press/dp/0262033844">Introduction to Algorithms, 3rd Edition (Mit Press)</a></p><p>3. My <a href="https://www.linkedin.com/in/aakashns/">brother</a> has been a huge source of motivation and guidance to me. He also happens to be a data science educator and provides a mentorship program, check out his courses to learn more:</p><p><a href="https://www.jovian.ai/">Jovian: Learn Data Science &amp; Machine Learning Online | Courses, Projects &amp; Tutorials</a></p><p>If you’re looking for guided interview preparation with one-on-one attention for the Data Analyst role, you could check out this Bootcamp:</p><p><a href="https://www.jovian.ai/data-analyst-bootcamp">Part-Time Data Analyst Certification Bootcamp | Jovian</a></p><h4>SQL</h4><p><a href="https://sqlzoo.net/">SQLZOO</a></p><h4>Pandas</h4><p><a href="https://jovian.ai/learn/data-analysis-with-python-zero-to-pandas">Data Analysis with Python: Zero to Pandas | Jovian</a></p><h4>A/B Testing</h4><p><a href="https://hbr.org/2017/06/a-refresher-on-ab-testing">A Refresher on A/B Testing</a></p><h3>Data Science Communities</h3><p>Data Science communities are a really effective way to get in touch with like-minded people, learn about current trends and events in the field, and meet and get advice /mentorship from industry experts. Here are a few communities I’m a part of :</p><ol><li>Jovian Pro provides a Slack community where you can get one-on-one guidance from industry experts, project recommendations, resume review, and find people to work with on group projects/ hackathons.</li></ol><p><a href="https://jovian.ai/pro?redirect=1">Jovian PRO | Jovian</a></p><p>2. Workera is a great platform that provides assessments to measure your data science skills and improve on them, it also gives you job recommendations based on your skill level. They have a Slack community as well.</p><p><a href="https://workera.ai/">Workera (a deeplearning.ai company)</a></p><p>3. I’m sure you’ve heard of Kaggle, it hosts a lot of data science competitions and has an amazing collection of datasets. Participating in these competitions can be a really good learning experience.</p><p><a href="https://www.kaggle.com/">Kaggle: The World&#39;s AI Proving Ground</a></p><h3>Study Materials</h3><h3>Fundamentals — Statistics, Linear Algebra, Programming and Database Management</h3><p>Whether you aim to be a computer vision scientist or a Data Analyst, you would need to have a strong understanding of these fundamental topics.</p><h4>Statistics</h4><ul><li><a href="https://www.coursera.org/learn/bayesian-statistics">Bayesian Statistics: From Concept to Data Analysis</a></li><li><a href="https://www.coursera.org/learn/probability-intro">Introduction to Probability and Data with R</a></li></ul><h4>Linear Algebra</h4><p>I highly recommend going through this series called the <a href="https://www.youtube.com/playlist?list=PLZHQObOWTQDPD3MizzM2xVFitgF8hE_ab">Essence of Linear Algebra</a>, which helped me get an intuitive understanding of basic concepts through amazing visuals and animations.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fvideoseries%3Flist%3DPLZHQObOWTQDPD3MizzM2xVFitgF8hE_ab&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fplaylist%3Flist%3DPLZHQObOWTQDPD3MizzM2xVFitgF8hE_ab&amp;image=https%3A%2F%2Fi9.ytimg.com%2Fs_p%2FPLZHQObOWTQDPD3MizzM2xVFitgF8hE_ab%2Flandscape_mqdefault.jpg%3Fsqp%3DCLC508oGir7X7AMICOqwiNsFEAE%3D%26rs%3DAOn4CLCggCWitdTd0HHnWhWxZI3HAePaAg%26v%3D1533155434%26days_since_epoch%3D20453&amp;type=text%2Fhtml&amp;schema=youtube" width="853" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/9e2730cffc4008b109f4d898b2db6566/href">https://medium.com/media/9e2730cffc4008b109f4d898b2db6566/href</a></iframe><h4>Programming</h4><p>Most data science jobs expect an intermediate-level proficiency in Python. Here’s a resource that helped me get started:</p><p><a href="https://www.udemy.com/course/python-for-data-science-and-machine-learning-bootcamp/">Learn Python for Data Science, Structures, Algorithms, Interviews</a></p><p>Apart from this, I’ve already mentioned a few more resources in the Interview preparation section above.</p><h4>Database Management Systems</h4><p>I learned DBMS through my courses in <a href="http://nitc.ac.in/">undergrad</a> but here is an online resource I found helpful:</p><p><a href="https://www.w3schools.com/sql/">W3Schools.com</a></p><h3>Machine Learning</h3><p>Andrew Ng’s Machine Learning course is probably the most popular one and for good reason, it helped me grasp a solid understanding of ML fundamentals.</p><p><a href="https://www.coursera.org/learn/machine-learning">Supervised Machine Learning: Regression and Classification</a></p><h3>Deep Learning</h3><ol><li>The deeplearning.ai specialization on Coursera is a great follow-up to the Machine learning course mentioned above.</li></ol><p><a href="https://www.coursera.org/specializations/deep-learning">Deep Learning</a></p><p>2. Another course I found very helpful is fast.ai offered by <a href="https://www.usfca.edu/faculty/jeremy-howard">Jeremy Howard</a>. This course in fact inspired me to pursue a <a href="https://www.usfca.edu/arts-sciences/graduate-programs/data-science">Master’s in Data Science</a> at the University of San Francisco</p><p><a href="https://course.fast.ai/">Practical Deep Learning for Coders - Practical Deep Learning</a></p><p>3. <strong>PyTorch</strong></p><p>Pytorch is a really popular machine learning framework and I’ve written several beginner-friendly blogs on it. I recommend checking out this course if you want a deeper understanding:</p><p><a href="https://jovian.ai/learn/deep-learning-with-pytorch-zero-to-gans">Deep Learning with PyTorch: Zero to GANs | Jovian</a></p><p>4. <strong>Tensorflow</strong></p><p>Tensorflow is the other most popular deep learning framework. It was developed by Google and is used in a lot of production-level machine learning applications, it also integrates very smoothly with the entire Google Cloud infrastructure. Here’s a good course that helped me get started with Tensorflow on Google cloud:</p><p><a href="https://www.coursera.org/specializations/machine-learning-tensorflow-gcp">Machine Learning with TensorFlow on Google Cloud Platform</a></p><h4>Natural Language Processing</h4><p>Here’s a blog I recently wrote for anyone who is just getting started with NLP</p><p><a href="https://towardsdatascience.com/getting-started-with-natural-language-processing-nlp-2c482420cc05">Getting Started with Natural Language Processing (NLP)</a></p><p>Once you’re familiar with the basics, this course from Stanford is a comprehensive one:</p><p><a href="http://web.stanford.edu/class/cs224n/">Stanford CS 224N | Natural Language Processing with Deep Learning</a></p><h4>Computer Vision</h4><p>I highly recommend this popular course on Computer Vision offered by Stanford</p><p><a href="http://cs231n.stanford.edu/">CS231n: Deep Learning for Computer Vision</a></p><h3>Conclusion</h3><p>Data Science is a rapidly growing field and it is important to always keep learning, I hope to keep adding to this list as I come across more useful resources.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5d2860b5c8d3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Recommender Systems: Matrix Factorization using PyTorch]]></title>
            <link>https://medium.com/data-science/recommender-systems-matrix-factorization-using-pytorch-bd52f46aa199?source=rss-f30e60797a83------2</link>
            <guid isPermaLink="false">https://medium.com/p/bd52f46aa199</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[pytorch]]></category>
            <category><![CDATA[recommendations]]></category>
            <dc:creator><![CDATA[Aakanksha NS]]></dc:creator>
            <pubDate>Wed, 02 Sep 2020 17:02:31 GMT</pubDate>
            <atom:updated>2020-11-10T22:08:48.512Z</atom:updated>
            <content:encoded><![CDATA[<h3>Recommender Systems: Matrix Factorization from scratch</h3><h4>Predicting Anime Ratings</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cV-57wp2PP2IYgAj0oU3KQ.png" /><figcaption><a href="https://pixabay.com/illustrations/chibi-kids-sports-chibi-boy-ball-4156788/">Source</a></figcaption></figure><p>We come across recommendations multiple times a day — while deciding what to watch on Netflix/Youtube, item recommendations on shopping sites, song suggestions on Spotify, friend recommendations on Instagram, job recommendations on LinkedIn…the list goes on! Recommender systems aim to predict the “rating” or “preference” a user would give to an item. These ratings are used to determine what a user might like and make informed suggestions.</p><p>There are two broad types of Recommender systems:</p><ol><li><strong>Content-Based systems</strong>: These systems try to match users with items based on items’ content (genre, color, etc) and users’ profiles (likes, dislikes, demographic information, etc). For example, Youtube might suggest me cooking videos based on the fact that I’m a chef, and/or that I’ve watched a lot of baking videos in the past, hence utilizing the information it has about a video’s content and my profile.</li><li><strong>Collaborative filtering</strong>: They rely on the assumption that similar users like similar items. Similarity measures between users and/or items are used to make recommendations.</li></ol><p>This article talks about a very popular collaborative filtering technique called Matrix factorization.</p><h3>Matrix Factorization</h3><p>A recommender system has two entities — users and items. Let’s say we have m users and n items. The goal of our recommendation system is to build an mxn matrix (called the <strong>utility matrix</strong>) which consists of the rating (or preference) for each user-item pair. Initially, this matrix is usually very sparse because we only have ratings for a limited number of user-item pairs.</p><p>Here’s an example. Say we have 4 users and 5 superheroes and we’re trying to predict the rating each user would give to each superhero. This is what our utility matrix initially looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JP7-90ven7w0rNyvPWWfbQ.png" /><figcaption>4x5 utility matrix for superhero ratings (Image by author)</figcaption></figure><p>Now, our goal is to populate this matrix by finding similarities between users and items. To get an intuition, for example, we see that User3 and User4 gave the same rating to Batman, so we can assume the users are similar and they’d feel the same way about Spiderman and predict that User3 would give a rating of 4 to Spiderman. In practice, however, this is not as straightforward because there are multiple users interacting with many different items.</p><p>In practice, The matrix is populated by decomposing (or factorizing) the Utility matrix into two tall and skinny matrices. The decomposition has the equation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/172/1*VsL3stRmAnK_cmz-7j2mbA.png" /><figcaption>The utility matrix can be broken down into a product of two lower-dimensional tall-skinny matrices representing users and items</figcaption></figure><p>where U is m x k and V is n x k. U is a representation of users in some low dimensional space, and V is a representation of items. For a user i, uᵢ gives the representation of that user, and for an item e, vₑ gives the representation of that item. The rating prediction for a user-item pair is simply:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/204/1*Z0vVFgUDn-xbhcyWY0DD0g.png" /><figcaption>Rating prediction for a user-item pair is simply the dot product of the user and item representations</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n-vTVJgQahPXRI09mMpKNA.png" /><figcaption>Matrix factorization ( numbers are random for the sake of illustration) (Image by author)</figcaption></figure><h3>Implementation</h3><p>To implement matrix factorization, we can use embeddings for the user and item embedding matrices and use Gradient Descent to get the optimal decomposition. If you’re unfamiliar with embeddings, you can check out this article where I’ve talked about them in detail:</p><p><a href="https://towardsdatascience.com/deep-learning-for-tabular-data-using-pytorch-1807f2858320">Deep Learning for Tabular Data using PyTorch</a></p><h4>Code</h4><p>All the code I’ve used in this article can be found here: <a href="https://jovian.ml/aakanksha-ns/anime-ratings-matrix-factorization">https://jovian.ml/aakanksha-ns/anime-ratings-matrix-factorization</a></p><h4>Dataset</h4><p>I’ve used the Anime Recommendations dataset from Kaggle:</p><p><a href="https://www.kaggle.com/CooperUnion/anime-recommendations-database">Anime Recommendations Database</a></p><p>We have a total of 69600 users and 9927 anime. There are 6337241 ratings provided (out of 690,919,200 possible ratings).</p><h4>Problem Statement</h4><p>Given a set of user ratings for anime, predict the rating for each user-anime pair.</p><h4>Data Exploration</h4><p>We see that there are lots of rows with a rating of -1 , indicating missing ratings, we can get rid of these rows.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fanime-ratings-matrix-factorization%2Fv%2F9%26cellId%3D3&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fanime-ratings-matrix-factorization%2Fv%2F9%26cellId%3D3&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/b047498c8280fb0224d21d616d9938b2/href">https://medium.com/media/b047498c8280fb0224d21d616d9938b2/href</a></iframe><p>We can also look at the distribution of ratings and number of ratings per user.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fanime-ratings-matrix-factorization%2Fv%2F10%26cellId%3D7&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fanime-ratings-matrix-factorization%2Fv%2F10%26cellId%3D7&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/1e043838f53bd6c0b4a27c9ac17c1a85/href">https://medium.com/media/1e043838f53bd6c0b4a27c9ac17c1a85/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fanime-ratings-matrix-factorization%2Fv%2F10%26cellId%3D10&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fanime-ratings-matrix-factorization%2Fv%2F10%26cellId%3D10&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/aff504b6b5ec125b545d866182d49565/href">https://medium.com/media/aff504b6b5ec125b545d866182d49565/href</a></iframe><h4>Preprocessing</h4><p>We need continuous IDs to be able to index into the embedding matrix and access each user/item embedding.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fanime-ratings-matrix-factorization%2Fv%2F10%26cellId%3D15&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fanime-ratings-matrix-factorization%2Fv%2F10%26cellId%3D15&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/4add91f5f786f9000ff10d675feb4cf8/href">https://medium.com/media/4add91f5f786f9000ff10d675feb4cf8/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fanime-ratings-matrix-factorization%2Fv%2F10%26cellId%3D16&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fanime-ratings-matrix-factorization%2Fv%2F10%26cellId%3D16&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/e953af143140f6963a362edad2faa485/href">https://medium.com/media/e953af143140f6963a362edad2faa485/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fanime-ratings-matrix-factorization%2Fv%2F10%26cellId%3D17&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fanime-ratings-matrix-factorization%2Fv%2F10%26cellId%3D17&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/ac738b27d22c625e2f719b0ae9dc49e4/href">https://medium.com/media/ac738b27d22c625e2f719b0ae9dc49e4/href</a></iframe><h4>Training</h4><p>Our goal is to find the optimal embeddings for each user and each item. We can then use these embeddings to make predictions for any user-item pair by taking the dot product of user embedding and item embedding</p><p><strong>Cost function: </strong>We are trying to minimize the Mean squared error over the utility matrix. Here N is the number of non-blank elements in the utility matrix.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/866/1*ahOZMlf7tiEiWj6dhZb_2w.png" /><figcaption>Cost function for Matrix factorization</figcaption></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fanime-ratings-matrix-factorization%2Fv%2F10%26cellId%3D25&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fanime-ratings-matrix-factorization%2Fv%2F10%26cellId%3D25&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/89161cc0bf63614e1377e6210cb4adf4/href">https://medium.com/media/89161cc0bf63614e1377e6210cb4adf4/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fanime-ratings-matrix-factorization%2Fv%2F10%26cellId%3D28&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fanime-ratings-matrix-factorization%2Fv%2F10%26cellId%3D28&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/603ef38bc8d50d9a12a136db891523b5/href">https://medium.com/media/603ef38bc8d50d9a12a136db891523b5/href</a></iframe><p><strong>Initializing user and item embeddings</strong>: There are many ways to initialize embedding weights and it’s an active area of research, for example, <a href="https://www.fast.ai/">fastai</a> uses something called <a href="https://discuss.pytorch.org/t/implementing-truncated-normal-initializer/4778">Truncated Normal initializer</a>. In my implementation, I’ve just initialized my embeddings with uniform values in (0, 11/K) (randomly, worked well in my case!) where K is the number of factors in the embedding matrix. K is a hyperparameter that’s usually decided empirically— it shouldn’t be too small because you want your embeddings to learn enough features but you also don’t want it to be too large as it can start overfitting to your training data and increase the computational time.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fanime-ratings-matrix-factorization%2Fv%2F10%26cellId%3D19&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fanime-ratings-matrix-factorization%2Fv%2F10%26cellId%3D19&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/066bc5721b4c1df782247235c17a8813/href">https://medium.com/media/066bc5721b4c1df782247235c17a8813/href</a></iframe><p><strong>Creating Sparse utility matrix: </strong>Since our cost function needs the utility matrix, we need a function to create this matrix.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fanime-ratings-matrix-factorization%2Fv%2F10%26cellId%3D21&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fanime-ratings-matrix-factorization%2Fv%2F10%26cellId%3D21&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/fee2313fcf347d1295bea55e666b65b3/href">https://medium.com/media/fee2313fcf347d1295bea55e666b65b3/href</a></iframe><p><strong>Gradient Descent</strong>:</p><p>The gradient descent equations are:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*aU9HD6gWwJJFSEXYFE2jZQ.png" /></figure><p>I’ve used momentum in my implementation which is a method that helps accelerate gradient descent in the relevant direction and dampens oscillations leading to faster convergence. I’ve also added regularization to ensure my model does not overfit to the training data. Hence, the gradient descent equations in my code are slightly more complex than the ones mentioned above.</p><p>The regularized cost function is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6h35XmJVDT1zmWW_PeD09Q.png" /></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fanime-ratings-matrix-factorization%2Fv%2F10%26cellId%3D30&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fanime-ratings-matrix-factorization%2Fv%2F10%26cellId%3D30&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/3f30c0f0386653079caa9b674bd56ef1/href">https://medium.com/media/3f30c0f0386653079caa9b674bd56ef1/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fanime-ratings-matrix-factorization%2Fv%2F10%26cellId%3D31&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fanime-ratings-matrix-factorization%2Fv%2F10%26cellId%3D31&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/18ce2815a4d445f5c1dcdd0459fd50ab/href">https://medium.com/media/18ce2815a4d445f5c1dcdd0459fd50ab/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fanime-ratings-matrix-factorization%2Fv%2F10%26cellId%3D32&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fanime-ratings-matrix-factorization%2Fv%2F10%26cellId%3D32&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/b87a0184a21d9be83a739236062d2d5e/href">https://medium.com/media/b87a0184a21d9be83a739236062d2d5e/href</a></iframe><h4>Prediction on Validation Set</h4><p>Because we cannot make predictions for users and anime (cold start problem) we have not encountered in our training set, we need to remove them from our unseen dataset.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fanime-ratings-matrix-factorization%2Fv%2F10%26cellId%3D34&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fanime-ratings-matrix-factorization%2Fv%2F10%26cellId%3D34&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/b05458ea90c0c3be1aa70c166a47ffd2/href">https://medium.com/media/b05458ea90c0c3be1aa70c166a47ffd2/href</a></iframe><p>Our model has slightly overfitted the training data, so the regularization factor (lambda) can be increased to make it generalize better.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fanime-ratings-matrix-factorization%2Fv%2F10%26cellId%3D36&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fanime-ratings-matrix-factorization%2Fv%2F10%26cellId%3D36&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/13ef99a14fa44f428784ff68d825df2c/href">https://medium.com/media/13ef99a14fa44f428784ff68d825df2c/href</a></iframe><p>Let’s look at a few predictions:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fanime-ratings-matrix-factorization%2Fv%2F10%26cellId%3D37&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fanime-ratings-matrix-factorization%2Fv%2F10%26cellId%3D37&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/26b0e6886ae746f79dc87260c955dd49/href">https://medium.com/media/26b0e6886ae746f79dc87260c955dd49/href</a></iframe><p>Given that these ratings are based solely on similarities between user behaviors, a root mean square of just 3.4 in a rating range of 1–10 is pretty impressive. It shows how powerful matrix factorization can be even though it is so simple.</p><h3>Limitations of Matrix Factorization</h3><p>Matrix factorization is a very simple and convenient way to make recommendations. However, it has its pitfalls, one of which we have already encountered in our implementation:</p><h4>Cold Start Problem</h4><p>We cannot make predictions for items and users we’ve never encountered in the training data because we don’t have embeddings for them.</p><p>The cold-start problem can be addressed in many ways including Recommending popular items, Asking the user to rate some items, using a content-based approach until we have enough data to use collaborative filtering.</p><h4>Hard to include additional context about user/item</h4><p>We only used user IDs and item IDs to create the embeddings. We are not able to use any other information about our users and items in our implementation. There are some complex hybrid models of content-based collaborative filtering that can be used to address this.</p><h4>Ratings are not always available</h4><p>It’s hard to get feedback from users. Most users rate things only when they really like something or absolutely hate it. In such cases, we often have to come up with a way to measure implicit feedback and use negative sampling techniques to come up with a reasonable training set.</p><h3>Conclusion</h3><p>Recommendation systems are really interesting but can also get too complex too easily, especially when implemented at scale where there are millions of users and millions of items. If you want to dive deeper into recommendation systems, check out various case studies — how Youtube recommends videos, LinkedIn job recommendations, Ad ranking. Often, you can find research papers/ videos/ engineering blogs corresponding to these case studies. Here are a few helpful resources:</p><ul><li><a href="https://engineering.linkedin.com/blog/2019/04/ai-behind-linkedin-recruiter-search-and-recommendation-systems">https://engineering.linkedin.com/blog/2019/04/ai-behind-linkedin-recruiter-search-and-recommendation-systems</a></li><li><a href="https://static.googleusercontent.com/media/research.google.com/en//pubs/archive/45530.pdf">https://static.googleusercontent.com/media/research.google.com/en//pubs/archive/45530.pdf</a></li><li><a href="https://labs.pinterest.com/user/themes/pin_labs/assets/paper/p2p-www17.pdf">https://labs.pinterest.com/user/themes/pin_labs/assets/paper/p2p-www17.pdf</a></li></ul><h3>References</h3><ul><li><a href="https://developers.google.com/machine-learning/recommendation/collaborative/matrix">https://developers.google.com/machine-learning/recommendation/collaborative/matrix</a></li><li><a href="https://medium.com/@paritosh_30025/recommendation-using-matrix-factorization-5223a8ee1f4">https://medium.com/@paritosh_30025/recommendation-using-matrix-factorization-5223a8ee1f4</a></li><li>University of San Francisco MSDS — Advanced Machine Learning course</li><li><a href="https://www.youtube.com/watch?v=ZspR5PZemcs">https://www.youtube.com/watch?v=ZspR5PZemcs</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bd52f46aa199" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/recommender-systems-matrix-factorization-using-pytorch-bd52f46aa199">Recommender Systems: Matrix Factorization using PyTorch</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Bounding Box Prediction from Scratch using PyTorch]]></title>
            <link>https://medium.com/data-science/bounding-box-prediction-from-scratch-using-pytorch-a8525da51ddc?source=rss-f30e60797a83------2</link>
            <guid isPermaLink="false">https://medium.com/p/a8525da51ddc</guid>
            <category><![CDATA[pytorch]]></category>
            <category><![CDATA[computer-vision]]></category>
            <category><![CDATA[object-detection]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Aakanksha NS]]></dc:creator>
            <pubDate>Tue, 07 Jul 2020 02:28:36 GMT</pubDate>
            <atom:updated>2020-07-07T02:28:36.260Z</atom:updated>
            <content:encoded><![CDATA[<h4>Multi-Task learning — Bounding Box Regression + Image Classification</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F0CMfx6bGUHWjFSgQWSAEg.jpeg" /><figcaption>Image clicked by author</figcaption></figure><p>Object detection is a very popular task in Computer Vision, where, given an image, you predict (usually rectangular) boxes around objects present in the image and also recognize the types of objects. There could be multiple objects in your image and there are various state-of-the-art techniques and architectures to tackle this problem like <a href="https://arxiv.org/pdf/1506.01497.pdf">Faster-RCNN</a> and <a href="https://pjreddie.com/media/files/papers/YOLOv3.pdf">YOLO v3</a>.</p><p>This article talks about the case when there is only one object of interest present in an image. The focus here is more on how to read an image and its bounding box, resize and perform augmentations correctly, rather than on the model itself. The goal is to have a good grasp of the fundamental ideas behind object detection, which you can extend to get a better understanding of the more complex techniques.</p><p>Here’s a link to the notebook consisting of all the code I’ve used for this article: <a href="https://jovian.ml/aakanksha-ns/road-signs-bounding-box-prediction">https://jovian.ml/aakanksha-ns/road-signs-bounding-box-prediction</a></p><p>If you’re new to Deep Learning or PyTorch, or just need a refresher, this might interest you:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FGIsg-ZUy0MY%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DGIsg-ZUy0MY&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FGIsg-ZUy0MY%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/0ee179108e3f2fe5e1a12777da313b71/href">https://medium.com/media/0ee179108e3f2fe5e1a12777da313b71/href</a></iframe><h3>Problem Statement</h3><p>Given an image consisting of a road sign, predict a bounding box around the road sign and identify the type of road sign.</p><p>There are four distinct classes these signs could belong to:</p><ul><li>Traffic Light</li><li>Stop</li><li>Speed Limit</li><li>Crosswalk</li></ul><p>This is called a multi-task learning problem as it involves performing two tasks — 1) regression to find the bounding box coordinates, 2) classification to identify the type of road sign</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m_z7HSOmWdwgaU_eU5h7ew.png" /><figcaption>Sample images. <a href="https://www.kaggle.com/andrewmvd/road-sign-detection">Source</a></figcaption></figure><h3>Dataset</h3><p>I’ve used the Road Sign Detection Dataset from Kaggle:</p><p><a href="https://www.kaggle.com/andrewmvd/road-sign-detection">Road Sign Detection</a></p><p>It consists of 877 images. It’s a pretty imbalanced dataset, with most images belonging to the speed limit class, but since we’re more focused on the bounding box prediction, we can ignore the imbalance.</p><h3>Loading the Data</h3><p>The annotations for each image were stored in separate XML files. I followed the following steps to create the training dataframe:</p><ul><li>Walk through the training directory to get a list of all the .xml files.</li><li>Parse the .xml file using xml.etree.ElementTree</li><li>Create a dictionary consisting of filepath, width , height , the bounding box coordinates ( xmin , xmax , ymin , ymax ) and class for each image and append the dictionary to a list.</li><li>Create a pandas dataframe using the list of dictionaries of image stats.</li></ul><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D6&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D6&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/6f8ab7d10c2e28879fd7c7f2cffecc41/href">https://medium.com/media/6f8ab7d10c2e28879fd7c7f2cffecc41/href</a></iframe><ul><li>Label encode the class column</li></ul><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D8&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D8&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/e8cb70c0f2d3153e8d4eb94f3f480ac4/href">https://medium.com/media/e8cb70c0f2d3153e8d4eb94f3f480ac4/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D9&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D9&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/2ee7980af1f4114a1ac248f120bf429a/href">https://medium.com/media/2ee7980af1f4114a1ac248f120bf429a/href</a></iframe><h3>Resizing Images and Bounding Boxes</h3><p>Since training a computer vision model needs images to be of the same size, we need to resize our images and their corresponding bounding boxes. Resizing an image is straightforward but resizing the bounding box is a little tricky because each box is relative to an image and its dimensions.</p><p>Here’s how resizing a bounding box works:</p><ul><li>Convert the bounding box into an image (called mask) of the same size as the image it corresponds to. This mask would just have 0 for background and 1 for the area covered by the bounding box.</li></ul><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D18&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D18&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/b8f614867cc972d393259441cffa11e7/href">https://medium.com/media/b8f614867cc972d393259441cffa11e7/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D19&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D19&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/c22de075486b12af41a767bbf9596080/href">https://medium.com/media/c22de075486b12af41a767bbf9596080/href</a></iframe><ul><li>Resize the mask to the required dimensions.</li><li>Extract bounding box coordinates from the resized mask.</li></ul><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D12&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D12&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f0807687d9a5a5cea0b21c41e062b9f/href">https://medium.com/media/1f0807687d9a5a5cea0b21c41e062b9f/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D13&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D13&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/6b30f35f278ae85c9586f565a2caa0ff/href">https://medium.com/media/6b30f35f278ae85c9586f565a2caa0ff/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D14&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D14&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/df56cbe4ef6df0c909627dd1db8ee4ae/href">https://medium.com/media/df56cbe4ef6df0c909627dd1db8ee4ae/href</a></iframe><h3>Data Augmentation</h3><p>Data Augmentation is a technique to generalize our model better by creating new training images by using different variations of the existing images. We have only 800 images in our current training set, so data augmentation is very important to ensure our model doesn’t overfit.</p><p>For this problem, I’ve used flip, rotation, center crop and random crop. I’ve talked about various data augmentation techniques in this article:</p><p><a href="https://towardsdatascience.com/image-processing-techniques-for-computer-vision-11f92f511e21">Image Processing Techniques for Computer Vision</a></p><p>The only thing to remember here is ensuring that the bounding box is also transformed the same way as the image. To do this we follow the same approach as resizing — convert bounding box to a mask, apply the same transformations to the mask as the original image, and extract the bounding box coordinates.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D21&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D21&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/eda0425d8aeb4f807d9343767e6ec4d8/href">https://medium.com/media/eda0425d8aeb4f807d9343767e6ec4d8/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D22&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D22&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/8f8da2bcefd933b9dd2ef37fddb9d032/href">https://medium.com/media/8f8da2bcefd933b9dd2ef37fddb9d032/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D23&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D23&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/b912fa8a452bfd3b84fff5473db4d4f1/href">https://medium.com/media/b912fa8a452bfd3b84fff5473db4d4f1/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D25&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D25&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/7eb38e9117d5e1bea6706c9f1d746019/href">https://medium.com/media/7eb38e9117d5e1bea6706c9f1d746019/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D26&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D26&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/14e19e192ac36e0dc955953730102293/href">https://medium.com/media/14e19e192ac36e0dc955953730102293/href</a></iframe><h3>PyTorch Dataset</h3><p>Now that we have our data augmentations in place, we can do the train-validation split and create our PyTorch dataset. We normalize the images using ImageNet stats because we’re using a pre-trained ResNet model and apply data augmentations in our dataset while training.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D30&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D30&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/55171844dc7d48bc0b693261443724fc/href">https://medium.com/media/55171844dc7d48bc0b693261443724fc/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D32&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D32&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/898e967fdcffab38e4cb9ea602ffbb95/href">https://medium.com/media/898e967fdcffab38e4cb9ea602ffbb95/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D33&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D33&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/3df047b44ceebc154cb7c8a310644e81/href">https://medium.com/media/3df047b44ceebc154cb7c8a310644e81/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D34&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D34&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/9d64e4af11a17577df502871168f968c/href">https://medium.com/media/9d64e4af11a17577df502871168f968c/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D35&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D35&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/8e4105ad733689589f8d614e3dbcabd6/href">https://medium.com/media/8e4105ad733689589f8d614e3dbcabd6/href</a></iframe><h3>PyTorch Model</h3><p>For the model, I’ve used a very simple pre-trained resNet-34 model. Since we have two tasks to accomplish here, there are two final layers — the bounding box regressor and the image classifier.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D37&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D37&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/1beaea2f10014f82403b3259354ba4e1/href">https://medium.com/media/1beaea2f10014f82403b3259354ba4e1/href</a></iframe><h3>Training</h3><p>For the loss, we need to take into both classification loss and the bounding box regression loss, so we use a combination of cross-entropy and L1-loss (sum of all the absolute differences between the true value and the predicted coordinates). I’ve scaled the L1-loss by a factor of 1000 because to have both the classification and regression losses in a similar range. Apart from this, it’s a standard PyTorch training loop (using a GPU):</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D39&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D39&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/2aaea236e0a4cf73f102ecd011cde291/href">https://medium.com/media/2aaea236e0a4cf73f102ecd011cde291/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D40&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D40&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/84c79deca5efae3034f165a0462948aa/href">https://medium.com/media/84c79deca5efae3034f165a0462948aa/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D41&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D41&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/00408891277a4b72408ed0262316af96/href">https://medium.com/media/00408891277a4b72408ed0262316af96/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D42&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D42&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e97aaa55ad1322d605c771ce6e6c6f8/href">https://medium.com/media/0e97aaa55ad1322d605c771ce6e6c6f8/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D43&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D43&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/bb84716efa8ab096cda56988fe2dadc9/href">https://medium.com/media/bb84716efa8ab096cda56988fe2dadc9/href</a></iframe><h3>Prediction on Test Images</h3><p>Now that we’re done with training, we can pick a random image and test our model on it. Even though we had a fairly small number of training images, we end up getting a pretty decent prediction on our test image.</p><p>It’ll be a fun exercise to take a real photo using your phone and test out the model. Another interesting experiment would be to not perform any data augmentations and train the model and compare the two models.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D47&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D47&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/84e7cde00891f1298e56b40fb6971efd/href">https://medium.com/media/84e7cde00891f1298e56b40fb6971efd/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D48&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D48&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/36548abd5c89d25de7d07e02e44ea68d/href">https://medium.com/media/36548abd5c89d25de7d07e02e44ea68d/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D49&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D49&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/6c6e6b6eca3c59e529e6cacdf60ec2b9/href">https://medium.com/media/6c6e6b6eca3c59e529e6cacdf60ec2b9/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D50&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D50&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/14822872938df86b6f215badef056600/href">https://medium.com/media/14822872938df86b6f215badef056600/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D53&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Froad-signs-bounding-box-prediction%2Fv%2F2%26cellId%3D53&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Faakanksha-ns%2Fprofile_images%2F2b6b115e6ffc4ece9b41778be1122a18%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/afd1385647f3a6bf8e258042edcad0ed/href">https://medium.com/media/afd1385647f3a6bf8e258042edcad0ed/href</a></iframe><h3>Conclusion</h3><p>Now that we’ve covered the fundamentals of object detection and implemented it from scratch, you can extend these ideas to the multi-object case and try out more complex models like RCNN and YOLO! Also, check out this super cool library called <a href="https://github.com/albumentations-team/albumentations">albumentations</a> to perform data augmentations easily.</p><h3>References</h3><ul><li>Deep Learning summer elective at the University of San Francisco’s <a href="https://www.usfca.edu/arts-sciences/graduate-programs/data-science">Master’s in Data Science</a> program</li><li><a href="https://www.usfca.edu/data-institute/certificates/fundamentals-deep-learning">https://www.usfca.edu/data-institute/certificates/fundamentals-deep-learning</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a8525da51ddc" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/bounding-box-prediction-from-scratch-using-pytorch-a8525da51ddc">Bounding Box Prediction from Scratch using PyTorch</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building and Deploying a Login System backend using Flask, SQLAlchemy and Heroku]]></title>
            <link>https://medium.com/data-science/building-and-deploying-a-login-system-backend-using-flask-sqlalchemy-and-heroku-8b2aa6cc9ec3?source=rss-f30e60797a83------2</link>
            <guid isPermaLink="false">https://medium.com/p/8b2aa6cc9ec3</guid>
            <category><![CDATA[database]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[flask]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Aakanksha NS]]></dc:creator>
            <pubDate>Sun, 17 May 2020 15:59:56 GMT</pubDate>
            <atom:updated>2020-05-17T15:59:56.847Z</atom:updated>
            <content:encoded><![CDATA[<h4>with a Heroku Postgres Database</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ss1gUeTh6WowFARIM0esLg.png" /><figcaption>Image by author</figcaption></figure><p>Almost all websites these days have a login system, where you can register if you’re a new user or sign in if you’re an existing one. There can be a lot of moving pieces if you’re trying to build this system from scratch for your application. This article gives a step-by-step overview to create and deploy REST APIs for a login system but can be extended to any REST API in general.</p><p>All the code related to this article can be found in the following repo:</p><p><a href="https://github.com/aakanksha-ns/flask-heroku-login">aakanksha-ns/flask-heroku-login</a></p><h3><strong>1) Create a Heroku account and create a new application</strong></h3><p>The first thing you’d need to do is <a href="https://www.heroku.com/">create an account on Heroku</a> if you don’t already have one.</p><p>Sign in to your account and select the Create new app option to create a new Heroku application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cmNm0jK3ySP06ahtLtEEew.png" /><figcaption>Dashboard-&gt;New-&gt;Create new app (Image by author)</figcaption></figure><h3>2) Add Heroku Postgres to application</h3><p>Now you need to add Postgres to the application. Navigate to the page of the application you just created, select the Configure Add-ons option, search for Postgres and provision it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*GIzoH8_rOVAgVJbR6bcTJw.png" /><figcaption>Dashboard-&gt;’your application’-&gt;Configure Add-ons (Image by author)</figcaption></figure><h3>3) Install Heroku CLI, PostgreSQL, and PopSQL</h3><h4>Heroku CLI</h4><p>To be able to interact with Heroku from the command line, you’d have to install Heroku CLI using the following command (assuming you have <a href="https://brew.sh/">Homebrew</a> already installed):</p><p>$ brew tap heroku/brew &amp;&amp; brew install heroku</p><h4>PostgreSQL, and PopSQL</h4><p><strong>Why we need them: </strong>You could create your database’s tables from your Flask app. In my implementation, however, I’ve created the database and tables locally using PostgreSQL and pushed that to Heroku because I found it more straightforward creating tables using a normal SQL editor (I used PopSQL) as compared to SQLAlachemy. I’ve used Flask and SQLAlchemy only to add elements to the database or retrieve elements from it.</p><p><strong>Install Postgres</strong>: brew install postgres</p><p><strong>Start PostgreSQL server:</strong> pg_ctl -D /usr/local/var/postgres start</p><p><strong>Creating database and user:</strong> Postgres sets up a set of default users but they are super user accounts — they can do anything, including delete databases. It’s best to create new users with access only to a particular database:</p><pre>$ psql postgres</pre><pre>CREATE ROLE aakanksha WITH LOGIN PASSWORD ‘blah’;<br>CREATE DATABASE bookstore;<br>GRANT ALL PRIVILEGES ON DATABASE bookstore TO aakanksha;</pre><p><strong>Install PopSQL:</strong> PopSQL is a very popular and easy to use SQL editor. Download the application from <a href="https://popsql.com/">https://popsql.com/</a> and install it.</p><p><strong>Connect PopSQL to database:</strong> You’ll have to provide details like port number (postgres uses 5432 by default), database name etc. for PopSQL to be able to access your PostgreSQL database. You can just enter the following details:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JrVaFbxC0L9t0AyZQNldJQ.png" /><figcaption>(Image by author)</figcaption></figure><h3>4) Create Tables and push Database to Heroku</h3><p>Create Tables on the PopSQL scratchpad:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hPR5j2J5D8ZKaBi3GAidFw.png" /><figcaption>Execute the ‘DROP TABLE’ command only if you made a mistake while creating (Image by author)</figcaption></figure><p>Push to Heroku:</p><p>$ heroku pg:push bookstore DATABASE --app example-app-medium</p><h3>5) Install required libraries</h3><p>Now that the database is set up, we can move on to the API part. Make sure you install all the required libraries. You can just store the following list of libraries I’ve used into a text file ( requirements.txt ) and execute the command following it:</p><pre>certifi==2020.4.5.1<br>click==7.1.1<br>Flask==1.1.2<br>flask-heroku==0.1.9<br>Flask-SQLAlchemy==2.4.1<br>itsdangerous==1.1.0<br>Jinja2==2.11.1<br>MarkupSafe==1.1.1<br>psycopg2==2.8.5<br>SQLAlchemy==1.3.16<br>Werkzeug==1.0.1<br>gunicorn==19.5.0<br>flask_cors</pre><p>pip install -r requirements.txt</p><h3>6) Set up environment variable to access Heroku Database</h3><p>Since you have to access the remote database on Heroku through your flask app, to establish a connection to it, you’d need the database URL. Because the remote database’s URL can change due to many reasons, It is best practice to always fetch the database URL config var from the corresponding Heroku app when your application starts. You can do this by setting an environment variable.</p><p>Since this URL is particular to a particular project which is limited to a particular directory, it wouldn’t make sense to add it to your global environment variables. This is where <a href="https://direnv.net/">direnv</a> comes in. It is an environment switcher that allows to load/unload environment variables depending on the present working directory.</p><p>To set the variable for your Flask application you’d have to add the following command to a new file called .envrc and place it in the root directory of your project:</p><pre>export DATABASE_URL=$(heroku config:get DATABASE_URL -a example-app-medium)</pre><p>After creating the .envrc file, run $ direnv allow to activate the variables.</p><h3>5) Setup Flask app config</h3><p>Here’s some basic config you need in your config.py file in the root directory:</p><pre>import os</pre><pre>class Config(object):</pre><pre>SQLALCHEMY_TRACK_MODIFICATIONS = True<br>SECRET_KEY = os.urandom(24)<br>SQLALCHEMY_DATABASE_URI = os.environ[&#39;DATABASE_URL&#39;]</pre><h3>6) Setup application.py and app directory</h3><p>Here’s what my folder structure looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/596/1*TW8BUEBwPSoQJmv4NV4c-w.png" /><figcaption>(Image by author)</figcaption></figure><p>Flask names an app &#39;application’ by default, so you’d need an application.py file for the app to run. You can change the name of your app if you want to (using $ export FLASK_APP = ‘your file name&#39;.py)</p><p>I like putting all Flask related code into a subdirectory called app instead of the root.</p><p>My <a href="https://github.com/aakanksha-ns/flask-heroku-login/blob/master/application.py">application.py</a> looks like:</p><pre>from app import application</pre><p>And within the app subdirectory, here’s my <a href="https://github.com/aakanksha-ns/flask-heroku-login/blob/master/app/__init__.py">__init__.py</a> (initialization script that marks directories on disk as Python package directories):</p><pre>from flask import Flask<br>from config import Config<br>from flask_sqlalchemy import SQLAlchemy<br>from sqlalchemy import create_engine</pre><pre># Initialization<br>application = Flask(__name__)<br>application.config.from_object(Config)</pre><pre>DB_URI = application.config[&#39;SQLALCHEMY_DATABASE_URI&#39;]<br>engine = create_engine(DB_URI)</pre><pre>from app import routes</pre><p>The API code lies in the routes.py file.</p><h3>7) Create API and test locally</h3><p>To see the full routes.py code, follow this link:</p><p><a href="https://github.com/aakanksha-ns/flask-heroku-login/blob/master/app/routes.py">https://github.com/aakanksha-ns/flask-heroku-login/blob/master/app/routes.py</a></p><p>For a login system, you’d need two methods:</p><h4>Register</h4><p>This method creates an entry in the account table for a new user. You can do an additional check to make sure the username or email does not already exist in the table.</p><p>Since it’s not safe to store the raw password in a database due to security concerns, you can generate a password hash using the <a href="https://techmonger.github.io/4/secure-passwords-werkzeug/">werkzeug</a> library and store the hash instead of the real password.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d3fa6cb06dd8d2ad56d39f5c0a7519ad/href">https://medium.com/media/d3fa6cb06dd8d2ad56d39f5c0a7519ad/href</a></iframe><h4>Sign In</h4><p>This method just checks if the password entered matches with the original password for the given username</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5039db281254b4c7287685bbca321c13/href">https://medium.com/media/5039db281254b4c7287685bbca321c13/href</a></iframe><p>Once done, you can use $ flask run to start the flask server and test your API using Postman:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OOgxe71jXmU-htT6OZrPLA.png" /><figcaption>(Image by author)</figcaption></figure><h3>8) Create Procfile</h3><p>Procfile is a mechanism for declaring what commands are run by your application’s dynos on the Heroku platform.</p><p>You can use <a href="https://vsupalov.com/what-is-gunicorn/">gunicorn</a> that takes care of running multiple instances of your web application, making sure they are healthy and restart them as needed, distributing incoming requests across those instances and communicate with the web server.</p><pre>web: gunicorn application:application</pre><h3>9) Push code to Github Repo, connect repo to Heroku app</h3><p>To deploy the APIs you just created, the easiest way is to push your code to Github repository and connect this repo to your Heroku application:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pP5tVCwD6sp9x5vzE9HYqQ.png" /><figcaption>(Image by author)</figcaption></figure><h3>9) Deploy Flask app</h3><p>You can set up an automatic deployment or deploy the Flask app manually from the Heroku dashboard. If there are any errors during deployment, they’ll show up in the build log. Once deployed, you can hit the API using Postman and see that it’s working as expected. If there’s an error after deployment, you can view the log this way:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AH9kAPg7g7_CaxRvWLIu3g.png" /><figcaption>(Image by author)</figcaption></figure><h3>10) Create UI</h3><p>In case you’re interested in creating a frontend for your login system, you can check out my React and Redux based UI:</p><p><a href="https://github.com/aakanksha-ns/book-store">aakanksha-ns/book-store</a></p><p>Here are a few screenshots of the <a href="https://book-store-c8fb5.firebaseapp.com/">application I’ve built</a>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z3oyejtUg7PFh2V_U5c-iQ.png" /><figcaption>(Image by author)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ss1gUeTh6WowFARIM0esLg.png" /><figcaption>(Image by author)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ipk5leEmMeqH4dmJ-Lz1mQ.png" /><figcaption>(Image by author)</figcaption></figure><h3>References:</h3><ul><li><a href="https://thecodersblog.com/PostgreSQL-PostGIS-installation/">https://thecodersblog.com/PostgreSQL-PostGIS-installation/</a></li><li><a href="https://www.codementor.io/@engineerapart/getting-started-with-postgresql-on-mac-osx-are8jcopb">https://www.codementor.io/@engineerapart/getting-started-with-postgresql-on-mac-osx-are8jcopb</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8b2aa6cc9ec3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/building-and-deploying-a-login-system-backend-using-flask-sqlalchemy-and-heroku-8b2aa6cc9ec3">Building and Deploying a Login System backend using Flask, SQLAlchemy and Heroku</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Multiclass Text Classification using LSTM in Pytorch]]></title>
            <link>https://medium.com/data-science/multiclass-text-classification-using-lstm-in-pytorch-eac56baed8df?source=rss-f30e60797a83------2</link>
            <guid isPermaLink="false">https://medium.com/p/eac56baed8df</guid>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[lstm]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[pytorch]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Aakanksha NS]]></dc:creator>
            <pubDate>Tue, 07 Apr 2020 15:54:42 GMT</pubDate>
            <atom:updated>2020-04-07T15:54:42.547Z</atom:updated>
            <content:encoded><![CDATA[<h4>Predicting item ratings based on customer reviews</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MutFuG2Za6Wz1v89Oo_gww.png" /><figcaption>Image by author</figcaption></figure><p>Human language is filled with ambiguity, many-a-times the same phrase can have multiple interpretations based on the context and can even appear confusing to humans. Such challenges make natural language processing an interesting but hard problem to solve. However, we’ve seen a lot of advancement in NLP in the past couple of years and it’s quite fascinating to explore the various techniques being used. This article aims to cover one such technique in deep learning using Pytorch: Long Short Term Memory (LSTM) models.</p><p>Here’s a link to the notebook consisting of all the code I’ve used for this article: <a href="https://jovian.ml/aakanksha-ns/lstm-multiclass-text-classification">https://jovian.ml/aakanksha-ns/lstm-multiclass-text-classification</a></p><p>If you’re new to NLP or need an in-depth read on preprocessing and word embeddings, you can check out the following article:</p><p><a href="https://towardsdatascience.com/getting-started-with-natural-language-processing-nlp-2c482420cc05">Getting Started with Natural Language Processing (NLP)</a></p><h3>Gentle Intro to RNNs and LSTMs:</h3><p>What sets language models apart from conventional neural networks is their dependency on context. Conventional feed-forward networks assume inputs to be independent of one another. For NLP, we need a mechanism to be able to use sequential information from previous inputs to determine the current output. Recurrent Neural Networks (RNNs) tackle this problem by having loops, allowing information to persist through the network.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*TjGIdoBXI7KAAt8A.png" /><figcaption>An unrolled Recurrent Neural Network (Image by author)</figcaption></figure><p>However, conventional RNNs have the issue of exploding and vanishing gradients and are not good at processing long sequences because they suffer from short term memory.</p><p>Long Short Term Memory networks (LSTM) are a special kind of RNN, which are capable of learning long-term dependencies. They do so by maintaining an internal memory state called the “cell state” and have regulators called “gates” to control the flow of information inside each LSTM unit. Here’s an excellent source explaining the specifics of LSTMs:</p><p><a href="https://colah.github.io/posts/2015-08-Understanding-LSTMs/">Understanding LSTM Networks</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/850/0*pq30znWeMlVXUxxX.png" /><figcaption>Structure of an LSTM cell. (<a href="https://www.researchgate.net/publication/329362532_Designing_neural_network_based_decoders_for_surface_codes">source</a> : Varsamopoulos, Savvas &amp; Bertels, Koen &amp; Almudever, Carmen. (2018). Designing neural network based decoders for surface codes.)</figcaption></figure><h3>Basic LSTM in Pytorch</h3><p>Before we jump into the main problem, let’s take a look at the basic structure of an LSTM in Pytorch, using a random input. This is a useful step to perform before getting into complex inputs because it helps us learn how to debug the model better, check if dimensions add up and ensure that our model is working as expected.</p><p>Even though we’re going to be dealing with text, since our model can only work with numbers, we convert the input into a sequence of numbers where each number represents a particular word (more on this in the next section).</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D3&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D3&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/ddd61e58e4b47f584a6fd5f56709a525/href">https://medium.com/media/ddd61e58e4b47f584a6fd5f56709a525/href</a></iframe><p>We first pass the input (3x8) through an <a href="https://en.wikipedia.org/wiki/Word_embedding">embedding</a> layer, because word embeddings are better at capturing context and are spatially more efficient than one-hot vector representations.</p><p>In Pytorch, we can use the nn.Embedding module to create this layer, which takes the vocabulary size and desired word-vector length as input. You can optionally provide a padding index, to indicate the index of the padding element in the embedding matrix.</p><p>In the following example, our vocabulary consists of 100 words, so our input to the embedding layer can only be from 0–100, and it returns us a 100x7 embedding matrix, with the 0th index representing our padding element.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D5&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D5&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/f86903b7e34ee7d9e3cc9396c75cfe5a/href">https://medium.com/media/f86903b7e34ee7d9e3cc9396c75cfe5a/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D6&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D6&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/740185faed354a24922f2e36df208b25/href">https://medium.com/media/740185faed354a24922f2e36df208b25/href</a></iframe><p>We pass the embedding layer’s output into an LSTM layer (created using nn.LSTM), which takes as input the word-vector length, length of the hidden state vector and number of layers. Additionally, if the first element in our input’s shape has the batch size, we can specify batch_first = True</p><p>The LSTM layer outputs three things:</p><ul><li>The consolidated output — of all hidden states in the sequence</li><li>Hidden state of the last LSTM unit — the final output</li><li>Cell state</li></ul><p>We can verify that after passing through all layers, our output has the expected dimensions:</p><p>3x8 -&gt; embedding -&gt; 3x8x7 -&gt; LSTM (with hidden size=3)-&gt; 3x3</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D8&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D8&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/1480776f2c5d281b8c84f6a2bca0cb47/href">https://medium.com/media/1480776f2c5d281b8c84f6a2bca0cb47/href</a></iframe><h3>Multiclass Text Classification — Predicting ratings from review comments</h3><p>Let’s now look at an application of LSTMs.</p><p><strong>Problem Statement: </strong>Given an item’s review comment, predict the rating ( takes integer values from 1 to 5, 1 being worst and 5 being best)</p><p><strong>Dataset</strong>: I’ve used the following dataset from Kaggle:</p><p><a href="https://www.kaggle.com/nicapotato/womens-ecommerce-clothing-reviews">Women&#39;s E-Commerce Clothing Reviews</a></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D13&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D13&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/a0dc75fa3a3105ed8deebb9ee6cf8cf8/href">https://medium.com/media/a0dc75fa3a3105ed8deebb9ee6cf8cf8/href</a></iframe><h3>Metric</h3><p>We usually take accuracy as our metric for most classification problems, however, ratings are ordered. If the actual value is 5 but the model predicts a 4, it is not considered as bad as predicting a 1. Hence, instead of going with accuracy, we choose <strong>RMSE</strong> — root mean squared error as our North Star metric. Also, rating prediction is a pretty hard problem, even for humans, so a prediction of being off by just 1 point or lesser is considered pretty good.</p><h3>Preprocessing</h3><p>As mentioned earlier, we need to convert our text into a numerical form that can be fed to our model as input. I’ve used spacy for tokenization after removing punctuation, special characters, and lower casing the text:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D18&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D18&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/bac50572c8c0082c028fa4928c968827/href">https://medium.com/media/bac50572c8c0082c028fa4928c968827/href</a></iframe><p>We count the number of occurrences of each token in our corpus and get rid of the ones that don’t occur too frequently:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D19&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D19&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/9b04d48ef624e6bab8c84cd421f98d85/href">https://medium.com/media/9b04d48ef624e6bab8c84cd421f98d85/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D20&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D20&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/f2e0b4403fee14fff67f255299764310/href">https://medium.com/media/f2e0b4403fee14fff67f255299764310/href</a></iframe><p>We lost about 6000 words! This is expected because our corpus is quite small, less than 25k reviews, the chance of having repeated words is quite small.</p><p>We then create a vocabulary to index mapping and encode our review text using this mapping. I’ve chosen the maximum length of any review to be 70 words because the average length of reviews was around 60.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D21&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D21&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/3d63d7ef8abad658151578d59ce6c74e/href">https://medium.com/media/3d63d7ef8abad658151578d59ce6c74e/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D22&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D22&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/b10df00337ef8163d95f941af9448e66/href">https://medium.com/media/b10df00337ef8163d95f941af9448e66/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D23&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D23&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/62d3424cbb5a8482f625292243a15004/href">https://medium.com/media/62d3424cbb5a8482f625292243a15004/href</a></iframe><h3>Pytorch Dataset</h3><p>The dataset is quite straightforward because we’ve already stored our encodings in the input dataframe. We also output the length of the input sequence in each case, because we can have LSTMs that take variable-length sequences.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D27&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D27&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/e7fd5383e3fbdb91debe6fa07a413fa3/href">https://medium.com/media/e7fd5383e3fbdb91debe6fa07a413fa3/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D28&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D28&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/933e1a4d28b4d8f30f8e9b1f1e5e0652/href">https://medium.com/media/933e1a4d28b4d8f30f8e9b1f1e5e0652/href</a></iframe><h3>Pytorch training loop</h3><p>The training loop is pretty standard. I’ve used Adam optimizer and cross-entropy loss.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D29&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D29&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c091cc738855b4ff914fe2bc5ba2415/href">https://medium.com/media/5c091cc738855b4ff914fe2bc5ba2415/href</a></iframe><h3>LSTM Model</h3><p>I’ve used three variations for the model:</p><ol><li><strong>LSTM with fixed input size:</strong></li></ol><p>This pretty much has the same structure as the basic LSTM we saw earlier, with the addition of a dropout layer to prevent overfitting. Since we have a classification problem, we have a final linear layer with 5 outputs. This implementation actually works the best among the classification LSTMs, with an accuracy of about 64% and a root-mean-squared-error of only 0.817</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D32&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D32&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/7485ab4e75f3b9cd638ac42392cac775/href">https://medium.com/media/7485ab4e75f3b9cd638ac42392cac775/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D36&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D36&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/9c1b5ff379b57c7a418255f1c44021e7/href">https://medium.com/media/9c1b5ff379b57c7a418255f1c44021e7/href</a></iframe><p><strong>2. LSTM with variable input size:</strong></p><p>We can modify our model a bit to make it accept variable-length inputs. This ends up increasing the training time though, because of the pack_padded_sequence function call which returns a padded batch of variable-length sequences.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D38&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D38&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/8dbea1405ccbce7d89c8f4df4028d12c/href">https://medium.com/media/8dbea1405ccbce7d89c8f4df4028d12c/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D41&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D41&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/47d48a88e0b200f62f7a7cde15dfdba9/href">https://medium.com/media/47d48a88e0b200f62f7a7cde15dfdba9/href</a></iframe><p><strong>3. LSTM with fixed input size and fixed pre-trained Glove word-vectors:</strong></p><p>Instead of training our own word embeddings, we can use pre-trained Glove word vectors that have been trained on a massive corpus and probably have better context captured. For our problem, however, this doesn’t seem to help much.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D44&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D44&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/c2e2297a728593522bb22c8ca8e2b1ed/href">https://medium.com/media/c2e2297a728593522bb22c8ca8e2b1ed/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D45&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D45&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/15dc6654a5801efe64d9c0bef327c5a4/href">https://medium.com/media/15dc6654a5801efe64d9c0bef327c5a4/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D46&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D46&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/de0813a79d51a33903f6db06f9f862f4/href">https://medium.com/media/de0813a79d51a33903f6db06f9f862f4/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D47&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D47&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/39b928558ac85918e196dac6bb2cac7b/href">https://medium.com/media/39b928558ac85918e196dac6bb2cac7b/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D51&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D51&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/fdf079587194d4aeb0397e143c8dafa5/href">https://medium.com/media/fdf079587194d4aeb0397e143c8dafa5/href</a></iframe><h3>Predicting ratings using regression instead of classification</h3><p>Since ratings have an order, and a prediction of 3.6 might be better than rounding off to 4 in many cases, it is helpful to explore this as a regression problem. Not surprisingly, this approach gives us the lowest error of just 0.799 because we don’t have just integer predictions anymore.</p><p>The only change to our model is that instead of the final layer having 5 outputs, we have just one. The training loop changes a bit too, we use MSE loss and we don’t need to take the argmax anymore to get the final prediction.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D54&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D54&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/df22bdcd723b6ea687a0954853bde14c/href">https://medium.com/media/df22bdcd723b6ea687a0954853bde14c/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D57&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Flstm-multiclass-text-classification%2Fv%2F5%26cellId%3D57&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/ab90753c527228ea7dfe454df1a5544f/href">https://medium.com/media/ab90753c527228ea7dfe454df1a5544f/href</a></iframe><h3>Conclusion:</h3><p>LSTM appears to be theoretically involved, but its Pytorch implementation is pretty straightforward. Also, while looking at any problem, it is very important to choose the right metric, in our case if we’d gone for accuracy, the model seems to be doing a very bad job, but the RMSE shows that it is off by less than 1 rating point, which is comparable to human performance!</p><p><strong>References</strong>:</p><ul><li><a href="https://www.usfca.edu/data-institute/certificates/deep-learning-part-one">https://www.usfca.edu/data-institute/certificates/deep-learning-part-one</a></li><li><a href="https://colah.github.io/posts/2015-08-Understanding-LSTMs/">https://colah.github.io/posts/2015-08-Understanding-LSTMs/</a></li><li><a href="http://web.stanford.edu/class/cs224n/">http://web.stanford.edu/class/cs224n/</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eac56baed8df" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/multiclass-text-classification-using-lstm-in-pytorch-eac56baed8df">Multiclass Text Classification using LSTM in Pytorch</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Image Processing Techniques for Computer Vision]]></title>
            <link>https://medium.com/data-science/image-processing-techniques-for-computer-vision-11f92f511e21?source=rss-f30e60797a83------2</link>
            <guid isPermaLink="false">https://medium.com/p/11f92f511e21</guid>
            <category><![CDATA[computer-vision]]></category>
            <category><![CDATA[image-processing]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[image]]></category>
            <dc:creator><![CDATA[Aakanksha NS]]></dc:creator>
            <pubDate>Wed, 05 Feb 2020 14:32:32 GMT</pubDate>
            <atom:updated>2020-02-05T18:58:12.026Z</atom:updated>
            <content:encoded><![CDATA[<h3>Image Processing and Data Augmentation Techniques for Computer Vision</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JoWEV6jvmno0MHF_T9ei7Q.jpeg" /><figcaption><a href="https://www.topbots.com/most-important-ai-computer-vision-research/">source</a></figcaption></figure><p>Image Processing is an integral part of Computer vision. We almost always want to resize images, do data augmentation, see images in a grid, etc. <a href="https://docs.opencv.org/master/d9/df8/tutorial_root.html">OpenCV</a> (Open source computer vision), <a href="https://scikit-image.org/">scikit-image</a>, <a href="https://pillow.readthedocs.io/en/stable/index.html">Pillow</a> are some popular image processing libraries in Python. In this article, I’ve covered some of the most commonly used Image processing techniques.</p><p>Here’s the Jupyter notebook I’ve used for the article: <a href="https://jovian.ml/aakanksha-ns/image-processing">https://jovian.ml/aakanksha-ns/image-processing</a></p><h3>1) Reading an Image</h3><p>Images are represented as arrays consisting of pixel values. 8-bit images have pixel values ranging from 0 (black) to 255 (white). Depending on the color scale there are various <a href="https://en.wikipedia.org/wiki/Channel_(digital_image)">channels</a> in an image, each channel representing the pixel values for one particular color. RGB (Red, green, blue) is the most commonly used color scale and all images I’ve used in my examples are RGB images.</p><p>We can easily read the image array using the imread function from OpenCV. One thing to remember here is that OpenCV reads images in BGR order by default.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F1%26cellId%3D6&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F1%26cellId%3D6&amp;image=https%3A%2F%2Favatars0.githubusercontent.com%2Fu%2F15630475%3Fv%3D4&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/37aede6a4ff12def89fae1a625118305/href">https://medium.com/media/37aede6a4ff12def89fae1a625118305/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F1%26cellId%3D7&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F1%26cellId%3D7&amp;image=https%3A%2F%2Favatars0.githubusercontent.com%2Fu%2F15630475%3Fv%3D4&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/d632e8beda19aa6c83bb48c382ac0bcc/href">https://medium.com/media/d632e8beda19aa6c83bb48c382ac0bcc/href</a></iframe><h3>2) Cropping</h3><p>Cropping is a widely used augmentation technique. However, be careful as to not crop important parts of the image (pretty obvious, but easy to miss when you have too many images of various different sizes). Since images are represented using arrays, cropping is equivalent to taking out a slice from an array:</p><p><strong>Center crop:</strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F1%26cellId%3D11&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F1%26cellId%3D11&amp;image=https%3A%2F%2Favatars0.githubusercontent.com%2Fu%2F15630475%3Fv%3D4&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/e0dd48c79b36d74898265a0b43a1de8a/href">https://medium.com/media/e0dd48c79b36d74898265a0b43a1de8a/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F1%26cellId%3D12&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F1%26cellId%3D12&amp;image=https%3A%2F%2Favatars0.githubusercontent.com%2Fu%2F15630475%3Fv%3D4&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/8664c326fab2c9b548eed18b1722f171/href">https://medium.com/media/8664c326fab2c9b548eed18b1722f171/href</a></iframe><p><strong>Random crop:</strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F1%26cellId%3D14&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F1%26cellId%3D14&amp;image=https%3A%2F%2Favatars0.githubusercontent.com%2Fu%2F15630475%3Fv%3D4&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/b3a70d2615a9e50839e988d0d0a5f5b8/href">https://medium.com/media/b3a70d2615a9e50839e988d0d0a5f5b8/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F1%26cellId%3D16&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F1%26cellId%3D16&amp;image=https%3A%2F%2Favatars0.githubusercontent.com%2Fu%2F15630475%3Fv%3D4&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/2cf3f55738164e87734b1612e576e201/href">https://medium.com/media/2cf3f55738164e87734b1612e576e201/href</a></iframe><h3>3) Resizing</h3><p>Most deep learning model architectures expect all input images to be of the same dimensions.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F1%26cellId%3D18&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F1%26cellId%3D18&amp;image=https%3A%2F%2Favatars0.githubusercontent.com%2Fu%2F15630475%3Fv%3D4&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/df06ccaf17a1925d724b7ad6b993823b/href">https://medium.com/media/df06ccaf17a1925d724b7ad6b993823b/href</a></iframe><h3>4) Flipping image</h3><p>This is another very popular image augmentation technique. The only thing to remember here is that the flipping should make sense for your use case. For example, if you’re classifying building types, you wouldn’t encounter any inverted buildings in your test set so it doesn’t make sense to do a vertical flip in this case.</p><p><strong>Vertical Flip:</strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F1%26cellId%3D21&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F1%26cellId%3D21&amp;image=https%3A%2F%2Favatars0.githubusercontent.com%2Fu%2F15630475%3Fv%3D4&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/f9fc4ed48bb7e61a47a5f7e7edceeaa0/href">https://medium.com/media/f9fc4ed48bb7e61a47a5f7e7edceeaa0/href</a></iframe><p><strong>Horizontal Flip:</strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F1%26cellId%3D23&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F1%26cellId%3D23&amp;image=https%3A%2F%2Favatars0.githubusercontent.com%2Fu%2F15630475%3Fv%3D4&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/20b750f090a8049e248bd24420b409b5/href">https://medium.com/media/20b750f090a8049e248bd24420b409b5/href</a></iframe><h3>5) Rotate Image:</h3><p>In most cases, it is okay to rotate the image by a small angle. The naive way of doing this might change the entire orientation of the image in some cases like in this case:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F1%26cellId%3D25&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F1%26cellId%3D25&amp;image=https%3A%2F%2Favatars0.githubusercontent.com%2Fu%2F15630475%3Fv%3D4&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/4161f1a31b9fa03fec3d3d4a6c33a9cf/href">https://medium.com/media/4161f1a31b9fa03fec3d3d4a6c33a9cf/href</a></iframe><p>Hence, a better way of rotating is by doing an affine transform using OpenCV. An affine transformation preserves collinearity and ratios of distances (eg: the midpoint of a line segment continues to remain the midpoint even after transformation). You can also fill the borders by using the BORDER_REFLECT flag.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F1%26cellId%3D27&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F1%26cellId%3D27&amp;image=https%3A%2F%2Favatars0.githubusercontent.com%2Fu%2F15630475%3Fv%3D4&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/7cd51f9f134ef77407fda6bec11031fb/href">https://medium.com/media/7cd51f9f134ef77407fda6bec11031fb/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F1%26cellId%3D28&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F1%26cellId%3D28&amp;image=https%3A%2F%2Favatars0.githubusercontent.com%2Fu%2F15630475%3Fv%3D4&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/03e99bd846cae31082c02e532a505fad/href">https://medium.com/media/03e99bd846cae31082c02e532a505fad/href</a></iframe><h3>6) Change Brightness and Contrast:</h3><p>This involves applying the following function to each pixel:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/260/1*Ec2ulriPY9293I9mDIlz8A.png" /></figure><p>Here alpha (&gt;0) is called gain and beta is called bias, these parameters are said to control contrast and brightness respectively. Since we represent images using arrays, this function can be applied to each pixel by traversing through the array:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F1%26cellId%3D30&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F1%26cellId%3D30&amp;image=https%3A%2F%2Favatars0.githubusercontent.com%2Fu%2F15630475%3Fv%3D4&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/dce8dcd5f1da2744068e4c812bb333da/href">https://medium.com/media/dce8dcd5f1da2744068e4c812bb333da/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F2%26cellId%3D31&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F2%26cellId%3D31&amp;image=https%3A%2F%2Favatars0.githubusercontent.com%2Fu%2F15630475%3Fv%3D4&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/0fb0e76a4d6827708751d97930530b3b/href">https://medium.com/media/0fb0e76a4d6827708751d97930530b3b/href</a></iframe><p>However, this could take a while on bigger images (like the one below), so you’d want to use the optimized library functions:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F1%26cellId%3D33&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F1%26cellId%3D33&amp;image=https%3A%2F%2Favatars0.githubusercontent.com%2Fu%2F15630475%3Fv%3D4&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/9e8a7c48d4c2008e3e711697147fcac6/href">https://medium.com/media/9e8a7c48d4c2008e3e711697147fcac6/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F1%26cellId%3D34&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F1%26cellId%3D34&amp;image=https%3A%2F%2Favatars0.githubusercontent.com%2Fu%2F15630475%3Fv%3D4&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/4c3dd92ebd0310e51f8907730bf84a38/href">https://medium.com/media/4c3dd92ebd0310e51f8907730bf84a38/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F2%26cellId%3D35&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F2%26cellId%3D35&amp;image=https%3A%2F%2Favatars0.githubusercontent.com%2Fu%2F15630475%3Fv%3D4&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/2b1db743ab75cb6908d54955de804863/href">https://medium.com/media/2b1db743ab75cb6908d54955de804863/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F2%26cellId%3D36&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F2%26cellId%3D36&amp;image=https%3A%2F%2Favatars0.githubusercontent.com%2Fu%2F15630475%3Fv%3D4&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/36ccaa6bedaf8bfbdbf5b37a77530a29/href">https://medium.com/media/36ccaa6bedaf8bfbdbf5b37a77530a29/href</a></iframe><h3>7) Displaying Bounding Box:</h3><p>Object detection is a very popular computer vision problem that involves finding a bounding box enclosing the object of interest. Displaying the bounding box on the picture can help us visually inspect the problem and requirements. One thing to remember while dealing with these problems is that if you’re planning to flip the image, make sure you flip the box coordinates accordingly too. Here’s an easy way to display an image with its bounding box.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F2%26cellId%3D38&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F2%26cellId%3D38&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Flogo-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/61f129c6e99a62a16d2320367ed7308c/href">https://medium.com/media/61f129c6e99a62a16d2320367ed7308c/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F2%26cellId%3D39&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F2%26cellId%3D39&amp;image=https%3A%2F%2Favatars0.githubusercontent.com%2Fu%2F15630475%3Fv%3D4&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/99a3c73bf7725fa0084e0d0b9140d8fe/href">https://medium.com/media/99a3c73bf7725fa0084e0d0b9140d8fe/href</a></iframe><h3>8) Showing multiple images in a grid:</h3><p>Often we want to inspect multiple images in one go. It can be easily done using subplots in matplotlib.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F2%26cellId%3D41&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F2%26cellId%3D41&amp;image=https%3A%2F%2Favatars0.githubusercontent.com%2Fu%2F15630475%3Fv%3D4&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/e95786d83f198465551ed6216cf96456/href">https://medium.com/media/e95786d83f198465551ed6216cf96456/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F2%26cellId%3D42&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F2%26cellId%3D42&amp;image=https%3A%2F%2Favatars0.githubusercontent.com%2Fu%2F15630475%3Fv%3D4&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/547ef3fca23c43425079639e49828062/href">https://medium.com/media/547ef3fca23c43425079639e49828062/href</a></iframe><h3>9) Converting images to Black and white:</h3><p>Although not widely used in computer vision, it’s nice to know how to convert color images to greyscale.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F2%26cellId%3D44&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F2%26cellId%3D44&amp;image=https%3A%2F%2Favatars0.githubusercontent.com%2Fu%2F15630475%3Fv%3D4&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/e85a5cca92b8ba08c62ad56838de25ec/href">https://medium.com/media/e85a5cca92b8ba08c62ad56838de25ec/href</a></iframe><h3>10) Blur:</h3><p>This technique can be useful in making your model more robust to image quality issues. If a model can perform well on blurred images, it may indicate the model is doing well in general.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F2%26cellId%3D46&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Faakanksha-ns%2Fimage-processing%2Fv%2F2%26cellId%3D46&amp;image=https%3A%2F%2Favatars0.githubusercontent.com%2Fu%2F15630475%3Fv%3D4&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/e13e902c5fe9359212354119debbda74/href">https://medium.com/media/e13e902c5fe9359212354119debbda74/href</a></iframe><p>Of course, there are a lot more problem-specific image processing techniques. For example, for auto-driving cars, you might wanna mark other cars on the road and view it from various angles. However, for most problems, the above-mentioned functions should be useful!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=11f92f511e21" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/image-processing-techniques-for-computer-vision-11f92f511e21">Image Processing Techniques for Computer Vision</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deep Learning for Tabular Data using PyTorch]]></title>
            <link>https://medium.com/data-science/deep-learning-for-tabular-data-using-pytorch-1807f2858320?source=rss-f30e60797a83------2</link>
            <guid isPermaLink="false">https://medium.com/p/1807f2858320</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[pytorch]]></category>
            <category><![CDATA[multiclass-classification]]></category>
            <category><![CDATA[embedding]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Aakanksha NS]]></dc:creator>
            <pubDate>Sun, 12 Jan 2020 14:33:34 GMT</pubDate>
            <atom:updated>2020-06-29T05:34:37.148Z</atom:updated>
            <content:encoded><![CDATA[<h4>On a multiclass classification problem</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*czGE98me8cXsQcyALWIAgA.png" /><figcaption><a href="https://design-nation.icons8.com/intro-to-data-tables-design-349f55861803">source</a></figcaption></figure><p>Deep learning has proved to be groundbreaking in a lot of domains like Computer Vision, Natural Language Processing, Signal Processing, etc. However, when it comes to more structured, tabular data consisting of categorical or numerical variables, traditional machine learning approaches (such as Random Forests, XGBoost) are believed to perform better. As expected, Neural nets have caught up and in many instances shown to be performing equally well or even better at times.</p><p>The easiest way to perform deep learning with tabular data is through the <a href="https://docs.fast.ai/tabular.html">fast-ai library</a> and it gives really good results, but it might be a little too abstracted for someone who’s trying to understand what is really going on behind the scenes. Hence, in this article, I’ve covered how to build a simple deep learning model to deal with tabular data in Pytorch on a multiclass classification problem.</p><h3>A little background on Pytorch</h3><p>Pytorch is a popular open-source machine library. It is as simple to use and learn as Python. A few other advantages of using PyTorch are its multi-GPU support and custom data loaders. If you’re unfamiliar with the basics or need a revision, here’s a good place to start:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fvideoseries%3Flist%3DPLyMom0n-MBroupZiLfVSZqK5asX8KfoHL&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D1HWwDUCxGFU&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F1HWwDUCxGFU%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/62e923320b78aac5b7c334f8b49768f6/href">https://medium.com/media/62e923320b78aac5b7c334f8b49768f6/href</a></iframe><h3>Code</h3><p>If you wanna follow along with the code, here’s my Jupyter notebook:</p><p><a href="https://jovian.ml/aakanksha-ns/shelter-outcome">Jovian Viewer</a></p><h3>Dataset</h3><p>I’ve used the Shelter Animal Outcomes Kaggle competition data:</p><p><a href="https://www.kaggle.com/c/shelter-animal-outcomes/data">Shelter Animal Outcomes</a></p><p>It’s a tabular dataset consisting of about 26k rows and 10 columns in the training set. All columns except DateTime are categorical.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3XiJhpnLKKyhLvZVlI41hQ.png" /><figcaption>Sample data from the train set</figcaption></figure><h3>Problem Statement</h3><p>Given certain features about a shelter animal (like age, sex, color, breed), predict its outcome.</p><p>There are 5 possible outcomes: Return_to_owner, Euthanasia, Adoption, Transfer, Died. We are expected to find the probability of an animal’s outcome belonging to each of the 5 categories.</p><h3>Data Preprocessing</h3><p>Although this step depends largely on the particular data and problem, there are two necessary steps that need to be followed:</p><h4>Getting rid of Nan values:</h4><p>Nan (not a number) indicates a missing value in the dataset. The model doesn’t accept Nan values, hence they must be either deleted or replaced.</p><p>For numerical columns, a popular way of dealing with these values is to impute them with 0, mean, median, mode or some other function of the remaining data. Missing values might sometimes indicate an underlying feature in your dataset, so people often create a new binary column corresponding to the column with missing values to record whether the data was missing or not.</p><p>For categorical columns, Nan values can be considered as their own category!</p><h4>Label encoding all categorical columns:</h4><p>Since our model can only take numerical inputs, we convert all our categorical elements to numbers. This means instead of using strings to represent categories, we use numbers. The numbers chosen to represent the categories should be in the range of 0 to the total number of different categories (including Nan ) in the column. This is so that when we create categorical embeddings for the column, we want to be able to index into our embedding matrix which would have one entry for each category. Here’s a simple example of label encoding:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TMJOngp5lOd-J_48Wz5G_g.png" /></figure><p>I’ve used the <a href="https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.LabelEncoder.html">LabelEncoder</a> class from the scikit-learn library to encode the categorical columns. You could define a custom class to do this and keep track of the category labels because you’d need them to encode test data too.</p><h4>Label encoding the target:</h4><p>We also need to label encode the target if it has string entries. Also, make sure you maintain a dictionary mapping the encodings to original values because you’ll need it to figure out the final output of your model.</p><h4>Data Processing particular to the Shelter Outcome problem:</h4><p>Along with the above-mentioned steps, I did a little more processing for the example problem.</p><ol><li>Removed the AnimalID column because it’s unique and won’t help in training.</li><li>Removed the OutcomeSubtype column because it’s a part of the target but we’re not asked to predict it.</li><li>Removed DateTime column because exact Timestamp of when the record was entered didn’t seem like an important feature. In fact, I first tried to split it out into separate month and year columns but later realized that removing the column altogether gave me a better result!</li><li>Removed Name column because it had too many Nan values (more than 10k missing). Also, it did not seem like a very important feature in determining an animal’s outcome.</li></ol><p><strong>Note</strong>: In my notebook, I stacked the train and test columns and then did the preprocessing to avoid having to do label encoding based on the train set labels on the test set (because it would involve maintaining a dictionary of encoded labels to actual values). It was okay to do the stacking and processing here because there are no numerical columns (hence no imputing done) and the number of categories per column was fixed. In practice, we must never do this because it may leak some data from the test/validation sets to the training data and lead to an inaccurate evaluation of the model. For example, if you had missing values in a numerical column like age and decided to impute it with the average value, the average value should be calculated only on the train set (not stacked train-test-valid set) and this value should be used to impute missing values in validation and test sets too.</p><h3>Categorical Embeddings</h3><p><a href="https://arxiv.org/pdf/1604.06737.pdf">Categorical embeddings</a> are very similar to word embeddings which are commonly used in NLP. The basic idea is to have a fixed-length vector representation of each category in the column. How this is different from a one-hot encoding is that instead of having a sparse matrix, using embeddings, we get a dense matrix for each category with similar categories having values close to each other in the embedding space. Hence, this process not only saves up memory (as the one-hot encoding for columns having too many categories can really blow up the input matrix, also it is a very sparse matrix) but also reveals intrinsic properties of the categorical variables.</p><p>For example, if we had a column of colors and we find embeddings for it, we can expect red and pink to be closer in the embedding space than red and blue</p><p>Categorical embedding layers are equivalent to extra layers on top of each one-hot encoded input:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nan-wbyvY4AKSv6wIsL3Bw.png" /><figcaption>source: <a href="https://arxiv.org/pdf/1604.06737.pdf">Entity Embeddings of Categorical Variables</a> research paper</figcaption></figure><p>For our shelter outcome problem, we have only categorical columns but I’ll be considering columns with less than 3 values as continuous. To decide the length of each column’s embedding vector I’ve taken a simple function from the fast-ai library:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e031d3a58a29e9e97682bb175e5a9946/href">https://medium.com/media/e031d3a58a29e9e97682bb175e5a9946/href</a></iframe><h3>Pytorch Dataset and DataLoader</h3><p>We extend the <a href="https://pytorch.org/docs/stable/_modules/torch/utils/data/dataset.html#TensorDataset">Dataset</a> (abstract) class provided by Pytorch for easier access to our dataset while training and for effectively using the DataLoader module to manage batches. This involves overwriting the __len__ and __getitem__ methods as per our particular dataset.</p><p>Since we only need to embed categorical columns, we split our input into two parts: numerical and categorical.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6bc33882b50f71452b0959bd574367aa/href">https://medium.com/media/6bc33882b50f71452b0959bd574367aa/href</a></iframe><p>We then choose our batch size and feed it along with the dataset to the DataLoader. Deep learning is generally done in batches. DataLoader helps us in effectively managing these batches and shuffling the data before training.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/052f03798184d2772a079f1eb5272ca7/href">https://medium.com/media/052f03798184d2772a079f1eb5272ca7/href</a></iframe><p>To do a sanity check, you can iterate through the created DataLoaders to look at each batch:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*afKk4yOMf8Bvc7V6a-aedA.png" /></figure><h3>Model</h3><p>Our data is split into continuous and categorical parts. We first convert the categorical parts into embedding vectors based on the previously determined sizes and concatenate them with the continuous parts to feed to the rest of the network. This picture demonstrates the model I’ve used:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u9nlY-_6w9kKS-k3L5Wh5Q.png" /><figcaption>Shelter outcome model</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c3d996911afe7f45bc31f0cb13e0ab8e/href">https://medium.com/media/c3d996911afe7f45bc31f0cb13e0ab8e/href</a></iframe><h3>Training</h3><p>Now we train the model on the training set. I’ve used the Adam optimizer to optimize the cross-entropy loss. The training is pretty straightforward: iterate through each batch, do a forward pass, compute gradients, do a gradient descent and repeat this process for as many epochs as needed. You can look at <a href="https://jovian.ml/aakanksha-ns/shelter-outcome">my notebook</a> to understand the code.</p><h3>Test Output</h3><p>Since we’re interested in finding the probabilities for each class for our test inputs, we apply a Softmax function over our model output. I also made a Kaggle submission to see how well this model performs:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jc8eQkarx7hnlrLi26Hebw.png" /></figure><p>We’ve done very less feature engineering and data exploration and used a very basic deep learning architecture, yet our model has done better than about 50% of the solutions. This shows that this approach of modeling tabular data using neural networks is pretty powerful!</p><p><strong>References:</strong></p><ol><li><a href="https://www.usfca.edu/data-institute/certificates/fundamentals-deep-learning">https://www.usfca.edu/data-institute/certificates/fundamentals-deep-learning</a> — Lesson 2</li><li><a href="https://jovian.ml/aakashns/04-feedforward-nn">https://jovian.ml/aakashns/04-feedforward-nn</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1807f2858320" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/deep-learning-for-tabular-data-using-pytorch-1807f2858320">Deep Learning for Tabular Data using PyTorch</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Getting Started with Natural Language Processing (NLP)]]></title>
            <link>https://medium.com/data-science/getting-started-with-natural-language-processing-nlp-2c482420cc05?source=rss-f30e60797a83------2</link>
            <guid isPermaLink="false">https://medium.com/p/2c482420cc05</guid>
            <category><![CDATA[word-embeddings]]></category>
            <category><![CDATA[fasttext]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[word-cloud]]></category>
            <category><![CDATA[nlp]]></category>
            <dc:creator><![CDATA[Aakanksha NS]]></dc:creator>
            <pubDate>Sat, 21 Dec 2019 06:27:54 GMT</pubDate>
            <atom:updated>2019-12-21T07:01:06.265Z</atom:updated>
            <content:encoded><![CDATA[<h3>Getting Started with Natural Language Processing (NLP) — preprocessing, word embeddings, text classification, and more!</h3><h4>using simple Python libraries</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HpKh-MGZIq-GyRAiqEUnSw.png" /><figcaption><a href="https://deeplearninganalytics.org/natural-language-processing-machine-learning-why-deep-learning-is-perfect-for-nlp/">source</a></figcaption></figure><p>There’s so much going on in natural language processing these days (GRUs, LSTMs, XLNet, BERT and so on!). It can be confusing figuring out where to begin. This article talks about the basics of natural language processing including data cleaning, normalization, encoding, sentiment analysis and a simple text classifier using basic yet powerful Python libraries. This is often the first step before diving into complicated deep learning models.</p><h3>Data Cleaning and normalization</h3><p>Depending on the nature of the problem, this step may or may not be required. If our model is trying to learn the language to the largest extent, it may be best to use the data in its raw format, in fact, modern deep learning techniques recommend not to remove stop words, emojis or lowercase letters, because they provide additional context. However, if you’re trying to do a trend analysis or classification based on certain word occurrences (like in a <a href="https://en.wikipedia.org/wiki/Bag-of-words_model">bag-of-words</a> model), it helps to perform this step. There are a few common preprocessing steps I’d like to highlight here:</p><ol><li><strong>Removing punctuation</strong>: When trying to train a machine learning model, it helps to reduce overfitting by removing punctuation (like !,* etc.). However, be careful to not remove something important, for example, question marks (?) help to recognize questions.</li><li><strong>Removing emojis:</strong> Sometimes people attach emojis to words without spaces (for example: you❤ ) making it difficult to interpret such words. Removing emojis can help with such cases. Again, be careful while removing these as emojis might actually be really useful for tasks like sentiment analysis and topic classification.</li><li><strong>Removing stop words: </strong>For tasks like data exploration and trend analysis, it might not be very useful to see common words like ‘the’, ‘and’ , ‘of’ etc. The sklearn package actually has a <a href="https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/feature_extraction/_stop_words.py">collection of commonly used English stop words</a> that we can use to remove these.</li><li><strong>Making all text lowercase: </strong>This is the simplest way to normalize text. (after all, BeTTer and better do have the same semantic implication)</li><li><strong>Stemming words: </strong>Another way of normalizing is by replacing derived words with their root form (eg: ‘posting’, ‘posted’, ‘posts’ are all replaced by ‘post’). To stem words we use the <a href="https://tartarus.org/martin/PorterStemmer/">PorterStemmer</a> util provided by nltk.</li><li><strong>Extracting/Removing hashtags and mentions: </strong>Hashtags and mentions can be very useful in identifying trends in your data. It helps to extract them out of your text and analyze them separately.</li></ol><p>Here’s a simple function to perform the above-mentioned tasks:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa263768dd474b0f372afcdca1eacb69/href">https://medium.com/media/fa263768dd474b0f372afcdca1eacb69/href</a></iframe><h3>Word vectors — What are they?</h3><p>Machine learning algorithms are only capable of processing fixed-length numerical inputs i.e, they cannot take string inputs to process textual data! This is where word vectors come in, where we represent each word using vectors of fixed length. Individual word vectors are then used to encode sentences.</p><h4><strong>One-hot encoding:</strong></h4><p>This is the simplest way of encoding words. It assumes a bag-of-words representation where each word is considered as an independent entity and word relationships are ignored (for example, job and occupation are considered as completely independent words even though they practically have the same meaning). This method involves creating a vocabulary of distinct words from the entire corpus, the length of this vocabulary being the length of each word vector. Each vector has a designated index for itself in the word vector and that index is marked 1 while others are marked 0 to represent the particular word.</p><p><strong>Example:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J9-5WuqxuibdBOnxrwjr_w.png" /></figure><p>The vocabulary here consists of 9 distinct words and these words can be one hot encoded into vectors of length 9. The word vector representations aregoing : [1,0,0,0,0,0,0,0,0] good : [0,1,0,0,0,0,0,0,0] and so on..</p><p>Using this representation the text Tomorrow will be a good day can be encoded into: [0,1,1,0,1,1,0,1,0]. Notice how the word will is ignored because it doesn’t exist in the vocabulary at all. Having a good and extensive vocabulary is necessary for making this model work well. Also note how the word relationships (the order of occurrence, semantic relationships) are completely ignored in this representation.</p><h4>Word2Vec word embeddings:</h4><p>This method of word encoding (commonly known as <strong>word embeddings</strong>) takes context into consideration. For example, we can expect the words king and royal to have a smaller spatial distance than parrot and honey . Word2vec uses a shallow two-layer neural network to perform a specific task (based on the method used) and learn weights for the hidden layer for every word. These learned hidden layer weights are used as our final word vectors. You can read the <a href="https://arxiv.org/pdf/1301.3781.pdf">original paper</a> to get an in-depth understanding of how these word vectors are obtained. But at a high level, these are the two common methods of obtaining context-based word vectors using Word2Vec:</p><p><strong>CBOW (Continuous Bag of words):</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/510/1*tdm-R9q0kNP0BnSEvruD-Q.png" /><figcaption>source : <a href="https://arxiv.org/pdf/1301.3781.pdf">https://arxiv.org/pdf/1301.3781.pdf</a></figcaption></figure><p>The CBOW model architecture tries to predict the current target word (the center word) based on the source context words (surrounding words).</p><p><strong>Skip — Gram model:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*9h65DVJOmReTO9d2x26t-Q.png" /><figcaption>source: <a href="https://arxiv.org/pdf/1301.3781.pdf">https://arxiv.org/pdf/1301.3781.pdf</a></figcaption></figure><p>The Skip-gram model architecture tries to achieve the reverse of what the CBOW model does. It tries to predict the source context words (surrounding words) given a target word (the center word).</p><p>The window size (number of surrounding words considered on each side) is a hyperparameter in both cases.</p><h4>Glove:</h4><p>Glove is quite similar to Word2vec but unlike Word2vec, Glove takes advantage of the global co-occurrences of words rather than just the local context which makes it more powerful in some ways. Again, you can get a better understanding by going through the <a href="https://nlp.stanford.edu/pubs/glove.pdf">original paper</a>.</p><h3>Word Embeddings — How do I use them?</h3><p>Now that we have a rough idea about what word embeddings are and why they are useful, let’s talk about how we can use them to our advantage.</p><h4><strong>Using pre-trained word vectors:</strong></h4><p>There are many publicly available pre-trained word vectors of different vector lengths like <a href="https://nlp.stanford.edu/projects/glove/">Glove</a>, <a href="https://fasttext.cc/docs/en/english-vectors.html">fasttext</a>, etc. These have been trained on massive corpora ( Wikipedia, twitter and common crawl datasets) and can be downloaded and used to encode words in our corpus.</p><p><strong>Example: Finding the most similar document to a given document using word vector similarity</strong></p><p>Problem Statement:</p><p>Given a set of documents belonging to different topics (training set), when given a new document can we find the most similar document to it from the original set?</p><p>Approach:</p><ol><li>Load the pre-trained word vector file into a dictionary with the word as key and its vector representation as the value.</li><li>Find the centroid vector for each document in the training set by averaging the word vectors of words that exist in the particular document (ignore words that are not part of the vocabulary)</li><li>Find the centroid of the new document, pick the document from the training set whose centroid is closest to the new document’s centroid (using a suitable measure of similarity, like Euclidean distance, cosine similarity, etc.)</li></ol><p>Code:</p><p>Here’s some helper functions load the glove dictionary, find the centroid and find the distance between centroids:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/973378d96dbba18ab559c16561cae4a4/href">https://medium.com/media/973378d96dbba18ab559c16561cae4a4/href</a></iframe><h4>Training to generate word vectors from scratch:</h4><p>If you want to find word vectors for your particular corpus, you can use the gensim package for training.</p><p><strong>Example:</strong></p><p>Code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/94f92b711ef6323be80f69c93f541dfa/href">https://medium.com/media/94f92b711ef6323be80f69c93f541dfa/href</a></iframe><p>Output:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zt9axCd5lF8oXjs0XuE1DQ.png" /></figure><p>In the example above, I’ve just used two lines from <a href="https://en.wikipedia.org/wiki/Potato">this Wikipedia page</a>. The training is very fast and simple, all you need to input is the list of words, size of the word vectors you need, the window size (number of surrounding words to be considered), and the minimum number of occurrences of the word for it to be considered as part of the vocabulary. It’s easy to inspect the vocabulary, obtain the vector and also look at the most common words from the corpus. Of course, training from scratch might not always yield as good results as the pre-trained ones but it is good for problems that involves data that looks very different from the datasets used in pretraining.</p><h3>Data Exploration</h3><p>EDA with text data is not as straightforward as tabular or numerical data. However, there are some libraries that can make these tasks easier. For the rest of this article I’ve used the following dataset from Kaggle:</p><p><a href="https://www.kaggle.com/c/jigsaw-toxic-comment-classification-challenge/data">Toxic Comment Classification Challenge</a></p><h4>Exploration using spacy:</h4><p>Spacy is a very powerful NLP library that has a variety of uses. It can be used for named entity recognition, identifying the part of speech a word belongs to and even give the word vector and sentiment of the word.</p><p>Code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f9673268e252401e816500f3452fb0fa/href">https://medium.com/media/f9673268e252401e816500f3452fb0fa/href</a></iframe><p>Output:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xfBtTqeUzatGcGIv-56w-A.png" /></figure><p>The nlp function from spacy converts each word into a token having various attributes like the ones mentioned in the above example.</p><h4>Wordcloud:</h4><p>Wordclouds are a simple yet interesting way to visualize how frequently various words appear in our corpus. Let’s take the most frequently occurring nouns in our comments’ data for example:</p><p>Code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cc326bfad37ba763c2803cca51695a2e/href">https://medium.com/media/cc326bfad37ba763c2803cca51695a2e/href</a></iframe><p>Output:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vv8YNv2eOTFQbGuYX6r-pg.png" /><figcaption>article, talk, and page are the most frequently occurring nouns</figcaption></figure><h3>Sentiment Analysis</h3><p>A very common task in NLP is identifying how positive or negative a particular comment or piece of text is. The vaderSentiment package provides a quick and easy way to do this:</p><p>Code:</p><pre>from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer<br>sentiment_analyzer = SentimentIntensityAnalyzer()</pre><pre>def get_sentiment_score(text):<br>    return sentiment_analyzer.polarity_scores(text)[&#39;compound&#39;]</pre><p>Output:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*106P5WiWxV15suIfXScRPw.png" /><figcaption>The sentiment score for a toxic comment seems to be close to -1 whereas a non-toxic one has a score greater than 0 as expected</figcaption></figure><h3>Text Classification</h3><p>For classification, one of the easiest libraries I’ve used is fasttext . It was released by Facebook in 2016 and uses<strong> </strong>a linear technique both for combining the word vectors into the vector representing the text and for computing the classification criterion. It takes very little time to train and gives decent results for most common text-classification problems. It can be used to come up with a baseline model. You can read the <a href="https://arxiv.org/pdf/1607.01759.pdf">original paper</a> to get a better understanding of the mechanics behind the fasttext classifier.</p><p>This is how I used <a href="https://fasttext.cc/docs/en/supervised-tutorial.html">fasttext</a> to classify toxic vs non-toxic comments:</p><p><a href="https://jovian.ml/aakanksha-ns/toxic-comments">Jovian Viewer</a></p><p>We’ve touched upon most of the basics but of course, there’s a lot more to NLP. However, this article is a good starting point and hopefully helpful for beginners because these were the first things I learned when I started off!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2c482420cc05" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/getting-started-with-natural-language-processing-nlp-2c482420cc05">Getting Started with Natural Language Processing (NLP)</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Exploratory Data Analysis (EDA) —  Understanding the Gender Divide in Data Science Roles]]></title>
            <link>https://medium.datadriveninvestor.com/exploratory-data-analysis-eda-understanding-the-gender-divide-in-data-science-roles-9faa5da44f5b?source=rss-f30e60797a83------2</link>
            <guid isPermaLink="false">https://medium.com/p/9faa5da44f5b</guid>
            <category><![CDATA[gender-pay-gap]]></category>
            <category><![CDATA[eda]]></category>
            <category><![CDATA[pandas]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[ggplot2]]></category>
            <dc:creator><![CDATA[Aakanksha NS]]></dc:creator>
            <pubDate>Wed, 30 Oct 2019 04:32:52 GMT</pubDate>
            <atom:updated>2019-11-27T11:41:32.076Z</atom:updated>
            <content:encoded><![CDATA[<h3>Exploratory Data Analysis (EDA) — Understanding the Gender Divide in Data Science Roles</h3><p>with <a href="https://medium.com/u/3574e9a9e5c1">Shreejaya Bharathan</a> on 2018 Kaggle ML &amp; DS Survey data</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FTuQGa555Om5CmnUSWHjJQ.png" /></figure><p>Women have been historically underrepresented in STEM fields and face discrimination in the workplace. According to a study conducted in 2018, “63 percent of the time, women receive lower salary offers than men for the same job at the same company.’’</p><p>Does the Data Science industry inherit these biases from other traditional industries?</p><h4>Dataset</h4><p>We chose the 2018 Kaggle ML &amp; DS Survey to analyze the gender divide in Data Science. The survey has information about education, skills, age, income and roles which are relevant for our study.</p><p><a href="https://www.kaggle.com/kaggle/kaggle-survey-2018">2018 Kaggle ML &amp; DS Survey</a></p><p>We also used the 2017 version of the survey for comparative analysis.</p><p><a href="https://www.kaggle.com/kaggle/kaggle-survey-2017">Kaggle Machine Learning &amp; Data Science Survey 2017</a></p><p>Since most data scientists use Kaggle either to get datasets, collaborate or participate in data challenges and the survey has over 23K responses, we considered it to be a good representation of the whole community.</p><h4>EDA and visualization</h4><p>We preprocessed the data by removing all responses that took less than 5 minutes to complete, as there were about 50 questions and a genuine response would take longer than 5 minutes. Next, since we are doing a comparison between males and females, we removed all other genders and missing values. All data manipulation was done using pandas and plots created using ggplot2.</p><p>Check out our code here:</p><p><a href="https://jovian.ml/aakanksha-ns/gender-divide">Jovian - Share Jupyter notebooks instantly</a></p><h4>Only 16% of the respondents were women</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OiCY5FrmpbC3Pc49MMbDtQ.jpeg" /><figcaption>% Respondents 2018 — Female vs Male</figcaption></figure><p>It was quite shocking to see that such a small portion of the community is comprised of women.</p><h4>United States has the highest portion of women in Data Science</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kgCV4inTMHJ6--5SD4Xgpg.jpeg" /><figcaption>% Women by countries — 2017 vs 2018</figcaption></figure><p>We then compared the portion of women in 5 countries having the most number of respondents and how this portion changed from 2017 to 2018. We discovered that in the US, almost 1/4th of the data science community is women, and it’s doing much better than other countries in terms of the gender ratio. For Russia, the percentage of women actually seems to be going down!</p><h4>Women and Men have a similar distribution of Roles</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U6p48OlUrnZyAdJW2apJ5g.png" /></figure><p>It’s good to see that men and women have roughly the same distribution of roles. About half of the respondents are Data Scientists, 20–30 % Data Analysts and the rest are Research Scientists, Data Engineers, or DBA/Database Engineers.</p><p><a href="https://www.datadriveninvestor.com/2019/01/23/which-is-more-promising-data-science-or-software-engineering/">Which is More Promising: Data Science or Software Engineering? | Data Driven Investor</a></p><h4>Women hold more advanced degrees than men</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xY55P2K8VDIsoMg65E8o8A.png" /></figure><p>The percentage of respondents holding advanced degrees like a Master’s or Ph.D is greater for women.</p><h4>Women with the same level of education earn less than men</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zRhBim83cEwFEc17LcWkSA.png" /></figure><p>It was really sad to see that the highest average salary earned by women (for Ph.D) is, in fact, lower than the average salaries earned by men of any level of education. Women earn about 20–25% percent lesser than men at any given level of education!</p><h4>Income gap increases with age</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dGZ8j9eAj5spuRXazMus2Q.png" /></figure><p>Women and men tend to start off with similar salaries but the gap between salaries gets wider and wider as we look at higher age groups. This suggests that a lesser percentage of women are considered for promotions and higher roles.</p><h4>Summary</h4><p>The Kaggle DS and ML surveys gave us a thought-provoking insight into the state of women in Data Science. Being a considerably new field, it’s not too late for us to ensure that the Data Science community is inclusive and welcoming to everyone.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Fb2a0d6%3Fas_embed%3Dtrue&amp;dntp=1&amp;display_name=Upscribe&amp;url=https%3A%2F%2Fupscri.be%2Fb2a0d6%2F&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/0707f5c806284d01a4a13c7b13a91ce3/href">https://medium.com/media/0707f5c806284d01a4a13c7b13a91ce3/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9faa5da44f5b" width="1" height="1" alt=""><hr><p><a href="https://medium.datadriveninvestor.com/exploratory-data-analysis-eda-understanding-the-gender-divide-in-data-science-roles-9faa5da44f5b">Exploratory Data Analysis (EDA) —  Understanding the Gender Divide in Data Science Roles</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[Creating a web application powered by a fastai model]]></title>
            <link>https://medium.com/usf-datascience/creating-a-web-application-powered-by-a-fastai-model-d5ee560d5207?source=rss-f30e60797a83------2</link>
            <guid isPermaLink="false">https://medium.com/p/d5ee560d5207</guid>
            <category><![CDATA[firebase]]></category>
            <category><![CDATA[render]]></category>
            <category><![CDATA[fastai]]></category>
            <category><![CDATA[flask]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Aakanksha NS]]></dc:creator>
            <pubDate>Sun, 18 Aug 2019 19:01:48 GMT</pubDate>
            <atom:updated>2019-11-27T23:14:05.421Z</atom:updated>
            <content:encoded><![CDATA[<p>using React, Flask, Render.com, and Firebase</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FqOIIs0Ty7YhpfyC3uSeJg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/293/1*UeJ9vTsOHlefcpomnd1VBA.gif" /></figure><p>Developing an end-to-end Machine Learning based web-app can appear quite daunting. However, with libraries like <a href="https://www.fast.ai/">fastai</a>, training models has become a lot easier compared to the past. Deployment takes less than ten minutes with sites like <a href="https://render.com/">Render.com</a> and <a href="https://firebase.google.com/">Firebase</a>.</p><p>This article describes a simple, step by step approach to building a simple flower classification app. It takes an image input and returns the predicted flower category along with the probabilities of each of the classes. Here’s a live demo of the application I’ve built:</p><p><a href="https://flower-classifier-dad75.firebaseapp.com/">Flower Classifier</a></p><h4>Dataset</h4><p>I’ve used the flower recognition dataset from Kaggle:</p><p><a href="https://www.kaggle.com/alxmamaev/flowers-recognition">Flowers Recognition</a></p><p>It consists of 700–1000 images each of five types of flowers: daisy, dandelion, rose, sunflower, and tulip. If you wish to construct your own dataset using Google images follow the steps mentioned in the following article:</p><p><a href="https://medium.com/@aakanksha.ns/building-a-movie-genre-classifier-using-a-dataset-created-using-google-images-4752f75a1d79">Building a movie genre classifier using a dataset created using Google Images</a></p><p>Let’s begin building our web app!</p><h3>Step 1: Train your model</h3><p>I’ve explained in detail how to train a model using fastai through transfer learning in the following article:</p><p><a href="https://medium.com/@aakanksha.ns/building-a-flower-classifier-using-fast-ai-a815f9b25a04">Building a Flower Classifier using Fast.ai</a></p><p>After training, the accuracy of my model was 94.4%</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/628/1*6tvc9BgBFYqkCzzjx8oI-Q.png" /></figure><h3>Step 2: Export trained model</h3><p>Once we have trained our model, to put our model in production we export the minimal state of our <a href="https://docs.fast.ai/basic_train.html#Learner">Learne</a>r. A PKL file is created using <a href="https://www.datacamp.com/community/tutorials/pickle-python-tutorial">pickle, a Python module</a> that enables objects to be serialized to files on disk and deserialized back into the program at runtime.</p><pre>learn.export(‘trained_model.pkl’)</pre><p>Once exported, download the trained_model.pkl file.</p><h3>Step 3: Create a REST API using Flask</h3><p><a href="http://flask.palletsprojects.com/en/1.1.x/"><strong>Flask</strong></a> is a popular Python web framework. To load our model, we use the load_learner function from fastai.basic_train module to load the .pkl file we exported in step 2. We open our image obtained from the request using open_image from fastai.vision module and then call the predict function over the image to get our prediction. We then return this result in the form of a JSON object. The following code goes into our app.py file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bf4c72ed297ccaad1ebbf83ad5e8aaa7/href">https://medium.com/media/bf4c72ed297ccaad1ebbf83ad5e8aaa7/href</a></iframe><p>Additionally for our server to know what dependencies to install we would need a requirements.txt file that consists of:</p><pre>Flask<br>gunicorn<br>fastai<br>torch<br>flask_cors</pre><h3>Step 4: Deploy the API on Render.com</h3><p>For this step, we’ll require our app.py, requiements.txt and trained_model.pkl files to be on a <a href="https://help.github.com/en/articles/create-a-repo">Github repo</a>. Once we have pushed our code into a GitHub repo (let’s call it flower_classifier), we do the following:</p><ol><li>Go to <a href="https://render.com/">render.com</a> and login/sign up.</li><li>Go to services -&gt; New web service.</li><li>Connect the flower_classifier GitHub repo to Render</li><li>Use the following values during creation:</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*67BbxLbjEXHbh1N7WZ2FnQ.png" /></figure><p>Make sure you verify that the service is functioning the way it is supposed to once it’s deployed. I’ve used Postman to verify this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I3JYbfiZPAsYEPHl8jy46A.png" /></figure><h3>Step 5: Build UI</h3><p>I’ve used <a href="https://reactjs.org/">ReactJs</a> to build my frontend and a fetch API call to get my prediction. You can check out my code here:</p><p><a href="https://github.com/aakanksha-ns/flower-classifier/tree/master/flower-classifier-react">aakanksha-ns/flower-classifier</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/622/1*iBcpPHcr8r9KY92Wvzuivg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/632/1*rVL9ONTcvA1NxGKs93KvNw.png" /></figure><h3>Step 6: Deploy web-app on Firebase</h3><p>We’re almost done! The final and simplest step is to deploy our web-app on Firebase. To do this we first create a production build using:</p><pre>npm run build</pre><p>Then:</p><ol><li>Go to <a href="https://firebase.google.com/">https://firebase.google.com/</a> -&gt; login/sign up</li><li>Go to Console-&gt;Add Project</li><li>Enter the following commands on the command line:</li></ol><pre>$ npm install -g firebase-tools <br>$ firebase login<br>$ firebase init</pre><p>4. Select ‘Hosting’ when prompted</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qd3PD4K5S8kpiveTQjcHHg.png" /></figure><p>5. Deploy using:</p><pre>$ firebase deploy</pre><p>And we’re done!</p><h4>Few issues that I ran into:</h4><p>1)I tried deploying the API using Heroku instead of Render (because it’s free), however because of the dependencies being too large I faced the following error:</p><p><em>Compiled slug size: 997.6M is too large (max is 500M)</em></p><p>2)I also came across a CORS related issue while trying to call the API from my local machine while developing the UI. I handled it using flask_cors.</p><p><em>No ‘Access-Control-Allow-Origin’ header is present on the requested resource</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d5ee560d5207" width="1" height="1" alt=""><hr><p><a href="https://medium.com/usf-datascience/creating-a-web-application-powered-by-a-fastai-model-d5ee560d5207">Creating a web application powered by a fastai model</a> was originally published in <a href="https://medium.com/usf-datascience">USF-Data Science</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>