<?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 Suleka Helmini on Medium]]></title>
        <description><![CDATA[Stories by Suleka Helmini on Medium]]></description>
        <link>https://medium.com/@sulekahelmini9628?source=rss-ddf3befa5aec------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*xb__e25kV6RXUZefsibnYQ.jpeg</url>
            <title>Stories by Suleka Helmini on Medium</title>
            <link>https://medium.com/@sulekahelmini9628?source=rss-ddf3befa5aec------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 16:50:35 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sulekahelmini9628/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[How To Remote Debug Ballerina Compiler]]></title>
            <link>https://medium.com/@sulekahelmini9628/how-to-remote-debug-ballerina-compiler-c348cbf2d2e8?source=rss-ddf3befa5aec------2</link>
            <guid isPermaLink="false">https://medium.com/p/c348cbf2d2e8</guid>
            <category><![CDATA[debugging]]></category>
            <category><![CDATA[compilers]]></category>
            <category><![CDATA[ballerinalang]]></category>
            <category><![CDATA[programming-languages]]></category>
            <category><![CDATA[beginners-guide]]></category>
            <dc:creator><![CDATA[Suleka Helmini]]></dc:creator>
            <pubDate>Tue, 28 Jul 2020 06:59:11 GMT</pubDate>
            <atom:updated>2020-07-28T17:03:08.039Z</atom:updated>
            <content:encoded><![CDATA[<h4>A Beginner&#39;s Step By Step Guide</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*F3gbJgEubRokWiTf" /><figcaption>Photo by <a href="https://unsplash.com/@nihaldemirci?utm_source=medium&amp;utm_medium=referral">Nihal Demirci</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>To all those developers just starting out with Ballerina and wanting to dive deep into where the real action is, it’s important to have a basic knowledge of how the Ballerina compiler works its magic. If you are such an individual, this article will provide you with a step by step guide into debugging the <strong>Ballerina compiler </strong>using <strong>IntelliJ IDEA</strong>. Without further ado let’s get down to business.</p><h3>Step #1</h3><p>First You need to clone ballerina from <strong>GitHub.</strong></p><p><a href="https://github.com/ballerina-platform/ballerina-lang">GitHub - ballerina-platform/ballerina-lang: The Ballerina Programming Language</a></p><h3>Step #2</h3><p>Then follow the instruction in the below link and <strong>build Ballerina from the source</strong>.</p><p><a href="https://ballerina.io/learn/installing-ballerina/#building-from-source">Ballerina</a></p><h3>Step #3</h3><p><strong>Extract</strong> the built <strong>zip file</strong> (get the zip with runtime and tools) to a desired folder path.</p><p>ex: /home/Documents/Ballerina_build_sources/<strong>jballerina-tools-2.0.0-Preview3-SNAPSHOT</strong></p><h3>Step #4</h3><p>Open the cloned <strong>Ballerina-lang repository</strong> in IntelliJ IDEA and find <strong>org.wso2.ballerinalang.compiler.CompilerDriver class </strong>and put a debug point to <strong>Compile method</strong>.</p><p><strong>Note</strong>: You can put the debug point anywhere you like. However, Compile method in CompilerDriver class is the <strong>best</strong> place to start to <strong>understand</strong> how the <strong>Ballerina compiler </strong>works as of writing this article.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z4-yeY2w3D5eBajNkcELrw.png" /></figure><h3>Step #5</h3><p>Click “<strong>Run</strong>” from the <strong>menu</strong> and select “<strong>Edit Configurations</strong>” option to access “<strong>Run and Debug Configurations</strong>” window in<strong> IntelliJ IDEA</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tMTRU1ZahK7OB-Rvra_PVA.png" /></figure><p>Then click the “<strong>+</strong>” sign on the left top corner and from the resulted configuration list, select “<strong>Remote</strong>” (do not select “Ballerina Remote”).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iaJYsvRGHllPC6gBypQm8Q.png" /></figure><h3>Step #6</h3><p>Add a “<strong>Name”</strong> for the remote debug configuration and select a suitable “<strong>Port”. </strong>However, in our case <strong>changing</strong> the <strong>port</strong> number is <strong>optional </strong>so you can also keep the <strong>default</strong> <strong>port 5005 as it is</strong>. Finally, click “<strong>OK</strong>”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P6Ww5mPxiqmUsBgYOwKxng.png" /></figure><p>You will then be able to see the <strong>name</strong> of the created debug configuration appear near the “<strong>Debug</strong>” icon.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KjxP2CgFlpu-B61_WkOE5w.png" /></figure><h3>Step #7</h3><p>Create any Ballerina program you desire to inspect the Ballerina Compiler’s behavior.</p><p><strong>Note</strong>: You can refer the below link on how to set up IntelliJ IDEA for Ballerina and create a Ballerina program.</p><p><a href="https://ballerina.io/swan-lake/learn/setting-up-intellij-idea/">Ballerina</a></p><p>ex:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OKwI-go7wukWMPeMWSSBKA.png" /></figure><h3>Step #8</h3><p>In the Terminal, issue the following command:</p><p><strong>&lt;Name of Debug Configuration&gt;=&lt;Port&gt; &lt;Path to ballerina file in bin folder of the extracted Ballerina distribution zip&gt;/ballerina run &lt;Path to ballerina program&gt;/&lt;name_of_file&gt;.bal</strong></p><p>ex: BAL_JAVA_DEBUG=5005 <strong>Documents/Ballerina_build_sources/jballerina-tools-2.0.0-Preview3-SNAPSHOT/bin/ballerina </strong>run <strong>Documents/ballerina_projects/bal_test_1/test2.bal</strong></p><p>As shown below you will then see a process starting to listen on port 5005.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tAzM3gZ3Iwqlq3RSpTiw4Q.png" /></figure><h3>Step #9</h3><p>To start the remote debug process, go to IntelliJ IDEA and click on the “Debug” icon.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KmLybhBVMTOuMr_oZFHPIQ.png" /></figure><p>Congratulation! you have now successfully started to debug the Ballerina Compiler. Until next time, happy coding fellow geeks.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c348cbf2d2e8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[My Experience in Google Summer of Code 2019]]></title>
            <link>https://medium.com/good-audience/my-experience-in-google-summer-of-code-2019-2d8c0d1bdec4?source=rss-ddf3befa5aec------2</link>
            <guid isPermaLink="false">https://medium.com/p/2d8c0d1bdec4</guid>
            <category><![CDATA[experince]]></category>
            <category><![CDATA[google]]></category>
            <category><![CDATA[open-source]]></category>
            <category><![CDATA[google-summer-of-code]]></category>
            <dc:creator><![CDATA[Suleka Helmini]]></dc:creator>
            <pubDate>Thu, 29 Aug 2019 12:13:42 GMT</pubDate>
            <atom:updated>2019-09-05T09:55:33.001Z</atom:updated>
            <content:encoded><![CDATA[<h4>Important tips and how-to guide</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g5RBYeGe0VLB6t_ZsvO_wQ.png" /></figure><h3>What is Google Summer of Code?</h3><p>Google Summer of Code (GSoC) is an international annual program where Google awards stipends, to all selected students who successfully complete an open-source coding project of their choice which is published by a chosen mentoring open-source organization during the summer. This usually runs for 3 to 4 months and includes 3 evaluations where students will be awarded 30% of the stipend each upon passing the first 2 evaluations and 40% upon passing the final evaluation. Students will also receive a certificate of completion from Google along with some other goodies.</p><p>Through this article, I wanted to relate my experience as a student participating in GSoC 2019 and give some tips along the way to anyone hoping to apply next year.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/1*2KN_RqmI5nydTkPR6lS8Wg.gif" /></figure><h3>My Experience</h3><p>I initially got to know about Google Summer of Code during my 1st year as an undergraduate. Attending the awareness sessions, listening to all that I felt very overwhelmed and it all felt so out of reach and I gave up even without trying. When 2nd year rolled around, even though I wanted to apply and took the initiative to go through organizations and projects, I couldn’t pull through with the time frame and academic work. Thus, when 2019 came along, I vowed to myself that this year I will somehow apply for GSoC.</p><p>Since I already had an idea about which organization I wanted to apply for, I started going through projects only after Google published the accepted mentoring organizations, which was on the 26th of February 2019. In my opinion, I started a little late, so I suggest for anyone hoping to apply for GSoC to have a rough idea about which organizations you want to apply for before Google publish the organizations and start contributing to available projects <strong>from January or December</strong> to give yourself a better chance. The reason for this is that the <strong>number of pull requests (PR)</strong> you put to the <strong>organization </strong>of your choice needs to be put down in your <strong>project proposal</strong> and will play a <strong>MAJOR </strong>role in determining whether you get the project or not.</p><p>You must be thinking, how do I select an organization before Google announces the selected organizations. Well, the selected list of organizations doesn’t usually change. So, by going through the organizations that were selected for past GSoC programs you can select a reliable organization(s) that have been selected for a few years in a row. Even though it is not 100% guaranteed that it will also be selected this year, but the chances of it not getting selected is low.</p><p>You can then start to go through the available projects and select a few projects you would like to contribute to. Also, don’t forget to join the Gitter channel/mailing list of the organization, introduce yourself, elaborate on your skills and ask for any available projects that you can start contributing to. After selecting a project, join the Gitter channel or any other communication method available for that project and ask for help when you get stuck in any problem and offer help to other newcomers. Basically the goal is to let your presence be known and to put as much pull requests as possible.</p><p>After Google announces the selected organizations, you can go through the projects available for GSoC in that particular organization and select a project of your choice. This is where the advantage of starting early really comes into play because if the organization(s) you have been engaging with are in the selected list you already have a number of PRs under your belt and you are already familiar with the available projects and the organization, which will definitely increase your chance of writing a strong project proposal. In GSoC, the maximum number of project proposals one can submit is 2 but you will only be selected to do one of the projects.</p><p>In my case, I selected <a href="http://www.scorelab.org/">SCoRe Lab </a>as the organization and since I had a background in deep learning and machine learning, I selected the <a href="https://github.com/scorelab/TensorMap">TensorMap </a>project listed in their <a href="http://www.scorelab.org/gsoc-2019/">GSoC project list</a>. The project I selected was to build a web application that will allow users to create machine learning algorithms visually. It will support in reverse-engineering the visual layout to a Tensorflow implementation in a preferred language. The goal of the project was to let beginners play with machine learning algorithms in Tensorflow with less background knowledge about the library. With the time I had, I could only focus on one project so I only made a project proposal for the TensorMap project.</p><p>So, basically, there can be two types of projects offered by organizations for GSoC. New projects and already existing projects. If the project is an existing project you should work on getting yourself familiar with that project and putting as much PRs to that particular project before the project proposal deadline. When making PRs look for issues that you can solve or ask the community of that project for a beginner issue. If it is a new project they sometimes have a challenge task to select students for that particular project or they will consider PRs made to other repositories in the organization or both in some cases. In such a situation it is better to ask the mentor assigned for that particular project about the selection process so that you will be able to do the needful.</p><p>In my case, TensorMap was a new project and we were given a challenge task to showcase our skills. The mentors of my project stated that they will mainly focus on the challenge task rather than PRs in the selection process so I mainly focused on that, thus starting late didn’t really make much of an impact. The challenge task was to create a visual interface to build deep learning models similar to the <a href="https://playground.tensorflow.org/#activation=tanh&amp;batchSize=10&amp;dataset=circle&amp;regDataset=reg-plane&amp;learningRate=0.03&amp;regularizationRate=0&amp;noise=0&amp;networkShape=4,2&amp;seed=0.75914&amp;showTestData=false&amp;discretize=false&amp;percTrainData=50&amp;x=true&amp;y=true&amp;xTimesY=false&amp;xSquared=false&amp;ySquared=false&amp;cosX=false&amp;sinX=false&amp;cosY=false&amp;sinY=false&amp;collectStats=false&amp;problem=classification&amp;initZero=false&amp;hideText=false">TensorFlow playground</a>. The application needed to have a backend made using Django and frontend made using Angular. We were to utilize TensorFlow in model creation.</p><p>I can’t stress enough on the importance of joining the Gitter channel or any method of communication for your selected project as all important information regarding the project will be published there. I was unable to find the Gitter channel of TensorMap until 2 weeks prior to the proposal deadline (which was like mid-March) and only got to know about the challenge task through that. The challenge task was originally posted in the first week of February 2019 in the project’s Gitter channel. So, because of the mistake that I made I had a very short time to learn all the technologies as I was new to all of them and send the application to the mentors before the proposal deadline. I didn’t even think that it will be possible to finish it in time but I wanted to try anyway because even if I couldn’t then I will at least know that I did everything I possibly could. Since I was also engaged in an internship during this time, it was extremely stressful and it took a lot of all-nighters to complete the application in time. Throughout those 2 weeks, I kept pushing through even though I didn’t expect to get selected for GSoC, I really wanted to give my self the best possible chance of getting in so that even if I don’t I will not have any regrets. So don’t make the same mistake that I made and even if you do, keep pushing. This is the <a href="https://github.com/suleka96/TensorMap-Challange">link </a>to my challenge task code.</p><p>During this time you should also start making your project proposal. Sometimes organizations have their own proposal templates or you could make your own. In my opinion, the most important parts in the proposal are the PRs (to the existing project and to other projects in the organization) you have made and of course the improvements and your vision for the project during the GSoC period. This is a <a href="https://docs.google.com/document/d/1KCu9tV7zQ-qWWUerVCmbilxquJGnyTQrCV8mu9tZ9Hg/edit?usp=sharing">link </a>to my project proposal. The project proposal draft needs to be submitted to the GSoC dashboard for your mentors to see and provide comments on before you submit the final proposal to Google. If your mentors are busy, see to that you also personally send your proposal and get their feedback. You can access the GSoC dashboard by registering for GSoC through the <a href="https://summerofcode.withgoogle.com/">website</a>.</p><p>After the proposal deadline, the mentors will evaluate and assign students for projects. The selected students are usually announced somewhere around May. The funny part is that since I was so convinced that I will not get selected I did not even bother to go through the names when the selected list was released and only found out when a friend called in the middle of the night to congratulate. From there on started an awesome 4 months of learning new things and a lot of life lessons.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/450/1*0WEj8CEDJgOk--2bMdDQWQ.gif" /></figure><p>Depending on the workload of your project mentors might select more than one student for a project. That was the case in my project. There were 3 people including myself selected for the TensorMap project. Two guys from India and my self from Sri Lanka. Since we all fell under the same time zone it was not much of a hassle to arrange meetings and discussions. But I have to say, it was rather chaotic and challenging to manage my work with my fellow GSoCers especially when my work depended on another person’s work. Our mentors helped a lot in this to properly coordinate and manage all of our work.</p><p>As mentioned before, there are 3 evaluations you need to pass in GSoC. Since we had 3 people working simultaneously in the project. We couldn’t exactly stick to our original project proposals so we were each assigned separate tasks to complete in each evaluation period. Another thing to remember is that you should always discuss and resolve any kind of doubt you have with your mentors and also maintain a healthy relationship with them as they are the ones who will be evaluating your first 2 evaluations. The final evaluation is done by Google and your mentors.</p><p>It is really important to remember to fill the evaluation forms in your GSoC dashboard before the evaluation deadline. The rules are very strict and you will fail the evaluation and be taken out of GSoC for failing to fill the evaluation form before the deadline. One of the two people I was working on in the TensorMap project was failed for merely not filling the evaluation form before the deadline. The form only takes about 5 mins to fill so make sure that you fill it in time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*r9a55jCGDEI1F6ieY3fqIw.gif" /></figure><p>Discussing the work I did during the GSoC period, I mainly worked on designing the whole backend of the project and I also helped in designing and adding logic to several frontend components. This is a <a href="https://github.com/scorelab/GSoC/blob/master/GSoC-2019/Suleka-Helmini-Tensor-Map.md">link </a>to my final report.</p><p>GSoC was truly a unique experience. I got to learn new technologies, got over most of my introverted phobias of communicating with people and also learned a good amount of life lessons. The biggest challenge for me with GSoC was dividing time between GSoC and other work. During these 4 months I really learned that for a software engineer, it doesn’t matter how many languages or technologies you know, what really matters is your ability to learn and pick up things efficiently and the ability to apply them correctly.</p><p>GSoC 2019 is gradually coming to an end. The final evaluation submission ended on the 26th of August 2019 and at the time of writing this article the final mentor evaluations are underway. So, to anyone hoping to apply for GSoC 2020, I wish you all the best of luck and hope this article helped you to get an idea on how to get started. If you have any questions regarding this, I would be more than happy to help.</p><p>Where there is a will there is a way. Good luck my fellow geeks!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/424/1*kfK-xG6tCXoQ6b_kG2Gp2g.gif" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2d8c0d1bdec4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/good-audience/my-experience-in-google-summer-of-code-2019-2d8c0d1bdec4">My Experience in Google Summer of Code 2019</a> was originally published in <a href="https://medium.com/good-audience">Good Audience</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 Automate Hyperparameter Optimization]]></title>
            <link>https://medium.com/data-science/how-to-automate-hyperparameter-optimization-73950c9fdb5?source=rss-ddf3befa5aec------2</link>
            <guid isPermaLink="false">https://medium.com/p/73950c9fdb5</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[bayesian-optimization]]></category>
            <category><![CDATA[hyperparameter-tuning]]></category>
            <dc:creator><![CDATA[Suleka Helmini]]></dc:creator>
            <pubDate>Thu, 30 May 2019 03:44:59 GMT</pubDate>
            <atom:updated>2020-06-03T19:10:23.471Z</atom:updated>
            <content:encoded><![CDATA[<h4>A Beginner’s Guide to Using Bayesian Optimization With Scikit-Optimize</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*PGXAzu24ax4nA38r" /><figcaption>Photo by <a href="https://unsplash.com/@crew?utm_source=medium&amp;utm_medium=referral">Crew</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In the machine learning and deep learning paradigm, model “parameters” and “hyperparameters” are two frequently used terms where “parameters” define configuration variables that are internal to the model and whose values can be estimated from the training data and “hyperparameters” define configuration variables that are external to the model and whose values cannot be estimated from the training data ( <a href="https://machinelearningmastery.com/difference-between-a-parameter-and-a-hyperparameter/">What is the Difference Between a Parameter and a Hyperparameter?</a> ). Thus, the hyperparameter values need to be manually assigned by the practitioner.</p><p>Every machine learning and deep learning model that we make has a different set of hyperparameter values that need to be fine-tuned to be able to obtain a satisfactory result. Compared to machine learning models, deep learning models tend to have a larger number of hyperparameters that need optimizing in order to get the desired predictions due to its architectural complexity over typical machine learning models.</p><p>Repeatedly experimenting with different value combinations manually to derive the optimal hyperparameter values for each of these hyperparameters can be a very time consuming and tedious task that requires good intuition, a lot of experience, and a deep understanding of the model. Moreover, some hyperparameter values may require continuous values, which will have an undefined number of possibilities, and even if the hyperparameters require a discrete value, the number of possibilities is enormous, thus manually performing this task is rather difficult. Having said all that, hyperparameter optimization might seem like a daunting task but thanks to several libraries that are readily available in the cyberspace, this task has become more straightforward. These libraries aid in implementing different hyperparameter optimization algorithms with less effort. A few such libraries are<a href="https://scikit-optimize.github.io/"> Scikit-Optimize</a>, <a href="https://scikit-learn.org/stable/">Scikit-Learn</a>, and <a href="https://github.com/hyperopt/hyperopt">Hyperopt</a>.</p><p>There are several hyperparameter optimization algorithms that have been employed frequently throughout the years, they are Grid Search, Random Search, and automated hyperparameter optimization methods. Grid Search and Random Search both set up a grid of hyperparameters but in Grid Search every single value combination will be exhaustively explored to find the hyperparameter value combination that gives the best accuracy values making this method very inefficient. On the other hand, Random Search will repeatedly select random combinations from the grid until the specified number of iterations is met and is proven to yield better results compared to the Grid Search. However, even though it manages to give a good hyperparameter combination we cannot be certain that it is, in fact, the best combination. Automated hyperparameter optimization uses different techniques like Bayesian Optimization that carries out a guided search for the best hyperparameters ( <a href="https://www.kaggle.com/willkoehrsen/intro-to-model-tuning-grid-and-random-search">Hyperparameter Tuning using Grid and Random Search</a>). Research has shown that Bayesian optimization can yield better hyperparameter combinations than Random Search ( <a href="https://arimo.com/data-science/2016/bayesian-optimization-hyperparameter-tuning/">Bayesian Optimization for Hyperparameter Tuning</a>).</p><p>In this article, we will be providing a step-by-step guide into performing a hyperparameter optimization task on a deep learning model by employing Bayesian Optimization that uses the Gaussian Process. We used the <a href="https://scikit-optimize.github.io/#skopt.gp_minimize">gp_minimize</a> package provided by the <a href="https://scikit-optimize.github.io/">Scikit-Optimize (skopt)</a> library to perform this task. We will be performing the hyperparameter optimization on a simple stock closing price forecasting model developed using <a href="https://www.tensorflow.org/">TensorFlow</a>.</p><h3>Scikit-Optimize (skopt)</h3><p>Scikit-Optimize is a library that is relatively easy to use than other hyperparameter optimization libraries and also has better community support and documentation. This library implements several methods for sequential model-based optimization by reducing expensive and noisy black-box functions. For more information you can refer <a href="https://neptune.ai/blog/scikit-optimize">neptune.ai’s article </a>where they have done a comprehensive analysis on the capabilities and usage of skopt.</p><h3>Bayesian Optimization using the Gaussian Process</h3><p>Bayesian optimization is one of the many functions that skopt offers. Bayesian optimization finds a posterior distribution as the function to be optimized during the parameter optimization, then uses an acquisition function (eg. Expected Improvement-EI, another function etc) to sample from that posterior to find the next set of parameters to be explored. Since Bayesian optimization decides the next point based on more systematic approach considering the available data it is expected to yield achieve better configurations faster compared to the exhaustive parameter optimization techniques such as Grid Search and Random Search. You can read more about the Bayesian Optimizer in skopt from <a href="https://orbi.uliege.be/bitstream/2268/226433/1/PyData%202017_%20Bayesian%20optimization%20with%20Scikit-Optimize.pdf">here</a>.</p><h3>Code Alert!</h3><p>So, enough with the theory, let’s get down to business!</p><p>This example code is done using python and TensorFlow. Furthermore, the goal of this hyperparameter optimization task is to obtain the set of hyperparameter values that can give the lowest possible Root Mean Square Error (RMSE) for our deep learning model. We hope this will be very straight forward for any first-timer.</p><p>First, let us install Scikit-Optimize. You can install it using pip by executing this command.</p><pre>pip install scikit-optimize</pre><p>Please note that you will have to make some adjustments to your existing deep learning model code in order to make it work with the optimization.</p><p>First, let’s do some necessary imports.</p><pre>import skopt<br>from skopt import gp_minimize<br>from skopt.space import Real, Integer<br>from skopt.utils import use_named_args<br>import tensorflow as tf<br>import numpy as np<br>import pandas as pd<br>from math import sqrt<br>import atexit<br>from time import time, strftime, localtime<br>from datetime import timedelta<br>from sklearn.metrics import mean_squared_error<br>from skopt.plots import plot_convergence</pre><p>We will now set the TensorFlow and Numpy seed as we want to get reproducible results.</p><pre>randomState = 46<br>np.random.seed(randomState)<br>tf.set_random_seed(randomState)</pre><p>Shown below are some essential python global variables that we have declared. Among the variables, we have also declared the hyperparameters that we are hoping to optimize (the 2nd set of variables).</p><pre>input_size=1<br>features = 2<br>column_min_max = [[0, 2000],[0,500000000]]<br>columns = [&#39;Close&#39;, &#39;Volume&#39;]<br><br>num_steps = None<br>lstm_size = None<br>batch_size = None<br>init_learning_rate = None<br>learning_rate_decay = None<br>init_epoch = None<br>max_epoch = None<br>dropout_rate = None</pre><p>The “input_size” depicts a part of the shape of the prediction. The “features” depict the number of features in the data set and the “columns” list has the header names of the two features. The “column_min_max” variable contains the upper and lower scaling bounds of both the features (this was taken by examining validation and training splits).</p><p>After declaring all these variables it’s finally time to declare the search space for each of the hyperparameters we are hoping to optimize.</p><pre>lstm_num_steps = Integer(low=2, high=14, name=&#39;lstm_num_steps&#39;)<br>size = Integer(low=8, high=200, name=&#39;size&#39;)<br>lstm_learning_rate_decay = Real(low=0.7, high=0.99, prior=&#39;uniform&#39;, name=&#39;lstm_learning_rate_decay&#39;)<br>lstm_max_epoch = Integer(low=20, high=200, name=&#39;lstm_max_epoch&#39;)<br>lstm_init_epoch = Integer(low=2, high=50, name=&#39;lstm_init_epoch&#39;)<br>lstm_batch_size = Integer(low=5, high=100, name=&#39;lstm_batch_size&#39;)<br>lstm_dropout_rate = Real(low=0.1, high=0.9, prior=&#39;uniform&#39;, name=&#39;lstm_dropout_rate&#39;)<br>lstm_init_learning_rate = Real(low=1e-4, high=1e-1, prior=&#39;log-uniform&#39;, name=&#39;lstm_init_learning_rate&#39;)</pre><p>If you look closely you will be able to see that we have declared the ‘lstm_init_learning_rate’ prior to log-uniform without just putting uniform. What this does is that, if you had put prior as uniform, the optimizer will have to search from 1e-4 (0.0001 ) to 1e-1 (0.1) in a uniform distribution. But when declared as log-uniform, the optimizer will search between -4 and -1, thus making the process much more efficient. This has been <a href="https://scikit-optimize.github.io/notebooks/hyperparameter-optimization.html">advised</a> when assigning the search space for learning rate by the skopt library.</p><p>There are several data types using which you can define the search space. Those are Categorical, Real and Integer. When defining a search space that involves floating point values you should go for “Real” and if it involves integers, go for “Integer”. If your search space involves categorical values like different activation functions, then you should go for the “Categorical” type.</p><p>We are now going to put down the parameters that we are going to optimize in the ‘dimensions’ list. This list will be passed to the ‘gp_minimize’ function later on. You can see that we have also declared the ‘default_parameters’. These are the default parameter values we have given to each hyperparameter. <strong>Remember to type in the default values in the same order as you listed the hyperparameters in the ‘dimensions’ list.</strong></p><pre>dimensions = [lstm_num_steps, size, lstm_init_epoch, lstm_max_epoch,<br>lstm_learning_rate_decay, lstm_batch_size, lstm_dropout_rate, lstm_init_learning_rate]<br><br>default_parameters = [2,128,3,30,0.99,64,0.2,0.001]</pre><p>The most important thing to remember is that the hyperparameters in the “default_parameters” list will be the starting point of your optimization task. The Bayesian Optimizer will use the default parameters that you have declared in the first iteration and depending on the result, the acquisition function will determine which point it wants to explore next.</p><p>It can be said that if you have run the model several times previously and found a decent set of hyperparameter values, you can put them as the default hyperparameter values and start your exploration from there. What this may do is that it will help the algorithm find the lowest RMSE value faster (fewer iterations). However, do keep in mind that this might not always be true. Also, remember to assign a value that is within the search space that you have defined when assigning the default values.</p><p>What we have done up to now is setting up all the initial work for the hyperparameter optimization task. We will now focus on the implementation of our deep learning model. We will not be discussing the data pre-processing of the model development process as this article only focuses on the hyperparameter optimization task. We will include the GitHub link of the complete implementation at the end of this article.</p><p>However, to give you a little bit more context, we divided our data set into three splits for training, validation, and testing. The training set was used to train the model and the validation set was used to do the hyperparameter optimization task. As mentioned before, we are using the Root Mean Square Error (RMSE) to evaluate the model and perform the optimization (minimize RMSE).</p><p>The accuracy assessed using the validation split cannot be used to evaluate the model since the selected hyperparameters minimizing the RMSE with validation split can be overfitted to the validation set during the hyperparameter optimization process. Therefore, it is standard procedure to use a test split that has not used at any point in the pipeline to measure the accuracy of the final model.</p><p>Shown below is the implementation of our deep learning model:</p><pre>def setupRNN(inputs, model_dropout_rate):<br><br>  cell = tf.contrib.rnn.LSTMCell(lstm_size, state_is_tuple=True, activation=tf.nn.tanh,use_peepholes=True)<br><br>  val1, _ = tf.nn.dynamic_rnn(cell, inputs, dtype=tf.float32)<br><br>  val = tf.transpose(val1, [1, 0, 2])<br><br>  last = tf.gather(val, int(val.get_shape()[0]) -1, name=&quot;last_lstm_output&quot;)<br><br>  dropout = tf.layers.dropout(last, rate=model_dropout_rate, training=True,seed=46)<br><br>  weight = tf.Variable(tf.truncated_normal([lstm_size, input_size]))<br>  bias = tf.Variable(tf.constant(0.1, shape=[input_size]))<br><br>  prediction = tf.matmul(dropout, weight) +bias<br><br>  return prediction</pre><p>The “setupRNN” function contains our deep learning model. Still, you may not want to understand those details, as Bayesian optimization considers that function as a black-box that takes certain hyperparameters as the inputs and then outputs the prediction. So if you are not interested in understanding what we have inside that function, you may skip the next paragraph.</p><p>Our deep learning model contains an LSTM layer, a dropout layer and an output layer. The necessary information required for the model to work needs to be sent to this function (in our case, it was the input and the dropout rate). You can then proceed with implementing your deep learning model inside this function. In our case, we used an LSTM layer to identify the temporal dependencies of our stock data-set.</p><p>We then fed the last output of the LSTM to the dropout layer for regularization purposes and obtained the prediction through the output layer. Finally, remember to return this prediction (in a classification task this can be your logit) to the function that will be passed to the Bayesian Optimization ( “setupRNN” will be called by this function).</p><p>If you are performing a hyperparameter optimization for a machine learning algorithm (using a library like Scikit-Learn) you will not need a separate function to implement your model as the model itself is already given by the library and you will only be writing code to train and obtain predictions. Therefore, this code can go inside the function that will be returned to the Bayesian Optimization.</p><p>We have now come to the most important section of the hyperparameter optimization task, the ‘fitness’ function.</p><pre>@use_named_args(dimensions=dimensions)<br>def fitness(lstm_num_steps, size, lstm_init_epoch, lstm_max_epoch,<br>            lstm_learning_rate_decay, lstm_batch_size, lstm_dropout_rate, lstm_init_learning_rate):<br><br>    global iteration, num_steps, lstm_size, init_epoch, max_epoch, learning_rate_decay, dropout_rate, init_learning_rate, batch_size<br><br>    num_steps = np.int32(lstm_num_steps)<br>    lstm_size = np.int32(size)<br>    batch_size = np.int32(lstm_batch_size)<br>    learning_rate_decay = np.float32(lstm_learning_rate_decay)<br>    init_epoch = np.int32(lstm_init_epoch)<br>    max_epoch = np.int32(lstm_max_epoch)<br>    dropout_rate = np.float32(lstm_dropout_rate)<br>    init_learning_rate = np.float32(lstm_init_learning_rate)<br><br>    tf.reset_default_graph()<br>    tf.set_random_seed(randomState)<br>    sess = tf.Session()<br><br>    train_X, train_y, val_X, val_y, nonescaled_val_y = pre_process()<br><br>    inputs = tf.placeholder(tf.float32, [None, num_steps, features], name=&quot;inputs&quot;)<br>    targets = tf.placeholder(tf.float32, [None, input_size], name=&quot;targets&quot;)<br>    model_learning_rate = tf.placeholder(tf.float32, None, name=&quot;learning_rate&quot;)<br>    model_dropout_rate = tf.placeholder_with_default(0.0, shape=())<br>    global_step = tf.Variable(0, trainable=False)<br><br>    prediction = setupRNN(inputs,model_dropout_rate)<br><br>    model_learning_rate = tf.train.exponential_decay(learning_rate=model_learning_rate, global_step=global_step, decay_rate=learning_rate_decay,<br>                                                decay_steps=init_epoch, staircase=False)<br><br>    with tf.name_scope(&#39;loss&#39;):<br>        model_loss = tf.losses.mean_squared_error(targets, prediction)<br><br>    with tf.name_scope(&#39;adam_optimizer&#39;):<br>        train_step = tf.train.AdamOptimizer(model_learning_rate).minimize(model_loss,global_step=global_step)<br><br>    sess.run(tf.global_variables_initializer())<br><br>    for epoch_step in range(max_epoch):<br><br>        for batch_X, batch_y in generate_batches(train_X, train_y, batch_size):<br>            train_data_feed = {<br>                inputs: batch_X,<br>                targets: batch_y,<br>                model_learning_rate: init_learning_rate,<br>                model_dropout_rate: dropout_rate<br>            }<br>            sess.run(train_step, train_data_feed)<br><br>    val_data_feed = {<br>        inputs: val_X,<br>    }<br>    vali_pred = sess.run(prediction, val_data_feed)<br><br>    vali_pred_vals = rescle(vali_pred)<br><br>    vali_pred_vals = np.array(vali_pred_vals)<br><br>    vali_pred_vals = vali_pred_vals.flatten()<br><br>    vali_pred_vals = vali_pred_vals.tolist()<br><br>    vali_nonescaled_y = nonescaled_val_y.flatten()<br><br>    vali_nonescaled_y = vali_nonescaled_y.tolist()<br><br>    val_error = sqrt(mean_squared_error(vali_nonescaled_y, vali_pred_vals))<br><br>    return val_error</pre><p>As shown above, we are passing the hyperparameter values to a function named “fitness.” The “fitness” function will be passed to the Bayesian hyperparameter optimization process ( <em>gp_minimize</em>). Note that in the first iteration, the values passed to this function will be the default values that you defined and from there onward Bayesian Optimization will choose the hyperparameter values on its own. We then assign the chosen values to the python global variables we declared at the beginning so that we will be able to use the latest chosen hyperparameter values outside the fitness function.</p><p>We then come to a rather critical point in our optimization task. If you have used TensorFlow prior to this article, you would know that TensorFlow operates by creating a computational graph for any kind of deep learning model that you make.</p><p>During the hyperparameter optimization process, in each iteration, we will be resetting the existing graph and constructing a new one. This process is done to minimize the memory taken for the graph and prevent the graphs from stacking on top of each other. Immediately after resetting the graph you will have to set the TensorFlow random seed in order to obtain reproducible results. After the above process, we can finally declare the TensorFlow session.</p><p>After this point, you can start adding code responsible for training and validating your deep learning model as you normally would. This section is not really related to the optimization process but the code after this point will start utilizing the hyperparameter values chosen by the Bayesian Optimization.</p><p>The main point to remember here is to <strong>return</strong> the final metric value (in this case the RMSE value) obtained for the validation split. This value will be returned to the Bayesian Optimization process and will be used when deciding the next set of hyperparameters that it wants to explore.</p><p><strong>Note</strong>: if you are dealing with a classification problem you would want to put your accuracy as a negative value (eg. -96) because, even though the higher the accuracy the better the model, the Bayesian function will keep trying to reduce the value as it is designed to find the hyperparameter values for the <strong>lowest</strong> value that is returned to it.</p><p>Let us now put down the execution point for this whole process, the “main” function. Inside the main function, we have declared the “gp_minimize” function. We are then passing several essential parameters to this function.</p><pre>if __name__ == &#39;__main__&#39;:<br><br>    start = time()<br><br>    search_result = gp_minimize(func=fitness,<br>                                dimensions=dimensions,<br>                                acq_func=&#39;EI&#39;, # Expected Improvement.<br>                                n_calls=11,<br>                                x0=default_parameters,<br>                                random_state=46)<br><br>print(search_result.x)<br>print(search_result.fun)<br>plot = plot_convergence(search_result,yscale=&quot;log&quot;)<br><br>atexit.register(endlog)<br>logger(&quot;Start Program&quot;)</pre><p>The “func” parameter is the function you would want to finally model using the Bayesian Optimizer. The “dimensions” parameter is the set of hyperparameters that you are hoping to optimize and the “acq_func” stands for the acquisition function and is the function that helps to decide the next set of hyperparameter values that should be used. There are 4 types of acquisition functions supported by <em>gp_minimize</em>. They are:</p><ul><li>LCB: lower confidence bound</li><li>EI: expected improvement</li><li>PI: probability of improvement</li><li>gp_hedge: probabilistically choose one of the above three acquisition functions at every iteration</li></ul><p>The above information was extracted from the documentation. Each of these has its own advantages but if you are a beginner to Bayesian Optimization, try using “EI” or “gp_hedge” as “EI” is the most widely used acquisition function and “gp_hedge” will choose one of the above-stated acquisition functions probabilistically thus, you wouldn’t have to worry too much about that.</p><p>Keep in mind that when using different acquisition functions there might be other parameters that you might want to change that affects your chosen acquisition function. Please refer the parameter list in the <a href="https://scikit-optimize.github.io/#skopt.gp_minimize">documentation</a> for this.</p><p>Back to explaining the rest of the parameters, the “n_calls” parameter is the number of times you would want to run the fitness function. The optimization task will start by using the hyperparameter values defined by “x0”, the default hyperparameter values. Finally, we are setting the random state of the hyperparameter optimizer as we need reproducible results.</p><p>Now when you run the <em>gp_optimize</em> function the flow of events will be:</p><p>The fitness function will use with the parameters passed to x0. The LSTM will be trained with the specified epochs and the validation input will be run to get the RMSE value for its predictions. Then depending on that value, the Bayesian optimizer will decide what the next set of hyperparameter values it wants to explore with the help of the acquisition function.</p><p>In the 2nd iteration, the fitness function will run with the hyperparameter values that the Bayesian optimization has derived and the same process will repeat until it has iterated “n_call” times. When the complete process comes to an end, the Scikit-Optimize object will get assigned to the “search _result” variable.</p><p>We can use this object to retrieve useful information as stated in the documentation.</p><ul><li>x [list]: location of the minimum.</li><li>fun [float]: function value at the minimum.</li><li>models: surrogate models used for each iteration.</li><li>x_iters [list of lists]: location of function evaluation for each iteration.</li><li>func_vals [array]: function value for each iteration.</li><li>space [Space]: the optimization space.</li><li>specs [dict]`: the call specifications.</li><li>rng [RandomState instance]: State of the random state at the end of minimization.</li></ul><p>The “search_result.x” gives us optimal hyperparameter values and using “search_result.fun” we can obtain the RMSE value of the validation set corresponding to the obtained hyperparameter values (The lowest RMSE value obtained for the validation set).</p><p>Shown below are the optimal hyperparameter values that we obtained for our model and the lowest RMSE value of the validation set. If you are finding it hard to figure out the order in which the hyperparameter values are being listed when using “search_result.x”, it is in the same order as you specified your hyperparameters in the “dimensions” list.</p><p><strong>Hyperparameter Values:</strong></p><ul><li>lstm_num_steps: 6</li><li>lstm_size: 171</li><li>lstm_init_epoch: 3</li><li>lstm_max_epoch: 58</li><li>lstm_learning_rate_decay: 0.7518394019565194</li><li>lstm_batch_size: 24</li><li>lstm_dropout_rate: 0.21830825193089087</li><li>lstm_init_learning_rate: 0.0006401363567813549</li></ul><p><strong>Lowest RMSE:</strong> 2.73755355221523</p><h3>Convergence Graph</h3><p>The hyperparameters that produced the lowest point of the Bayesian Optimization in this graph is what we get as the optimal set of hyperparameter values.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*zAh9pLwrioujKHXE.jpg" /></figure><p>The graph shows a comparison of the lowest RMSE values recorded for each iteration (50 iterations) in Bayesian Optimization and Random Search. We can see that the Bayesian Optimization has been able to converge rather better than the Random Search. However, in the beginning, we can see that Random search has found a better minimum faster than the Bayesian Optimizer. This can be due to random sampling being the nature of Random Search.</p><p>We have finally come to the end of this article, so to conclude, we hope this article made your deep learning model building task easier by showing you a better way of finding the optimal set of hyperparameters. Here’s to no more stressing over hyperparameter optimization. Happy coding, fellow geeks!</p><h3>Useful Materials:</h3><ul><li>The complete code can be found through this <a href="https://github.com/suleka96/Bayesian_Hyperparameter_optimization/tree/master">link</a>.</li><li>For more information about Bayesian optimization, you can refer to this <a href="https://arxiv.org/pdf/1807.02811.pdf">paper</a>.</li><li>For more information about acquisition functions, refer to this <a href="https://www.cse.wustl.edu/~garnett/cse515t/spring_2015/files/lecture_notes/12.pdf">paper</a>.</li></ul><p><em>Originally published at </em><a href="https://dzone.com/articles/how-to-automate-hyperparameter-optimization"><em>https://dzone.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=73950c9fdb5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/how-to-automate-hyperparameter-optimization-73950c9fdb5">How to Automate Hyperparameter Optimization</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[All you need to know about RNNs]]></title>
            <link>https://medium.com/data-science/all-you-need-to-know-about-rnns-e514f0b00c7c?source=rss-ddf3befa5aec------2</link>
            <guid isPermaLink="false">https://medium.com/p/e514f0b00c7c</guid>
            <category><![CDATA[recurrent-neural-network]]></category>
            <category><![CDATA[tensorflow]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Suleka Helmini]]></dc:creator>
            <pubDate>Wed, 06 Mar 2019 13:18:20 GMT</pubDate>
            <atom:updated>2019-03-06T13:18:20.641Z</atom:updated>
            <content:encoded><![CDATA[<h4>A beginner’s guide into the implementation and data manipulation inside a RNN in TensorFlow</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*P83tkcIAj1UjJiBE" /><figcaption>Photo by <a href="https://unsplash.com/@laurentnaville_1467602_sink?utm_source=medium&amp;utm_medium=referral">Laurent Naville</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Introduction</h3><p>Researchers came up with neural networks to model the behaviour of a human brain. But if you actually think about it, normal neural networks don’t really do that much justice to its original intention. The reason for this statement is that feedforward vanilla neural networks cannot remember the things it learns. Each iteration you train the network it starts fresh, it doesn’t remember what it saw in the previous iteration when you are processing the current set of data. This is a big disadvantage when identifying correlations and data patterns. This is where Recurrent Neural Networks (RNN)came into the picture. RNNs have a very unique architecture that helps them to model memory units (hidden state) that enable them to persist data, thus being able to model short term dependencies. Due to this reason, RNNs are extensively used in time-series forecasting to identify data correlations and patterns.</p><p>Even though RNNs have been around for some time, everyone seems to have their own confusing way of explaining it’s architecture and no one really explains what happens behind the scenes. So let’s bridge the gap, shall we? This post is aimed at explaining the RNN architecture in a more granular level by going through its functionality.</p><h3>Is this for you?</h3><p>If you have blindly made simple RNN models using TensorFlow before and if you have been finding it hard to understand about what the inner workings of a RNN look like, then this article is just for you.</p><h3><strong>End Goal?</strong></h3><p>We will basically be explaining what happens behind the curtains when these two lines of TensorFlow code that are responsible for the declaration of the RNN and initiating the execution is run.</p><pre>cell = tf.contrib.rnn.BasicRNNCell(rnn_size,activation=tf.nn.tanh)<br><br>val1, state = tf.nn.dynamic_rnn(cell, inputs, dtype=tf.float32)</pre><h3>RNN Architecture</h3><p>If you ever searched for architectural information about RNNs, the architecture diagrams you might get are rather confusing if you start looking into them as a beginner. I will use an example approach to explain the RNN architecture.</p><p>Before we get down to business, an important thing to note is that the RNN input needs to have 3 dimensions. Typically it would be batch size, the number of steps and number of features. The number of steps depicts the number of time steps/segments you will be feeding in one line of input of a batch of data that will be fed into the RNN.</p><p>The RNN unit in TensorFlow is called the “RNN cell”. This name itself has created a lot of confusion among people. There are many questions on Stackoverflow that inquire if “RNN cell” refers to one single cell or the whole layer. Well, it’s more like the whole layer. The reason for this is that the connections in RNNs are recurrent, thus following a “feeding to itself” approach. Basically, the RNN layer is comprised of a single rolled RNN cell that unrolls according to the “number of steps” value (number of time steps/segments) you provide.</p><p>As we mentioned earlier the main speciality in RNNs is the ability to model short term dependencies. This is due to the hidden state in the RNN. It retains information from one time step to another flowing through the unrolled RNN units. Each unrolled RNN unit has a hidden state. The current time steps hidden state is calculated using information of the previous time step’s hidden state and the current input. This process helps to retain information on what the model saw in the previous time step when processing the current time steps information. Also, something to note is that all the connections in RNN have weights and biases. The biases can be optional in some architectures. This process will be explained further in later parts of the article.</p><p>Since you now have a basic idea, let’s break down the execution process with an example. Say your batch size is 6, RNN size is 7, the number of time steps/segments you would include in one input line is 5 and the number of features in one time step is 3. If this is the case, your input tensor (matrix) shape for one batch would look something like this:</p><p>Tensor shape of one batch = (6,5,3)</p><p>The data inside a batch would look something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/845/1*2NBMWLXqeTXh4kG7VuJxow.jpeg" /><figcaption>Fig 01 : Data representation inside a batch of data</figcaption></figure><p>Note: The data segmentation method used here is called the sliding window approach and is mostly used when doing time series analysis. You don’t have to worry about the data pre-processing process here.</p><p>When first feeding the data into the RNN. It will have a rolled architecture as shown below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/276/1*Ky7VgPH2h1EpP_aGBcUhjQ.jpeg" /><figcaption>Fig 02: Rolled version of RNN</figcaption></figure><p>But when the RNN starts to process the data it will unroll and produce outputs as shown below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8c5EY0XQEKir_5xxPZRloA.jpeg" /><figcaption>Fig 03: Unrolled version of RNN</figcaption></figure><h4><strong>Processing a batch:</strong></h4><p>When you feed a batch of data into the RNN cell it starts the processing from the 1st line of input. Likewise, the RNN cell will sequentially process all the input lines in the batch of data that was fed and give one output at the end which includes all the outputs of all the input lines.</p><h4><strong>Processing a single line of input:</strong></h4><p>In order to process a line of input, the RNN cell unrolls “number of steps” times. You can see this in the above figure (Fig 03). Since we defined “number of steps” as 5, the RNN cell has been unrolled 5 times.</p><p>The execution process is as follows:</p><ul><li>First, the initial hidden state (S), which is typically a vector of zeros and the hidden state weight (h) is multiplied and then the hidden state bias is added to the result. In the meantime, the input at the time step t ([1,2,3]) and the input weight (i) is multiplied and the input bias is added to that result. We can obtain the hidden state at time step t by sending the addition of the above two results through an activation function, typically tanh (f).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/561/1*CkZmIB1cqjw29jTWdHfhqg.jpeg" /><figcaption>Fig 04: Hidden state calculation of time step t</figcaption></figure><ul><li>Then, to obtain the output at time step t, the hidden state (S) at time step t is multiplied by the output weight (O) at time step t and then the output bias is added to the result.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/561/1*vvSNAaK3pgbKkkTUj-higg.jpeg" /><figcaption>Fig 05: Output calculation of time step t</figcaption></figure><ul><li>When calculating the hidden state at time step t+1, the hidden state (S) at time step t is multiplied by the hidden state weight (h) and the hidden state bias is added to the result. Then as mentioned before the input at time step t+1 ([4,5,6]) will get multiplied by the input weight (i) and the input bias will be added to the result. These two results will then be sent through an activation function, typically tanh (f).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/561/1*9785d656PnLcPW_ODUOrhg.jpeg" /><figcaption>Fig 06: Hidden state calculation of time step t+1</figcaption></figure><ul><li>Then, to obtain the output at time step t+1, the hidden state (S) at time step t+1 is multiplied by the output weight (O) at time step t+1 and then the output bias is added to the result. As you can see, when producing the output of time step t+1 it not only uses the input data of time step t+1 but also uses information of data in time step t via the hidden state at time step t+1.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/561/1*gAStky6c6K-s23YyaY6-0g.jpeg" /><figcaption>Fig 07: Output calculation of time step t+1</figcaption></figure><ul><li>This process will repeat for all the time steps</li></ul><p>After processing all time steps in one line of input in the batch, we will have 5 outputs of shape (1,7). So when all these outputs are concatenated together. the shape becomes (1,5,7). When all the input lines of the batch are done processing we get 6 outputs of size (1,5,7). Thus, the final output of the whole batch would be (6,5,7).</p><p><strong>Note: </strong>All the hidden state weights, output weights and input weights have the same value throughout all the connections in a RNN.</p><p>Coming back to the 2 lines of code we stated earlier:</p><pre>cell = tf.contrib.rnn.BasicRNNCell(rnn_size,activation=tf.nn.tanh)<br><br>val1, state = tf.nn.dynamic_rnn(cell, inputs, dtype=tf.float32)</pre><p>The 1st line basically defines the activation function and the RNN size of the RNN cell that we want to create. The 2nd line executes the processing procedure of the input data by feeding it into the RNN. The processing will happen according to what we discussed earlier. Finally, the output (value with shape (6,5,7) ) of that batch will be assigned to the “val1” variable. The final value of the hidden state will be assigned to the “state” variable.</p><p>We have now come to the end of the article. In this article, we discussed the data manipulation and representation process inside of a RNN in TensorFlow. With all the provided information, I hope that now you have a good understanding of how RNNs work in TensorFlow.</p><h3>Useful Materials:</h3><ul><li>For general information about the RNN architecture, you can refer to this <a href="http://colah.github.io/posts/2015-08-Understanding-LSTMs/">article</a>.</li><li>For a deep dive into the RNN architecture refer to this <a href="https://medium.com/@jianqiangma/all-about-recurrent-neural-networks-9e5ae2936f6e">article</a></li><li>Coursera <a href="https://www.coursera.org/lecture/nlp-sequence-models/recurrent-neural-network-model-ftkzt">course</a> on RNN</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e514f0b00c7c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/all-you-need-to-know-about-rnns-e514f0b00c7c">All you need to know about RNNs</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Empirical Analysis on Email Classification Using the Enron Dataset]]></title>
            <link>https://medium.com/data-science/empirical-analysis-on-email-classification-using-the-enron-dataset-19054d558697?source=rss-ddf3befa5aec------2</link>
            <guid isPermaLink="false">https://medium.com/p/19054d558697</guid>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[classification]]></category>
            <dc:creator><![CDATA[Suleka Helmini]]></dc:creator>
            <pubDate>Sun, 11 Nov 2018 10:26:53 GMT</pubDate>
            <atom:updated>2019-06-17T17:10:59.302Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*8eJhfKzFb_0yg61H4Bq5EA.jpeg" /></figure><p>In this article we will first evaluate the performance of 3 machine learning algorithms, namely, logistic regression, support vector classification and random forest classifier. In the latter part we will compare the performance of the machine learning algorithms with the results obtained using a recurrent neural network model (LSTM). We will be using the<a href="http://www2.aueb.gr/users/ion/data/enron-spam/"> Enron dataset</a> throughout this evaluation process.</p><p><strong>1. About the Dataset</strong></p><p>Enron dataset consists of emails sent mostly by the senior management of the Enron Corporation. In this experiment we are using a processed version of this dataset specifically made for spam and ham classification. The dataset consists of 30207 emails of which 16545 emails are labeled as ham and 13662 emails are labeled as spam.</p><p><strong>2.</strong> <strong>Methodology</strong></p><p><strong>2.1 Data Pre-Processing</strong></p><p><strong>2.1.1 Machine Learning</strong></p><p>For the machine learning algorithms I utilized the the bag of words approach. It is a way of extracting features from the text for use in machine learning algorithms [1]. In this, we first tokenize the emails and make a dictionary of all the words in all the emails. We then take the most common 3000 words from it as our final dictionary. Then we iterate through each email and record the occurrence frequency of each token in the email corresponding the token in the dictionary. This way the maximum sequence length becomes 3000 for all the emails.</p><p><strong>2.1.2 Recurrent Neural Network</strong></p><p>A slightly different bag of words approach was used to pre-process emails for the RNN model. We start with tokenizing the emails and making a dictionary of all the words in all the emails just like we did for the machine learning models. Then all the words will be indexed starting from 1. The collection of emails will then be iterated and for each word in the email, the dictionary will be referred to map the word to the corresponding index. This approach ensures that the sequence of the words in the email retains. But we now have a problem of each email having a different sequence length. To mitigate this we will get the length of the longest email and pad all the emails to that length. The padding will be done by adding “0”s (zero) to the beginning of each sequence so that it will not impact the training procedure. In this scenario the sequence length of all the emails is 3425.</p><p><strong>2.2 Machine Learning Algorithms</strong></p><p>The Enron dataset will be used to train the machine learning models to classify an email as spam or ham.All the utilized machine learning algorithms were taken from the scikit learn library. Parameter tuning for all the algorithms were done manually. The reason for this is that since the size of the data set is 30207, running a grid search is too time intensive</p><p><strong>2.2.1 Splitting Data</strong></p><p>For all the algorithms, the data was split into three groups as the training, validation and testing set. The validation set was used to tune the parameters. After the parameter tuning process the testing set was used to obtain the final results. The training set comprised of 80% of the data and 20% was given for the validation set. Then this 20% was further divided equally so that 50% of it was taken for validation the other 50% was taken for testing.</p><p><strong>2.2.2 Support Vector Classification (SVC)</strong></p><p>Support Vector Classification algorithm models the situation by creating a feature space, which is a finite-dimensional vector space, each dimension of which represents a “feature” of a particular object [2].</p><p>First, observations were carried out without any optimizations. Shown below are the initial results:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/950/1*r64S8ynGVyMEwQvTbdorvw.jpeg" /></figure><p>After obtaining the initial results using the linear kernel, the C parameter was tuned to obtain the final results. Shown below are the final results received for the validation and the test set. These are the highest validation scores obtained during the observation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/797/1*50yBssX7-KTBl52Im3PBFg.jpeg" /></figure><p><strong>2.2.3 Random Forest Classifier</strong></p><p>The Random forest classifier creates a set of decision trees from a randomly selected subset of a training set. It then aggregates the votes from different decision trees to decide the final class of the test object [3].</p><p>Initially, observations were carried out without any optimizations like in the SVC algorithm. Shown below are the initial results:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/796/1*2BwfL-YZO7XtlPA7HDxJ8A.jpeg" /></figure><p>The initial results were improved by altering the different parameters that has a likelihood of making the most impact to the end result. The explored parameters are: n_estimators, max_features and Max_depth. Shown below are the final results:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/801/1*jC0f_Cquu3GUVHDUSgcnlw.jpeg" /></figure><p><strong>2.2.4 Logistic Regression</strong></p><p>Logistic regression is used to describe data and to explain the relationship between one dependent binary variable and one or more nominal, ordinal, interval or ratio-level independent variables [4].</p><p>The algorithm was run with the default settings as there were no parameters to optimize. Shown below are the obtained results of the testing set.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/803/1*FSzJMYBjQewB9OurIfuN8g.jpeg" /></figure><p><strong>2.2.5 Summary</strong></p><p>When comparing these models we will mainly be focusing on the f1 score. As per the observations, despite the optimizations done to the svc algorithm, a significant improvement could not be achieved. But the random forest classifier showed a rather significant improvement after tuning the parameter values. Even though both of these algorithms gave good results, the final result of the logistic regression algorithm was the highest. As you can see the end result of the logistic regression algorithm and the svc algorithm are not that far apart, but we will have to give the win to the logistic regression algorithm as it is the highest we were able to obtain.</p><p>We will explore further on this subject and see if we can improve the end results by using a different approach. Namely, a Recurrent Neural Network. The type of recurrent neural network we will be using for this observation is a LSTM model.</p><p><strong>2.3 LSTM model</strong></p><p>LSTM stands for Long Short Term Memory networks, these are a special kind of RNN, capable of learning long-term dependencies. These are specially designed to avoid the long-term dependency problem present in most models. LSTMs remember information for long periods of time helping to model long term dependencies [5].</p><p>The Tensorflow framework was utilized to make the LSTM model. The LSTM model consists of several layers.<strong> </strong>When feeding data into the model we will first pass it through an embedded layer. The functionality of an embedded layer is that it will represent each index that represents a word in a dictionary into a specified sized vector of random numerical values within a range. This approach helps to model the relationship between the words. Then the output of the embedded layer will be sent through a LSTM layer. We used the ‘basic LSTM cell’ made available by Tensorflow for this task. The LSTM layer will add a dropout to it’s output to randomly mute some neurons in the neural network in order to decreases the possibility of over-fitting [6]. A hidden layer will then consume the output of the LSTM layer and produce the final output.</p><p>When calculating the cost function, the logit (end result that has not been sent through an activation function) will be passed to it. The utilised cost function is ‘softmax cross entropy with logits’. The optimization function will then be used to reduce the cost. The utilized optimization function is the Adam optimizer.</p><p>Predictions were made by passing the obtained logit value through an activation function. Sigmoid function was used for this as the concerned task is a binary classification.</p><p><strong>2.3.1 Splitting data</strong></p><p>Data was split into 3 groups as the training, validation and testing set as mentioned before in the machine learning approach. After obtaining the initial result, the validation set was used to tune the hyper-parameters in order to improve the final scores. The testing set was then used to make the final evaluation. The percentages of training, validation and testing data was the same as the ones used for the machine learning approach.</p><p><strong>2.3.2 Results</strong></p><p>Shown below are the initial results obtained using the validation dataset:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/861/1*0l9gRWd6Xp1UpcC9LK7-2g.jpeg" /></figure><p>The below results show the final output after tuning the hyper-parameters</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/1*OjFIkDD6tjCHw399POUkvw.jpeg" /></figure><p>After obtaining satisfactory results using the above model. Observations were carried out by stacking more hidden layers to see if the result can be further improved.</p><p>Shown below are the results obtained by using 2 hidden layers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/842/1*ohZ4eHFeOXRGj3AlrfWnwg.jpeg" /></figure><p><strong>2.3.3 Summary</strong></p><p>To evaluate the model we will be mainly considering the f1 score as we did with machine learning algorithms. Considering the obtained results after optimizing the parameters, we can see a slight improvement in the scores.</p><p>When trying to further improve the result by adding more hidden layers, as you can see it has given a much lower output. The reason for this is that, even though on paper, neural networks are supposed to give better scores with stacked layers, in this case we have a shortage of data. The amount of data that we have is not enough to train multiple hidden layers.</p><p><strong>3. Who did a better job?</strong></p><p>As discussed above, the logistic regression algorithm gave better results than both the other machine learning algorithms. So now we come to the question, who did a better job considering the logistic regression algorithm and the LSTM model. Shown below is a comparison of the final results of the logistic regression algorithm and the LSTM model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/836/1*9IhwkM2kqO_g91b7DU0EOw.jpeg" /></figure><p>Looking at the table, we can clearly see that<strong> </strong>the<strong> </strong>f1 score of the LSTM is better than that of the logistic regression algorithm. Basically all the scores of the LSTM model is better than the logistic regression algorithm. This gives an easy win to the LSTM model.</p><p><strong>4. Conclusion</strong></p><p>Considering the obtained results, the LSTM model outperformed all the other considered machine learning algorithms. The reason for this is the LSTMs ability to model long term dependencies. Enron is a text dataset thus, being able to remember dependencies between words throughout an email increases the chance of making a better guess at if its a spam or a ham email. Since the LSTM by design excels at this task, it was able to give better results than the rest of the machine learning algorithms.</p><p>In conclusion, we can say that the LSTM is truly capable of capturing long term dependencies in data and outperforming other machine learning algorithms using it’s memory aspect in the architecture.</p><p><strong>5. References</strong></p><p>[1] <a href="https://medium.com/greyatom/an-introduction-to-bag-of-words-in-nlp-ac967d43b428">https://medium.com/greyatom/an-introduction-to-bag-of-words-in-nlp-ac967d43b428</a></p><p>[2] <a href="https://mindmajix.com/support-vector-machine-algorithm">https://mindmajix.com/support-vector-machine-algorithm</a></p><p>[3] <a href="https://medium.com/machine-learning-101/chapter-5-random-forest-classifier-56dc7425c3e1">https://medium.com/machine-learning-101/chapter-5-random-forest-classifier-56dc7425c3e1</a></p><p>[4] <a href="http://www.statisticssolutions.com/what-is-logistic-regression/">http://www.statisticssolutions.com/what-is-logistic-regression/</a></p><p>[5] <a href="http://colah.github.io/posts/2015-08-Understanding-LSTMs/">http://colah.github.io/posts/2015-08-Understanding-LSTMs/</a></p><p>[6] <a href="https://www.quora.com/How-does-the-dropout-method-work-in-deep-learning-And-why-is-it-claimed-to-be-an-effective-trick-to-improve-your-network">https://www.quora.com/How-does-the-dropout-method-work-in-deep-learning-And-why-is-it-claimed-to-be-an-effective-trick-to-improve-your-network</a></p><p><strong>NOTE</strong>: The complete code used for this evaluation can be found through <a href="https://github.com/suleka96/Email-Classification">this link</a>. Please do feel free to play around with the code and try this yourself. Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=19054d558697" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/empirical-analysis-on-email-classification-using-the-enron-dataset-19054d558697">Empirical Analysis on Email Classification Using the Enron Dataset</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Bitcoin in a Nutshell]]></title>
            <link>https://medium.com/@sulekahelmini9628/bitcoin-in-a-nutshell-d2ae221ee30?source=rss-ddf3befa5aec------2</link>
            <guid isPermaLink="false">https://medium.com/p/d2ae221ee30</guid>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[bitcoin]]></category>
            <dc:creator><![CDATA[Suleka Helmini]]></dc:creator>
            <pubDate>Mon, 05 Nov 2018 19:29:02 GMT</pubDate>
            <atom:updated>2018-11-05T19:29:02.401Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*np2BFmZCfeu1IxZnI1ogjQ.jpeg" /></figure><p>Hey fellow geeks, as my <a href="https://medium.com/@sulekahelmini9628/what-is-a-blockchain-6ce5a08e03b8">previous </a>article was a general explanation on how the blockchain works, this article will be dedicated on discussing how Bitcoin’s block chain works. If you did not go through my previous article or you do not understand what a blockchain is, first I recommend that you give read that and get a basic understanding.</p><p><strong>Bitcoin vs Blockchain</strong></p><p>Most individuals fail to understand the difference between the words Bitcoin and Blockchain. At it’s core Bitcoin is an implementation of the blockchain. It is a cryptocurrency that was introduced back in 2009 which employed a public and anonymous peer to peer blockchain network for it’s transactions.</p><p>The blockchain solved the big problem most digital currencies had, which was called the “double spending problem”. The double spending problem, as the name implies, refers to the risk of a digital currency being spent twice. Furthermore, imagine a person who only has $20 in his account and tries to transfer that $20 to two people at the same time, this what we call the double spending problem. Bitcoin solved this problem by following a certain process in validating transactions, which we will talk about later in this article.</p><p>Bitcoin was initially made as an alternative to using government handled currencies when doing online transactions. The underlying architecture that they employed for this is the blockchain.</p><p><strong>How does Bitcoin Work?</strong></p><p>Bitcoin can be considered as a digital ledger that stores transaction information. Keeping with the blockchain’s features, this ledger is the information that is being decentralized and distributed among the participants in the network.</p><p>Traditional way of making transactions, involving some sort of Fiat money needs a central body like a bank to validate the transactions. But when it comes to Bitcoin, transactions are processed and validated by a large number of computers in a distributed network running dedicated software.</p><p>When a transaction occurs, the sender’s and the receiver’s bitcoin addresses and the amount transferred will be recorded. A collection of the above information about transactions will be encapsulated in a bock in the blockchain. The block will take all these information and run a hashing function on it to generate a unique key. The generated key will be used to uniquely identify the block. This block will also contain the unique key of the previous block.</p><p>Basically a block will contain :</p><ul><li>Unique key of block</li><li>Unique key of previous block</li><li>information</li></ul><p><strong>Miners?</strong></p><p>In the blockchain, a verification process will be carried out by multiple points in the network to ensure that every computer processing Bitcoin has the same copy of the blockchain thus to see if a newly created block is valid. This verification is performed by miners. Miners group recent transactions into blocks that needs to be accepted by the rest of the network in order to be added into the blockchain. This will happen only if the blocks are hashed properly.</p><p>Miners get paid by the system itself. As mentioned before, the creation of new bitcoins is tied with validating the transactions so that double spending does not occur. Miners do something called the proof of work, which basically means running an algorithm to find the correct hash for a block. Miners compete with each other to guess the correct hash first. Whoever guesses the correct hash first, gets to add the new block to the blockchain and will also be awarded a percentage of a Bitcoin. In Bitcoin’s blockchain, it usually takes 10 minutes to find the correct hash for a block, so every 10 mins someone in the network is will be awarded a percentage of a bitcoin.</p><p>The amazing thing about bitcoin is that, there can only be 21 million bitcoins to go around. Every 4 years half the remaining bitcoins will be given out. When the year 2140 dawns no more Bitcoins will be issued.</p><p>This is only a very brief overview about Bitcoin. I hope reading this article gave you some insight about how Bitcoin works and motivated you to dig deeper into the workings of Bitcoin. Until next time, happy reading fellow geeks.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d2ae221ee30" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What is a Blockchain?]]></title>
            <link>https://medium.com/@sulekahelmini9628/what-is-a-blockchain-6ce5a08e03b8?source=rss-ddf3befa5aec------2</link>
            <guid isPermaLink="false">https://medium.com/p/6ce5a08e03b8</guid>
            <category><![CDATA[blockchain]]></category>
            <dc:creator><![CDATA[Suleka Helmini]]></dc:creator>
            <pubDate>Mon, 05 Nov 2018 19:22:18 GMT</pubDate>
            <atom:updated>2018-11-05T19:22:18.740Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*DqRpzi6xtE7Xkq7aO46pGA.png" /></figure><p>The word ‘blockchain’ has become the buzz of the era with the increasing popularity of Bitcoin. Although the concept of the technology has been around since the late 90’s, its potential went rather unrecognized until Bitcoin made use of it. The blockchain we see today was deviced in 2008 by Satoshi Nakamoto. It is also believed that Satoshi Nakamoto can be a group of people. His origins or any information about Satoshi Nakamoto stays unknown to this date.</p><p>As interesting as it may sound, most individuals struggle to understand the concept of the blockchain technology. This article is aimed at explaining the basic concept of what a block chain is and how it works, so let’s get down to business.</p><h3>What is a blockchain?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n-aeHHakh5j2h1tLt0hT7Q.jpeg" /></figure><p>At a very basic level, think of the blockckain as a <strong>chain of blocks</strong> linked in a <strong>chronological </strong>order, where each block has the ability to store information. This chain is not owned by any ‘one’ person or company (decentralized ), because an exact copy of this chain is present in every personal computer that is in the blockchain network (distributed). The blockchain network is a <strong>peer to peer</strong> network where any computer in the network can connect to another computer in the network directly without having a middleman. If an individual joins the network right at this moment, he too will get an exact copy of the chain as of now. This feature makes the blockchain <strong>decentralized </strong>as well as <strong>distributed</strong>.</p><p>The blockchain system enables anyone in the network to add valid blocks of data into the chain but changing any of the data inside a block is almost impossible thanks to the use of cryptography and other protection mechanisms. That is why the blockchain is known as an <strong>append only</strong> chain.</p><h3>How does it work?</h3><p>Let us first consider the characteristics of a block in the blockchain. Each block in the blockchain contains 3 pieces of information.</p><ul><li>Hash of the block</li><li>Hash of the previous block</li><li>Data</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/921/1*m3X-zIGQWRzql7Ksr2cLCA.png" /></figure><p>The very first block in the blockchain stores only its hash and it’s data. It is also called the genesis block. The information stored as data in a block depends on the type of the blockchain (ex: blocks in Bitcoin’s blockchain store transaction information containing the sender the receiver and the amount that was sent). A single block can be uniquely identified using it’s hash. The process of storing the hash of the previous block creates the chronological order.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2OSm2JTsg_1wPURfmRA0zA.jpeg" /></figure><p>Once information is recorded inside a block it becomes very hard to change that data. There are 3 main reasons for that.</p><p>One reason is the hash of each block. Changing any of the data inside a particular block will cause the hash of that block to also change, making the rest of the blocks in that chain also invalid.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yiyQ0OrTlhWffT6uLeMD6w.jpeg" /></figure><p>The second reason is the process of calculating the proof of work. Blockchains utilize something called the proof of work that is a certain mechanism that slows down the creation of new blocks. It is a calculation that needs to be done in order to add a new block to the chain (ex: It takes 10 minutes to calculate the proof of work and add a new block to Bitcoin’s blockchain).</p><p>The third reason is the fact that the blockchain is decentralized and distributed. Due to this reason, more than 50% of the nodes in the peer to peer network must come into a general agreement on which blocks have been tempered with, thus proving to be invalid and which ones are not. If a particular block has been tampered with, that block will be rejected by the other nodes in the network.</p><p>If an individual really wanted to tamper with a block he would have to re-calculate all the hashes of the other blocks in the network, calculate the proof of work of each of the blocks and also take over more than 50% of the network. Doing all of this is highly impossible, which makes the blockchain secure.</p><p>When a new member joins the blockchain network, a full copy of the chain as of that moment will get downloaded to his personal computer. This network is open to anyone, so anyone who wants to join can join in to this peer to peer network. When a new block is created it will be sent to all the people in the network and they will come to a common agreement on if this block has been altered or not. If more than 50% of the participants in the network acknowledges that a block is valid, each node in the network will add that new block to it’s chain. If more than 50% of the network thinks that the block has been altered that block will get rejected by all the nodes.</p><p>This is the basic concept of how the blockchain works. Bitcoin’s blockchain is rather similar to this but it has more concepts like mining. I hope you were able to get a basic understanding of the blockchain by reading this article.</p><p>Have a nice day fellow geeks. Happy reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6ce5a08e03b8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[First experience of building a LSTM model with TensorFlow]]></title>
            <link>https://medium.com/good-audience/first-experience-of-building-a-lstm-model-with-tensorflow-e632bde911e1?source=rss-ddf3befa5aec------2</link>
            <guid isPermaLink="false">https://medium.com/p/e632bde911e1</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Suleka Helmini]]></dc:creator>
            <pubDate>Tue, 28 Aug 2018 20:23:24 GMT</pubDate>
            <atom:updated>2018-09-06T00:51:29.322Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*vlkAwKkZeXXwFMXJbJVbBg.jpeg" /></figure><p>Diving head first into the world of machine learning and deep learning for the first time, I was totally new to all the concepts and practices. So as a newbie, I had to read through piles and piles of theory for two weeks to get a taste of what I’m dealing with. After all the reading, I was expecting this to be a very straightforward task but little did I know the struggle that was waiting.</p><p>Building LSTM models with TensorFlow as a newbie, was rather challenging specially because of the no-so-detailed documentation of TensorFlow and the setting up process. It took me quite a while to get a good idea about how the code works and how to go about it. So, I thought of writing an article dedicated to anyone out there like me who wants a kick start into understanding some code with Tensorflow, so that you can use this to build your way up.</p><h3>Goal</h3><p>The goal of this article will be to explain a simple code that can be used to build a basic LSTM model. I will not be discussing and analyzing the results. This is merely to get you started with some coding.</p><h3>Is this for you?</h3><p>Well, If you have a basic understanding of how LSTMs and RNNs work and just want to get an introduction into some coding, this article is specially for you. But hey, if you want to learn about LSTMs and RNNs, I will put some links below this article, you can refer them, get some basic understanding and come back and follow along.</p><p>So enough chit chat, let’s get down to business.</p><h3>Setting up the environment</h3><p>I will be using python to code the LSTM in this article.The environment set up is as follows:</p><p>To save yourself the pain of setting up the environment, I recommend that you download the pycharm IDE and download Tensorflow and all the other libraries through the IDE to your project. You can follow the below steps to set up your environment.</p><ol><li>Download <a href="https://www.jetbrains.com/pycharm/">PyCharm IDE</a></li><li><a href="https://www.jetbrains.com/help/pycharm/quick-start-guide.html">Create a project</a></li><li>Download Tensorflow, NumPy, SciPy, scikit-learn and Pandas to your project. You can do this for each package by going to:</li></ol><p>files → settings → a pop up will show → go to project interpreter → click the small ‘+’ sign in the right side of the window → type the package name in the search bar → select the package → press ‘install package’ button.</p><p>4. Create a new python file inside your project</p><p>If you want to manually create the environment, listed below are the compatible versions of python and Tensorflow and all the other libraries you need to follow this article.</p><p><strong>Note</strong>: Do not use a different version of python than what is mentioned as it might not be compatible with the given Tensorflow version. When downloading scikit-learn, first download NumPy and SciPy packages.</p><ul><li>Python: 3.6.5</li><li>Tensorflow: 1.9.0</li><li>scikit-learn: 0.19.1</li><li>Pip: 10.0.1</li><li>NumPy: 1.14.3</li><li>Pandas: 0.23.0</li><li>SciPy: 1.1.0</li></ul><h3>What on earth did we download?</h3><ol><li><a href="https://www.tensorflow.org/">Tensorflow</a>: This is a machine learning framework.</li><li><a href="https://pandas.pydata.org/">Pandas</a>: This is a library that helps to analyze and do data preparation with ease.</li><li><a href="http://scikit-learn.org/stable/">scikit-learn</a>: This is a machine learning library that has various machine leaning algorithms and also perform essential helper functions.</li><li><a href="http://www.numpy.org/">NumPy</a>: This is a library that can perform high-level mathematical functions on multi-dimensional arrays and matrices.</li><li><a href="https://www.scipy.org/">SciPy</a>: This is a library used for scientific computing and technical computing.</li><li><a href="https://pypi.org/project/pip/">Pip</a>: This is a package management system for Python that can install and manage software packages written in Python.</li></ol><h3>Data?</h3><p>For this article We will be using a credit card fraud dataset that was published on kaggle. We will be using this data set to train our LSTM model to classify if a transaction is a fraudulent transaction or a normal transaction.</p><p>You can get the csv file of the data set form <a href="https://www.kaggle.com/mlg-ulb/creditcardfraud/version/2">here</a>. After you download it, put it inside your project for the ease of usage. It will be helpful if you could go through the data and <a href="https://www.kaggle.com/mlg-ulb/creditcardfraud/version/2/home">read and understand</a> what each column of data represents before you start working with it.</p><p><strong>Note</strong>: This data set will give better scores if you test it on a machine learning algorithm like the ‘Random Forest Classifier’, but we can use this data set as a starting point for building a LSTM model. Also you should keep in mind that this data set has a very high class imbalance.</p><h3>Let’s code</h3><p>First, let’s import everything that we need for this code to work.</p><pre>import tensorflow as tf<br>import pandas as pd<br>import numpy as np<br>from tensorflow.contrib import rnn<br>from sklearn.model_selection import train_test_split<br>from sklearn.metrics import f1_score, accuracy_score, recall_score, precision_score</pre><p>Now, we have to load all the data into our program. we can do this using Pandas.</p><pre>data = pd.read_csv(&#39;creditcard.csv&#39;, skiprows=[0], header=None)</pre><p>The above piece of code will load all the data from the csv file leaving out the row with the headers, so the loaded data now looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/525/1*Azl7Cb93AnaERM2mganXuQ.png" /><figcaption>First 5 rows of data</figcaption></figure><p>Next we are going to separate the loaded data into labels and features.</p><pre>features = data.iloc[:, 1:30]<br>labels = data.iloc[:, -1]</pre><p>If you went through the credit card data you would have noticed that there are 31 columns in it. The 1st line of code loads data from the 1st column (leaving out the 0th column) to the 30th column as features.</p><p>As stated by kaggle, the last column is the column that specifies if the transaction was fraudulent or not. So in the 2nd line of code, we are loading the data in the last column as labels.</p><p><strong>Note</strong>: we are leaving out the 0th column as it only provides information about the sequence of the transactions and does not add real value to our training. We can retain the sequence of the data by feeding it according to it’s occurrence order.</p><p>We now have our labels and features and we need to divide these labels and features into a training set and a testing set. We can use scikit-learn’s ‘train test split function’ for this.</p><pre>X_train,X_test,y_train,y_test = train_test_split(features, labels, test_size=0.2, shuffle=False, random_state=42)</pre><p>This function automatically divides the features and the labels into a testing and a training set. “X_train” and “X_test” are the training and testing set of the features and “y_test” and “y_train” are the training and testing set of the labels. The parameter “test_size” specifies that the test set should be 20% from the whole data set. The “shuffle” parameter specifies if the data should be shuffled before it is split into the test and the training set. Here we have set it to false in order to retain the occurrence order of the transactions. We use the “random_state” parameter in order to get the same output every time we run this function. You can read up about it more from <a href="https://stackoverflow.com/questions/42191717/python-random-state-in-splitting-dataset/42197534">here</a>.</p><p>We are now going to specify our hyperparameters</p><pre>epochs = 8<br>n_classes = 1<br>n_units = 200<br>n_features = 29<br>batch_size = 35</pre><ol><li>epochs: epochs correspond to the number of iterations we are going to run the data set through our model.</li><li>n_classes: this corresponds to the number of classes we are doing this classification for. In our case we have a binary classification, where we are assigning 0 if it is a normal transaction and assigning 1 if it is a fraudulent transaction. On a very basic level if we take one column we can represent fraudulent and normal transactions with 0 and 1 very easily. Thus, we can put the number of classes as 1.</li><li>n_units: This corresponds to the size of the hidden state of the LSTM (both c and h).</li><li>n_features: As the name specifies, this represents the number of features in the dataset.</li><li>batch_size: This refers to the size of each batch of data you are going to feed into the model. Back-propagation will be done once per batch.</li></ol><p>Next, we are going to define the place-holders for the batch of data we are going to feed into the model.</p><pre>xplaceholder= tf.placeholder(&#39;float&#39;,[None,n_features])<br>yplaceholder = tf.placeholder(&#39;float&#39;)</pre><p>The ‘xplaceholder’ placeholder holds the batch of feature-data and ‘yplaceholder’ holds the corresponding batch of label-data. The shape ‘xplaceholder’ has been specified to be (&lt;batch_size&gt;,n_features) as the ‘None’ value allows the length to be flexible to what ever the length that is fed into the placeholder but the fed matrix should have the same number of features specified in the code. As a shape has not been explicitly specified for ‘yplaceholder’ it will be a vector but the length of it will depend on what is fed into the placeholder. In our case y will be a vector of length batch_size.</p><p>We are done with most of our prep work so, we are now going to design, train and test our LSTM model</p><pre>def recurrent_neural_network_model():</pre><pre>    layer ={ &#39;weights&#39;: tf.Variable(tf.random_normal([n_units, n_classes])),&#39;bias&#39;: tf.Variable(tf.random_normal([n_classes]))}<br><br>    x = tf.split(xplaceholder, n_features, 1)<br>    print(x)<br><br>    lstm_cell = rnn.BasicLSTMCell(n_units)<br>    <br>    outputs, states = rnn.static_rnn(lstm_cell, x, dtype=tf.float32)<br>   <br>    output = tf.matmul(outputs[-1], layer[&#39;weights&#39;]) + layer[&#39;bias&#39;]<br><br>    return output<br><br>def train_neural_network():</pre><pre>    logit = recurrent_neural_network_model()<br>    logit = tf.reshape(logit, [-1])<br><br>    cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=logit, labels=yplaceholder))</pre><pre>    optimizer = tf.train.AdamOptimizer().minimize(cost)<br><br>    with tf.Session() as sess:<br><br>        tf.global_variables_initializer().run()<br>        tf.local_variables_initializer().run()<br><br>        for epoch in range(epochs):<br>            epoch_loss = 0<br><br>            i = 0<br>            for i in range(int(len(X_train) / batch_size)):<br><br>                start = i<br>                end = i + batch_size<br><br>                batch_x = np.array(X_train[start:end])<br>                batch_y = np.array(y_train[start:end])<br>                <br>                _, c = sess.run([optimizer, cost], feed_dict={xplaceholder: batch_x, yplaceholder: batch_y})</pre><pre>                epoch_loss += c</pre><pre>                i += batch_size<br><br>            print(&#39;Epoch&#39;, epoch, &#39;completed out of&#39;, epochs, &#39;loss:&#39;, epoch_loss)<br><br>        pred = tf.round(tf.nn.sigmoid(logit)).eval({xplaceholder: np.array(X_test), yplaceholder: np.array(y_test)})</pre><pre>        f1 = f1_score(np.array(y_test), pred, average=&#39;macro&#39;)</pre><pre>        accuracy=accuracy_score(np.array(y_test), pred)</pre><pre>        recall = recall_score(y_true=np.array(y_test), y_pred= pred)<br>        precision = precision_score(y_true=np.array(y_test), y_pred=pred)</pre><pre>        print(&quot;F1 Score:&quot;, f1)<br>        print(&quot;Accuracy Score:&quot;,accuracy)<br>        print(&quot;Recall:&quot;, recall)<br>        print(&quot;Precision:&quot;, precision)<br><br><br>train_neural_network()</pre><p>Let’s understand the code. I will first explain what the code inside ‘recurrent_neural_network_model()’ function is doing.</p><ol><li>In this line we are defining the shapes of the weights and the biases manually. We are assigning ‘weights’ and ‘bias’ a TensorFlow variable with a random value of the shape [rnn_size, n_classes] and [n_classes] respectively .There is a way to do this automatically as well. You can read about it from <a href="https://www.tensorflow.org/api_docs/python/tf/contrib/layers/fully_connected">here</a>.</li><li>Now we are assigning our data to ‘x’ as a sequence. To do that, we are splitting the feature batch which is a 2 dimensional Tensor along it’s vertical dimension (dimension: 1) into 29 slices. Each slice is an element of the sequence given as input to the LSTM layer.(shape of one element of the sequence will be: (&lt;batch_size&gt;,1))</li><li>Then we are creating the LSTM layer. This function instantiates variables for all gates.</li><li>‘outputs’ variable contains all the outputs of the LSTM layer for each time step and ‘state’ contains the values of the last state of both the hidden states (h and c).</li><li>Here we have taken only the last output of the LSTM layer using ‘outputs[-1]’ and we have multiplied it with the previously defined weight matrix and added the bias value to it. The resulting value is the logit value of the forward propagation.</li><li>Finally we have returned the logit value to the calling function ‘train_neural_network()’.</li></ol><p>Now let’s understand what the code inside ‘train_neural_network()’ function is doing.</p><ol><li>Here we are assigning the received logit value to a variable. A logit value is the inverse of an activation.</li><li>Then we are reshaping the matrix into a vector because when feeding it to the cost function the shape of the labels and the logits should be equal.</li><li>We have defined the cost function here. The “sigmoid_cross_entropy_with_logits” function is used because we are doing a binary classification. If this was a multi class classification we should use a cost function like ‘‘softmax_cross_entropy_with_logits’’.</li><li>Then we have passed our cost to the optimazer. We have used the “AdamOptimizer()” as it has fairly better performance.</li></ol><p>Up untill this point, all the code we discussed were not inside the Tensorflow session. From here onwards all the discussed code will be inside the TensorFlow session.</p><p>The starting point of the Tensorflow session will be where the execution starts, after calling the “train_neural_network()” to initiate the program.</p><p>5. We are initializing all the global variables that we have declared so far.</p><p>6. Then we are initializing all the local variables in the project.</p><p>7. Here we are defining a loop to run until the number of iterations (epochs) that we defined is met.</p><p>8. Resetting the epoch loss to 0 at the beginning of each epoch.</p><p>9. Defining a variable to keep track of the start and end computation when splitting the data into batches</p><p>10. Here we are again defining a loop to run until the number of batches meets it’s calculated threshold.</p><p>11. and 12. We are using ‘start’ and ‘end’ variables to keep track from where the data was split in each iteration.</p><p>13. and 14. A batch of features and labels will be assigned to ‘batch_x’ and ‘batch_y’ variables respectively in each iteration.</p><p>15. This line is where the magic happens. In this line we are telling Tensorflow to run the subgraph necessary to to compute the optimizer and the cost by feeding the values in ‘batch_x’ and ‘batch_y’ variables to the previously mentioned placeholders. As a result, the value of the optimizer and the cost will be computed and assigned to the “throw-away” variable and the “c” variable respectively.</p><p>16. The loss of the current batch will be added to the “epoch_loss” variable.</p><p>17. This line helps to iterate through each batch of data (features and labels).</p><p>18. Printing the total loss of that epoch</p><p>What we discussed upto this point is the training aspect of the model. The training will continue until the number of epochs reach the threshold value.</p><p>Next few lines of code is for testing the model.</p><p><strong>Note</strong>: Ideally we would divide the data set to 3 sets as, training, validating and testing. We will keep aside the testing set and use the validation set to see the performance of the model and change the hyper-parameters to achieve a better performance. When we come to a point where we think the model has been improved enough, then only we will be taking the testing set to see how the model performs. For this article we only took two sets as training set and testing set to keep it simple.</p><p>19. This is where we are feeding the testing data set into the model and telling Tensorflow to run the subgraph necessary to to compute the logit. Then we are passing the logit value through a sigmoid activation to get the prediction. This value is rounded off to remove the decimal places of the predicted values.</p><p>20. Here we are calculating the F1 score. F1 Score is the weighted average of Precision and Recall. You can read more about it from <a href="http://blog.exsilio.com/all/accuracy-precision-recall-f1-score-interpretation-of-performance-measures/">here</a>.</p><p>21. Then we are calculating the Accuracy score.</p><p>22. The recall is getting calculated here. Recall is the ratio of correctly predicted positive observations to all positive observations.</p><p>23. We are also calculating the precision. Precision is the ratio of correctly predicted positive observations to the total predicted positive observations.</p><p>24. to 27. Printing out all the calculated scores.</p><p>So, we are finally done with the code. You can now press the run button and watch as Tensorflow does it’s magic.</p><p>I hope this article helped you to understand the process of building a basic LSTM with Tensorflow. Please note that this model is a very basic version and a good starting point to build upon and improve. So, until next time, happy coding!</p><p>You can get the complete source code from <a href="https://github.com/suleka96/RNN-and-ML-models/blob/master/RNN_CreditCard.py">here</a>.</p><p>Listed below are some links to resources about RNNs and LSTMs</p><ul><li><a href="https://towardsdatascience.com/learn-how-recurrent-neural-networks-work-84e975feaaf7">How recurrent neural networks work</a></li><li><a href="https://www.youtube.com/watch?v=6niqTuYFZLQ">Lecture on RNNs, LSTMs and GRUs</a></li><li><a href="http://www.wildml.com/2015/09/recurrent-neural-networks-tutorial-part-1-introduction-to-rnns/">Recurrent Neural Networks Tutorial, Part 1 — Introduction to RNNs</a></li></ul><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2F7b0181%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2F7b0181%2F&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/748a06e9019942f2677bba917ef55804/href">https://medium.com/media/748a06e9019942f2677bba917ef55804/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e632bde911e1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/good-audience/first-experience-of-building-a-lstm-model-with-tensorflow-e632bde911e1">First experience of building a LSTM model with TensorFlow</a> was originally published in <a href="https://medium.com/good-audience">Good Audience</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>