<?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[Machine Learning at  PetiteProgrammer - Medium]]></title>
        <description><![CDATA[All about Machine Learning &amp; PetiteProgrammer - Medium]]></description>
        <link>https://medium.com/machine-learning-at-petiteprogrammer?source=rss----a837a38bdf2b---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Machine Learning at  PetiteProgrammer - Medium</title>
            <link>https://medium.com/machine-learning-at-petiteprogrammer?source=rss----a837a38bdf2b---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 04:52:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/machine-learning-at-petiteprogrammer" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Sampling Strategies for Recurrent Neural Networks]]></title>
            <link>https://medium.com/machine-learning-at-petiteprogrammer/sampling-strategies-for-recurrent-neural-networks-9aea02a6616f?source=rss----a837a38bdf2b---4</link>
            <guid isPermaLink="false">https://medium.com/p/9aea02a6616f</guid>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[recurrent-neural-network]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Daniël Heres]]></dc:creator>
            <pubDate>Wed, 31 Aug 2016 14:43:53 GMT</pubDate>
            <atom:updated>2016-08-31T09:36:22.231Z</atom:updated>
            <content:encoded><![CDATA[<p>Recurrent Neural Networks are currently one of the most powerful Machine Learning models. They are the method behind many advances in speech recognition, machine translation and natural language processing.<br>What makes Recurrent Neural Networks great is that they can not only <strong>detect</strong> things, you can also use them to <strong>generate</strong> sequences. (For example, check my <a href="https://algorithmia.com/algorithms/PetiteProgrammer/pythoncodeprediction">Python Code Prediction API</a> or this <a href="http://karpathy.github.io/2015/05/21/rnn-effectiveness/">nice blog post by Andrej Karpathy</a>).</p><p>A Recurrent Neural Network learns to <strong>predict the next words or</strong> characters given some <strong>input</strong>. From a high level, this works like in the following picture:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*Pu3GhiA9eZYqN7yL8r6VzA.jpeg" /></figure><p>In the example, first the word The is the most likely. Then, given **The**, the word <strong>Cat</strong> is most likely. Then, given <strong>The Cat</strong>, <strong>The Cat Is</strong> is most likely, etc.</p><p>However, when learning, the output of an RNN is a probability distribution instead of one word. <br>When generating text we choose only <strong>one</strong> of the words ourselves given the probabilities and feed that back into the network. This is called <strong>sampling</strong>.</p><h3>Greedy Search</h3><p>The most simple form of sampling is <strong>greedy search</strong>. At each point, it will pick the most likely next word. For example <strong>The</strong> ⇾ <strong>The Cat</strong> ⇾ <strong>The Cat Is</strong> ⇾ <strong>The Cat Is Home</strong>. You’ll see it in the next picture:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*6gP46ySUWkMTwfn2mcvUEw.jpeg" /></figure><p>However this method has a few drawbacks. First it will generate only <strong>one sample</strong>, if you would like to have more samples to choose from: bummer! Furthermore it can not <strong>look ahead</strong>: it could for example have created <strong>The Cat The Cat</strong> instead of <strong>The Cat Is Home</strong>, because <strong>The</strong> after <strong>The Cat</strong> had the highest probability. But it couldn’t see that The Cat Is Home was better than anything it cloud create after <strong>The Cat The</strong>. For example in a real world example you can get something like this as output:</p><blockquote>and the senstic and the some the senses and the some and the senses and and the sense the such and the such there as a sore and all the some and there and and and and and the senses of the such the sence of the some and and the some and and the such and the sense and the some the more and the some and the sinces with the socrence and the some and a concertions and the some and as</blockquote><p>Not very original, is it? Note the repetitions of for example the word <strong>and</strong> and a lot of <strong>some</strong>, <strong>such</strong> and <strong>senses</strong>.</p><h3>Beam Search</h3><p>A popular way to avoid this problem is using <strong>beam search.</strong> It works like this: instead of selecting the best next word it selects <strong>k</strong> next words. k is called the <strong>beam width</strong> of this method.<br>This could be an example of a beam search:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*0NqGXo-jPhMFTF-8PlzDPA.jpeg" /></figure><p>You see, it can generate <strong>multiple samples</strong>, and the best of those are often better than samples created using greedy search.</p><h3>Random Sampling</h3><p>Now on to <strong>random sampling</strong>. Random search works by selecting the next word with the probabilities of each word taken from the output of the model (the <strong>probability distribution</strong>). For example: the word The has probability <strong>0.8</strong>, so <strong>8 out of 10 times</strong> the next word is <strong>The</strong>,<strong> </strong>2 out of 10 times it will generate another word. Look at this picture for example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*GUttF-eo4kQJJAtCqjS21A.jpeg" /></figure><p>The nice thing about random search is that the results are very diverse, however, often the results are <strong>too diverse</strong>, they look too random. To fix this randomness, we increase the probability of the most probable words, and decrease the probabilities of less probable ones using <strong>temperature</strong>. The higher the temperature, the more diverse results it gives and vice versa.</p><h3>Conclusion</h3><p>I walked you through some different popular sampling strategies for Recurrent Neural Networks. It would be interesting to see whether there are better techniques than these methods that are often used. Probably, we currently miss some part and need also to learn a <strong>grader</strong>, or something like <a href="https://arxiv.org/abs/1406.2661"><strong>Generative Adversial Networks</strong></a>. Who knows, I’ll write about it in some next article!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9aea02a6616f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/machine-learning-at-petiteprogrammer/sampling-strategies-for-recurrent-neural-networks-9aea02a6616f">Sampling Strategies for Recurrent Neural Networks</a> was originally published in <a href="https://medium.com/machine-learning-at-petiteprogrammer">Machine Learning at  PetiteProgrammer</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Detecting the Programming Language of Source Code Snippets using Machine Learning and Neural…]]></title>
            <link>https://medium.com/machine-learning-at-petiteprogrammer/detecting-the-programming-language-of-source-code-snippets-using-machine-learning-and-neural-e275ca4ce93e?source=rss----a837a38bdf2b---4</link>
            <guid isPermaLink="false">https://medium.com/p/e275ca4ce93e</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[programming-languages]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[neural-networks]]></category>
            <dc:creator><![CDATA[Daniël Heres]]></dc:creator>
            <pubDate>Wed, 31 Aug 2016 14:43:29 GMT</pubDate>
            <atom:updated>2018-08-15T20:53:38.087Z</atom:updated>
            <content:encoded><![CDATA[<h3>Detecting the Programming Language of Source Code Snippets using Machine Learning and Neural Networks</h3><h4>(TL;DR: I made a programming language classifier, try it using the <a href="https://algorithmia.com/algorithms/PetiteProgrammer/ProgrammingLanguageIdentification">API</a>.)</h4><p>Skilled programmers often can write software in and can recognize dozens of programming languages, but computer scientists and engineers have given birth to thousands and thousands of programming languages. O’Reilly has made a great poster <a href="http://archive.oreilly.com/pub/a/oreilly//news/languageposter_0504.html?CMP=ILC-VX7490272994">publicly available</a> to visualize the history of the most popular programming languages until 2004.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/650/1*Q8WEcEnee_rY5yRpwAtqAA.gif" /><figcaption><a href="http://archive.oreilly.com/pub/a/oreilly//news/languageposter_0504.html?CMP=ILC-VX7490272994">History of Programming Languages, O&#39;Reilly</a></figcaption></figure><p>But programmers can not detect every language, especially not less popular and older ones. They also often like to search and group source code by programming language. Some websites and infrastructure like GitHub will try to classify source code based on some heuristics, like the file extension or the presence of some string, for example using regular expressions. When the file contains for example the string SELECT * and the file extension is .sql, this may be evidence that the query language <strong>SQL</strong> is used.</p><p>But these heuristics are not always sufficient to detect the language of source code snippets for different reasons. Source code snippets posted on the web may not always include the <em>file extension</em>, and there is some <em>overlap</em> in extension names between languages. Languages may also be very <em>similar</em>, like <strong>C#</strong> and <strong>Java</strong>. Code also may contain multiple languages, <strong>SQL</strong> and <strong>HTML</strong> are for example often embedded in source code of other languages. We can try to solve this problem using machine learning. To do this, we need first a relatively large and representative set of data, programs written in the languages we want to identify. In our model which you can try <a href="https://algorithmia.com/algorithms/PetiteProgrammer/ProgrammingLanguageIdentification">here</a> we use data from the <a href="http://rosettacode.org/">Rosetta Code</a> project and open source repositories hosted on <a href="https://github.com/">GitHub</a>. In total I used <strong>more than 240.000 documents</strong> to train this model.</p><p>Our machine learning pipeline consists of two parts: feature extraction and an algorithm that learns to classify the data. For this task I used the machine learning project <a href="http://scikit-learn.org/">scikit-learn</a>.</p><p>Feature extraction transforms unstructured sequences like text to a vector of numbers. To do this, we divide each document into <strong>n-grams</strong> (sequences of n tokens). For Natural Language Processing (<strong>NLP</strong>) tasks, n-grams are often computed at the word level, but I found that computing it at the character level gives better performance in our source code task. Another advantage of character level models is that you don’t have to tokenize the text, you don’t have to break it into words. For each document the frequency of each possible token is computed. If the text is “print” for example, the bi-gram (all sequences of 2 tokens) is the list of these tokens: [“pr”, “ri”, “in”, “nt”]. We convert the character level bi-gram combined with other n-grams to an array of numbers by counting the occurrences of the token sequences in the document. We call this numeric representation of a document the <strong>feature vector</strong>.</p><p>The second stage of the pipeline is the classifier: an algorithm that gradually learns to <strong>predict outcomes</strong> (labels) given the extracted feature vector. We can experiment with a lot of different classifiers that have all their strengths and weaknesses, for the moment I use a neural network with three hidden layers. The model will learn to identify features or feature combinations that are related to the programming languages. For example, the neural network may respond to features found in the sequence &lt;?php, and learns that the class <strong>PHP</strong> is most likely. The neural network learns to gradually improve its predictions by an algorithm called <strong>back-propagation</strong>: it computes at each step how much the prediction is wrong by computing the <strong>error</strong> or <strong>gradient</strong> of the current model and propagates it back to the parameters in the network.</p><p>The relation of token occurrences and the language may be nonlinear, e.g. the language <strong>Swift</strong> may be more than two times as likely when both let and in are given rather than just let or in. This can be one of the reasons the neural network model performs better than other models in our task.</p><p>Our pipeline has a lot of parameters which can be tuned as well, like the length n of tokens to be used for computing the n-grams in the feature extraction. These parameters are often called hyperparameters. We can optimize those hyperparameters using grid search by trying all possible combinations of parameters you want to try on your pipeline and selecting the combination that performs best on a validation set, a set that isn’t used for training. This is often vital for good performance of machine learning algorithms.</p><p>Using our optimized pipeline, we can now apply the same method to each new program: extract features from the raw text and identify the programming language. The performance on the data is very good: <strong>for more than 99% of the documents we predict the right language</strong> on a validation set.</p><p>In the future I will experiment with other classifiers, improved feature extraction, further tuning, support for more languages and more data.</p><p>Stay tuned and happy programming!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e275ca4ce93e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/machine-learning-at-petiteprogrammer/detecting-the-programming-language-of-source-code-snippets-using-machine-learning-and-neural-e275ca4ce93e">Detecting the Programming Language of Source Code Snippets using Machine Learning and Neural…</a> was originally published in <a href="https://medium.com/machine-learning-at-petiteprogrammer">Machine Learning at  PetiteProgrammer</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>