<?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 Olga Petrova on Medium]]></title>
        <description><![CDATA[Stories by Olga Petrova on Medium]]></description>
        <link>https://medium.com/@OlgaPaints?source=rss-3ec417445414------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*hZA5Ih4glO9qpG93KQERQg.jpeg</url>
            <title>Stories by Olga Petrova on Medium</title>
            <link>https://medium.com/@OlgaPaints?source=rss-3ec417445414------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 14:25:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@OlgaPaints/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[Installing CVAT (Intel’s Computer Vision Annotation Tool) on the cloud]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/data-science/installing-cvat-intels-computer-vision-annotation-tool-on-the-cloud-c7759ae28f0e?source=rss-3ec417445414------2"><img src="https://cdn-images-1.medium.com/max/1401/1*po9SJcQl65LC_r5muWaeww.png" width="1401"></a></p><p class="medium-feed-snippet">Smart data annotation for your computer vision projects</p><p class="medium-feed-link"><a href="https://medium.com/data-science/installing-cvat-intels-computer-vision-annotation-tool-on-the-cloud-c7759ae28f0e?source=rss-3ec417445414------2">Continue reading on TDS Archive »</a></p></div>]]></description>
            <link>https://medium.com/data-science/installing-cvat-intels-computer-vision-annotation-tool-on-the-cloud-c7759ae28f0e?source=rss-3ec417445414------2</link>
            <guid isPermaLink="false">https://medium.com/p/c7759ae28f0e</guid>
            <category><![CDATA[cloud]]></category>
            <category><![CDATA[dataannotations]]></category>
            <category><![CDATA[data-annotation-tools]]></category>
            <category><![CDATA[computer-vision]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Olga Petrova]]></dc:creator>
            <pubDate>Thu, 01 Apr 2021 20:29:43 GMT</pubDate>
            <atom:updated>2022-07-02T00:39:30.895Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Active and Semi-Supervised machine learning: Nov 16–Dec 4]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/data-science/active-and-semi-supervised-machine-learning-nov-16-dec-4-e0120a3b49a3?source=rss-3ec417445414------2"><img src="https://cdn-images-1.medium.com/max/2600/1*BShmyto3BhaA0NUaESehgw.jpeg" width="5472"></a></p><p class="medium-feed-snippet">A selection of the latest arXiv preprints concerning active (and occasionally semi- or weakly-supervised) deep learning</p><p class="medium-feed-link"><a href="https://medium.com/data-science/active-and-semi-supervised-machine-learning-nov-16-dec-4-e0120a3b49a3?source=rss-3ec417445414------2">Continue reading on TDS Archive »</a></p></div>]]></description>
            <link>https://medium.com/data-science/active-and-semi-supervised-machine-learning-nov-16-dec-4-e0120a3b49a3?source=rss-3ec417445414------2</link>
            <guid isPermaLink="false">https://medium.com/p/e0120a3b49a3</guid>
            <category><![CDATA[active-learning-news]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Olga Petrova]]></dc:creator>
            <pubDate>Wed, 09 Dec 2020 04:35:03 GMT</pubDate>
            <atom:updated>2020-12-09T04:35:03.815Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Active and Semi-Supervised machine learning: Oct 26 — Nov 13]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/data-science/active-and-semi-supervised-machine-learning-oct-26-nov-13-35f11a03e6c9?source=rss-3ec417445414------2"><img src="https://cdn-images-1.medium.com/max/2600/1*qkQ6WffvxxxE_zwAEk6qiA.jpeg" width="4368"></a></p><p class="medium-feed-snippet">A selection of the latest arXiv preprints concerning active (and occasionally semi- or weakly-supervised) deep learning</p><p class="medium-feed-link"><a href="https://medium.com/data-science/active-and-semi-supervised-machine-learning-oct-26-nov-13-35f11a03e6c9?source=rss-3ec417445414------2">Continue reading on TDS Archive »</a></p></div>]]></description>
            <link>https://medium.com/data-science/active-and-semi-supervised-machine-learning-oct-26-nov-13-35f11a03e6c9?source=rss-3ec417445414------2</link>
            <guid isPermaLink="false">https://medium.com/p/35f11a03e6c9</guid>
            <category><![CDATA[active-learning-news]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Olga Petrova]]></dc:creator>
            <pubDate>Wed, 18 Nov 2020 04:42:43 GMT</pubDate>
            <atom:updated>2020-11-18T04:42:43.586Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[VAE: giving your Autoencoder the power of imagination]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/scaleway-cloud/vae-giving-your-autoencoder-the-power-of-imagination-461378a539dd?source=rss-3ec417445414------2"><img src="https://cdn-images-1.medium.com/max/1320/1*C_XG4fjZuwbsVJIL2yXUqA.png" width="1320"></a></p><p class="medium-feed-snippet">Take a detailed look at the generative machine learning algorithm called the Variational Autoencoder. Stay tuned for the PyTorch&#x2026;</p><p class="medium-feed-link"><a href="https://medium.com/scaleway-cloud/vae-giving-your-autoencoder-the-power-of-imagination-461378a539dd?source=rss-3ec417445414------2">Continue reading on Scaleway »</a></p></div>]]></description>
            <link>https://medium.com/scaleway-cloud/vae-giving-your-autoencoder-the-power-of-imagination-461378a539dd?source=rss-3ec417445414------2</link>
            <guid isPermaLink="false">https://medium.com/p/461378a539dd</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[generative-model]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <dc:creator><![CDATA[Olga Petrova]]></dc:creator>
            <pubDate>Thu, 05 Nov 2020 05:57:00 GMT</pubDate>
            <atom:updated>2021-05-10T13:24:02.878Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Active and Semi-Supervised machine learning: Oct 12–23]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/data-science/active-and-semi-supervised-machine-learning-oct-12-23-6bfe5470253d?source=rss-3ec417445414------2"><img src="https://cdn-images-1.medium.com/max/2600/1*udERcstFK-Bsd18uHnLofw.jpeg" width="3978"></a></p><p class="medium-feed-snippet">A selection of the latest arXiv preprints concerning active (and occasionally semi- or weakly-supervised) deep learning</p><p class="medium-feed-link"><a href="https://medium.com/data-science/active-and-semi-supervised-machine-learning-oct-12-23-6bfe5470253d?source=rss-3ec417445414------2">Continue reading on TDS Archive »</a></p></div>]]></description>
            <link>https://medium.com/data-science/active-and-semi-supervised-machine-learning-oct-12-23-6bfe5470253d?source=rss-3ec417445414------2</link>
            <guid isPermaLink="false">https://medium.com/p/6bfe5470253d</guid>
            <category><![CDATA[active-learning-news]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Olga Petrova]]></dc:creator>
            <pubDate>Wed, 28 Oct 2020 03:07:40 GMT</pubDate>
            <atom:updated>2020-10-28T03:07:40.185Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Active and Semi-Supervised machine learning: Sep 28–Oct 9]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/data-science/active-and-semi-supervised-machine-learning-sep-28-oct-9-50813688c616?source=rss-3ec417445414------2"><img src="https://cdn-images-1.medium.com/max/2600/1*CH-HrHzADceuGzkSGldVAQ.jpeg" width="5184"></a></p><p class="medium-feed-snippet">A selection of the latest arXiv preprints concerning active (and occasionally semi- or weakly-supervised) deep learning</p><p class="medium-feed-link"><a href="https://medium.com/data-science/active-and-semi-supervised-machine-learning-sep-28-oct-9-50813688c616?source=rss-3ec417445414------2">Continue reading on TDS Archive »</a></p></div>]]></description>
            <link>https://medium.com/data-science/active-and-semi-supervised-machine-learning-sep-28-oct-9-50813688c616?source=rss-3ec417445414------2</link>
            <guid isPermaLink="false">https://medium.com/p/50813688c616</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[active-learning-news]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Olga Petrova]]></dc:creator>
            <pubDate>Tue, 13 Oct 2020 12:41:47 GMT</pubDate>
            <atom:updated>2020-10-13T12:41:47.180Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Active and Semi-Supervised Machine Learning: Sep 14–25]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/data-science/active-and-semi-supervised-machine-learning-sep-14-25-93f19ff60fb1?source=rss-3ec417445414------2"><img src="https://cdn-images-1.medium.com/max/2600/1*Gef7TJLC7qES0ZU_J74TbQ.jpeg" width="6240"></a></p><p class="medium-feed-snippet">A selection of the latest arXiv preprints concerning active (and occasionally semi- or weakly-supervised) deep learning</p><p class="medium-feed-link"><a href="https://medium.com/data-science/active-and-semi-supervised-machine-learning-sep-14-25-93f19ff60fb1?source=rss-3ec417445414------2">Continue reading on TDS Archive »</a></p></div>]]></description>
            <link>https://medium.com/data-science/active-and-semi-supervised-machine-learning-sep-14-25-93f19ff60fb1?source=rss-3ec417445414------2</link>
            <guid isPermaLink="false">https://medium.com/p/93f19ff60fb1</guid>
            <category><![CDATA[active-learning-news]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Olga Petrova]]></dc:creator>
            <pubDate>Thu, 01 Oct 2020 22:04:13 GMT</pubDate>
            <atom:updated>2020-10-01T22:04:13.437Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Active and Semi-Supervised machine learning: Aug 31 — Sep 11]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/data-science/active-and-semi-supervised-machine-learning-aug-31-sep-11-f985d593bd99?source=rss-3ec417445414------2"><img src="https://cdn-images-1.medium.com/max/618/0*mLdl3nxYxhFusxgn.jpg" width="618"></a></p><p class="medium-feed-snippet">A selection of the latest arXiv preprints concerning active (and occasionally semi- or weakly-supervised) deep learning</p><p class="medium-feed-link"><a href="https://medium.com/data-science/active-and-semi-supervised-machine-learning-aug-31-sep-11-f985d593bd99?source=rss-3ec417445414------2">Continue reading on TDS Archive »</a></p></div>]]></description>
            <link>https://medium.com/data-science/active-and-semi-supervised-machine-learning-aug-31-sep-11-f985d593bd99?source=rss-3ec417445414------2</link>
            <guid isPermaLink="false">https://medium.com/p/f985d593bd99</guid>
            <category><![CDATA[active-learning-news]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Olga Petrova]]></dc:creator>
            <pubDate>Wed, 16 Sep 2020 02:32:27 GMT</pubDate>
            <atom:updated>2020-09-16T02:32:27.114Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Active and Semi-Supervised machine learning: Aug 17–28]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/data-science/active-and-semi-supervised-machine-learning-aug-17-28-77eaa9dbc311?source=rss-3ec417445414------2"><img src="https://cdn-images-1.medium.com/max/2600/1*gcZxlDfIi9nNHaWWI4gB-w.jpeg" width="3537"></a></p><p class="medium-feed-snippet">A selection of the latest arXiv preprints concerning active (and occasionally semi- or weakly-supervised) deep learning</p><p class="medium-feed-link"><a href="https://medium.com/data-science/active-and-semi-supervised-machine-learning-aug-17-28-77eaa9dbc311?source=rss-3ec417445414------2">Continue reading on TDS Archive »</a></p></div>]]></description>
            <link>https://medium.com/data-science/active-and-semi-supervised-machine-learning-aug-17-28-77eaa9dbc311?source=rss-3ec417445414------2</link>
            <guid isPermaLink="false">https://medium.com/p/77eaa9dbc311</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[active-learning-news]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Olga Petrova]]></dc:creator>
            <pubDate>Tue, 01 Sep 2020 18:19:15 GMT</pubDate>
            <atom:updated>2020-09-17T14:13:35.450Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[CPU or GPU for your recommendation engine?]]></title>
            <link>https://faun.pub/cpu-or-gpu-for-your-recommendation-engine-949beea27f28?source=rss-3ec417445414------2</link>
            <guid isPermaLink="false">https://medium.com/p/949beea27f28</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[pytorch]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[recommendation-system]]></category>
            <dc:creator><![CDATA[Olga Petrova]]></dc:creator>
            <pubDate>Wed, 17 Jun 2020 14:23:48 GMT</pubDate>
            <atom:updated>2020-10-27T16:00:35.331Z</atom:updated>
            <content:encoded><![CDATA[<h4>Learn how to generate product recommendations 15 times faster with PyTorch</h4><blockquote><em>In today’s data-driven world, GPUs are the hardware of choice for training Deep Learning models. What about tasks that do not involve artificial neural networks? For instance, is there a benefit to using a GPU for making product recommendations? Continue reading to find out!</em></blockquote><p><em>This article was first published on June 17, 2020 on </em><a href="https://blog.scaleway.com/cpu-or-gpu-for-your-recommendation-engine/?utm_source=faun&amp;utm_medium=medium&amp;utm_campaign=kubernetes"><em>Scaleway’s official blog</em></a><em> and is reposted here for your convenience.</em></p><p>Anyone selling anything these days makes recommendations. <em>“Customers who bought this item also bought these ones.”</em> <em>“Here are the top 10 TV series that we bet you’ll enjoy.”</em> Sometimes these recommendations are based on the intrinsic properties of the products, but more often they come from the behaviours of users such as yourself.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F3Xilyz3nKEhxBJIi_zSBg.jpeg" /><figcaption>Image credit: amazon.com</figcaption></figure><p>Let us say we want to build a simple book recommender system. The data that we need for it is available on any website containing users’ reviews of books: e.g. <a href="http://www2.informatik.uni-freiburg.de/~cziegler/BX/">this dataset</a> has been collected from <a href="https://www.bookcrossing.com/?">BookCrossing.com</a>, a website dedicated to the practice of “releasing books into the wild” — leaving them in public places to be picked up and read by other members of the community. There are three data tables available, but we will only be needing two of them today: BX-Books and BX-Book-Ratings containing information on the books and the bookcrossers&#39; book ratings respectively (pardon the excessive use of <em>book</em> in the preceding sentence, finding a suitable synonym is no easy task!). Each book in BX-Books is identified by a unique ISBN, and each row of BX-Book-Ratings lists the ISBN of the title that the user&#39;s rating refers to.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*mTBac-RiMb59vzSZukeYaQ.gif" /><figcaption>Image credit: giphy.com</figcaption></figure><h3>Using cosine similarity to make product recommendations</h3><p>First, let us discuss how the ratings can be leveraged to generate appropriate book recommendations. If you are already familiar with the basics of recommender systems (or simply uninterested in the details), feel free to skip to the next section for the CPU vs. GPU comparison.</p><p>For each book, there are multiple ratings posted by different users, and it is this information that we will be using to infer the “likeliness” of the books from. Consider <em>Harry Potter and the Sorcerer’s Stone,</em> <em>Harry Potter and the Chamber of Secrets</em> (the first two tomes of the series), and a textbook called <em>Quantum Computation and Quantum Information</em>. To illustrate with a simple example, let us say we have a total of five readers. Four of them read both Harry Potter books and ranked them highly, and one of the four has also enjoyed reading about quantum information:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/952/1*Y_2PnYtkJfzH4qGTvRPlpA.jpeg" /></figure><p>Eric (that’s what E stands for) has made a New Year’s resolution to read more in 2020, and, as it often happens, made no effort in keeping to it until mid year. Eric read the first Harry Potter book, and would like to use the book ratings of his friends to decide what to read next. How can he do that?</p><p>First, let’s replace the - (not read) signs in the table above with zeros:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/954/1*8EwbxVl33A1evwPSGlmwDw.jpeg" /></figure><p>Now, each book corresponds to a 5-dimensional vector containing the scores each reader has assigned to it. Eric would like to know which book, Harry Potter 2 or the quantum information textbook, would be most similar to Harry Potter 1 in terms of the readers’ feedback. Mathematically this means that we are going to consider two pairs of vectors: HP1 and HP2, and HP1 and QCQI. A popular measure of how similar two vectors are, is called the <a href="https://en.wikipedia.org/wiki/Cosine_similarity">cosine similarity</a>, given by the dot product of the two vectors divided by the product of their magnitudes:</p><p>cos(θ) = <strong>A</strong>·<strong>B</strong> / ||<strong>A</strong>|| ||<strong>B</strong>||</p><p>When two vectors are aligned with one another, the cosine of the (zero) angle between them is 1, meaning the similarity is maximised. The similarity is zero for two vectors that are perpendicular to each other (e.g. if there is no overlap in the users who read the two books), and can also be negative if we allowed for negative ratings in our data table. The cosines for the two pairs in question are calculated as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vt1FTk63y55mO8o6mxKMNw.jpeg" /></figure><p>Thus, when we make a recommendation on which book to read next based on Eric’s interest in <em>Harry Potter and the Sorcerer’s Stone, Harry Potter and the Chamber of Secrets</em> is a good bet (much better than <em>Quantum Computation and Quantum Information</em>!)</p><p>As you can imagine, the amount of effort to calculate cosine similarities for each pair of vectors grows quite quickly with the number of books as well as the number of users. Let us first see how long this will take on a CPU (10 core vCPU from a high end <a href="https://ark.intel.com/content/www/us/en/ark/products/120489/intel-xeon-gold-6148-processor-27-5m-cache-2-40-ghz.html">Intel Xeon Gold 6148 </a>processor to be precise), using the popular pandas and sklearn libraries.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*zNiG9TU49-gSbE4FswzsbA.gif" /><figcaption>Image credit: giphy.com</figcaption></figure><h3>The CPU route</h3><p>Let us start by loading the data using pandas.</p><pre>import pandas as pd</pre><pre>datadir = &#39;reco/BX-CSV-Dump/&#39;</pre><pre>books = pd.read_csv(datadir+&#39;BX-Books.csv&#39;, sep=&#39;;&#39;,   error_bad_lines=False, encoding=&quot;latin-1&quot;)</pre><pre>books.columns = [&#39;ISBN&#39;, &#39;bookTitle&#39;, &#39;bookAuthor&#39;, &#39;yearOfPublication&#39;, &#39;publisher&#39;, &#39;imageUrlS&#39;, &#39;imageUrlM&#39;, &#39;imageUrlL&#39;]<br></pre><pre>ratings = pd.read_csv(datadir+&#39;BX-Book-Ratings.csv&#39;, sep=&#39;;&#39;, error_bad_lines=False, encoding=&quot;latin-1&quot;)</pre><pre>ratings.columns = [&#39;userID&#39;, &#39;ISBN&#39;, &#39;bookRating&#39;]</pre><p>Now we can inspect the contents of the two tables:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OJepNbhz3nARNIGAuZDFUA.jpeg" /></figure><p>and</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*d-6LbrDMeCLGKjg2A791qQ.jpeg" /></figure><p>Unless your final goal is to be able to say: “Here is the list of books that users with reading history similar to yours may or may not have hated with passion”, you will probably want to remove all ratings below a certain threshold:</p><pre># Keep only Ratings above 5:<br>ratings = ratings[ratings.bookRating &gt; 5]</pre><p>I will also drop the columns that we will not be needing from the Books table, make sure each ISBN in it corresponds to a single book entry, and set ISBN as the table’s index:</p><pre>columns = [&#39;yearOfPublication&#39;, &#39;publisher&#39;, &#39;imageUrlS&#39;, &#39;imageUrlM&#39;, &#39;imageUrlL&#39;]</pre><pre>books = books.drop(columns, axis=1)<br>books = books.drop_duplicates(subset=&#39;ISBN&#39;, keep=&quot;first&quot;)<br>books = books.set_index(&#39;ISBN&#39;, verify_integrity=True)</pre><p>An additional pre-processing step that we will take is to filter out books that have been rated by fewer than three users. This filter will be applied to the Ratings, rather than the Books table:</p><pre># Keep only those books, that have at least 2 ratings:<br>ratings_count = ratings.groupby(by=&#39;ISBN&#39;)[&#39;bookRating&#39;].count().reset_index().rename(columns={&#39;bookRating&#39;:&#39;ratingCount&#39;})</pre><pre>ratings = pd.merge(ratings, ratings_count, on=&#39;ISBN&#39;)<br>ratings = ratings[ratings.ratingCount &gt; 2]<br>ratings = ratings.drop([&#39;ratingCount&#39;], axis=1)</pre><pre>print(ratings.shape[0])</pre><p>This leaves us with 207,073 book ratings. Now let’s get them into the form that we can use to compute the cosine similarities as we did above. We’ll want each row to correspond to an ISBN, and each column to correspond to a user who rated at least one book out of the ISBNs that we have in our table.</p><pre>import time</pre><pre>start = time.time()<br>matrix = ratings.pivot(index=&#39;ISBN&#39;, columns=&#39;userID&#39;, values=&#39;bookRating&#39;).fillna(0)<br>end = time.time()<br>print(&#39;Time it took to pivot the ratings table: &#39;, end - start)</pre><pre>matrix.head()</pre><p>Naturally, most users have not read most books, so the resulting table will contain mainly zeroes:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yXe6R48fnIipjrCMFOIWGQ.jpeg" /></figure><p>The dimensions of this final matrix are 25159 x 50180, meaning that our recommendations will be based on 25159 books and their ratings coming from 50180 readers.</p><p>Now we can use the NearestNeighbors algorithm from sklearn to find book recommendations based on cosine similarity to, for instance, the aforementioned <em>Harry Potter and the Sorcerer&#39;s Stone</em> (or, as far the the model is concerned, the row indexed by its ISBN, 059035342X).<em> </em>Since our matrix is sparse, we can try converting it to a special<em> Compressed Sparse Row</em> format and see if that improves the model&#39;s performance.</p><pre>from scipy.sparse import csr_matrix<br>from sklearn.neighbors import NearestNeighbors</pre><pre>start = time.time()<br>book_matrix = csr_matrix(matrix.values)<br>print(&#39;Time to convert to Compressed Sparse Row matrix format: &#39;, time.time()-start)</pre><pre>start = time.time()<br>recommender = NearestNeighbors(metric=&#39;cosine&#39;, algorithm=&#39;brute&#39;, n_neighbors=10).fit(book_matrix)<br>_, nearestBooks = recommender.kneighbors(matrix.loc[&#39;059035342X&#39;].values.reshape(1, -1))<br>print(&#39;Time to make a recommendation using the CSR matrix: &#39;, time.time()-start)</pre><pre>start = time.time()<br>recommender = NearestNeighbors(metric=&#39;cosine&#39;, algorithm=&#39;brute&#39;, n_neighbors=10).fit(matrix)<br>_, nearestBooks = recommender.kneighbors(matrix.loc[&#39;059035342X&#39;].values.reshape(1, -1))<br>print(&#39;Time to make a recommendation using the original matrix: &#39;, time.time()-start)</pre><pre>Time to convert to Compressed Sparse Row matrix format:  31.45536208152771<br>Time to make a recommendation using the CSR matrix:  0.015137195587158203<br>Time to make a recommendation using the original matrix:  6.317066431045532</pre><p>CSR conversion requires an upfront investment, but results in much faster inference time when it comes to actually generating a recommendation. Recommending books off the original zero-filled matrix took us whopping 6 seconds, whereas the frequency with which you have to spend half a minute removing those pesky zeros depends on how often you want to update the data that you base your recommendations on. What about the recommendations themselves, do they make sense?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aHySReYjdTaN6WDQSuYgYw.jpeg" /></figure><p>The first title showing up in the nearestBooks list is, of course, the one that we want to base our recommendations on, <em>Harry Potter and the Sorcerer&#39;s Stone </em>(since it is also included in the ratings matrix)<em>.</em> The novels that are closest to it are, unsurprisingly, some of the other Harry Potter books, and, well, others:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WpzfD1EOD21s0hP1T2wg8g.jpeg" /><figcaption>Okay. Image credit: goodreads.com</figcaption></figure><h3>The GPU way</h3><p>If you thought that TensorFlow and PyTorch were used exclusively for deep learning, you’ll be in for a surprise: both of these frameworks can actually be used to compute the cosine similarity needed for our recommendation engine. Let us see how this can be done with PyTorch on a <a href="https://www.scaleway.com/en/gpu-instances/?utm_source=faun&amp;utm_medium=medium&amp;utm_campaign=kubernetes">Scaleway RENDER-S instance that comes with a dedicated NVIDIA P100 GPU</a>:</p><pre>import torch<br><br># In PyTorch, you need to explicitely specify when you want an <br># operation to be carried out on the GPU. <br>device = torch.device(&quot;cuda:0&quot; if torch.cuda.is_available() else &quot;cpu&quot;)<br><br># Now we are going to simply append .to(device) to all of our torch <br># tensors and modules, e.g.:<br><br>cos_sim = torch.nn.CosineSimilarity(dim=1, eps=1e-6).to(device)<br><br># We start by transferring our recommendation matrix to the GPU:<br>start = time.time()<br>torch_matrix = torch.from_numpy(matrix.values).float().to(device)<br>print(&#39;Time to transfer the recommendation matrix to the GPU: &#39;, time.time()-start)<br><br># Now, let&#39;s get our Harry Potter recommendations<br><br>start = time.time()<br><br># The vector corresponding to &quot;Harry Potter and the Sorcerer&#39;s Stone&quot;: <br>ind = matrix.index.get_loc(&#39;059035342X&#39;)<br>HPtensor = torch_matrix[ind,:].reshape(1, -1)<br><br># Now we can compute the cosine similarities:<br>similarities = cos_sim(HPtensor, torch_matrix)<br><br>_, nearestBooks = torch.topk(similarities, k=10)<br><br>print(&#39;Time to make a recommendation with PyTorch: &#39;, time.time()-start)</pre><pre>Time to transfer the recommendation matrix to the GPU:  4.763129711151123<br>Time to make a recommendation with PyTorch:  0.0011758804321289062</pre><p>Compared to the best (sparse format) result on the CPU, the matrix preparation step takes 4.8 seconds instead of 31.5, and the recommendation itself takes a mere 0.001 seconds compared to 0.015 on the CPU. When time = money, what is the 15x speedup worth to your business? 😉</p><figure><a href="https://console.scaleway.com/register?utm_source=faun&amp;utm_medium=medium&amp;utm_campaign=kubernetes"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lDXG_yYIwD1L8ZlvQwgAHQ.jpeg" /></a></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2F4mhwkn%3Fas_embed%3Dtrue&amp;dntp=1&amp;display_name=Upscribe&amp;url=https%3A%2F%2Fupscri.be%2F4mhwkn&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/33420ed61f64fe0400d58c174122a9b8/href">https://medium.com/media/33420ed61f64fe0400d58c174122a9b8/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=949beea27f28" width="1" height="1" alt=""><hr><p><a href="https://faun.pub/cpu-or-gpu-for-your-recommendation-engine-949beea27f28">CPU or GPU for your recommendation engine?</a> was originally published in <a href="https://faun.pub/">FAUN.dev() 🐾</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>