<?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 Nick Powell on Medium]]></title>
        <description><![CDATA[Stories by Nick Powell on Medium]]></description>
        <link>https://medium.com/@nickpowell?source=rss-8a418e1acb5c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*OK7_NQG_nUAt5UkWxE0v0g.png</url>
            <title>Stories by Nick Powell on Medium</title>
            <link>https://medium.com/@nickpowell?source=rss-8a418e1acb5c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 22:29:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nickpowell/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[Converting HTML documents to Grakn knowledge bases (and back again!)]]></title>
            <link>https://medium.com/vaticle/converting-html-documents-to-grakn-knowledge-bases-and-back-again-b35f86303436?source=rss-8a418e1acb5c------2</link>
            <guid isPermaLink="false">https://medium.com/p/b35f86303436</guid>
            <category><![CDATA[html]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[data]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[xml]]></category>
            <dc:creator><![CDATA[Nick Powell]]></dc:creator>
            <pubDate>Mon, 18 Sep 2017 20:16:03 GMT</pubDate>
            <atom:updated>2022-03-31T09:17:32.144Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9mn0NbRm5kAtKPJ2mhAi4g.jpeg" /><figcaption>Image by <a href="https://www.flickr.com/photos/150654414@N02/">Hilary Halliwell</a> is licensed under <a href="https://creativecommons.org/licenses/by/2.0/">CC BY 2.0</a></figcaption></figure><p>I wrote in my <a href="https://blog.grakn.ai/using-grakn-ai-to-enhance-neural-network-models-for-knowledge-base-completion-bb84bfd1dfdd">previous post</a> about how <a href="http://www.vaticle.com/">Grakn</a> does really well with information that is structured in a hierarchy. In such systems, you have data — let’s call each individual piece of data a <em>node </em>— that leads to more data nodes by way of special relationships. If you’ve ever taken an algorithms class you’re probably pretty familiar with <strong><em>trees</em> </strong>of various kinds. In trees, <em>parent </em>nodes lead to <em>child </em>nodes, which are themselves parents of yet more nodes, all the way down the tree until you reach the <em>leaves</em>, which have no child nodes below. Nodes that have the same parent are called <em>siblings</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/333/1*uGdo-pliEAOd41D38k-unA.png" /><figcaption>Grakn models tree-like structures such as this quite well.</figcaption></figure><p>In <strong>HTML, </strong>documents follow this general architecture, where it is called the Document Object Model (DOM for short). In it, the various parts of the code are structured much like the tree you see above, with various sections leading to other sections in a hierarchical manner. When your web browser loads the data from the server, it parses the tree and delivers it to you as a beautiful (or ugly, if the UI developer sucks) webpage:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/428/1*zOnIQ_zlpZrVjvBBVv7jTA.png" /></figure><p>I thought it would be cool to write a program that took an HTML document and <em>encoded</em> it into a Grakn knowledge base. You could then do analytics on that HTML document, or you could add and remove elements from it — much more quickly and elegantly, I might add, than manually poring through an HTML document and changing everything you can find. You could also change design patterns in the blink of an eye. Then, when you’re done, you could <em>decode</em><strong> </strong>it back into HTML and load it in your browser. Pretty neat, right?</p><p><strong>Disclaimer</strong>: the program is a sort of proof-of-concept, and doesn’t always work for non XHTML documents (i.e. where XML rules do not apply). For example, link and meta tags (and several others) do not take a closing backslash in non-XML documents, so there is no end tag and the program thinks that every successive link is a level deeper in the DOM tree as a result. To fix this, you would need to recognize the type of document you have and whether these elements take end tags.</p><p>The GitHub repo is below!</p><p><a href="https://github.com/mokbull/grakn-dom">mokbull/grakn-dom</a></p><h3>Encoding</h3><p>Every Grakn project begins with defining a schema! Here, everything revolves around the HTML tags, which are the modular elements that give function to the various parts of the document. There are <em>many </em>tags out there — so many, in fact, that it would be unreasonable to define each one schematically. At the same time, you want to define as many as possible so that you can actually do meaningful things with the document in Grakn. The same thing goes with the tag attributes — some tags have many possible attributes, so it was prudent to define as resources only the commonly used ones, and preserve the others in a generic “other” attribute that could then be used to re-construct the original document.</p><p>I ended up with <strong>18 </strong>defined HTML tags subclassed from an entity called “global”. Since most HTML elements take attributes from a global superclass, this makes sense. I also included a “container” entity for all other tags, as well as a “data” entity for the text content of a node.</p><p>Adding Grakn support for tags or resources not included in this is very simple — you would just have to add it to the schema and the default_tags<em> </em>dictionary, and then you could query Grakn for information on that particular tag or attribute. Below you can see the schema’s default tags, as well as the default attributes:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/519/1*mJZiavarO1HAnUCr3_jaDQ.png" /></figure><h4>Parsing the DOM Tree</h4><p>I used Python, and specifically the HTMLParser module, to do the parsing. You feed it the document and it executes methods to handle each type of element it comes across — start tags, end tags, data content, even declarations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/650/1*77v8cnRNk8VdU85ITjlr_A.gif" /></figure><p>I custom overrode these methods in domEncoder.py so that the program could properly insert each element into Grakn and perform the necessary housekeeping.</p><h4>Relationships</h4><p>I took the parent-child relationship inherent to tree structures and made that a Grakn relationship. I also made siblinghood a relationship, so that a sequence of elements like the links below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/346/1*d7MlLQyMM2J2ZIoaw4Eslw.png" /></figure><p>would be siblings <strong>in the order they appeared in the document</strong>. This is because, for the decoding step, it matters in which order a node’s children are visited. Otherwise you would get elements of the webpage appearing all over the place!</p><p>When you encode a page, the program gives the URL you provide a unique hash, which is then the keyspace of your knowledge base.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RHu3fpge_iuZuUM-m2dnNQ.png" /></figure><h3>Decoding</h3><p>Now that you know how a website is encoded in Grakn, it’s not difficult to imagine how the decoding step works.</p><p>Essentially, using the Grakn Client Python (which you can find <a href="http://dev.grakn.ai/docs/client-api/python">here</a>!), we recursively step through the graph, looking for children and right siblings, and iteratively extending a string representing the HTML document as it is being processed. Once every element in the keyspace has been un-parsed (if that’s a word), we have the final string, which we save as the HTML document.</p><p>A couple notes:</p><ul><li>As mentioned above, documents that don’t follow XML specifications are likely to be messed up.</li><li>There are a lot of edge cases in HTML, and although I’ve tried to find as many as I could, I’m sure I haven’t found them all. Keep that in mind if the program doesn’t work (and consider it an exercise to find the edge cases and fix it!)</li></ul><h3>To Sum Up</h3><p>It’s not much use for me to only explain how this program works — it’s something that you have to use yourself! Hope you like this idea. Overall, I think there are a lot of interesting things that could be accomplished with this, and would love to hear your feedback.</p><p>If you enjoyed this article, please hit the clap button below, so others can find it, too. Please get in touch if you’ve any questions or comments, either below, via our Community <a href="http://www.vaticle.com/discord">Slack</a> channel, or via our <a href="https://discuss.grakn.ai/">discussion forum</a>.</p><p>Find out more from <a href="http://www.vaticle.com/">https://grakn.ai</a>.</p><p><em>Feature Image credit: “</em><a href="https://www.flickr.com/photos/150654414@N02/33704104352/in/photolist-8Jtz9Q-67p1kK-e1PGbW-or8n2j-et5mJh-9ArRA2-avUcvv-TmjkZs-no8FVw-bmAz1-et274r-hUtP5d-et27bX-f9oj2C-6V4Evf-7Uahs1-nZi5Wc-5vsbCi-et5M1o-iPaKKA-7T27xW-tfTy8-qwXZNW-et5kxN-5nwc5b-7ZddkJ-eYcLS9-et5LEE-7WdxCi-et5mM5-hJU6bS-5v6DBH-69THJB-ydc2C-fDfuk7-4rawjf-qzAbfs-pgfthB-8ayiD4-5DC811-59g7-et26WZ-qkhW2s-7eKKLj-9qrnoZ-84hd8L-2193x-c5Dct-68a6dt-9GU4BC"><em>Tree</em></a><em>”by </em><a href="https://www.flickr.com/photos/150654414@N02/"><em>Hilary Halliwell</em></a><em> is licensed under </em><a href="https://creativecommons.org/licenses/by/2.0"><em>CC BY 2.0</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b35f86303436" width="1" height="1" alt=""><hr><p><a href="https://medium.com/vaticle/converting-html-documents-to-grakn-knowledge-bases-and-back-again-b35f86303436">Converting HTML documents to Grakn knowledge bases (and back again!)</a> was originally published in <a href="https://medium.com/vaticle">TypeDB</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using Grakn to enhance neural network models for knowledge base completion]]></title>
            <link>https://medium.com/vaticle/using-grakn-ai-to-enhance-neural-network-models-for-knowledge-base-completion-bb84bfd1dfdd?source=rss-8a418e1acb5c------2</link>
            <guid isPermaLink="false">https://medium.com/p/bb84bfd1dfdd</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[big-data]]></category>
            <category><![CDATA[tensorflow]]></category>
            <category><![CDATA[startup]]></category>
            <dc:creator><![CDATA[Nick Powell]]></dc:creator>
            <pubDate>Thu, 31 Aug 2017 18:11:28 GMT</pubDate>
            <atom:updated>2022-03-31T09:20:10.190Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SchtijvwFqXHNk2UbJMQAg.png" /><figcaption>Image by <a href="https://www.flickr.com/photos/enneafive/">Johan Neven</a> is licensed under <a href="https://creativecommons.org/licenses/by/2.0/">CC BY 2.0</a></figcaption></figure><blockquote>Just as electricity transformed every major industry starting about 100 years ago, AI is now poised to do the same. Several large tech companies have built AI divisions, and started transforming themselves with AI. But in the next few years, companies of all sizes and across all industries will realize that they too must be part of this AI-powered future.</blockquote><p>So <a href="https://medium.com/@andrewng/deeplearning-ai-announcing-new-deep-learning-courses-on-coursera-43af0a368116">claims Andrew Ng</a>, the polymath who has brought deep learning to the masses. Ng has served as Baidu’s chief scientist, head of Google Brain, professor at Stanford and founder of Coursera, where he now publicly distributes coursework that helps people worldwide learn AI. Electricity was harnessed by a niche few, but once people began to realize its enormous potential, it very quickly became indispensable in all walks of life.</p><p><a href="http://www.vaticle.com/">Grakn</a> is, of course, the database for AI, and so we too have been working hard to integrate our product with the world of deep learning.</p><p>Artificial neural networks — the ones that run on anything from our MacBook Pros to the most powerful GPU clusters around — are among the most widely applicable learning techniques out there. And due to the <a href="https://en.wikipedia.org/wiki/Universal_approximation_theorem">universality</a> of neural nets, you can theoretically approximate any function with a precise enough model, which is pretty crazy if you think about it.</p><p>Neural networks are trained, like any machine learning algorithm, with a set of training data, and are evaluated on a separate set of test data. Training data creates, or “trains”, the weights and biases of the network, which are mathematical formulations of the inputs and outputs of the network’s neurons. We start off with a randomly generated vector of weights and biases, and with enough data and time arrive at a set of parameters that can accurately predict complex inputs.</p><p>To get a better sense of how we can utilize Grakn in tandem with neural nets, we should first briefly review some of Grakn’s design patterns as well as explain the project that forms the basis of this blog post.</p><p>Code for this project can be found below:</p><p><a href="https://github.com/mokbull/grakn-kbc">mokbull/grakn-kbc</a></p><h3>The Project</h3><p>The particular graphs used in Grakn are often referred to as “knowledge bases”, which you can read more about <a href="https://en.wikipedia.org/wiki/Knowledge_base">here</a>. A knowledge base is a graphical representation of known information; relationships, hierarchies, etc. These can be queried to see whether an edge exists between 2 given nodes either explicitly in the graph itself or implicitly, through schemas. Expanding a graph expands the knowledge base it is associated with, and thus the ground truth. One particular type of relational system that Grakn handles particularly well is a <em>hierarchy</em>, in which relationship are uni-directional and indicate some underlying vertical ordering of concepts.</p><p>In the world of artificial intelligence, learning improvements are the holy grail. If you can make a model learn more accurately, or more quickly, or more precisely, you can open up the domain you’re working in to a whole new set of use cases. However, every dataset will always have incomplete information; this can produce flawed insights. Thus, if you can improve the quality of data in your database, you will improve any of the machine learning that is built on top of it. This can be achieved through <em>“Knowledge Base completion”</em>, which is the concept of expanding existing knowledge bases with new or improved information through some established rules of inference (and by adding more data, of course). Previously, KBC was attempted by making reasoned human inductions about the types of relationships that should hold. Of course, human inductions can only go so far.</p><p>Using deep learning to facilitate knowledge base completion is, then, a natural approach. With the existing knowledge base as a training set, you can program the neural net as a binary classifier to find likely relationships and then insert them back into the graph. It turns out that Grakn can do all of the legwork as a knowledge base!</p><p><a href="https://nlp.stanford.edu/pubs/SocherChenManningNg_NIPS2013.pdf">This paper</a> presents a neural network for knowledge base completion. It is built to accommodate two separate databases — <a href="https://wordnet.princeton.edu/">WordNet</a> and <a href="https://en.wikipedia.org/wiki/Freebase">Freebase</a>. For the purposes of this project, we will only concern ourselves with WordNet.</p><p>WordNet, a text corpus produced by Princeton University, is a lexical database of English and contains entities (words with unique identifiers) as well as relations (ways of describing the lexical relationship between two related entities). The goal of the neural network is to be able to predict whether a given entity <em>e1 </em>is related to an entity <em>e2</em> by way of relation <em>r</em>. For instance, if</p><pre>e_1 = __atlantic_1<br>e_2 = __north_sea_1<br>r = _has_part</pre><p>then we are asking whether the Atlantic → has part → North Sea, which is true. As another example, we could ask regression curve → part of → shell bean — clearly false.</p><p>There are 11 relations in the dataset and nearly 40,000 different entities. The neural tensor network is like most neural nets in that it trains a set of weights and a bias; one major distinction, however, is an extra <em>tensor </em>layer that multiplicatively relates entities and relations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/596/1*nOVT79Vbjt3FYkpSm6txSA.png" /></figure><p>If the math behind this isn’t clear, don’t worry. All we need to know is that the neural tensor network is able to handle multiple types of entity pairs per instantiation of entity-relation-entity (up to <em>k </em>slices, as you can see in the equation). You can refer to the paper I linked above for more specifics.</p><p>As I mentioned above, one aspect of this data that makes it particularly amenable to Grakn manipulation is its hierarchical nature. The relations indicate increasing or decreasing levels of specificity, such that if A →r →B and B →r →C, then A →r →C. Grakn models hierarchies<strong><em> </em></strong>like this very well.</p><p>Once the network has been trained, we can do two things with the results:</p><ol><li>Classify an input e1-r-e2 triplet as correct or incorrect based on tuned thresholds.</li><li>Determine the likeliest second entity for a given e1-r (first entity — relation) combination.</li></ol><p>Of course, the classification of #1 only requires one pass-through of the neural net, whereas #2 requires as many passes as there are entities, since we are calculating a likelihood for each one.</p><p>This presents a big challenge — verifying a triplet is simple enough, but improving a knowledge base by adding the most relevant relationships is a <strong><em>lot</em></strong> more computationally expensive. I will explain below how I dealt with that.</p><h3>The Implementation</h3><p>This project was implemented in two parts. The first was the actual neural tensor network, as explained above, which produces the weights for each embedding and provides the initial set of predictions for the test data. The second part was the Grakn<em> </em>base, which stores all the entity-entity relationships as well as the rules of inference, and which can be used to check for a graph connection between two entities.</p><p>The neural network is a pure NumPy/SciPy implementation of the knowledge base completion paper. The base of the neural network code, not written by me, can be found <a href="https://github.com/siddharth-agrawal/Neural-Tensor-Network">here</a>. Because this implementation does not make use of a deep learning library like TensorFlow, and is set up to run on CPU, not GPU, the time it takes to train the neural network is comparatively slow. I have optimized it in my implementation but it still takes several hours to complete; luckily, the neural net only needs to be run once before we “plug it in” to Grakn. Of course, in a production environment, you could make use of TensorFlow to improve runtime.</p><p>On the Grakn side, the flow goes something like this:</p><ol><li>Build the ontology and rule set, and insert the relation data from the training set into Grakn.</li><li>After the neural net has been run (and an accuracy has been calculated), loop through the test set again, this time checking Grakn for relationships; use any inferred relationships to come up with a modified accuracy.</li><li>Return to the output of the neural net, choose a subset of x entity-relation-entity triplets that the neural network gives a high score to, and insert these triplets into Grakn. Note, of course, that some of these triplets might be false!</li><li>Repeat steps 2 &amp; 3 a set number of times, each time calculating an updated accuracy.</li></ol><h4>Our goals are two-fold:</h4><ul><li>Maintain Grakn as a versatile and robust knowledge base even as additional (possibly false) relationships are added to it.</li><li>See if the accuracy of the neural net classifier is improved with Grakn inferences!</li></ul><p>What exactly is happening here? In Step 1 of the flow, by building a schema and a set of inference rules specific to the project, we can augment the power of the neural network by letting Grakn check for relationships that the network might not have caught. <strong>Having a good number of reliable inference rules is critical to making the most of Grakn</strong>; I explain in the results<em> </em>section what happens when <em>a priori </em>inferences are few and far between.</p><p>To improve accuracy, we have to improve either our Type I or our Type II error rate. In an incomplete knowledge base, it is difficult to check for false positives (Type I) — a graph claiming that a relationship does not exist when the neural network claims otherwise <em>might </em>just be down to a lack of information in the knowledge base itself. Therefore, in step 2, we look for Type II errors — false negatives — by looping through every item in the test set that the neural network classified as false, and trying to find a relationship that would reject that classification.</p><p>The augmenting happens in Step 3, which is where we return to the obstacle I mentioned in the paragraphs above the <em>Implementation</em> header. We want to add the most likely WordNet triplets in every iteration of the algorithm without explicitly checking every e1-r-e2 combination (this would be 40 000 * 40 000 * 11 checks, since there are 40 000 entities in WordNet). Instead, we can choose <em>x </em>entities at random for each <em>e1</em> entity, calculate triplet scores across each of the 11 relations for these <em>x </em>entities, and choose the most confident one.</p><p>As long as <em>x </em>is large enough that we can be fairly sure that each addition we make has a high likelihood of being true, we can make additions to the graph in a reasonable amount of time without sacrificing too much accuracy. This is an important tradeoff to consider though; if we make additions to the knowledge base that we are not completely positive are true, we risk contaminating the knowledge base with incorrect information. One question we will want to answer is how the ratio of correct/incorrect Grakn inferences changes over time.</p><h3>Results</h3><p>There were 2 main takeaways from running this program:</p><ul><li>Actual accuracy changes were minimal, since the inferences rules simply weren’t finding many graph connections</li><li>However, ratio of correct / incorrect inferences was between 2/1 and 4/1 throughout, indicating that the knowledge base was absorbing correct information faster than incorrect information</li></ul><p>The first bullet point speaks to something I discussed earlier, which is that Grakn is at its most useful when there are many inference rules that can quickly produce new inferred edges from any entity that we insert. With WordNet, there were few rules that I was able to objectively verify. For example, you might have A → subordinate instance of → B, and C → member meronym → B. These two relations indicate similar (but slightly different relationships), but you cannot say with certainty what sort of relations might hold between A and C. You might think that A → similar to → C or A → part of → C are true statements, but this is not necessarily the case and counterexamples can be found in the test set. The English language is complicated.</p><p>The inference rules I did use were pure transitive and reflexive relations — the sort that say if</p><blockquote>A → domain region → B <strong><em>and</em></strong> B → domain region → C</blockquote><p>then</p><blockquote>A → domain region → C</blockquote><p>or if</p><blockquote>A → similar to → B</blockquote><p>then</p><blockquote>B → similar to → A</blockquote><p>I found these rules to be limited in scope, however. Taken alone, they did not connect different relations with each other and only hierarchically extended existing relationships. Even so, they are a good starting point for lexical inferences.</p><p>Despite this, I found that the ability of the graph to improve the accuracy of the system was consistent. Depending on various random factors, every time I ran the program I got anywhere between a 2/1 to a 4/1 ratio of correct to incorrect inferences from neural network-inserted relations. Although some graph insertions produce type I errors, most reversed what had originally been type II errors. This is very promising, because it tells us that false additions to a Grakn knowledge base don’t wildly propagate through the inference chain.</p><p>Below are some screencaps from a) before any re-inserts (just the inference rules), b) after 1 re-insert, and c) after 20 re-inserts.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/872/1*-IBXIchTdKTguhC-Sdjgxg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/866/1*j2Qw0iSwf6YJkkjVPBsqzQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/892/1*QffcFVjV2Nxkd2U4d22n_g.png" /></figure><p>The net accuracy gain was approximately 0.21%, meaning that we got rid of about 1% of the total error.</p><p>With a more inter-connected graph, you could see a much bigger improvement than that. The point is that it is doable. Consistently, over many runs, the number of correct inferences dwarfed the number of incorrect inferences.</p><h3>Conclusions</h3><p>As we’ve seen above, Grakn is a natural structure for modeling hierarchical knowledge bases. Training data can be fed into the database to create relationships between entities. The graph can be expanded by re-inserting likely entities, as determined by the network of tensors, into the Grakn database. Moreover, Grakn gives a user the ability to scope out potential Type II errors in test data that has been fed through a neural net. It is even possible for Grakn to improve the program’s prediction accuracy in such situations.</p><p>So this is one application of deep learning principles to Grakn — <strong>hierarchical relationship matching</strong>. Train a neural net, and then use Grakn inferences on the test data to iteratively improve the accuracy of your predictions. But there are of course other ways that Grakn can improve the efficiency and operation of neural networks.</p><p>For instance, in n-ary classification or regression settings, you could use Grakn analytics to intelligently initialize the neural network’s vector spaces, rather than simply using random initializations. Grakn relationships and inferences could potentially give you advance information about ground truths that could make your network learn more quickly.</p><p>Another idea, which we will be tackling, is to use the output of the neural net to build inference rules for your graph. Using only neural pathways with a very high confidence, you could add not only entities and relationships to a Grakn graph, but even the rules themselves! This would be especially useful in situations where explicit inference rules are scarce, such as in the project you’ve just read about.</p><p>The potential for applying Grakn to deep learning principles is limitless, but of course you have to be smart about what you do, and you have to understand the purpose of the technologies you are using. I hope this post gave you some inspiration, and don’t hesitate to reach out with questions!</p><p>If you enjoyed this article, please hit the clap button below, so others can find it, too. Please get in touch if you’ve any questions or comments, either below, via our Community <a href="http://www.vaticle.com/discord">Slack</a> channel, or via our <a href="https://discuss.grakn.ai/">discussion forum</a>.</p><p>Find out more from <a href="http://www.vaticle.com/">https://grakn.ai</a>.</p><p><em>Feature Image credit: “</em><a href="https://www.flickr.com/photos/enneafive/35561479232/"><em>Green Power?</em></a><em>” by </em><a href="https://www.flickr.com/photos/enneafive/"><em>Johan Neven</em></a><em> is licensed under </em><a href="https://creativecommons.org/licenses/by/2.0"><em>CC BY 2.0</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bb84bfd1dfdd" width="1" height="1" alt=""><hr><p><a href="https://medium.com/vaticle/using-grakn-ai-to-enhance-neural-network-models-for-knowledge-base-completion-bb84bfd1dfdd">Using Grakn to enhance neural network models for knowledge base completion</a> was originally published in <a href="https://medium.com/vaticle">TypeDB</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to build a movie recommender with Grakn]]></title>
            <link>https://medium.com/vaticle/how-to-build-a-movie-recommender-with-grakn-ai-174d838e762d?source=rss-8a418e1acb5c------2</link>
            <guid isPermaLink="false">https://medium.com/p/174d838e762d</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[movies]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Nick Powell]]></dc:creator>
            <pubDate>Tue, 08 Aug 2017 11:16:04 GMT</pubDate>
            <atom:updated>2022-03-31T09:57:47.021Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q00vqBUboIZ9Ah17P7OBAA.jpeg" /><figcaption>Image by <a href="https://www.flickr.com/photos/jeepersmedia/">Mike Mozart</a> is licensed under <a href="https://creativecommons.org/licenses/by/2.0/">CC BY 2.0</a></figcaption></figure><h3>Hey all,</h3><p>I’ve been interning at <a href="http://www.vaticle.com/">Grakn</a> for about three weeks now and have already gotten my hands dirty with some projects. One of them is a synthesis of Grakn and neural nets — keep your eyes peeled in a couple weeks for a blog post on that one!</p><p>Today, though, I will demonstrate a movie recommendations program that I wrote on top of Grakn. This is meant to be a command-line tool for you to play with and tweak as you like, so I am putting all of the code on GitHub, and you can learn how to get it running by taking a look at the readme there. I will be explaining how to build such a recommendation system using only basic NumPy and Pandas capabilities and — of course — a Grakn graph.</p><p><a href="https://github.com/graknlabs/grakn-movie-recommender">graknlabs/grakn-movie-recommender</a></p><p>Clicking above will take you to the GitHub repo.</p><h3>The Data</h3><p>To start off with, we need some data. The canonical, comprehensive online movie repository is MovieLens (<a href="https://grouplens.org/datasets/movielens/">https://grouplens.org/datasets/movielens/</a>), which offers both a “small” and “full” dataset. These data come complete with user ratings, timestamps, movie genres, movie titles, and — in the case of the full dataset — a “tag genome”, which calculates the predicted relevance of each of 1,128 tags to every one of the movies. For this project, we are going to focus on <em>users</em>, <em>movies</em>, and <em>genres</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/1*S4sw_0RwHCNYGAglXZ0dhQ.jpeg" /></figure><h3>How are recommendations made?</h3><p>Typically, recommendation systems do their work either from contextual information about user behaviours or directly from knowledge of intrinsic truths about the object being recommended. The second option is a lot more involved, using things like audio analysis and facial recognition to answer the fundamental questions, and often requiring deep domain knowledge (a typical exception to this is NLP, which has a comparatively low barrier to entry). The first approach, on the other hand, tends to ask <strong>collaborative </strong>or <strong>content-based </strong>questions, and is a much simpler and faster way to provide answers. It is also the approach we will be investigating here.</p><p>So what questions do we want to have Grakn help us answer? Well, the collaborative questions we might want answered are questions like:</p><blockquote>Who enjoys the same movies I do?</blockquote><blockquote>Who dislikes the same movies I do?</blockquote><blockquote>What other movies do these people like?</blockquote><p>The content-based questions might take the following form:</p><blockquote>What types of genres do I enjoy watching?</blockquote><blockquote>What general characteristics of a movie might lead me to enjoy it?</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*ncNDd_9p1q3IccbMCStM2Q.png" /><figcaption>Diagram representation of collaborative analysis</figcaption></figure><p>I took a combined approach when writing this program, and tried to consider both types of questions. Collaborative and content-based filtering, by the way, are both incredibly easy to implement within Grakn. My goals in this post are the following:</p><ul><li>How<strong> </strong>to construct meaningful Grakn ontologies and rulesets from an open-ended question.</li><li>How to best query a graph in Grakn to get the results you need.</li><li>How to think about recommendation systems graphically</li></ul><h3>The Ontology</h3><p>For this project, I started by creating an ontology with two entity types - <em>user </em>and <em>movie</em>. A user<em> </em>interacts with a movie<em> </em>insofar as he/she contributes a rating to that movie; a user can be said to “like” a movie if their rating for that movie is greater than or equal to their mean rating for all movies. Conversely, a user “dislikes” a movie if their rating for that movie is less than the mean. This is a tweakable parameter, by the way; if you wanted to change the threshold for “liking” a movie, you could do so in 2 lines of code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*yQB_e33BnUFtyO3_TTByjw.png" /></figure><p>Throughout this post I will make references to situations where my choice of parameters is malleable and can lead to different results that may suit different endeavours better or worse.</p><p>I could have added a third entity corresponding to <em>genre</em>, and that was indeed my first approach, but I found it easier to simply assign each genre to a number between 0 and 18, inclusive, and encode each movie’s set of genres as a bit vector in a resource of datatype long that I assigned to the <em>movie </em>entity in the ontology. So for example in the movies file downloaded from MovieLens,</p><pre>48,Pocahontas (1995),Animation|Children|Drama|Musical|Romance</pre><p>corresponds to the 1995 movie <em>Pocahontas</em>, which has 5 genres that I encoded in the long as</p><pre>.....0000101000010001100</pre><p>The least significant bit corresponds to the first genre, alphabetically, in the dataset, and it goes down the alphabet from there.</p><p>I included 4 <em>relations </em>in my ontology. Two of the relations are user-movie relations: user liking a movie, and user disliking a movie. The other two are movie relations: recommended movie (given a movie <strong>A</strong>, relates any other movie that was liked by at least one person who liked <strong>A</strong>), and neg-recommended movie (given a movie <strong>B</strong>, relates any other movie that was liked by at least one person who *disliked* <strong>B</strong>). These latter relations are not hard-coded into the ontology and are instead produced through inference rules in the <em>movieRules.gql</em> file. These relationships can be visualized below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/760/1*e1dxvkeF85zWth7oPyoujA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/767/1*Nu8V8cJgRhr52p37idLOUw.png" /></figure><p>You can think of this process as a basic clustering algorithm for binary movie classification.</p><p>— — — — — — — — — — — — —</p><h3>Let’s get some recommendations!</h3><p>Now it’s time to see how the program actually works. The first step is to parse all the lines of the movies file — the schema for which I showed you above — and insert the relevant information into the Grakn graph. Step 2 is to ingest the ratings data, which comes in the form</p><pre>1,2968,1.0,1260759200<br>1,3671,3.0,1260759117<br>2,10,4.0,835355493<br>2,17,5.0,835355681</pre><p>where the 1st column is userId, the 2nd column is movieId, the 3rd column is rating given by userId for movieId, and the 4th column is the rating’s timestamp.</p><p>A recommender can only recommend if it is given some inputs off of which to base its search. A (small) dataset to train on, if you will. The program I have written takes in user input one-by-one and stores the information, making correspondence with Grakn through a command-line Graql query after every input.</p><p>The program gives the ‘player’ random movies from the movie dataset and allows them to respond in one of three ways. If the player likes the displayed movie, they should respond with a &#39;Y&#39; or a &#39;Yes&#39;. If the player dislikes the movie, they respond &#39;N&#39; or &#39;No&#39;. If the player does not know or have an opinion on the movie, they respond with ?. The player must give a yes/no response to n movies, at which point the engine calculates the recommendations.</p><p>The choice of n is somewhat arbitrary, and in the shell snippet below I have set it to 10. Think about what happens when you increase or decrease that value, though. If n is too low, say 3, you don’t have a large enough sample size to pursue meaningful content-based filtering, and you will be choosing with less refined search terms. If n is very high, say 50, then it will take a long time for the user to go through and respond to every suggestion, and you tend to get many of the same movies recommended every time, since they will have a lot of connections in the Grakn graph.</p><p>You can change the value of n in the getProgramParameters() function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4SzIIh2I_M_K3xPyaub4hg.png" /></figure><p>After <em>each </em>input, a query is sent to Grakn to go through and select (max) 100 movies randomly that have an inferred relationship with the input movie.If the user input is a <em>yes</em>, then Grakn returns all inferred recommendations (movies liked by users that also liked the input movie). If the user input is <em>no, </em>Grakn returns all inferred neg-recommendations (movie liked by users that also disliked the input movie).</p><p>The ‘maximum number of movies selected’ parameter is also tweakable from getProgramParameters() as fetch_limit. Setting a limit on the number of results does two things. First, it makes program execution faster, since Grakn is not forced to return every result from a massively inter-connected graph. Second, it introduces some randomness into the recommendation process, which means you will get diversity in the movies you are recommended.</p><p>By the end of this process, the program has a mapping of movieId’s recommended by the inference engine to the number of times they <em>were</em> matched, out of a total of <em>n</em> user inputs. The values therefore range from 1 to n. This is the <strong><em>collaborative filtering </em></strong>aspect of the recommendation engine.</p><p>We have also kept track of the movies that the user has liked and disliked, and this is translated into a length 19 (the number of genres in the data) vector corresponding to the total ‘genre-interest’ score of the user. We add 1 to element i if the user ‘likes’ a movie classified as genre i , and we subtract 1 from element i if the user ‘dislikes’ a movie with that genre. Movies can have multiple genres. We turn this vector into a unit vector by dividing by its norm and are left with a set of genre weights. This is <strong><em>content-based filtering </em></strong>in action.</p><p>All that is left to do is combine the two methods. For each movie in the mapping, we obtain the bit vector corresponding to its genre classifications and convert it into a vector of 1’s and 0’s. Let us call this vector <strong><em>a </em></strong>and the vector of genre-weights <strong><em>b</em></strong>. We take the dot product of the two as such,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/418/1*29u9uzIsBJ207Dcl6tmffQ.gif" /></figure><p>and are left with a scalar that represents the overall alignment of the genres of the movie with the genre affinity of the user. We multiply this scalar by the number of times Grakn counted an inferred relationship (the key’s value in the movie mapping). Doing this gives us a score for each movie in the mapping, and we rank scores in descending order. Here, I’ve chosen the top 10.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/522/1*kVxCrSA2B97Rig2v0QxCfA.png" /></figure><p>Above are the results of one round of recommendations. You will notice that there is an interesting blend of horror and children’s movies. I personally<em> </em>don’t know anyone with this kind of taste, but it makes sense given the responses I provided. I responded ‘yes’ to movies such as <em>Addams Family Reunion </em>and <em>Angels in the Outfield</em> — typical children’s films — as well as skin-crawlers like <em>Stonehearst Asylum, Intacto, </em>and <em>The Hunger</em>, which Roger Ebert described as ‘an agonizingly bad vampire movie’. If I actually liked the movies I just mentioned, then the recommendations above would be quite accurate.</p><h4>Future extensions</h4><p>There is a lot more you can leverage to refine this program, of course. For example, I didn’t filter movies by their average rating, so there is no guarantee that the recommended movies are well reviewed, beyond likely having been seen by many people. Some more possibilities are below:</p><ul><li><strong>Machine learning</strong> for binary classification. You could train something like a support vector machine on top of the graph.</li><li><strong>Tags </strong>— the larger dataset has ‘tag genome’ data, as I mentioned at the beginning. That would be a great addition to the content-based aspect of the recommender.</li><li><strong>IMDB links</strong> — the dataset also contains links to movie aggregation websites, which you could scrape to get more data. Actors, director, writers, you name it.</li><li><strong>Rating strength — </strong>I only split ratings into ‘like’ and ‘dislike’, but you could certainly go further than that.</li></ul><p>Thanks for reading! Hope you enjoyed this article and please hit recommend if you did. Be on the lookout for my next post, on the topic of neural nets and Grakn, which should be out in a few weeks.</p><p>In the meantime, we would love to hear your thoughts or comments below, on our <a href="https://discuss.grakn.ai/">discussion boards</a>, or via our <a href="http://www.vaticle.com/discord">Slack</a> channel. To download and install <a href="http://www.vaticle.com/">Grakn</a>, check out our <a href="http://www.vaticle.com/">setup guide</a>.</p><p><em>Feature Image credit: </em><a href="https://www.flickr.com/photos/jeepersmedia/13755866913/in/photolist-mXypZc-EVA6UZ-6SicVz-4rw2XT-zqxh2-7qYc6n-iPM7M-p2WVvB-FaoeP-9k8SSh-psesMj-ds8CKP-cF7MwC-nF92My-5Vs3Rk-6Mc38Y-4H4bQ-97MTNL-6qqKQy-9k8TAW-Eo2xQ-qbLXXE-6G8ALQ-ctRenA-pVueRK-aG48vX-6CjC98-5xD1iL-6Mc2Kb-7K6wS-4Rnqg2-bX12eZ-9k8TqW-88zHTG-gYPTT-3nDMwr-e5gXfH-ds8LAY-aG4tKi-5hg5AB-7YFrXn-UiKQiQ-9k8SWs-iVCPYU-mTUMxZ-k7ayoz-oDCqw9-ds8Dm8-8q3on4-5UXnf4"><em>“</em>Movie Theater: Holiday Stadium Cinemas 14<em>”</em></a><em> by </em><a href="https://www.flickr.com/photos/jeepersmedia/">JeepersMedia</a><em> is licensed under </em><a href="https://creativecommons.org/licenses/by/2.0"><em>CC BY 2.0</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=174d838e762d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/vaticle/how-to-build-a-movie-recommender-with-grakn-ai-174d838e762d">How to build a movie recommender with Grakn</a> was originally published in <a href="https://medium.com/vaticle">TypeDB</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>