{"id":150,"date":"2018-02-02T18:10:53","date_gmt":"2018-02-02T18:10:53","guid":{"rendered":"http:\/\/phylanx.stellar-group.org\/?p=150"},"modified":"2018-02-02T18:10:53","modified_gmt":"2018-02-02T18:10:53","slug":"introduction-to-phylanx-coding","status":"publish","type":"post","link":"https:\/\/phylanx.stellar-group.org\/index.php\/2018\/02\/02\/introduction-to-phylanx-coding\/","title":{"rendered":"Introduction to Phylanx Coding"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">In this post I\u2019ll go through the simple implementation of <\/span><a href=\"https:\/\/en.wikipedia.org\/wiki\/Logistic_regression\" target=\"_blank\"><span style=\"font-weight: 400;\">LRA<\/span><\/a><span style=\"font-weight: 400;\"> (Logistic Regression Algorithm) to outline the Phylanx architecture and also demonstrate how one might go about writing their own programs in Phylanx. The complete version of the code discussed in this post can be found in the project\u2019s GitHub <\/span><a href=\"https:\/\/github.com\/STEllAR-GROUP\/phylanx\" target=\"_blank\"><span style=\"font-weight: 400;\">repository<\/span><\/a><span style=\"font-weight: 400;\"> under examples\/algorithms directory and the corresponding dataset can be found <\/span><a href=\"https:\/\/archive.ics.uci.edu\/ml\/datasets\/Breast+Cancer+Wisconsin+(Diagnostic)\" target=\"_blank\"><span style=\"font-weight: 400;\">here<\/span><\/a><span style=\"font-weight: 400;\">.<\/span><!--more--><\/p>\n<h2><span style=\"font-weight: 400;\">Phylanx Architecture<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">The core of Phylanx has been implemented in <\/span><a href=\"https:\/\/github.com\/STEllAR-GROUP\/hpx\/\" target=\"_blank\"><span style=\"font-weight: 400;\">HPX<\/span><\/a><span style=\"font-weight: 400;\"> (a C++ Standard Library for Concurrency and Parallelism), and has three components (figure 1):<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\"><i><span style=\"font-weight: 400;\">PhySL<\/span><\/i><span style=\"font-weight: 400;\">&#8211; a minimal language developed for testing purposes (the syntax is subject to change).<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Phylanx AST- constructed by the Phylanx (PhySL) parser and will be used for analyses and optimizations.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Execution Tree- compiled from the AST and will run asynchronously for maximum performance.<\/span><\/li>\n<\/ol>\n<div id=\"attachment_151\" style=\"width: 445px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-151\" loading=\"lazy\" class=\"wp-image-151 \" src=\"http:\/\/phylanx.stellar-group.org\/wp-content\/uploads\/2018\/02\/phylanx_overview-300x206.png\" alt=\"\" width=\"435\" height=\"299\" srcset=\"https:\/\/phylanx.stellar-group.org\/wp-content\/uploads\/2018\/02\/phylanx_overview-300x206.png 300w, https:\/\/phylanx.stellar-group.org\/wp-content\/uploads\/2018\/02\/phylanx_overview-509x350.png 509w, https:\/\/phylanx.stellar-group.org\/wp-content\/uploads\/2018\/02\/phylanx_overview-150x103.png 150w, https:\/\/phylanx.stellar-group.org\/wp-content\/uploads\/2018\/02\/phylanx_overview.png 576w\" sizes=\"(max-width: 435px) 100vw, 435px\" \/><p id=\"caption-attachment-151\" class=\"wp-caption-text\">Figure 1. Phylanx Components<\/p><\/div>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">We recognize the fact that Phylanx will mostly be used by domain experts working on massive datasets. Therefore, to lift the burden of learning a new language, we also provide a Python interface (frontend) to Phylanx. The Python frontend enables programmers to write their code once, in a familiar language, and run it on any platform (whether a PC or large cluster) without changing the source code. At the moment this feature is available through the use of \u00a0<\/span><a href=\"https:\/\/wiki.python.org\/moin\/PythonDecorators\" target=\"_blank\"><span style=\"font-weight: 400;\">Python decorators<\/span><\/a><span style=\"font-weight: 400;\">. Any valid Python code with Phylanx constructs can be wrapped into a function which in turn is decorated with <\/span><span style=\"font-weight: 400;\">*@phyfun*<\/span><span style=\"font-weight: 400;\"> function (the name is subject to change). @phyfun will automatically generate the equivalent PhySL string, and subsequently compile, and evaluate it.<\/span><\/p>\n<h2>LRA Example<\/h2>\n<p><span style=\"font-weight: 400;\">In this example we will demonstrate how to write an LRA with the Python frontend. We will use a breast cancer diagnostic dataset as an input to our algorithm. This dataset contains 569 rows (1 for each patient) with 30 columns containing the features and one last row with the diagnosis.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">As mentioned above, any code snippet to be translated into PhySL must first be wrapped in a function. Here, we call the function\u00a0<em>lra<\/em> and decorate it with @phyfun.<\/span><\/p>\n<div class=\"codecolorer-container python default\" style=\"overflow:auto;white-space:nowrap;width:700px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/><\/div><\/td><td><div class=\"python codecolorer\"><span class=\"kw1\">import<\/span> phylanx<br \/>\n<span class=\"kw1\">from<\/span> phylanx.<span class=\"me1\">util<\/span> <span class=\"kw1\">import<\/span> *<br \/>\n<br \/>\n<span class=\"sy0\">@<\/span>phyfun<br \/>\n<span class=\"kw1\">def<\/span> lra<span class=\"br0\">&#40;<\/span>file_name<span class=\"sy0\">,<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; xlo1<span class=\"sy0\">,<\/span> xhi1<span class=\"sy0\">,<\/span> ylo1<span class=\"sy0\">,<\/span> yhi1<span class=\"sy0\">,<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; xlo2<span class=\"sy0\">,<\/span> xhi2<span class=\"sy0\">,<\/span> ylo2<span class=\"sy0\">,<\/span> yhi2<span class=\"sy0\">,<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; alpha<span class=\"sy0\">,<\/span> iterations<span class=\"sy0\">,<\/span> enable_output<span class=\"br0\">&#41;<\/span>:<br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"co1\"># &lt;code to be transformed into PhySL&gt;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">return<\/span> weights<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><span style=\"font-weight: 400;\">Now we can write our program as the body of the\u00a0<em>lra<\/em> function and Phylanx will automatically translate the code into PhySL.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">As the first step we need to read data which is stored in CSV format. Phylanx provides a primitive (function) <em>read_csv_file<\/em> to read this type of file. We read the file and store the feature values in matrix\u00a0<em>x<\/em> and diagnoses in vector <em>y<\/em>:<\/span><\/p>\n<div class=\"codecolorer-container python default\" style=\"overflow:auto;white-space:nowrap;width:700px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/><\/div><\/td><td><div class=\"python codecolorer\">data <span class=\"sy0\">=<\/span> file_read_csv<span class=\"br0\">&#40;<\/span>file_name<span class=\"br0\">&#41;<\/span><br \/>\nx <span class=\"sy0\">=<\/span> data<span class=\"br0\">&#91;<\/span>xlo1:xhi1<span class=\"sy0\">,<\/span>ylo1:yhi1<span class=\"br0\">&#93;<\/span><br \/>\ny <span class=\"sy0\">=<\/span> data<span class=\"br0\">&#91;<\/span>xlo2:xhi2<span class=\"sy0\">,<\/span>ylo2:yhi2<span class=\"br0\">&#93;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><span style=\"font-weight: 400;\">Next we\u2019ll initialize the prediction, gradient and error vectors to zero using the <em>constant<\/em>\u00a0function:<\/span><\/p>\n<div class=\"codecolorer-container python default\" style=\"overflow:auto;white-space:nowrap;width:700px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/><\/div><\/td><td><div class=\"python codecolorer\">pred <span class=\"sy0\">=<\/span> constant<span class=\"br0\">&#40;<\/span><span class=\"nu0\">0.0<\/span><span class=\"sy0\">,<\/span> shape<span class=\"br0\">&#40;<\/span>x<span class=\"sy0\">,<\/span> <span class=\"nu0\">0<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span> \/\/ prediction<br \/>\nerror <span class=\"sy0\">=<\/span> constant<span class=\"br0\">&#40;<\/span><span class=\"nu0\">0.0<\/span><span class=\"sy0\">,<\/span> shape<span class=\"br0\">&#40;<\/span>x<span class=\"sy0\">,<\/span> <span class=\"nu0\">0<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><br \/>\ngradient <span class=\"sy0\">=<\/span> constant<span class=\"br0\">&#40;<\/span><span class=\"nu0\">0.0<\/span><span class=\"sy0\">,<\/span> shape<span class=\"br0\">&#40;<\/span>x<span class=\"sy0\">,<\/span> <span class=\"nu0\">1<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><span style=\"font-weight: 400;\">We also store the transpose of the\u00a0<em>x<\/em> as we\u2019ll use it many times later within the while loop:<\/span><\/p>\n<div class=\"codecolorer-container python default\" style=\"overflow:auto;white-space:nowrap;width:700px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"python codecolorer\">transx <span class=\"sy0\">=<\/span> transpose<span class=\"br0\">&#40;<\/span>x<span class=\"br0\">&#41;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><span style=\"font-weight: 400;\">Once everything is initialized and loaded, we can start the iterations to find the (optimal) weight function. Within the loop, we calculate the prediction (using Phylanx functions <em>exp<\/em> and <em>dot<\/em>), error, gradient (using <em>dot<\/em>) and weight. One should note that division, summation, negation are all also Phylanx primitives.<\/span><\/p>\n<div class=\"codecolorer-container python default\" style=\"overflow:auto;white-space:nowrap;width:700px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/><\/div><\/td><td><div class=\"python codecolorer\"><span class=\"kw1\">while<\/span> step <span class=\"sy0\">&lt;<\/span> iterations:<br \/>\n&nbsp; &nbsp; pred <span class=\"sy0\">=<\/span> <span class=\"nu0\">1.0<\/span> \/ <span class=\"br0\">&#40;<\/span><span class=\"nu0\">1.0<\/span> + exp<span class=\"br0\">&#40;<\/span>-dot<span class=\"br0\">&#40;<\/span>x<span class=\"sy0\">,<\/span> weights<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; error <span class=\"sy0\">=<\/span> pred - y<br \/>\n&nbsp; &nbsp; gradient <span class=\"sy0\">=<\/span> dot<span class=\"br0\">&#40;<\/span>transx<span class=\"sy0\">,<\/span> error<span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; weights <span class=\"sy0\">=<\/span> weights - <span class=\"br0\">&#40;<\/span>alpha * gradient<span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; step +<span class=\"sy0\">=<\/span> <span class=\"nu0\">1<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><span style=\"font-weight: 400;\">Now, when the <em>lra<\/em>\u00a0function is called, Phylanx will automatically generate the PhySL and evaluate it. We can print the result using <em>phy_print<\/em> function.<\/span><\/p>\n<div class=\"codecolorer-container python default\" style=\"overflow:auto;white-space:nowrap;width:700px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/><\/div><\/td><td><div class=\"python codecolorer\">res <span class=\"sy0\">=<\/span> lra<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;breast_cancer.csv&quot;<\/span><span class=\"sy0\">,<\/span> <span class=\"nu0\">0<\/span><span class=\"sy0\">,<\/span><span class=\"nu0\">569<\/span><span class=\"sy0\">,<\/span><span class=\"nu0\">0<\/span><span class=\"sy0\">,<\/span><span class=\"nu0\">30<\/span><span class=\"sy0\">,<\/span> <span class=\"nu0\">0<\/span><span class=\"sy0\">,<\/span><span class=\"nu0\">569<\/span><span class=\"sy0\">,<\/span><span class=\"nu0\">30<\/span><span class=\"sy0\">,<\/span><span class=\"nu0\">31<\/span><span class=\"sy0\">,<\/span> <span class=\"nu0\">1e-5<\/span><span class=\"sy0\">,<\/span><span class=\"nu0\">750<\/span><span class=\"sy0\">,<\/span><span class=\"nu0\">0<\/span><span class=\"br0\">&#41;<\/span><br \/>\nphy_print<span class=\"br0\">&#40;<\/span>res<span class=\"br0\">&#41;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><span style=\"font-weight: 400;\">We have also done some preliminary performance analysis of our toolchain.\u00a0 We found that the performance achieved by Phylanx is comparable to that of <\/span><a href=\"https:\/\/github.com\/numpy\/numpy\" target=\"_blank\"><span style=\"font-weight: 400;\">numpy<\/span><\/a><span style=\"font-weight: 400;\">:<\/span><\/p>\n<p><img loading=\"lazy\" class=\" wp-image-136 aligncenter\" src=\"http:\/\/phylanx.stellar-group.org\/wp-content\/uploads\/2018\/01\/scaling-300x117.png\" alt=\"\" width=\"698\" height=\"272\" srcset=\"https:\/\/phylanx.stellar-group.org\/wp-content\/uploads\/2018\/01\/scaling-300x117.png 300w, https:\/\/phylanx.stellar-group.org\/wp-content\/uploads\/2018\/01\/scaling-150x58.png 150w, https:\/\/phylanx.stellar-group.org\/wp-content\/uploads\/2018\/01\/scaling.png 624w\" sizes=\"(max-width: 698px) 100vw, 698px\" \/><\/p>\n<p>We are confident that as we continue to expand and improve Phylanx we will be able to pare down our execution time and improve upon the scalability we currently observe.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post I\u2019ll go through the simple implementation of LRA (Logistic Regression Algorithm) to outline the Phylanx architecture and also demonstrate how one might go about writing their own programs in Phylanx. The complete version of the code discussed in this post can be found in the project\u2019s GitHub\u2026 <a class=\"continue-reading-link\" href=\"https:\/\/phylanx.stellar-group.org\/index.php\/2018\/02\/02\/introduction-to-phylanx-coding\/\">Continue reading<\/a><\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[20,21,18,17,5,19,13],"_links":{"self":[{"href":"https:\/\/phylanx.stellar-group.org\/index.php\/wp-json\/wp\/v2\/posts\/150"}],"collection":[{"href":"https:\/\/phylanx.stellar-group.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/phylanx.stellar-group.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/phylanx.stellar-group.org\/index.php\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/phylanx.stellar-group.org\/index.php\/wp-json\/wp\/v2\/comments?post=150"}],"version-history":[{"count":53,"href":"https:\/\/phylanx.stellar-group.org\/index.php\/wp-json\/wp\/v2\/posts\/150\/revisions"}],"predecessor-version":[{"id":204,"href":"https:\/\/phylanx.stellar-group.org\/index.php\/wp-json\/wp\/v2\/posts\/150\/revisions\/204"}],"wp:attachment":[{"href":"https:\/\/phylanx.stellar-group.org\/index.php\/wp-json\/wp\/v2\/media?parent=150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phylanx.stellar-group.org\/index.php\/wp-json\/wp\/v2\/categories?post=150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phylanx.stellar-group.org\/index.php\/wp-json\/wp\/v2\/tags?post=150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}