<?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 Matthew Relich on Medium]]></title>
        <description><![CDATA[Stories by Matthew Relich on Medium]]></description>
        <link>https://medium.com/@mrelich?source=rss-7c9a0a458ec0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*iKymWGkfp2f_dB3g-j4XAw.png</url>
            <title>Stories by Matthew Relich on Medium</title>
            <link>https://medium.com/@mrelich?source=rss-7c9a0a458ec0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 21:49:51 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mrelich/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[Search Query Parsing]]></title>
            <link>https://medium.com/scribd-data-science-engineering/search-query-parsing-ce16d7d2db88?source=rss-7c9a0a458ec0------2</link>
            <guid isPermaLink="false">https://medium.com/p/ce16d7d2db88</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Matthew Relich]]></dc:creator>
            <pubDate>Mon, 12 Feb 2018 19:49:00 GMT</pubDate>
            <atom:updated>2018-02-12T19:49:00.850Z</atom:updated>
            <content:encoded><![CDATA[<p>Scribd has a variety of content to offer and connecting our users with their desired content is a crucial aspect of our product. One of the main ways that users find content on Scribd is through search, and in this post I want to delve into an analysis we did regarding parsing out valuable information from a user’s query in order to better serve them relevant results, and also learn more about what they are searching for.</p><p><strong>The Opportunity</strong></p><p>This is essentially a goldmine, where we can not only explore improvements to the underlying model that serves search results, but also pull in the product and design teams to create the best search experience possible, as well as inform our content acquisition team on what to pursue next. I will highlight a few areas that I personally find interesting and useful that connect Scribd’s various departments across the company.</p><p>Our search infrastructure has a few components that could benefit from being able to parse out relevant information in the query. One of which is boosting relevant content types (books, audiobooks, etc) if certain keywords appear in the user’s query. Whenever a user searches at Scribd, we have to make a decision on how to rank each row of content, essentially giving a priority to specific content based on a combined score from Elasticsearch and a gradient boosting model. If we can flag that that the user is searching for an audiobook, for example, we can rank the audiobook first instead of a book, or even show them only audiobooks. This should help get the user to the correct or relevant content as fast as possible, which hopefully creates an enjoyable experience for them.</p><p>Another way that this can help serve search results is if we can generate extra, relevant search results based on being able to identify specific parts of the query. Imagine a user’s query is “power systems chomsky”. The user is specifically after the title “Power Systems”, but might also be interested in other titles by Chomsky. Instead of bringing forward other titles that have similar words, we can say, “Hey, here’s Power Systems, but check out all these other great books from Noam Chomsky that we have”. Below is an illustration how how it might be hard to get to explore other title’s by Chomsky:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RGruQfNk9kTDMOxP1wubaA.png" /></figure><p>The opportunity is lost on these top results to show that we have other books by Chomsky, which you would have to scroll down to the bottom of the page to find:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/788/1*y_yxOvST7-8GCdDd-URJWw.png" /></figure><p>This can communicate to the user that we understand their search, and we have something additional that’s relevant to offer, which again adds to the relationship we are striving for with our users.</p><p>A final example is that this can help to inform our content acquisition team. Right now we have two ways of looking to see what unavailable content we have on Scribd that users are searching for. The first is simply to rank the queries based on how frequently they are searched for, and then manually go through and see what looks like a title we don’t have or an author we are missing, which is quite time consuming since it’s a manual process. The second is to look at unavailable titles that are surfaced during search and look at the most frequent ones. This works, but it means we have to have that book in our system. For example, we don’t carry any of the Harry Potter series (at the time this post was written) and this title is not a part of our unknown book database, so this method would never tell us that we should add any of Harry Potter books to our catalog. Being able to flag and parse out author and title entities from the rest of the query allows us to explore not only what titles are the most popular, but also what authors and which ones we are missing. This is still a somewhat manual process, but it segments the data in a convenient way such that our content team can easily filter through the results.</p><p><strong>Approach</strong></p><p>After seeing the power of seq2seq for <a href="https://medium.com/scribd-data-science-engineering/neural-spelling-corrections-and-the-importance-of-accuracy-977c0063d20f">spelling corrections</a> we decided to try that model framework for this problem as well. As a quick reminder, the basic setup is an encoder and a decoder, which consist of memory cells (LSTM/GRU) that help the network to remember long sequences. An example of architecture can be found below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/639/1*-o2XSThNH_PqU63FEG6okQ.png" /></figure><p>For this case, the input would be tokenized words from the user query, and the output would be each word with an associated tag putting it into a predefined category. The categories are:</p><ul><li>name</li><li>title</li><li>topic</li><li>publication</li><li>publisher</li><li>content type</li></ul><p>So, for example, the input query “Power Systems Chomsky” should output “title title name”. Then in our search framework we could serve “Power Systems” as well as all other books/audiobooks we have by or about Chomsky.</p><p>The final model architecture chosen uses an embedding input layer followed by two bidirectional LSTMs for the encoder and two for the decoder. The output is run through a softmax activation layer. The model is defined in keras below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d486b131ffc63bfd7b65a58ad1309c02/href">https://medium.com/media/d486b131ffc63bfd7b65a58ad1309c02/href</a></iframe><p>This model was exported to tensorflow so that we could easily incorporate it as part of our go microservice.</p><p><strong>Training and Results</strong></p><p>The data used to train this model comes from Scribd’s data warehouse which includes titles, authors, topics, publishers, and publications. A hand curated list of useful content types was used, factoring in different user spellings (eg. audiobook and audio book). This content was then combined to create training, test, and validation datasets. These datasets contain both single tag entries (eg. only title words) and also double tag entries (eg. title and content type words, name and title words, etc). Some examples are given below:</p><ul><li>forbidden love series, title title title</li><li>selina hastings, name name</li><li>audiobooks inspirational devotional, content_type topic topic</li><li>the last virgin from las vegas geoff benge, title title title title title title name name</li></ul><p><strong>Note:</strong> I mixed these randomly, so the author doesn’t necessarily have to be associated with the book. This was a choice motivated by the fact that I don’t really care if the model learns the correct title author matches, just that it can identify which parts are title like and which are author (name) like. An interesting follow up would be to keep the correct author with the book, and see if this performs better.</p><p>The training set consists of 3 million entries, and the validation and test sets are much smaller, around 4k each. The vocabulary of the model is limited to the top 100k most frequently appearing words, and any unknown word is labeled as unknown.</p><p>The training was performed on a g2.2xlarge AWS instance. It ran for 30 epochs, which took approximately 12 hours and reached 96% accuracy on the test set. Here accuracy is defined as the number of entries that the model predicts all of the correct tags. For example, if the model predicts “title title title” for “power systems chomsky”, that would be marked as incorrect.</p><p>This model scores great on well formed data, but our user’s queries are not well formed, meaning they contain spelling errors, sparse information, and different languages. I hand scored 100 user queries to get an idea on how well it performs on real word data, and I found an accuracy of 70%, which is good enough for us to get this into production and test the impact on our users.</p><p><strong>Future Iterations and Final Thoughts</strong></p><p>We are in the process of getting this into production and will begin experimenting with the different ways in which we can utilize the output of this model to create a better search experience. However, there are still a few ways we can iterate on this model in order to improve:</p><ul><li>Expand the tag categories to include things like ‘series’ (eg. Game of Thrones). This was overlooked and these end up in the title bucket.</li><li>Expand the vocabulary used, which should help to reduce the amount of guesswork involved in unknown words.</li><li>Correcting spelling would be a huge help, and would likely lead to better segmentation by mapping all words into vocabulary.</li><li>Reduce contamination between tag groups. We actually have an author named <a href="https://www.scribd.com/book/353160208/Law-Liberty-and-the-Constitution-A-Brief-History-of-the-Common-Law">Harry Potter</a>, however this is likely not what users are after when they search this term. Removing ambiguity with a more carefully crafted training set will help to reduce this issue.</li></ul><p>I’m excited to see the results and will do a follow up post regarding the results and what we learned in the future. Stay tuned!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ce16d7d2db88" width="1" height="1" alt=""><hr><p><a href="https://medium.com/scribd-data-science-engineering/search-query-parsing-ce16d7d2db88">Search Query Parsing</a> was originally published in <a href="https://medium.com/scribd-data-science-engineering">Scribd Data Science &amp; Engineering Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Neural Spelling Corrections and the Importance of Accuracy]]></title>
            <link>https://medium.com/scribd-data-science-engineering/neural-spelling-corrections-and-the-importance-of-accuracy-977c0063d20f?source=rss-7c9a0a458ec0------2</link>
            <guid isPermaLink="false">https://medium.com/p/977c0063d20f</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Matthew Relich]]></dc:creator>
            <pubDate>Fri, 05 Jan 2018 00:58:34 GMT</pubDate>
            <atom:updated>2018-01-05T00:58:34.294Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Introduction</strong></p><p>I can remember a time in middle or high school when the goto service for locating things on the internet was Ask Jeeves. We even had a tutorial on how to properly search, including when to use quotes and other special characters in order to get the most relevant results. The days of ‘query engineering’ are almost a thing of the past, made obsolete by Google’s ability to almost intuitively know what we are actually hoping to find. For better or worse, Google has trained us to expect great results with a simple set of keywords, malformed questions, and careless spelling. The last point is something I would like to discuss in a bit more detail, and how we at Scribd attempted to solve it using neural networks.</p><p>Unlike Google, our search traffic is in the hundreds of thousands of queries per day, not billions, so it can take us some time to amass enough statistics on common misspellings in the search queries that we receive. This statistical machine learning approach is how Scribd, and I’m guessing many other companies, handle this at the moment, but we started to wonder whether or not we could build a generic model that could be proactive instead of reactive. Meaning, can we start to correct spellings on the fly, for all words, instead of waiting for the statistics to roll in.</p><p>In the next few sections, I will briefly introduce seq2seq and then I will highlight two similar approaches and the pitfalls/shortcomings of each. In addition, I will give some speculative ideas as to how we might improve upon these first attempts.</p><p><strong>Brief Introduction to seq2seq</strong></p><p>Both of the models described below were implementations of a seq2seq model. There are already a significant number of blogs and talks out there about seq2seq, and I would like to recommend a few and briefly highlight how it works (at a high level). I really loved the talk by Quoc Le [1] where he describes in great detail how to conceptually think about seq2seq as well as give some excellent examples of where this model works well. For more implementation details, I found [6] pretty useful for understanding the underlying network.</p><p>For applications, in general, if you can form your problem into an input sequence and an output sequence that you want to predict, seq2seq is likely a good approach. The basic setup is an encoder and a decoder, which consist of memory cells (LSTM/GRU) that help the network to remember long sequences. These cells differentiate them from <em>vanilla RNNs, </em>which typically do poorly when the sequence increases in length<em>. </em>A simple example architecture is given below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/639/1*-o2XSThNH_PqU63FEG6okQ.png" /></figure><p>In the example above, you can see that the basic input to these models will be a sequence of some sort (in the example, words) with the end having a special key (in this case &lt;EOL&gt;) to let the encoder know that we’re done. The same is true for the output used during training, and at inference time, your decoder will try to predict when to use this special token so that you know to stop accepting output.</p><p>It’s easy to see how this can be used for translation, where your target is a different language, or for spelling corrections, where your target is the correctly spelled word.</p><p><strong>First attempt: Deep Spell</strong></p><p>I want to thank the author of the Deep Spell [2] blog for inspiring the idea and giving us a great starting point. I was really blown away by the quoted 95.5% accuracy that Deep Spell had achieved after only a few days of training. Surely this was the answer to all my problems! And he published his code [3], what a great guy!</p><p>So, like a good student, I downloaded the code, read through it to understand the pieces and how it worked, downloaded the test data mentioned in the blog post [2] and ran it. After about a day, I started to get nervous… why wasn’t I getting into the 90% accuracy range? So I reached out to my internet-mentor and asked for clarification [4]. I showed my training / validation curves for loss and accuracy and asked for some clarification.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*_f6YRAygJ9KTw7ho." /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*C0zFn-phd1Ljzy3E." /></figure><p>Given that I’m very new to deep learning, I assumed I was doing something incorrect, however it turned out that the quoted accuracy achieved on the blog post was done with a private dataset. Bummer, but that’s fine, I decided to press on and manufacture my own dataset and steal the meat of the code found in [3] (this model has been posted below in a snippet). The model architecture was exactly the same, only this time I used my own dataset pulled from Scribd’s data warehouse which contained a list of author names and titles of books. I then randomly injected noise (or misspellings) into words, by either omitting, adding, or changing letters. Examples of each edit would be:</p><ul><li>Character omission — “po er systems”</li><li>Character removal — “poer systems”</li><li>Inserting whitespace — “pow er systems”</li><li>Duplicating a character — “poweer systems”</li><li>Incorrect character — “powar systems”</li></ul><p>The maximum number of allowed edits was 3, and I used a random number generator to decide how many to incorporate for each phrase. Input into the model was tokenized, for example, “margaret atwood” would be inserted into a fixed length vector like `[20,1,5,2,1,5,8,10,27,1,10,13,15,15,17,0,…,0]`, where the whitespace between the words also is included as a token. This vectorization was done for both the source (misspellings) and the targets (correct spelling). It was then reshaped into a M x N matrix where M is the length of the vector and N is the number of tokens. In this case, each row will have either a 0 or 1, indicating whether or not that character is present.</p><p>This was used to train the model for nearly a day, where I ran a total of 92 epochs (before killing it) with a batch size of 200 and 1000 steps per epoch. Much to my surprise, I hit close to the 90s for accuracy and loss looked good as well!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*pmP9wHDByO-F8W5A." /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*jVKcq2AkM6T7oR_3." /></figure><p>Had I constructed an awesome dataset? Was there some unseen bug in the previous dataset or preparation scheme? I honestly don’t know, but after I started to look at some of the predictions, I realized something wasn’t right. What I found was that the model either wasn’t adding any correction at all, or it was adding in mistakes to words that were already fine. This led to the second discovery during this process: what does accuracy actually mean?</p><p>In Keras, when the loss is categorical cross-entropy and your target metric is accuracy, it’s actually looking at the average accuracy per input, then averaged over the entire dataset to give a final number. So if you misspelled the title of “Power Systems” as “Pover Sistems” your accuracy would be 84%, since you got 11 / 13 characters correct:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/738/1*8EXPtHSwjO0oDgsItUBLkA.png" /></figure><p>But who cares about that accuracy?! I want to know how often I get back to the right word. If I look at that, it’s terrible:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*qQ0jQ95rygqlio7u." /></figure><p>It tops out around 13%, but at least it is not at a plateau yet, so it could continue to get better. I stopped exploring this method because while this was running I found another seq2seq implementation that had some more fancy pieces included, so I pressed onward from there.</p><p><strong>Second Attempt: OpenNMT</strong></p><p>OpenNMT [5] stands for “open neural machine translation”, which is quite a mouthful. Neural machine translation is an exciting field trying to solve the problem of translating between languages, and the ‘open’ part simply describes that the code is open source, and has a receptive development group around it. In fact, I was able to implement a new feature [7] to translate at each epoch, something that was already on the feature request list. While my contribution wasn’t directly incorporated, it definitely got the ball rolling, so I consider this an open source contribution through nagging :-) Anyway, it’s a great package that is very easy to steer based on a configuration file where you can set various parameters, including what type of model, how you want attention to be handled, etc.</p><p>In order to run this, I had to slightly modify the input data to get it in the right format that OpenNMT could understand. Thankfully, this takes as input raw text, and evaluates to text, so it’s not too difficult. The model expects to receive words where your input is in your start language (eg. English) and your output is in your target language (eg. Chinese). The trick to make this work for spelling is to treat each character as a word, and to give whitespace a unique character. So “margaret atwood” became “m a r g a r e t _ a t w o o d”. And that’s it basically, you’re ready to train.</p><p>For our tests I tried a few different model structures including a basic RNN, a bidirectional RNN (bRNN), a deep bRNN, a bRNN with more cells, and a bRNN with a different attention mechanism. OpenNMT does give loss and perplexity, but these metrics still suffer similar to accuracy mentioned in the previous section, so I am just quoting the final accuracy that I trained the models too (along with the number of epochs). All models used LSTMs with 500 cells (unless otherwise noted) and the general attention mechanism in OpenNMT is used.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TDSqWiw08cUTqc2Y-5hSDA.png" /></figure><p>Not bad, much better than 13%! Here are some examples of those that it got correct:</p><ul><li>Mistake reuniting dhe divided statess ameeica<br>Guess: reuniting the divided states america</li><li>Mistake pygmalion urld classics unabtidged <br>Guess: pygmalion world classics unabridged</li><li>Mistake rcg nna bethory<br>Guess: regina bethory</li></ul><p>Looking at some failed cases, we can see that some words are difficult (eg. names) and others there just isn’t enough information:</p><ul><li>Truth: colonel tracey goetz<br>Mistake cplonel tracey gietz<br>Guess: colonel tracey gietz</li><li>Truth: fuan kang<br>Mistake fuan keng<br>Guess: ryan king</li><li>Truth: make good pub<br>Mistake nake good ub<br>Guess: make good club</li><li>Truth: birthday wishes smitten novella<br>Mistake birthday wishes smutten novellaa<br>Guess: birthday wishes smutten novella</li></ul><p>This showed that sometimes, it doesn’t get it back to the right word, but it does correct to an in-vocab word. If instead I look at the before and after percentage of in-vocab words when compared to the total words used I find a jump from 56% before to 91% after! So it is correcting a majority of the words to something, but that something isn’t always right.</p><p><strong>Parting thoughts</strong></p><p>In the end we couldn’t use this in production because the direct accuracy is what we cared about the most. It actually reminded me of one of my professor’s comments from grad school, “We are not searching for an answer. We are looking for the answer.” So for the time being, we haven’t found <em>the</em> answer, and we continue to be reactive with statistical machine learning.</p><p>Even though we didn’t arrive at a production ready model at the end, we learned a ton. Neural spelling corrections do seem possible, and that’s evidenced by the 56% accuracy in correcting whole phrases and the jump to 91% total words in the vocabulary from 56% at start time. The next step would be to try to add some contextual information so that when the model sees ‘ub’ and is preceded by ‘make good’ it should know to pick ‘pub’ instead of ‘club’.</p><p>[1] <a href="https://www.youtube.com/watch?v=G5RY_SUJih4&amp;feature=youtu.be">https://www.youtube.com/watch?v=G5RY_SUJih4&amp;feature=youtu.be</a></p><p>[2] <a href="https://medium.com/@majortal/deep-spelling-9ffef96a24f6">https://medium.com/@majortal/deep-spelling-9ffef96a24f6</a></p><p>[3] <a href="https://github.com/MajorTal/DeepSpell">https://github.com/MajorTal/DeepSpell</a></p><p>[4] <a href="https://github.com/MajorTal/DeepSpell/issues/11">https://github.com/MajorTal/DeepSpell/issues/11</a></p><p>[5] <a href="http://opennmt.net/">http://opennmt.net/</a></p><p>[6] <a href="http://suriyadeepan.github.io/2016-12-31-practical-seq2seq/">http://suriyadeepan.github.io/2016-12-31-practical-seq2seq/</a></p><p>[7] <a href="http://forum.opennmt.net/t/save-validation-translations-at-each-epoch/286">http://forum.opennmt.net/t/save-validation-translations-at-each-epoch/286</a></p><p><strong>Pass 1 model in Keras:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/62e2e97e44f48338642b98221074f20c/href">https://medium.com/media/62e2e97e44f48338642b98221074f20c/href</a></iframe><p><strong>Pass 2 Config File</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f41901aac7168054260e585b015c1180/href">https://medium.com/media/f41901aac7168054260e585b015c1180/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=977c0063d20f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/scribd-data-science-engineering/neural-spelling-corrections-and-the-importance-of-accuracy-977c0063d20f">Neural Spelling Corrections and the Importance of Accuracy</a> was originally published in <a href="https://medium.com/scribd-data-science-engineering">Scribd Data Science &amp; Engineering Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>