<?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 Chris Mahoney on Medium]]></title>
        <description><![CDATA[Stories by Chris Mahoney on Medium]]></description>
        <link>https://medium.com/@chrimaho?source=rss-56d03114dd5a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ClLRGGe_YbSAh0gNiHjPXA.jpeg</url>
            <title>Stories by Chris Mahoney on Medium</title>
            <link>https://medium.com/@chrimaho?source=rss-56d03114dd5a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 06:13:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@chrimaho/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[Print Python Lists in Columns]]></title>
            <link>https://chrimaho.medium.com/print-python-lists-in-columns-bb6376cc8dbf?source=rss-56d03114dd5a------2</link>
            <guid isPermaLink="false">https://medium.com/p/bb6376cc8dbf</guid>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Chris Mahoney]]></dc:creator>
            <pubDate>Mon, 24 Feb 2025 11:00:22 GMT</pubDate>
            <atom:updated>2025-02-24T20:09:44.031Z</atom:updated>
            <content:encoded><![CDATA[<h4>Better utilise the space in your console when printing Python Iterables</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*KbzSYwIYU-AYZ8hr" /><figcaption>Photo by <a href="https://unsplash.com/@rubaitulazad?utm_source=medium&amp;utm_medium=referral">Rubaitul Azad</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Introduction</h3><p>When we use the programming language Python, it is not as easy to output our lists in a column format. This is because Python does not have a built-in way to output lists in a column format. The two most common ways to output your lists in Python are the print() and pprint() functions. However, these functions do not provide a way to output your lists in a convenient and easy to read column format.</p><p>In this article, we will discuss how to output your Python lists as columns, and how to use the list_columns() function in the toolbox_python package to do this. This function is very useful when you have a long list of strings that you want to output in a more readable format. It is very easy to use, and provides a lot of customisation options to suit your needs.</p><h3>How does it look in R?</h3><p>When we use the programming language <a href="https://www.r-project.org/">R</a>, it is very easy to output our lists in a column format. This is very useful when you have a long list of strings that you want to output in a more readable format. Here is an example of how to output a list in R:</p><pre>&gt; my_list &lt;- c(&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;date&#39;, &#39;elderberry&#39;, &#39;fig&#39;, &#39;grape&#39;)<br>&gt; str(my_list)<br>[1] &quot;apple&quot;      &quot;banana&quot;     &quot;cherry&quot;     &quot;date&quot;       &quot;elderberry&quot;<br>[6] &quot;fig&quot;        &quot;grape&quot;</pre><p>This output is very easy to read, and is very useful when you have a long list of strings. For developers coming from R to Python, they may miss this formatting.</p><h3>Existing Processes in Python</h3><p>When we use the programming language <a href="https://www.python.org/">Python</a>, it is not as easy to output our lists in a column format. This is because Python does not have a built-in way to output lists in a column format.</p><h4>The `print()` function</h4><p>The <a href="https://docs.python.org/3/library/functions.html#print">print()</a> function is the most basic way to output your lists in Python. It is very simple to use and is the default way to output your lists. However, it does not provide a way to output your lists in a column format.</p><pre>&gt;&gt;&gt; my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;date&#39;, &#39;elderberry&#39;, &#39;fig&#39;, &#39;grape&#39;]<br>&gt;&gt;&gt; print(my_list)<br>[&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;date&#39;, &#39;elderberry&#39;, &#39;fig&#39;, &#39;grape&#39;]</pre><p>This output is fine, and it works well. However, if you have a long list, it can be difficult to read. For example, let’s say you have a list of 100 items:</p><pre>&gt;&gt;&gt; my_list = list(range(100))<br>&gt;&gt;&gt; print(my_list)<br>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 98, 99]</pre><p>(To save space in this article, the ... is added by me; but Python will print the whole thing).</p><p>This is useful, but does not allow you to print your data in a column format.</p><h4>The pprint() function</h4><p>The <a href="https://docs.python.org/3/library/pprint.html#pprint.pprint">pprint()</a> function is a built-in Python module that provides a way to pretty-print your lists. It is very useful when you have a long list and you want to output it in a more readable format.</p><pre>&gt;&gt;&gt; from pprint import pprint<br>&gt;&gt;&gt; my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;date&#39;, &#39;elderberry&#39;, &#39;fig&#39;, &#39;grape&#39;]<br>&gt;&gt;&gt; pprint(my_list)<br>[&#39;apple&#39;,<br> &#39;banana&#39;,<br> &#39;cherry&#39;,<br> &#39;date&#39;,<br> &#39;elderberry&#39;,<br> &#39;fig&#39;,<br> &#39;grape&#39;]</pre><p>This output is much more readable than the print() function. However, if you have a long list, it can still be difficult to read. For example, let’s say you have a list of 100 items:</p><pre>&gt;&gt;&gt; my_list = list(range(100))<br>&gt;&gt;&gt; pprint(my_list)<br>[0,<br> 1,<br> 2,<br> 3,<br> 4,<br> 5,<br> 6,<br> 7,<br> 8,<br> 9,<br> 10,<br> ...<br> 98,<br> 99]</pre><p>(To save space in this article, the ... is added by me; but Python will print the whole thing).</p><p>It does print your list in a single column, but this is not very useful when you have a long list, as there is lots of wasted white space.</p><h3>New `list_columns()` function</h3><p>There is a very convenient function in the toolbox_python package called <a href="https://data-science-extensions.com/toolbox-python/latest/code/output/#toolbox_python.output.list_columns">list_columns()</a> that allows you to print your lists as columns. This is very useful when you have a list of strings that you want to print in a column format.</p><p><a href="https://data-science-extensions.com/toolbox-python/latest/">Python Toolbox</a></p><p>You can install the package using pip:</p><pre>pip install toolbox_python</pre><p>And then import it into your module:</p><pre>&gt;&gt;&gt; from toolbox_python.output import list_columns</pre><p>Here is an example of how to use the list_columns() function:</p><pre>&gt;&gt;&gt; my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;date&#39;, &#39;elderberry&#39;, &#39;fig&#39;, &#39;grape&#39;]<br>&gt;&gt;&gt; print(list_columns(my_list, print_output=False))<br>apple         cherry        elderberry    grape         <br>banana        date          fig                         </pre><p>You’ll notice here that:</p><ol><li>The default output is in 4 columns. It’d be nice if it were optimised to print the number of columns based on the width of your screen; but it is not that advance.</li><li>The print_output=False argument is used to return the output as a string, rather than printing it to the console. This is useful if you want to use the output in a different way. Here, we have turned <em>off</em> the print_output argument, because we are parse’ing the output of the function to a new print() operation.</li><li>The default number of spaces between each columns is 4 spaces.</li></ol><p>The power of this function really begins to shine when we are dealing with large lists. For example, let’s say you have a list of 100 items:</p><pre>&gt;&gt;&gt; my_list = list(range(100))<br>&gt;&gt;&gt; print(list_columns(my_list, print_output=False))<br>0     25    50    75    <br>1     26    51    76    <br>2     27    52    77    <br>3     28    53    78    <br>4     29    54    79    <br>5     30    55    80    <br>6     31    56    81    <br>7     32    57    82    <br>8     33    58    83    <br>9     34    59    84    <br>10    35    60    85    <br>11    36    61    86    <br>12    37    62    87    <br>13    38    63    88    <br>14    39    64    89    <br>15    40    65    90    <br>16    41    66    91    <br>17    42    67    92    <br>18    43    68    93    <br>19    44    69    94    <br>20    45    70    95    <br>21    46    71    96    <br>22    47    72    97    <br>23    48    73    98    <br>24    49    74    99    </pre><p>This output is a lot easier to read, and is much more useful than the print() or pprint() functions.</p><p>There are a couple of different ways in which we can customise the output of the list_columns() function.</p><p>1. We can change the number of columns utilised:</p><pre>&gt;&gt;&gt; print(list_columns(list(range(100)), print_output=False, cols_wide=8))<br>0     13    26    39    52    65    78    91    <br>1     14    27    40    53    66    79    92    <br>2     15    28    41    54    67    80    93    <br>3     16    29    42    55    68    81    94    <br>4     17    30    43    56    69    82    95    <br>5     18    31    44    57    70    83    96    <br>6     19    32    45    58    71    84    97    <br>7     20    33    46    59    72    85    98    <br>8     21    34    47    60    73    86    99    <br>9     22    35    48    61    74    87          <br>10    23    36    49    62    75    88          <br>11    24    37    50    63    76    89          <br>12    25    38    51    64    77    90</pre><p>2. We can change the gap between columns:</p><pre>&gt;&gt;&gt; print(list_columns(list(range(100)), print_output=False, cols_wide=8, gap=8))<br>0         13        26        39        52        65        78        91        <br>1         14        27        40        53        66        79        92        <br>2         15        28        41        54        67        80        93        <br>3         16        29        42        55        68        81        94        <br>4         17        30        43        56        69        82        95        <br>5         18        31        44        57        70        83        96        <br>6         19        32        45        58        71        84        97        <br>7         20        33        46        59        72        85        98        <br>8         21        34        47        60        73        86        99        <br>9         22        35        48        61        74        87                  <br>10        23        36        49        62        75        88                  <br>11        24        37        50        63        76        89                  <br>12        25        38        51        64        77        90                  </pre><p>3. We can print <em>rowwise</em> instead of the default <em>colwise</em>:</p><pre>&gt;&gt;&gt; print(list_columns(list(range(100)), print_output=False, cols_wide=8, columnwise=False))<br>0     1     2     3     4     5     6     7     <br>8     9     10    11    12    13    14    15    <br>16    17    18    19    20    21    22    23    <br>24    25    26    27    28    29    30    31    <br>32    33    34    35    36    37    38    39    <br>40    41    42    43    44    45    46    47    <br>48    49    50    51    52    53    54    55    <br>56    57    58    59    60    61    62    63    <br>64    65    66    67    68    69    70    71    <br>72    73    74    75    76    77    78    79    <br>80    81    82    83    84    85    86    87    <br>88    89    90    91    92    93    94    95    <br>96    97    98    99    </pre><p>4. We are not just limited to list objects; we can use set, tuple, and generators too:</p><pre>&gt;&gt;&gt; print(<br>...     &quot;list:&quot;,<br>...     list_columns(list(range(50)), print_output=False, cols_wide=8),<br>...     sep=&quot;\n&quot;,<br>... )<br>list:<br>0     7     14    21    28    35    42    49    <br>1     8     15    22    29    36    43          <br>2     9     16    23    30    37    44          <br>3     10    17    24    31    38    45          <br>4     11    18    25    32    39    46          <br>5     12    19    26    33    40    47          <br>6     13    20    27    34    41    48<br><br>&gt;&gt;&gt; print(<br>...     &quot;tuple:&quot;,<br>...     list_columns(tuple(range(50)), print_output=False, cols_wide=8),<br>...     sep=&quot;\n&quot;,<br>... )<br>tuple:<br>0     7     14    21    28    35    42    49    <br>1     8     15    22    29    36    43          <br>2     9     16    23    30    37    44          <br>3     10    17    24    31    38    45          <br>4     11    18    25    32    39    46          <br>5     12    19    26    33    40    47          <br>6     13    20    27    34    41    48<br><br>&gt;&gt;&gt; print(<br>...     &quot;set:&quot;,<br>...     list_columns(set(range(50)), print_output=False, cols_wide=8),<br>...     sep=&quot;\n&quot;,<br>... )<br>set:<br>0     7     14    21    28    35    42    49    <br>1     8     15    22    29    36    43          <br>2     9     16    23    30    37    44          <br>3     10    17    24    31    38    45          <br>4     11    18    25    32    39    46          <br>5     12    19    26    33    40    47          <br>6     13    20    27    34    41    48<br><br>&gt;&gt;&gt; print(<br>...     &quot;generator:&quot;,<br>...     list_columns((val for val in range(50)), print_output=False, cols_wide=8),<br>...     sep=&quot;\n&quot;,<br>... )<br>generator:<br>0     7     14    21    28    35    42    49    <br>1     8     15    22    29    36    43          <br>2     9     16    23    30    37    44          <br>3     10    17    24    31    38    45          <br>4     11    18    25    32    39    46          <br>5     12    19    26    33    40    47          <br>6     13    20    27    34    41    48</pre><h3>Conclusion</h3><p>This list_columns() function is a very useful tool to have in your Python toolbox. It allows you to output your lists in a column format, which is very useful when you have a long list of strings that you want to output in a more readable format. It is very easy to use, and provides a lot of customisation options to suit your needs, including the number of columns, the gap between columns, and whether to print rowwise or colwise. Be sure to use it in your next Python project!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bb6376cc8dbf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Rotary Youth Leadership Awards]]></title>
            <link>https://chrimaho.medium.com/rotary-youth-leadership-awards-56a7918488f0?source=rss-56d03114dd5a------2</link>
            <guid isPermaLink="false">https://medium.com/p/56a7918488f0</guid>
            <category><![CDATA[leadership-in-action]]></category>
            <category><![CDATA[learning-cycle]]></category>
            <category><![CDATA[experiential-learning]]></category>
            <category><![CDATA[rotary]]></category>
            <category><![CDATA[leadership-development]]></category>
            <dc:creator><![CDATA[Chris Mahoney]]></dc:creator>
            <pubDate>Fri, 27 Jan 2023 11:55:55 GMT</pubDate>
            <atom:updated>2023-01-27T12:25:07.712Z</atom:updated>
            <content:encoded><![CDATA[<h4>A Reflectively Experiential Approach to Leadership Development</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/611/1*14vYa5VTsnPdKy15VAqRgQ.png" /><figcaption><strong>Cover Image</strong>: RYLA and ERLA, as related to Kolb (<a href="#2b05">1984</a>)<br>(Image by Author, <a href="https://viewer.diagrams.net/?tags=%7B%7D&amp;highlight=0000ff&amp;edit=_blank&amp;layers=1&amp;nav=1&amp;title=RYLA%20and%20ERLA#R7VvbcqM4EP0aP84WIEOcR8fJzkxVtjaVTG0mjzK0DTMYeYSI7f36FVjiItkJO4agXPLgoEbc%2BvTldAtGaLbafqZ4Hf5FAohHjhVsR%2Bhy5DgT%2B5z%2F5oLdXuBKwZJGwV5kV4K76F8QQktIsyiAtDGRERKzaN0U%2BiRJwGcNGaaUbJrTFiRuXnWNl6AJ7nwc69L7KGCheCzXquRfIFqG8sq2JfassJwsBGmIA7KpidDVCM0oIWy%2FtdrOIM51J%2FWyP%2B7PI3vLG6OQsDYH%2FLrfAv7G7j%2Ffumc3l%2F883H%2FZZp%2FEWR5xnIkH%2FhYCF1zdXk%2F5v2vANImSJd%2Bc7fw435OlEOS6Zfzn9iGfVTwc20mNUZIlAeQXtUfoYhNGDO7W2M%2F3briJcFnIVrHYjakvMLf4KGWU%2FIQZiQnlkoQkfMfFI1AWcUCmcbRMuJiR%2FBwLkjBxpDPmY%2FEcfC5sjyrILtXOzRXIChjd8SnyAImUMFUHifGmAt6T8IY10NG5EGJhbMvy3BUefENA8j%2FgmWjwaOqGgNurGBLKQrIkCY6vKulFE5BqzjXJNVkIfwBjO6FOnDHSBInrj%2B6%2B5xj94crhQ33f5VYAuB%2FtxGgRxbEEc%2BSgAMNk4Wsw8z2eP4H5okQxf6SnMeQaIBn14QnVOYexphBjFj02z38IN3HoDYkSVtnIWLERNFGgZ5gugYmjFPTL2%2Fh9gzg3wiDeILCu1QR2PH5ZYGW%2Be%2FfI8vxeaPqJee6JFnBSSLb1lPkB1JF53pBAORpOt4TfcX4DDyTjCb2gNwHQNIzWfDDdYBqkp9EZBRV7jm1wBFupyRfF3zHCU6c2VjfUBnlKdLN0alPSnzq1UdNbd%2BiM37wbPese9qn%2B0ZKjqKls79%2B9pTLH%2FYDWnvTDUibPsJS%2BoT1789A%2Bm%2FzctiZwNmT2032wquU7zHBHVK9nMrujIh0pHuAeyGSTA4lMdZTuEpn34RJeW5c4H9IldKCmPotI8rodwlGpnTOwQ9jICIcom1blIG9Z1TpYR5pWsI1YrdfFR2Wri29XB%2BUDIxpdskvZe9l8hJKMFfvz2lGSKaV4V5u2ziek7a%2FjNtvdfGN%2Fxm67MmZUKb%2FbgC1tuWbJlV0baMttudWgjQXbjPqmMoouApxlrlG0ZRfoSKI8McCpjBOpAa7vzrDOW2Yk8SkwGDlezDVyMad8a5lvXW3XQCNIuEI7YzWCuxwkNB0QGFW%2FJVsZjMDoRe50zp8e%2B0xXeA4FrFmG4yjF3bLJvvWuEEfXa9kT7E%2Fx%2BnLnLSxi8Avv1VT%2F9zwF%2Bvi6lO6py0yDs3V9RXF6RN%2F72LLiT9qBzvtSsG1aOSSvbwxbaFLIih88zRZkAWQ8hbRbNyNObdGeZhZmLCO%2BmypZ5hbD27bSCkwxi7deW8j01%2F9a3WlmYVhP7a33IWQJYHhHG%2BlJpI8C0IR1nnIReygeh%2FTI3H8Z%2Fm5qbqQHuBcp%2Fd5NnYfM7mSbUYZAEkzzbwZUU%2BhmnabtuyPIOmxap74W5D3TxzyyUPP8io%2BjnKjnhijSK2oZK177Yq72FvLgYUNvPtdzGz%2BpJXRfDNQ4Xbxzsp829VkPobqGweRwJ68DVLjrNLPlgU7pIVRUv%2BgOlRavYaUhXuebFH5lEYXga8KALgrl1rUqv665xnOIb0gaFU6ELueEMbLK1f4TmB%2BKkNoErIlIAAucFfi%2FCChIlm4lKLYGyvglMWnxrY7RmFAi%2Brro8pM96ejFY2d8ZhRK4wO5o%2Fe3woeLXK6nRi5d%2FS8aucYt2p5Ge0kHoHhIjVzOsD7RoudoNCa9RC7XUyNXXyjxYfUV8J4kV59So6v%2FAA%3D%3D">source</a>)</figcaption></figure><h3>Introduction</h3><p>In January this year, I was part of the facilitation team on the annual <a href="https://www.d9685ryla.org.au/">Rotary Youth Leadership Awards</a> (RYLA) course in North Sydney. It is a week-long experiential leadership program, aimed at empowering the next generation of leaders to achieve great things.</p><p>The program is designed around a reflectively experiential approach to leadership, employing the abstraction of ‘ERLA’ to assist the participants through their journey. ERLA is an abbreviation for: Experience, Reflection, Learning, Action. This concept is continually reinforced throughout the course of the week.</p><p>It is also steeped in academia, particularly in its liken to David Kolb’s work (<a href="#2b05">1984</a>), which conceptualises learning as a four-phase continuous cycle: Concrete Experience, Reflective Observation, Abstract Conceptualisation and Active Experimentation (<a href="#e4b2"><em>Figure 1</em></a>). This learning process, in other forms, has been referred to the ‘Plan-Do-Check-Act’ model, or the ‘Experience-Reflection-Conceptualise-Test’ model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/555/0*V8HYWS6mjttcZSJR" /><figcaption><strong>Figure 1</strong>: Kolb’s continuous learning cycle<br>(<a href="#2b05">Kolb 1984</a>)</figcaption></figure><p>The ERLA concept is continually recycled for participants throughout the RYLA experience, and the week itself also acts as a standalone experience for future reflection and development.</p><p>Two of the key objectives of RYLA are:</p><ol><li>To empower and equip the next generations of leaders to be the best version of themselves that they can be; and</li><li>To provide a challenging, engaging, and safe environment for participants to try, test, apply and enable their own learning and development.</li></ol><p>Many researchers have attempted to synthesise the intricacies and nuances of leadership (eg: <a href="#851e">Avolio et al. 2009</a>; <a href="#bc2d">Busse 2014</a>; <a href="#ca34">Jackson &amp; Parry 2011</a>; <a href="#0bad">Northouse 2015</a>). However, the resulting array of theories &amp; conceptualisations are quite frankly diverse, convoluted, and in some instances inconsistent (as discussed by <a href="#75f9">Alveeson &amp; Spicer 2012</a>). What the discourse does agree on is the fact that leadership is inherently difficult and complex. Yet it is the capstone that is necessary in any successful business, and it is the fuel behind a highly effective and productive team.</p><p>The environment created by RYLA, particularly resulting from the ERLA methodology, is one which can facilitate this learning for each individual, and can create a successful and beneficial leadership learning experience.</p><h3>Experience</h3><p>The <em>Experience</em> aspect of the ERLA cycle is in line with the Concrete Experience phase of Kolb’s model. Each day of RYLA has a particular activity in which the participants have an opportunity to apply their learning. While these activities can be quite varied in their objectives and methods, they all require active participation and engagement between RYLArians. The activities interweave real-life scenarios (for example, a <a href="https://rotarydistrict9685.org.au/sitepage/more-about-muna">Model United Nations</a> type of negotiations), and also fictitious scenarios (for example, doing the Spiders Web activity). This provides an environment for the participants to continually challenge themselves and to experience different aspects of leadership.</p><p>Expanding on Kolb’s (<a href="#2b05">1984</a>) work, Freed et al. (<a href="#0c65">2010</a>) discuss the benefits of having a practical approach to leadership learning, while addressing both leader development and leadership development. Freed et al. articulate how practical leadership experiences allow “the opportunity for individual development of ‘competency’ in a variety of leadership areas, rather than focus on a pre-determined set of classroom requirements” (<a href="#0c65">p. 30</a>).</p><p>Freed et al.’s comments are congruent with Van Velsor et al.’s (<a href="#6e53">2010</a>) assessment that while leaders learn primarily through their experiences, not all experiences are equally developmental. Van Velsor and Co. also discuss that any learning experience (for example a training program, a leadership course, a university assessment, a personal relationship) can be richer and more developmental when it incorporates three key elements alongside three core contexts. <a href="#1dd9"><em>Figure 2</em></a> shows how these three elements (assessment, challenge, and support) contribute to enhanced developmental experiences, while the three contexts (variety of experiences, leader development, ability to learn) encapsulate the leadership development process.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/549/0*sQZWCGjN7SyLySOu" /><figcaption><strong>Figure 2</strong>: Van Velsor et al.’s conceptualisation of Development Experiences and the Leadership Context<br>(<a href="#6e53">Van Velsor et al. 2010</a>)</figcaption></figure><p>Therefore, having experience and practical activities is a vital aspect of the overall RYLA opportunity, for it allows individuals an opportunity to learn and develop their own leadership competencies.</p><h3>Reflection</h3><p><em>Reflection</em> (the Reflective Observation phase) is a key aspect of the RYLA experience. Participants engage daily (sometimes twice a day) in scheduled reflection sessions with 4 to 5 of their fellow RYLArians, and a Facilitator. These sessions allow participants to discuss what they have experienced, listen to the experiences of their teammates, and to verbalise what they have done (or will do) differently. It’s also an opportunity for individuals to link their experience on-RYLA with their past experiences before-RYLA, and to form an idea for how to apply these skills after-RYLA.</p><p>Participants are continually drawing cognitive interrelationships between their experiences on the RYLA week and the ‘real world’, outside of RYLA. Some feedback received from the participants after RYLA’s conclusion indicates that this reflective engagement was an incredibly beneficial aspects of the experience, and is what enabled them to ‘tie’ everything together. As a result, individuals are able to take so much more learning and development from their RYLA experience, as a direct result of reflection.</p><p>Many specific definitions of reflection have been developed in academia (eg: <a href="#9a1d">Dewey 1933</a>; <a href="#cf6a">Schön 1983</a>; <a href="#2b05">Kolb 1984</a>; <a href="#a500">Boud et al. 1985</a>; <a href="#36b1">Mezirow 2000</a>; <a href="#edbc">Yorks &amp; Marsick 2000</a>). While there is some discussion around the lack of clarity and consensus of what constitutes ‘reflection’ (<a href="#5efc">Procee 2006</a>), there are some consistent tenets between the discourse.</p><p>For example, O’Neil (<a href="#bc03">1999</a>) identified four key types of reflectors (as shown in <a href="#3a40"><em>Figure 3</em></a>): the tacit reflector (incidental reflection), the scientific reflector (content reflection), the experimental reflector (content and process reflection), and the critical reflector (content, process, and premise reflection).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/0*irYO-llAFeHiCo7m" /><figcaption><strong>Figure 3</strong>: O’Neil shows the four types of Reflectors<br>(<a href="#bc03">O’Neil 1999</a>)</figcaption></figure><p>However, regardless of the type of reflection occurring, the benefits of reflection are clear. As discussed by Rausch (<a href="#a48c">2007</a>) “reflection allowed these participants to move beyond descriptive accounts and to analyse, interrelate, and synthesize their various experiences in relation to their learning” (p. 103). Resultantly, the reflective aspects of leadership development have become a pivot aspect of the RYLA journey, and one which has added an undefinable amount of benefit for so many past participants.</p><h3>Learning</h3><p>The <em>Learning</em> aspect of RYLA (the Abstract Conceptualisation phase) is an incredibly dynamic and versatile aspect for each participant, because it is an entirely individual process which everyone does differently. Throughout the RYLA week, individuals have and opportunity to test and check themselves in the scheduled daily activities, which enables them to continually experiment and learn for themselves. Furthermore, the reflection sessions is also an opportunity for individuals to discuss their actions with their piers, and to learn about themselves from the actions of their teammates, which is also an enabler for deeper learning throughout the week.</p><blockquote>RYLA is truly jam-packed with learning opportunities, ripe for any participant to learn from any aspect of the week.</blockquote><p>Kolb (<a href="#2b05">1984</a>) discusses the Abstract Conceptualisation phase as a pivotal aspect of the learning cycle, for it is when an individual is able to figuratively grasp the concept, and begin to comprehend its nuances. Richards (<a href="#2ea8">2018</a>) shows that the Abstract Conceptualisation phase does not need to be fully completed for an individual to move to the next phase in the learning cycle (Active Experimentation), for in fact comprehension and conceptualisation may continue to be formed over many phases of experimentation. Conversely, Jarvis (<a href="#f7fb">1987</a>) points out the role of ‘prior knowledge’ in learning (which is notably absent from Kolb’s original model), and how learning is able to occur in an open-ended ‘ecosystem’ of continual experiences, observations, lessons, and experiments.</p><p>It is based on this premise that RYLA is able to provide such varied and prolific learning opportunities for participants, as it is an enabler for individuals to perceive learning in their own terms, and comprehend the nuances in their own manner.</p><h3>Action</h3><p>The <em>Action</em> phase (Active Experimentation) is core to the entire process, and there are countless opportunities provided to participants throughout the week to implement their learning, test their understanding, try their skills, and expand their comfort zone. For example, the activities and the reflections conducted on the Monday of RYLA allows the participants to experiment and try new things on the Tuesday’s activities. Then, after further reflection, the Wednesday’s activities provide another opportunity for further experimentation and implementation.</p><p>Furthermore, if considered as a whole, the entire RYLA week itself is a learning opportunity which individuals are able to bring back to their everyday life, and implement new actions and try new skills and techniques outside of RYLA. This continuous cycle of learning repeats itself indefinitely, and allows individuals to grow and evolve throughout their RYLA journey.</p><p>Active Experimentation is appearing as the final phase in Kolb’s (<a href="#2b05">1984</a>) model; but it need not be. Richards (<a href="#2ea8">2018</a>) discusses that in its wider usage, Kolb’s cycle can be viewed as having different starting points and trajectories, particularly as knowledge is formed through the different assimilative/accommodative and convergent/divergent quadrants (refer to <a href="#4833"><em>Figure 4</em></a>).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/723/0*cFWvTEMkTEFFNx2k" /><figcaption><strong>Figure 4</strong>: Richards’ assessment of the learning cycle through four different quadrants<br>(<a href="#2ea8">Richards 2018</a>)</figcaption></figure><p>Richards takes this even further by discussing that learning is a linked and continually interactive cycle of ‘actions’ (and thus also ‘reflective practice’). This can be perceived as spontaneous events, planned interventions, or specific constructions of meaning.</p><p>The conceptual formation of knowledge can manifest differently in various cycles of the feedback loop:</p><ul><li>Either as an alternation between the initial trial-and-error experimentation and subsequent repetition/reinforcement, or</li><li>In an intentional ‘opening up’ vs. ‘closing down’ of an individuals desire to learn, resulting from either a positive or negative self-fulfilling prophecy (the expected result of a given, expected situation).</li></ul><p>Experimentation is a vital aspect of RYLA, and is one which is offered to participants many times, over many days, and in many different forms. The level of engagement and experimentation from participants is an entirely personal choice, and their journey through the RYLA experience can be discussed and unpacked throughout subsequent reflection sessions, thus making the experience so much more impactful.</p><h3>Conclusion</h3><p>As demonstrated, the RYLA program uses the ERLA acronym ‘Experience, Reflection, Learning, Action’ for facilitating learning, growth and empowerment of its participants. ERLA is also grounded in academia. It is based primarily on Kolb’s (<a href="#2b05">1984</a>) learning cycle of ‘Concrete Experience, Reflective Observation, Abstract Conceptualisation, Active Experimentation’.</p><p>The comprehension and experimentation aspects of this methodology can be perceived as both a singular isolated learning event, and as an ecosystem of continual experiences/lessons, which forms the larger reflectively experiential journey for the RYLA participants. The reflection sessions that are conducted throughout the RYLA week are a vital key in the entire journey, for it allows individuals to verbalise their thoughts and experiences, both for their personal benefit and for the benefit of their fellow RYLArians.</p><p>Most importantly, the continual activities throughout the week provide many opportunities for the participants to experiment in their own way. There are opportunities to take leadership roles, or supportive roles, or even consoling/mentoring/outgoing/accommodating/asserting roles. RYLA facilitates all these opportunities for participants. As a result, the RYLA program is able to provide a positive and beneficial impact to all those who participate, and continues to make a substantially beneficial impact to our society.</p><p>Moreover, Alveeson and Spicer (<a href="#75f9">2012</a>) provide a critical performative approach to leadership, which recognises that leadership is contextual, and achieves better performance when seen as an infrequent, temporal, situation-specific, and dynamic process, rather than a permanent state in the leader-follower relationship.</p><p>Anyone who wishes to become a leader cannot learn it from reading academic discourse alone, nor can the skills be learnt overnight. Leadership is something that is learnt, developed, tested, grown, critiqued, reflected upon, built upon, and continues to evolve over time, over many years of application.</p><p>Cramp (<a href="#8252">2016</a>) suggests that an international study visit will greatly grow ones’ leadership experience, while Dalglish &amp; Evans (<a href="#c8a9">2007</a>) suggests that one should first begin with attaining qualifications and experience before embarking on a leadership development journey, and even Jackson &amp; Parry (<a href="#ca34">2011</a>) indicates that one does not stop learning leadership, for any and all leadership experience can add to the successfulness of a persons’ leadership.</p><p>As a practitioner of leadership, the burden of responsibility lies on the individual to develop their own views of what leadership means to them, and how that applies to their own lives. RYLA provides an ideal environment for this learning, and a catalyst for ongoing learning, as facilitated by ERLA, complemented by reflection, and enhanced by peers.</p><h3>References</h3><p>Alveeson &amp; Spicer 2012, ‘Critical leadership studies: The case for critical performativity’, <em>Human Relations</em>, vol. 65, no. 3, pp. 367–90.</p><p>Avolio, Walumbwa &amp; Weber 2009, ‘Leadership: Current theories, research and future directions’, <em>Annual Review of Psychology</em>, vol. 60, pp. 421–49.</p><p>Boud, Keogh &amp; Walker 1985, <em>Reflection: Turning Experience into Learning</em>, Kogan Page, London.</p><p>Busse 2014, ‘Comprehensive leadership review: Literature, theories and research’, <em>Advances in Management</em>, vol. 7, no. 5, pp. 52–66.</p><p>Cramp 2016, ‘Enhancing school leadership through an international study visit’, <em>Professional Development in Education</em>, pp. 1–14, ISSN: 1941–5257.</p><p>Dalglish &amp; Evans 2007, ‘Julie Hammer’, <em>Leadership in the Australian Context</em>, 1st edn., Tilde University Press, Prahran, VIC, Australia.</p><p>Dewey 1933, <em>How We Think: A Restatement of the Relation of Reflective Thinking to the Educative Process</em>, D. C. Health, Boston</p><p>Freed, Covrig &amp; Baumgartner 2010, ‘Learning while leading: the Andrews University leadership program’, <em>The Journal of Applied Christian Leadership</em>, vol. 4, no. 1, pp. 27–56, ProQuest Central database, ISSN: 19333978.</p><p>Jackson, B &amp; Parry K 2011, <em>A Very Short, Fairly Interesting and Reasonably Cheap Book About Studying Leadership</em>, 2nd edn., Sage, London.</p><p>Jarvis 1987, <em>Adult Learning in the Social Context</em>, Croom Helm, London, England.</p><p>Kolb 1984, <em>Experimental learning: Experience as the source of learning and development</em>, Prentice Hall, Englewood Cliffs, NJ.</p><p>Mezirow 2000, <em>Learning as Transformation: Critical Perspectives on a Theory in Progress</em>, Wiley, New York.</p><p>Northouse 2015, <em>Leadership: Theory and Practice</em>, SAGE Publications, Thousand Oaks, California.</p><p>O’Neil 1999, <em>The Role of the Learning Advisor in Action Learning</em>, Doctoral dissertation, Teachers College, Columbia University, New York, retrieved from ProQuest Digital Dissertations (AAT 9939533).</p><p>Procee 2006, ‘Reflection in education: A Kantian epistemology’, <em>Educational Theory</em>, vol. 56, no. 3, pp. 237–53.</p><p>Rausch 2007, <em>Demonstrating Experiential Learning at the Graduate Level Using Portfolio Development and Reflection</em>, Doctoral dissertation, Andrews University, Berrien Springs, MI, retrieved from ProQuest Digital Dissertations (AAT 3289968).</p><p>Richards 2018, ‘Human lifecycle development and the experiential learning foundations of an integrated lifelong education framework’, <em>Journal of Adult and Continuing Education</em>, vol. 24, no. 2, pp. 250–71.</p><p>Schön 1983, <em>The Reflective Practitioner</em>, Basic Books, New York.</p><p>Van Velsor, McCauley &amp; Ruderman 2010, <em>The Center for Creative Leadership Handbook of Leadership Development</em>, Wiley Imprint, Jossey-Bass, San Francisco.</p><p>Yorks, &amp; Marsick 2000, ‘Organizational learning and transformation’, in J. Mezirow &amp; Associates (Eds.), <em>Learning as transformation: Critical perspectives on a theory in progress</em>, pp. 253–81, Jossey-Bass, San Francisco.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FyAYen5c0YEoJhdt10r_PQ.jpeg" /><figcaption><strong>Reflective Discussions</strong><br>(<a href="https://www.shutterstock.com/image-photo/unposed-group-creative-business-people-open-474417268">Image</a> by <a href="https://www.shutterstock.com/g/julief514">AYA Images</a> under license to Author, on <a href="https://www.shutterstock.com/">ShutterStock</a>)</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=56a7918488f0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Use GitHub Webhooks, Docker, and Python for Automatic End-to-End Deployments]]></title>
            <link>https://medium.com/better-programming/github-webhooks-docker-and-python-for-automatic-app-deployments-a7f18d23d5b7?source=rss-56d03114dd5a------2</link>
            <guid isPermaLink="false">https://medium.com/p/a7f18d23d5b7</guid>
            <category><![CDATA[github]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Chris Mahoney]]></dc:creator>
            <pubDate>Fri, 29 Apr 2022 15:06:03 GMT</pubDate>
            <atom:updated>2022-04-29T21:58:14.631Z</atom:updated>
            <content:encoded><![CDATA[<h4>An in-depth look into the process for automatically keeping your App up to date</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_b5eEcBkRTLFSHyqKns6yA.png" /><figcaption><strong>Repo’s to Apps via Webhooks and API’s</strong><br>(Image by Author, source <a href="https://viewer.diagrams.net/?tags=%7B%7D&amp;highlight=0000ff&amp;edit=_blank&amp;layers=1&amp;nav=1&amp;title=diagrams.drawio#Uhttps%3A%2F%2Fraw.githubusercontent.com%2Fchrimaho%2Fupdate-from-git%2Fmain%2Fblog%2Fdiagrams%2Fdiagrams.drawio">here</a> and <a href="https://github.com/chrimaho/update-from-git/blob/main/blog/diagrams/cover-image.png">here</a>)</figcaption></figure><h3>Table of Contents</h3><p>1. <a href="#c73a">Motivation</a><br><a href="#241b"><strong>Section One: Context and Overview</strong></a><strong><br></strong>2. <a href="#b214">Context</a><br>3. <a href="#0fdc">Other Methods</a><br> — 3.1. <a href="#637e">Manual Process</a><br> — — 3.1.1. <a href="#c1cb">Advantages</a><br> — — 3.1.2. <a href="#8ff4">Disadvantages</a><br> — 3.2 <a href="#1337">Docker Process</a><br> — — 3.2.1. <a href="#5ddb">Advantages</a><br> — — 3.2.2. <a href="#ba70">Disadvantages</a><br><a href="#2aa5"><strong>Section Two: Details About the App</strong></a><strong><br></strong>4. <a href="#9220">The Automated Webhook is Method</a><br>5. <a href="#ffad">Explanation of the Key Files</a><br> — 5.1. <a href="#4aeb">File: /requirements.txt</a><br> — 5.2. <a href="#9d9d">File: /docker/uvicorn.Dockerfile</a><br> — 5.3. <a href="#def7">File: /docker/docker-compose.yml</a><br> — — 5.3.1. <a href="#4881">The environment section</a><br> — — 5.3.2. <a href="#a56b">The volumes section</a><br> — — 5.3.3. <a href="#6781">The ports section</a><br> — 5.4. <a href="#3e61">File: /src/api/main.py</a><br> — — 5.4.1. <a href="#ce60">Import Libraries</a><br> — — 5.4.2. <a href="#7b66">Compile Variables</a><br> — — 5.4.3. <a href="#d1d7">Set Landing Page</a><br> — — 5.4.4. <a href="#3b74">Instantiate App</a><br> — — 5.4.5. <a href="#177d">Set Custom Functions</a><br> — — 5.4.6. <a href="#c939">Set Custom Classes</a><br> — — 5.4.7. <a href="#55bd">Define Endpoint: Landing Page</a><br> — — 5.4.8. <a href="#6cef">Define Endpoint: Health Check</a><br> — — 5.4.9. <a href="#cd20">Define Endpoint: Main Endpoint</a><br> — 5.5. <a href="#39e4">File: /templates/landing_page.html</a><br><a href="#2088"><strong>Section Three: How to Use the App</strong></a><strong><br></strong>6. <a href="#65ea">How to Use It</a><br> — 6.1. <a href="#ecb0">Create Server (using AWS)</a><br> — 6.1. <a href="#a07e">Add docker-compose.yml file</a><br> — 6.1. <a href="#0b25">Launch Docker on Server</a><br> — 6.1. <a href="#a5e6">Add Webhook on Git (using GitHub)</a><br> — 6.1. <a href="#faf0">Test it</a><br><a href="#7fac"><strong>Section Four: More Info</strong></a><strong><br></strong>7. <a href="#1f33">What’s next</a><strong><br></strong>8. <a href="#cc2c">Where to Find More Information</a><br>9. <a href="#76bf">The full file for: /src/api/main.py</a><br>10. <a href="#22a6">Contact</a></p><h3>1. Motivation</h3><p>If you’re anything like me, you’re a curious creature. So, when I started learning about what <a href="https://en.wikipedia.org/wiki/Webhook">Webhooks</a> are, and how to use them, it felt like I was pulling at a loose thread on my tee shirt. The more I pulled, the more the world of Webhooks unraveled, the more I learnt, the more I understood, the more I unveiled about the mysterious world of API’s and Webhooks and Automation, and the more I wanted to learn more!</p><p>So, the motivation here is to create a streamlined and seamless way for doing deployments from GitHub to production servers. I feel like it is possible to write a simple Python App to do this. So let’s find out how!</p><p>This article will be split in to four sections, each outlining a different aspect of this process. <a href="#a099">Section Two</a> is incredibly detailed, but I’ve tried to include as much description and screen shots as necessary, in order to make it easier to comprehend what is happening here. <a href="#2088">Section Three</a> contains hands-on instructions for how to use the App. Enjoy!</p><h3>Section One: Context and Overview</h3><h3>2. Context</h3><p>I’ve been coding for a few years now, and I have quite a few Repo’s on <a href="https://github.com/">GitHub</a> (and <a href="https://gitlab.com/">GitLab</a>, and a few others). And I see this button:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_AOnXQYX4aL5B1HkRur6ng.png" /><figcaption><strong>GitHub &gt; *</strong><strong>Repo* &gt; Settings &gt; Webhooks &gt; Add webhook</strong><br>(Image by Author)</figcaption></figure><p>So, I read a bit about it on GitHub’s <a href="https://docs.github.com/en/developers/webhooks-and-events/webhooks/about-webhooks">Webhooks Guide</a> page, along with a few Blogs on how to set it up (like <a href="https://medium.com/@samarthgvashist2000/setting-up-a-github-webhook-ccd895e7b85c">this one</a> and <a href="https://betterprogramming.pub/how-to-automatically-deploy-from-github-to-server-using-webhook-79f837dcc4f4">this one</a>). The way I understand it is like this:</p><blockquote>When you set up Webhooks on a Repo, and you commit then push to the Repo, GitHub will automatically send a HTTP POST request to the URL you specify.</blockquote><p>The payload for that request will contain the <em>metadata </em>for the event from GitHub, but will not contain the content from Repo itself. Therefore, the system that is <em>receiving </em>the Webhook request will then need to take some sort of action to pull or clone the latest data from the Repo.</p><p>This Webhook methodology is not exclusive to <a href="https://docs.github.com/en/developers/webhooks-and-events/webhooks/about-webhooks">GitHub</a>, but is also available on <a href="https://docs.gitlab.com/ee/user/project/integrations/webhooks.html">GitLab</a> and <a href="https://support.atlassian.com/bitbucket-cloud/docs/manage-webhooks/">BitBucket</a>, and many other Git platforms.</p><h3>3. Other Methods</h3><p>First, it’s important to understand that there are many, many (<em>many!</em>) different ways to deploy apps in production environments. Let’s look at two other common methods:</p><h4>3.1. Manual Process</h4><p>The manual intervention process would look something like this:</p><ol><li>To do git commit then git push on Local PC to push to the Upstream Repo.</li><li>When ready to deploy, to manually ssh in to the target server.</li><li>To do git clone (if for the first time) or git pull (for subsequent updates) to fetch the latest code from the Repo to the Server.</li></ol><p>Obviously there are lot of steps, and a lot of complexity in this manual process. However, I’ve kept it to just the high-level deployment steps, so that you can get the idea of the process.</p><p><strong>3.1.1. Advantages:<br></strong> — Easy to maintain<br> — Developer has full control<br> — No requirement/dependency on external tools or apps (eg. Docker)</p><p><strong>3.1.2. Disadvantages:<br></strong> — Requires time, effort, concentration by the Developer during deployment<br> — One wrong move, and mistakes can easily be made; and the Developer may not even realise it until much later<br> — Any dependencies (eg. Other apps, other libraries, etc), will need to be manually executed in addition to this Git process.</p><h4>3.2. Docker Process</h4><p>The <a href="https://www.docker.com/">Docker</a> app streamlines this process a lot, and makes the life of the Developer a lot easier. The high-level process is:</p><ol><li>Run docker build and docker run on Local PC to ensure the image/container is working correctly (this can be streamlined running docker compose up)</li><li>Publish the container using docker login then docker push to upload the container to the container repository (this can be <a href="https://aws.amazon.com/ecr/">AWS ECR</a> or <a href="https://azure.microsoft.com/en-us/services/container-registry/">Azure ACR</a> or <a href="https://hub.docker.com/">Docker Hub</a>, or any other container hosting platform)</li><li>Then to ssh on to the Server, and to do docker pull, followed by docker build and docker run (or just simply docker compose up, assuming the compose file is correctly set up).</li></ol><p>While it seems like there are more steps in this process, actually the process is a lot simpler. Because it’s not deploying <em>through</em> a Git Repository, but rather a Container Repository. Meaning to say that the container can be fully built and working on your Local PC, and when deploying to the Server it will work the exact same way.</p><p><strong>3.2.1. Advantages:</strong><br> — Docker will run on any system, exactly the same way; so it is very reliable<br> — All dependencies are already handled through Docker, therefore less effort/concentration/focus needed from the Developer</p><p><strong>3.2.2. Disadvantages:</strong><br> — The configuration setup can sometimes be a little confusing<br> — If the Developer is not familiar with Docker, the setup and debugging can take a little while to finish</p><h3>Section Two: Details About the App</h3><h3>4. The Automated Webhook Method</h3><p>It is very easy to Automate this process. The solution here is to create a very very simple Python App (using <a href="https://fastapi.tiangolo.com/">FastAPI</a>) which will receive the HTTP POST request from Git, then call a Python script to then pull the latest update from the Git upstream Repo. It is very similar to the abovementioned manual process; wherein the developer will pull the latest code from the Repo. However, the difference here is that we will use the power of Python to automatically pull the latest code for us.</p><p>The end-to-end process will look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EUyA65T0u8eEuTpzBn9uqQ.png" /><figcaption><strong>End-to-End Process Flow<br></strong>(image by Author, source <a href="https://viewer.diagrams.net/?tags=%7B%7D&amp;highlight=0000ff&amp;edit=_blank&amp;layers=1&amp;nav=1&amp;page-id=I7LHdRjH9WNjt-zTn0nW&amp;title=diagrams.drawio#Uhttps%3A%2F%2Fraw.githubusercontent.com%2Fchrimaho%2Fupdate-from-git%2Fmain%2Fblog%2Fdiagrams%2Fdiagrams.drawio">here</a> and <a href="https://github.com/chrimaho/update-from-git/blob/main/blog/diagrams/end-to-end.png">here</a>)</figcaption></figure><p>To break it down step-by-step, this is what is happening:</p><ol><li>The developer will work on the App on their local PC</li><li>The developer will push updates to the upstream Git repo (in this example, we use GitHub; but any other hosting platform can also work),</li><li>The Git Repo will then trigger the Webhook process, and will send a HTTP POST request to a specified URL,</li><li>Behind that URL is an IP address, which should be hosted on a cloud computing platform (here, we use AWS, but any other cloud computing platform, like Azure, will also work),</li><li>The Depending on if the Git repo uses HTTP or HTTPS, it will hit the Server on port 80 (for HTTP) or port 443 (for HTTPS),</li><li>The server will then have it’s port 80 and port 443 exposed to the public, to allow for network traffic to pass through.</li><li>Hosted on the server is a Docker container and the container will map the external ports 80 and 443 to an internal port 8880, which can then be consumed by the internal App,</li><li>Mounted within the Docker container is a FastAPI App which will listen to the port 8880 and the Endpoint which is defined by the developer,</li><li>Once the HTTP POST message hits the endpoint URL, then the FastAPI App will call a Python script,</li><li>That Python script will then trigger a git clone (or git pull) process to fetch the latest code from the upstream repo,</li><li>Python will then save that info to a directory on Docker (in this instance, we’ll save it to the /app/repo directory, but this is configurable and you can save it anywhere),</li><li>Docker will then use a process called <a href="https://docs.docker.com/storage/volumes/">volume mapping</a> to persist the data from that directory on the container to a directory outside the container (which means that as soon as the /app/repo directory on the container is updated, the /repo directory outside the container will immediately also be updated),</li><li>Once the /repo directory is updated on the server-level, then it is possible to execute any other App as necessary.</li></ol><p>To the astute eye, you will note here that it is actually possible to cluster multiple Docker containers together, so that one will run the App itself, instead of relying on the Server-level to run it. Yes, that’s right, and it’s also very common to do it like this. However, for simplicity, I have kept the process here to simply show the Webhook process from Git to the Server. Using this as a basis, it is then possible to add much more complexity on top.</p><h3>5. Explanation of the Key Files</h3><p>There are a number of key files that are used in this process. The next few sections will explain these files in detail.</p><h4>5.1. File: /requirements.txt</h4><p>The requirements are super simple. We only need three packages:</p><ol><li><a href="https://fastapi.tiangolo.com/">fastapi</a>: For actually building the API endpoints</li><li><a href="https://gitpython.readthedocs.io/en/stable/index.html">gitpython</a>: For clone&#39;ing/ pull&#39;ing from the upstream Repo</li><li><a href="https://pypi.org/project/python-decouple/">python-decouple</a>: For handling default environment variables</li></ol><p>There will be some other libraries which will be imported at run-time; however, these will be part of the Python built-in’s, and not needed to be added to the requirements.txt file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/11de6af2487ab754a57f384cc5fe0b48/href">https://medium.com/media/11de6af2487ab754a57f384cc5fe0b48/href</a></iframe><h4>5.2. File: /docker/uvicorn.Dockerfile</h4><p>Within the world of <a href="https://www.docker.com/">Docker</a>, the <a href="https://docs.docker.com/engine/reference/builder/">Dockerfile</a> is a ‘set of instructions’ which should be executed on a particular container, in order to set it up for it’s operation. While there are many, many (<em>many</em>!), different options and configurations available, here we have kept things quite simple.</p><p>The specific steps are:</p><ol><li>Use the base image as <a href="https://hub.docker.com/r/tiangolo/uvicorn-gunicorn-fastapi">tiangolo/uvicorn-gunicorn-fastapi</a>, which contains all the source infrastructure needed for establishing and running a server for successfully running FastAPI.</li><li>Copy the requirements.txt file from the local environment on to the container.</li><li>Running three different PIP statements to:<br>1. Upgrade <a href="https://pip.pypa.io/en/stable/">pip</a>, in case it is outdated,<br>2. Install the packages from the requirements.txt file,<br>3. Upgrade the <a href="https://www.uvicorn.org/">uvicorn</a> package, in case it is outdated.</li><li>Copy all the required files from the src and the templates directories from local environment on to the container.</li><li>Set the working directory in to the /app directory within the container.</li><li>Set the command line, which will be executed once the container is finished being built, and is ready to be run.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9444bef4166b691a12c0128f1ea5b606/href">https://medium.com/media/9444bef4166b691a12c0128f1ea5b606/href</a></iframe><p>The final line (the CMD one) is quite interesting. Let me point out a few important bits of information:</p><ul><li>It is going to execute the uvicorn application, which is effectively a server for handling the FastAPI processes and commands</li><li>It will execute the application which is called <a href="https://github.com/chrimaho/update-from-git/blob/main/src/api/main.py#L65">app</a>, found within the <a href="https://github.com/chrimaho/update-from-git/blob/main/src/api/main.py">main.py</a> module.</li><li>The main.py module is actually within a sub-directory on the container, to be found within the /src/api directory.</li><li>It is hosted to monitor the IP address: 0.0.0.0. Which is effectively saying ‘monitor the localhost’. This is because the Server IP address is used by the external systems (via the URL), but the signal is within the server itself, it will be found on the localhost IP. So, therefore, Uvicorn only needs to monitor the localhost within the server itself.</li><li>The system will monitor port 8880, which is why it’s important to map the external ports to the internal ports (which will be covered in the next section, about the docker-compose.yml file).</li><li>The root-path of the application is on the current folder (denoted with the &quot;.&quot; syntax).</li><li>The output from the application will be printed to the terminal to use some pretty colours; which is helpful for reading the log files and messages at a later time.</li></ul><h4>5.3. File: /docker/docker-compose.yml</h4><p>After the Dockerfile, the <a href="https://docs.docker.com/compose/compose-file/">docker-compose.yml</a> file is the logical next step, which will take the process further again. It can be used to create clusters of containers, each of which can be set to work in unison and depending off one another. While this is an amazing functionality, it is not needed for our purposes here. In this instance, we will only require one container, which will be built using the Dockerfile as defined above. The reason why we use the docker-compose.yml methodology is because it is convenient; it allows for easy addition of environment variables and volume mappings.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/03b63a92b4dcafd64aad09706e704a1a/href">https://medium.com/media/03b63a92b4dcafd64aad09706e704a1a/href</a></iframe><p>Note here that this is a SAMPLE file for the docker-compose.yml. The Git URL from which this is pulling data is not actually an application (merely a collection of code snippets). So, this is merely showing the process how of to set up the application.</p><p><strong>5.3.1. The </strong><strong>environment section:</strong></p><p>There are 9 values that could/should be included in the environment section, including:</p><ol><li>GIT_URL (<em>Mandatory</em>): The URL from which the Repo will be cloned.</li><li>API_ENDPOINT (<em>Optional</em>, with default /api/webhook): The Endpoint which will be called by the Webhook.</li><li>REPO_DIR (<em>Mandatory</em>): The DIR to which the Repo will be cloned.</li><li>VERSION (<em>Optional</em>, with default 0.0.1): The version number for the app.</li><li>TITLE (<em>Optional</em>, with default Update from Git): The title of the app.</li><li>DESCRIPTION (<em>Optional</em>, with default Automated process for pulling from Git repo upon webhook call.): The description of the app.</li><li>CONTACT_NAME (<em>Optional</em>, with default None): The name of the person to contact about the app.</li><li>CONTACT_URL (<em>Optional</em>, with default None): The website for the contact person.</li><li>CONTACT_EMAIL (<em>Optional</em>, with default None): The email for the contact person.</li></ol><p>See <a href="https://github.com/chrimaho/update-from-git#explanation-on-the-environment-variables">here</a> for more details.</p><p><strong>5.3.2. The </strong><strong>volumes section:</strong></p><p>In order to utilise storage processes between the parent container (ie. the Server) and the Docker container, the <a href="https://docs.docker.com/storage/volumes/">volumes</a> section is the mechanism designed to achieve persistence between the two environments. Effectively, the value on the left-hand-side of the colon (:) is the directory location on the <em>parent </em>container, and the value on the right-hand-side is the directory location on the <em>Docker </em>container itself. By doing so, any changes whatsoever to either directory will instantaneously update the other.</p><p>Default values:</p><pre>- ../repo:/app/repo</pre><p><strong>5.3.3. The </strong><strong>ports section:</strong></p><p>The <a href="https://docs.docker.com/compose/networking/">port mappings</a> are important to ensure that the external Ports can be correctly mapped to the internal processes within Docker. Since port 80 is for HTTP, and port 443 is for HTTPS processes, it is reasonable to expect any upstream Git process to utilise either one of these. And since the FastAPI app is listening to port 8880, it is logical to map these two external ports (80 and 443) to the internal 8880 port. The mapping for 8880:8880 is likely redundant on the Server, but included here because it is necessary for doing testing on Local PC.</p><p>For ease of reference, the value on the left-hand-side of the colon (:) is the <em>external</em> port, and the value on the right-hand-side is the <em>internal</em> port.</p><p>Default values:</p><pre>- 8880:8880<br>- 443:8880<br>- 80:8880</pre><h4>5.4. File: /src/api/main.py</h4><p>Due to the size of the file, I have broken it in to chunks and explained each section individually. The full file is copied to the end of this article (see <a href="#76bf">here</a>).</p><p><strong>5.4.1. Import Libraries</strong></p><p>First thing to do is to import the required libraries. The way that this is structured is to not import the full code base from each library; but instead to only import the required functions/classes from each.</p><p>More specifically:</p><ul><li><a href="https://docs.python.org/3/library/sys.html#sys.exc_info">exec_info</a> for handling errors and returning them back through the API</li><li><a href="https://github.com/henriquebastos/python-decouple/#usage">config</a> for handling the default environment variables</li><li><a href="https://fastapi.tiangolo.com/tutorial/first-steps/#first-steps">FastAPI</a> and <a href="https://fastapi.tiangolo.com/tutorial/query-params/#query-parameters">Query</a> for running the API endpoints</li><li><a href="https://fastapi.tiangolo.com/tutorial/response-model/?h=base+model#response-model">BaseModel</a> as a pedantic (or, ‘pydantic’ 😂) method for handling objects through the API endpoints</li><li><a href="https://fastapi.tiangolo.com/advanced/custom-response/?h=htmlresponse#plaintextresponse">PlainTextResponse</a>, <a href="https://fastapi.tiangolo.com/advanced/custom-response/?h=htmlresponse#jsonresponse">JSONResponse</a>, and <a href="https://fastapi.tiangolo.com/advanced/custom-response/?h=htmlresponse#htmlresponse">HTMLResponse</a> are the specific objects which will be returned by the FastAPI endpoints</li><li><a href="https://gitpython.readthedocs.io/en/stable/tutorial.html#meet-the-repo-type">Repo</a> for actually calling the <a href="https://git-scm.com/docs/git-clone">git clone</a> and <a href="https://git-scm.com/docs/git-pull">git pull</a> methods</li><li><a href="https://docs.python.org/3/library/os.path.html#os.path.exists">exists</a> for checking whether the /repo folder exists or not</li><li>And finally, <a href="https://docs.python.org/3/library/shutil.html#shutil.rmtree">rmtree</a> for physically deleting the data in the /repo before executing the git processes.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/06d813276ccc0839d774cf307eca8651/href">https://medium.com/media/06d813276ccc0839d774cf307eca8651/href</a></iframe><p><strong>5.4.2. Compile Variables</strong></p><p>As mentioned <a href="#4881">above</a>, there are 9 environment variables used in this process. Two of which are <em>Mandatory</em>, and the rest are <em>Optional</em>. The way that these mandatory and optional processes are handled are set up using the config() function of the python-decouple package. Effectively, this will allow the developer to define a default value for the Environment variable, and to cast whatever value is parsed in to the desired data type.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/47ea9adff575c5d46022a70aae7b1f83/href">https://medium.com/media/47ea9adff575c5d46022a70aae7b1f83/href</a></iframe><p><strong>5.4.3. Set Landing Page</strong></p><p>The landing page is incredibly simple. It’s just a basic HTML table, which allows the Developer (and indeed any other user) to see the key environment variables directly on the Landing page for the app. The template which is loaded will be further outlined below in <a href="#55bd">this</a><strong> </strong>paragraph. The process to physically import it in to the Python environment is actually quite simple, as shown below. Moreover, the str.format() method is utilised to parse the specific Environment variables directly in to the HTML string. Computationally speaking, this is very efficient.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c34c2f892c535ab11760798b715424bd/href">https://medium.com/media/c34c2f892c535ab11760798b715424bd/href</a></iframe><p><strong>5.4.4. Instantiate App</strong></p><p>Once all packages are loaded and constants defined, it’s time to instantiate the App. The FastAPI package has a suite of very intuitive, very simple, and very useful processes. Most of the default values are perfectly usable, which eliminates a lot of the complexity (and, indeed, ambiguity) on behalf of the developer. In just a few lines of code, the entire FastAPI application can be defined and is ready to use. As shown below</p><p>The only aspect which needs to be elaborated upon is the openapi_tags section. This is an aesthetic choice, so that when the /swagger page is loaded, it is very easy to see which API endpoints are grouped together in to which sections. This is another entirely optional (yet highly advised) configuration to use.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e4a2dfb9c1861d30d9b7e23d7d6a912c/href">https://medium.com/media/e4a2dfb9c1861d30d9b7e23d7d6a912c/href</a></iframe><p><strong>5.4.5. Set Custom Functions</strong></p><p>While I’ve reserved a section for crafting some custom functions, actually there’s only really one which I’ve added in here. It’s a helper function which is used for removing all the files from within a directory. This is necessary because sometimes the gitpython package does not like to clone a repo in to a directory when there is already other data and files existing in the same location. So, this function will readily remove the outdated files.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8e8783d8939d4a7c68d3b1be124d043a/href">https://medium.com/media/8e8783d8939d4a7c68d3b1be124d043a/href</a></iframe><p><strong>5.4.6. Set Custom Classes</strong></p><p>The FastAPI package relies on the helpful processes contained within the BaseModel class of the pydantic package. By using this class, it is incredibly easy to define request and response objects to be used by the API. More details can be found <a href="https://fastapi.tiangolo.com/tutorial/body/">here</a>. For our purposes here, I’ve only defined three custom classes, and the structure of them is very simple and straightforward.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cc701d5e2fc2bfaeb505863b5913088d/href">https://medium.com/media/cc701d5e2fc2bfaeb505863b5913088d/href</a></iframe><p><strong>5.4.7. Define Endpoint: Landing Page</strong></p><p>Now that everything is set up and ready to go, it’s time to define the actual endpoints. Here, we will use three specific endpoints; the first of which is the Landing page. Each of these endpoints will utilise FastAPI’s <a href="https://peps.python.org/pep-0318/">decorator</a> methodology, specifically the @app.get() or the @app.post() methods.</p><p>For this first Endpoint, we know that this is the landing page because we have defined path=&quot;/&quot;, which basically means that this will be the root directory for this website. Everything else will be built on top of this root directory. It is a very simple endpoint which returns a <a href="https://fastapi.tiangolo.com/advanced/custom-response/?h=htmlre#return-an-htmlresponse-directly">HTMLResponse</a> class, which contains the LANDING_PAGE which we have defined earlier. Also, the schema for this endpoint is turned off, so that when the /swagger page is inspected, the reader is not seeing unnecessary and redundant schema definitions.</p><p>The code looks like this 👇</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e6d68033adddc121a4e56534920ff3a3/href">https://medium.com/media/e6d68033adddc121a4e56534920ff3a3/href</a></iframe><p>When it’s running, the Swagger page looks like this 👇</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6Tl8-iE1JKD4UHtRxt_Jaw.png" /><figcaption><strong>The Swagger page for the Landing Page Endpoint<br></strong>(image by Author)</figcaption></figure><p>When it’s running, the URL looks like this 👇</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iE3ww8ibHjBIxFxkZG6fDw.png" /><figcaption><strong>The URL page for the Landing Page Endpoint<br></strong>(image by Author)</figcaption></figure><p><strong>5.4.8. Define Endpoint: Health Check</strong></p><p>The second API Endpoint to declare is a health check. This is important so that the user is able to know if the entire application is up and running, and is returning a healthy response. It is best practice to set up a single endpoint for this purpose.</p><p>The structure of this endpoint is just as simple as the Landing Page. Perhaps even more simple, because it returns a <a href="https://fastapi.tiangolo.com/advanced/custom-response/?h=htmlre#plaintextresponse">PlainTextResponse</a> instead, which is only going to return a short string of text.</p><p>The code looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e8f0b14155c2c03560dc83c9505a3a6/href">https://medium.com/media/4e8f0b14155c2c03560dc83c9505a3a6/href</a></iframe><p>When it’s running, the Swagger page looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CNL1Tqc-gIlApoDsLnY2ng.png" /><figcaption><strong>The Swagger page for the Health Check Endpoint<br></strong>(image by Author)</figcaption></figure><p>When it’s running, the URL looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/822/1*hqOVc7l0_wqQ_wQQW-gpoQ.png" /><figcaption><strong>The URL page for the Health Check Endpoint<br></strong>(image by Author)</figcaption></figure><p><strong>5.4.9. Define Endpoint: Main Endpoint</strong></p><p>And finally, we reach the Main Endpoint. This is where the main magic happens. While this Endpoint looks long, it’s really not. This code chunk can be broken in to three parts:</p><ol><li>The decorator part:<br> — This is the section that actually defines that it is a HTTP POST method.<br> — The path is a parameter, as defined by the user in the API_ENDPOINT part of the environment variables.<br> — The description is split over three lines (mainly so I can make it look aesthetically pleasing on the screen 😉), and it also includes the specific GIT_URL and REPO_DIR, as defined by the user also in the environment variables.<br> — It will always return a JSON object.<br> — Depending on how the data is parsed <em>to</em> this API Endpoint, it will either return one of three statuses: 200 for Success, 422 for Validation Error, or 500 for when there is an Internal Server Error. These are the three custom classes that we defined <a href="#c939">above</a>.</li><li>The declaration part:<br> — This section is only two parameters: the git_url for where the data is being <em>pulled from</em>, and the repo_dir for where the data will be <em>saved to</em>.<br> — Under the hood, each of them will be a <a href="https://fastapi.tiangolo.com/tutorial/query-params-str-validations/">Query</a> objects.<br> — The only look long because the description part covers multiple lines.</li><li>The function execution part<br> — The actual code execution part is going to try to to two things: 1) Check the target directory (and delete anything that exists in there, if necessary), and 2) Clone the latest data from the upstream repo.<br> — If it meets an error, it will return a InternalServerError response to the caller through the API so they can see and debug the error if necessary.<br> — If it is successful, then it will return a Success response back to the caller.</li></ol><p>The code looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/049e45aae5e0ae8e493866a42ef058d6/href">https://medium.com/media/049e45aae5e0ae8e493866a42ef058d6/href</a></iframe><p>When it’s running, the Swagger page looks like this 👇</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JOLA4fs7r9XtAcGu_b6LOQ.png" /><figcaption><strong>The Swagger page for the Main Endpoint<br></strong>(image by Author)</figcaption></figure><h4>5.5. File: /templates/landing_page.html</h4><p>As mentioned <a href="#d1d7">above</a>, I’ve written a super simple HTML page which can be used for the Landing Page for the API. You’ll notice two perculiarities here:</p><ol><li>The values within the &lt;style&gt; tag are not proper CSS syntax, as they have double-curly-brackets ({{,}}). This is because this info will be parse’d in to Python, which will then interpret anything within the curly brackets to be parameters, and attempt to assign variables to them. To escape this, the double-curly-bracket syntax is used. This process is defined in the <a href="https://docs.python.org/3/library/string.html#format-string-syntax">Python documentation</a>.</li><li>The values within the single-curly-brackets are the Global Constants, as defined by the environmental variable (see <a href="#7b66">above</a>).</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3e895e6ca95ad61e996f9ff460e91630/href">https://medium.com/media/3e895e6ca95ad61e996f9ff460e91630/href</a></iframe><h3>Section Three: How to Use the App</h3><h3>6. How to Use It</h3><p>Now that we have built the app, and we understand what all of the components are doing, it’s now time to set it up and begin using it.</p><h4>6.1. Create Server (using AWS)</h4><p>First thing’s first, it’s necessary to create the server in the cloud. It’s possible to use many different cloud-computing platforms; the two most popular ones are the <a href="https://aws.amazon.com/ec2">Elastic Compute Cloud</a> (aka EC2) on the <a href="https://aws.amazon.com/">Amazon Web Services</a> (aka AWS) platform, and the <a href="https://azure.microsoft.com/services/virtual-machines/">Azure Virtual Machine</a> (aka AVM) on the <a href="https://azure.microsoft.com/">Microsoft Azure</a> (aka Azure) platform. If you want to read about the differences between the two platforms, take a look at this article: <a href="https://docs.microsoft.com/en-us/azure/architecture/aws-professional/compute">Comparing AWS and Azure Compute Services</a>.</p><p>For the purposes of this tutorial, we’re going to use the AWS EC2 platform. Here, there are many, <em>many</em> different tutorials about creating EC2 instances. Such as <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EC2_GetStarted.html">this one</a> or <a href="https://medium.com/serverlessguru/creating-an-aws-ec2-instance-d5cf332fdb0c">this one</a> or <a href="https://medium.com/edureka/aws-ec2-tutorial-16583cc7798e">this one</a>. I have personally found the articles by <a href="https://testdriven.io/authors/giacomelli/">Jan Giacomelli</a> to be the most helpful, because he includes plenty of details, explanations and screen shots. See tutorial: <a href="https://testdriven.io/blog/django-docker-https-aws/">Deploying Django to AWS with Docker</a>.</p><p>Please pay close attention to the step in this Tutorial to <a href="https://testdriven.io/blog/django-docker-https-aws/#install-docker">Install Docker</a> on the server. To add to this, there are two other pages on Docker Docs to help guide you with this installation: <a href="https://docs.docker.com/engine/install/ubuntu/">Install Docker Engine on Ubuntu</a> and <a href="https://docs.docker.com/compose/cli-command/#install-on-linux">Install Docker Compose V2 on Linux</a>. Once you ssh in to the server, the specific set of instructions which you will need to run are:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ae652b847fdff2b4d0f43c25dec68d94/href">https://medium.com/media/ae652b847fdff2b4d0f43c25dec68d94/href</a></iframe><p>Once you have created the EC2 instance and have it up and running successfully, it’s important to add one extra step. That is to whitelist the IP addresses from GitHub, so that when GitHub will trigger the HTTP POST request against your EC2 instance, it will not be blocked by the networking rules which are set up by default on the EC2. For more information about why this is important, see details on <a href="https://docs.github.com/en/enterprise-cloud@latest/authentication/keeping-your-account-and-data-secure/about-githubs-ip-addresses">About GitHub’s IP Addresses</a> and <a href="https://github.com/stakater/gitwebhookproxy/issues/62">Possibility to Whitelist Webhooks IP addresses</a> and <a href="https://github.blog/changelog/2019-04-09-webhooks-ip-changes/">GitHub Webhooks IP Ranges</a>.</p><p>To whitelist these IP’s, follow the instructions given on this tutorial: <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/authorizing-access-to-an-instance.html">Authorize Inbound Traffic for your EC2 Instances</a>. Once you have done so, your Inbound Rules should look like this 👇</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gMkRcTTY3hdHpp30NsaFRw.png" /><figcaption><strong>Inbound Rules required for GitHub to access EC2 Instance<br></strong>(image by Author)</figcaption></figure><p>You may ask the question: “But why do I even need to do this?” or maybe “How do I know it has worked?” or maybe “How do I know if GitHub is failing because the IP was not whitelisted?”. Well, the answer to all these questions is the same.</p><p>Specifically: if the GitHub IPs are <em>not </em>whitelisted, then when the Webhook is triggered from GitHub to AWS, it will be blocked and the error message which is returned will be: failed to connect to host. See below screen shot for an example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0kocPuMiyaLvJDTch89EtA.png" /><figcaption><strong>Error message received when IP Addresses are not correctly whitelisted<br></strong>(image by Author)</figcaption></figure><h4>6.2. Add docker-compose.yml file to Server</h4><p>Now that the EC2 instance is ready, the next steps is to add the docker-compose.yml file to it. For this, there are three methods for adding the file to the Server:</p><ol><li>Using ssh:<br> — Follow the instructions <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AccessingInstancesLinux.html">here</a> and <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-connect-methods.html">here</a> for how to log on to the EC2 instance using ssh.<br> — Once on the server, add an empty file using: touch docker-compose.yml.<br> — Open the file using nano docker-compose.yml.<br> — Literally copy-and-paste the text from your Local PC in to the server directly.<br> — Save and close the file by pressing ctrl+O then ctrl+X.</li><li>Using scp:<br> — Follow the instructions <a href="https://stackoverflow.com/questions/6558080/scp-secure-copy-to-ec2-instance-without-password">here</a> and <a href="https://medium.com/srcecde/copy-file-directory-to-from-ec2-using-scp-secure-copy-685c46636399">here</a> for how to execute an scp script to push files to a secure server.<br> — The command should be something like:<br>scp -i path/to/pem/file/ec2_identity_file.pem /path/on/localpc/docker-compose.yml username@server:path/on/server</li><li>Using FileZilla (or other similar FTP software):<br> — Follow the instructions <a href="https://stackoverflow.com/questions/16744863/connect-to-amazon-ec2-file-directory-using-filezilla-and-sftp">here</a> and <a href="https://www.youtube.com/watch?v=e9BDvg42-JI">here</a> and <a href="https://filezillapro.com/docs/v3/basic-usage-instructions/ssh-private-keys-for-sftp/">here</a> and <a href="https://medium.com/@shivambhanvadia/how-to-upload-files-on-ec2-using-filezilla-and-host-a-static-website-ae1eccad141d">here</a> for how to push files to an EC2 instance using FileZilla.</li></ol><p>The exact file which you should be copying to the server is given below. Make sure you update the &lt;update&gt; sections with your specific information.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dcd34c2bfe562df37f12284dba967fda/href">https://medium.com/media/dcd34c2bfe562df37f12284dba967fda/href</a></iframe><h4>6.3. Launch Docker on Server</h4><p>Now that everything is set up and ready to go, the next bit is easy. Just one line of code to fire up the Docker container and start the API listener 👇</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e7cf9769cedce5229573030a68be1163/href">https://medium.com/media/e7cf9769cedce5229573030a68be1163/href</a></iframe><p>And when you run it on the server, then this is what it looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*T_zLS_esHoaZ1af6p_4G9w.png" /><figcaption><strong>Script for setting up Docker on the EC2 Server<br></strong>(image by Author)</figcaption></figure><h4>6.4. Add Webhook on Git (using GitHub)</h4><p>Next, we need to actually configure GitHub to trigger the Webhook. The <a href="https://docs.github.com/en/developers/webhooks-and-events/webhooks/about-webhooks">documentation provided by GitHub</a> is actually really helpful with this, and it is quite easy to follow. In addition, there are some quite good Blogs and Tutorials available online for this, including: <a href="https://medium.com/the-sysadmin/deploy-from-github-gitlab-to-server-using-webhook-d1cb6496368f">Deploy from Github/Gitlab to server using Webhook</a> and <a href="https://towardsdatascience.com/create-your-first-ci-cd-pipeline-with-jenkins-and-github-6aefe21c9240">Create your first CI/CD pipeline with Jenkins and GitHub</a>.</p><p>In this example, I’ll use another Repo of mine called <a href="https://github.com/chrimaho/code-snippets/settings/hooks">code-snippets</a>. While it’s technically not an App, per se, it is still simple enough to help us to understand the process.</p><p>When we navigate to the Repo, we can then open the <strong>Settings </strong>then <strong>Webhooks </strong>section, then press the <strong>Add Webhook</strong> button. As shown below.</p><p>The URL for this is also quite simple: <a href="https://github.com/">https://github.com/&lt;username&gt;/&lt;repo&gt;/settings/hooks</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ir9iamCazHCeYLMA-P69xQ.png" /><figcaption><strong>Where to find Webhooks on GitHub Repo<br></strong>(image by Author)</figcaption></figure><p>The next menu requires you to add the URL. This is the Elastic IP address which you had set in the EC2 Instance (see <a href="https://testdriven.io/blog/django-docker-https-aws/#elastic-ip">this section</a>). If you know how to add sub-domains to your existing websites, then you can do that also. The part <em>after</em> the IP address is the same value which you had configured in the API_ENDPOINT value of the environment section of the docker-compose.yml file. It’s all starting to fall into place now.</p><p>For <strong>Content-Type</strong>, select application/json, leave the <strong>Events</strong> at just the push event, and then select <strong>Add Webhook</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cx8gq4MHcw0QY1hbEEexIg.png" /><figcaption><strong>Settings for adding a Webhook on GitHub<br></strong>(image by Author)</figcaption></figure><p>Once saved, you’ll see a message at the top of the screen:</p><pre>Okay, that hook was successfully created. We sent a ping payload to test it out!</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zkNWMIlrsVwUX2fyGxV_tA.png" /><figcaption><strong>Successfully Created Webhook on GitHub Repo<br></strong>(image by Author)</figcaption></figure><p>And when you go in to that Webhook to inspect the Recent Deliveries, you’ll find one Request has already been sent, and a response already received. And, <em>hopefully</em>, if all of your configuration is successfully set up, you will see a green tick and a response status_code of: 200.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xuAdnAWHa8N7rzpO_bchaQ.png" /><figcaption><strong>Confirmation of Successful response from GitHub Webhook ping<br></strong>(image by Author)</figcaption></figure><h4>6.5. Test it</h4><p>Testing it is super easy. First, let’s hop on to the EC2 server and check the docker compose logs are looking good. Then we will check the contents of the directory using ls repo. This is the result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/937/1*acLaYPU3p9eupC09avRj9g.png" /><figcaption><strong>Checking the files on the Server<br></strong>(image by Author)</figcaption></figure><p>First thing’s first, you can see that the ping request from GitHub has come through successfully! And that the /repo directory is <em>already</em> containing the updated info from the upstream Git repo!</p><p>Next, on our local PC, within the directory for the repo that you want to push to GitHub, let’s just create one <em>very small</em> text file, called test.txt. Then we will do git add, git commit, git push to get it tot he upstream Repo. The code looks like this 👇</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6d17614e9da4509789806a3953aa100a/href">https://medium.com/media/6d17614e9da4509789806a3953aa100a/href</a></iframe><p>And the terminal looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/901/1*rF1bkWNLoP7AqKZHo-Hkug.png" /><figcaption><strong>Add a new file to Git<br></strong>(image by Author)</figcaption></figure><p>And then when we go back to the EC2 server to check it, we can re-run docker compose logs to check it has come through successfully (which it has!), and also check the ls repo to confirm that the file is now existing (which it is!). This is what it looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/956/1*i9cErOlzZDH1LwlXlyhgPQ.png" /><figcaption><strong>Re-Checking the files on the Server<br></strong>(image by Author)</figcaption></figure><p>Everything is now working like a charm!</p><h3>Section Four: More Info</h3><h3>7. What’s Next</h3><p><strong>Note: </strong>Now that the Webhooks are set up and your Server is automatically being kept up to date, there is technically <em>one more step</em> which you would need to do. And that is usually to restart the app which you have running on the server, to ensure that it is always referencing latest source files. However, because various apps have various ways of being restarted, I am not going to mention them here.</p><p>Also, more recently, I’ve found that there is a Webhook process available for various Docker Container processes. Including on <a href="https://docs.docker.com/docker-hub/webhooks/">Docker Hub</a>, <a href="https://aws.amazon.com/blogs/containers/create-a-ci-cd-pipeline-for-amazon-ecs-with-github-actions-and-aws-codebuild-tests/">AWS ECR</a>, and <a href="https://docs.microsoft.com/en-us/azure/container-registry/container-registry-webhook">Azure ACR</a>. This is inspiration for me to do another blog for how to use this!</p><h3>8. Where to Find More Information</h3><p>Thankfully, this process is not difficult. I’ve set it up to be open source, and readily available. All source code is saved to <a href="https://github.com/chrimaho/update-from-git">GitHub</a>, and the ready-to-use container is hosted on <a href="https://hub.docker.com/r/chrimaho/update-from-git">Docker Hub</a>.</p><ul><li><a href="https://github.com/chrimaho/update-from-git">GitHub - chrimaho/update-from-git: A Python App, hosted on a Docker Image for listening to, and then pulling from, a git repository</a></li><li><a href="https://hub.docker.com/r/chrimaho/update-from-git">Docker Hub</a></li></ul><h4>9. The full file for: /src/api/main.py</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f49fc5ab646db9de7507487e6899a4a3/href">https://medium.com/media/f49fc5ab646db9de7507487e6899a4a3/href</a></iframe><p>Thanks for reading.</p><pre><strong>Want to Connect?</strong></pre><pre>Shoot me queries,ideas and suggestions at <a href="https://chrimaho.com/">https://chrimaho.com</a>.</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a7f18d23d5b7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/better-programming/github-webhooks-docker-and-python-for-automatic-app-deployments-a7f18d23d5b7">How to Use GitHub Webhooks, Docker, and Python for Automatic End-to-End Deployments</a> was originally published in <a href="https://betterprogramming.pub/">Better Programming</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Power of Code Snippets]]></title>
            <link>https://medium.com/codex/the-power-of-code-snippets-9b0359e7ae2d?source=rss-56d03114dd5a------2</link>
            <guid isPermaLink="false">https://medium.com/p/9b0359e7ae2d</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[r]]></category>
            <category><![CDATA[code-snippet]]></category>
            <category><![CDATA[vscode]]></category>
            <category><![CDATA[rstudio]]></category>
            <dc:creator><![CDATA[Chris Mahoney]]></dc:creator>
            <pubDate>Mon, 02 Aug 2021 03:26:52 GMT</pubDate>
            <atom:updated>2021-08-02T03:26:52.035Z</atom:updated>
            <content:encoded><![CDATA[<h4>How To Turn Your Code from Good to Great</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*d7_9UH8XjbRKWtK9S6VSDg.jpeg" /><figcaption><strong>The Pythonic Algorithm for Success<br></strong>(<a href="https://wallpapercave.com/w/wp3105541">CoverImage</a> by <a href="https://wallpapercave.com/">WallPaperCave</a>)</figcaption></figure><h3>Contents</h3><p>1. <a href="#fdeb">Introduction</a><br>2. <a href="#4c91">R + RStudio</a><br>3. <a href="#1b61">Python + VSCode</a><br>4. <a href="#46b7">Conclusion</a><br>5. <a href="#edbf">More Resources</a></p><h3>1. Introduction</h3><p>Modern day programmers write a lot of code. That’s part of the job. But one of the main principles of writing good code is the DRY principle: Don’t Repeat Yourself (see <a href="https://www.earthdatascience.org/courses/earth-analytics/automate-science-workflows/write-efficient-code-for-science-r/#dont-repeat-yourself---dry">The DRY Principle: R Functions</a> or <a href="https://scientificallysound.org/2018/07/19/python-functions/">The DRY Principle: Python Functions</a> or <a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself">Wikipedia</a> or any number of other online sources). In essence, this principle effectively states that if you are going to write the same code twice, then don’t; instead, write the code in a function, then call the function twice.</p><p>There’s also another principle, which is equally if not more important: Document Your Work. For this, function docstrings are extremely helpful. Basically, you want to write your code for someone else (even if that someone else is the future version of you…), so that they can understand your code better. Here, you want to explain <em>why</em> your code is doing what it is doing (the reader can see <em>what</em> the code is doing by simply reading the code itself). Such as if there is some complicated logic, or to handle a quirk in the data, then it’s best to write a docstring. But don’t go overboard though! There are many reasons why too much documentation can be a bad thing (such as <a href="https://www.elegantthemes.com/blog/wordpress/how-to-comment-your-code-like-a-pro-best-practices-and-good-habits">How to Comment Your Code Like a Pro: Best Practices and Good Habits</a> and <a href="https://www.freecodecamp.org/news/code-comments-the-good-the-bad-and-the-ugly-be9cc65fbf83">Putting Comments in Code: The Good, the Bad, and the Ugly</a>).</p><p>It is the intersection of these two principles that gives rise to the use of a fantastic thing in the world of programming: Code Snippets. These snippets are effectively ‘saved chunks of code’, which can allow you to speed up the implementation of your code, and the overall readability of your code. And ultimately, will turn your code from <em>Good</em> to <strong>Great</strong>.</p><p>When approaching this from a Data Science perspective, I will focus on the two most popular coding languages in this field, and the popular IDE for each of them: <a href="https://cran.r-project.org/">R</a> + <a href="https://www.rstudio.com/">RStudio</a> and <a href="https://www.python.org/">Python</a> + <a href="https://code.visualstudio.com/">VSCode</a>.</p><h3>2. R + RStudio</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/504/1*8FmBnVoO_wUjsa1Xp4Rl1g.png" /><figcaption><strong>R Code Snippet<br></strong>(<a href="https://user-images.githubusercontent.com/4662568/71432979-72160c00-2717-11ea-8b32-fc06d145189e.png">Image</a> by <a href="https://disqus.com/by/renkun_ken/">Kun Ren</a> on <a href="https://renkun.me/">RenKun.me</a>)</figcaption></figure><p>To start off with, here’s some good sources for using docstrings and Code Snippets in RStudio:</p><ul><li><a href="https://style.tidyverse.org/documentation.html">The tidyverse Style Guide: Documentation</a></li><li><a href="https://cran.r-project.org/web/packages/docstring/vignettes/docstring_intro.html">Introduction to docstrings</a></li><li><a href="https://stackoverflow.com/questions/5931292/is-there-a-sensible-way-to-do-something-like-docstrings-in-r">Is there a sensible way to do something like docstrings in R?</a></li><li><a href="https://github.com/Dasonk/docstring">docstring package</a></li><li><a href="https://medium.com/@manishfirewall/common-r-code-snippets-eb5c72750339?source=search_post---------0">Common R code snippets</a></li><li><a href="https://support.rstudio.com/hc/en-us/articles/204463668-Code-Snippets">Code Snippets in the RStudio IDE</a></li><li><a href="https://www.infoworld.com/article/3396616/how-to-use-rstudio-code-snippets.html">How to use RStudio code snippets</a></li><li><a href="https://jozef.io/r906-rstudio-snippets/">4 ways to be more efficient using RStudio’s Code Snippets, with 11 ready to use examples</a></li></ul><p>To find the Code Snippets for RStudio, just navigate to: <strong>Tools</strong> &gt; <strong>Global Options</strong> &gt; <strong>Code</strong> &gt; <strong>Editing</strong> &gt; <strong>Edit Snippets</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DNEhlQCGwSlVqJMoEpt26g.png" /><figcaption><strong>Finding Code Snippets<br></strong>(image by Author)</figcaption></figure><p>There, you’ll find a whole bunch of Snippets which have been provided already by the RStudio team. Including one for writing functions:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e51d813936496074d63831c6da55fdab/href">https://medium.com/media/e51d813936496074d63831c6da55fdab/href</a></iframe><p>But I think that we can do better than that.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/39b0f67425e826f42c711f1b78be6fc4/href">https://medium.com/media/39b0f67425e826f42c711f1b78be6fc4/href</a></iframe><p>This function will follow the standard syntax for doing docstrings. Including using the special comment characters #&#39; at the start of the line, and various @ attributes. See above resources for why these are important and how to use them.</p><p>By using this code snippet, you will be able to:</p><ol><li>Quickly and easily implement your new function.</li><li>Effectively ‘document as you write’ your function as you write it for the first time; as opposed to needing to come back and ‘re-write’ your docstring sometime after you have written your function for the first time.</li><li>Easily tab through the different sections of the function, and fill in different sections <em>at the same time</em>, thanks to the use of ${1:FunctionName} appearing in different locations.</li><li>Return a ready-to-run, and well-documented function in merely a matter of minutes.</li></ol><p>But you don’t only need to do this for function declarations. You can also add various headers and sections to your file.</p><h4><strong>!!EASTER EGG!!</strong></h4><p>If you end the comment line with four identical characters (like #### or ----), then RStudio will add that line to the Document Outline panel! Which makes for <em>awesome</em> document navigation. But you can take this one step further. If you add multiple # characters at the beginning of the line, then you can automatically indent the Document Outline headers! This will make your life amazing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3Z5U4rs9vZvOmVzJvy5Hfw.png" /><figcaption><strong>Document Panel Amazingness<br></strong>(image by Author)</figcaption></figure><p>If you wanted the raw files for these Snippets, check them out here:</p><ul><li><a href="https://github.com/chrimaho/code-snippets/blob/97fc70d87902cb55c50b9f543d6b0485f4818015/R/Snippets_Input.txt">chrimaho/code-snippets</a></li><li><a href="https://github.com/chrimaho/code-snippets/blob/97fc70d87902cb55c50b9f543d6b0485f4818015/R/Snippets_Output.R">chrimaho/code-snippets</a></li></ul><h3>3. Python + VSCode</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*0W1gvoUvlBpDOiIG" /><figcaption><strong>Python Code Snippet<br></strong>(<a href="https://images.unsplash.com/photo-1515879218367-8466d910aaa4?ixlib=rb-1.2.1&amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;auto=format&amp;fit=crop&amp;w=1950&amp;q=80">Image</a> by <a href="https://unsplash.com/@cdr6934">Chris Ried</a> on <a href="https://unsplash.com/">UnSplash</a>)</figcaption></figure><p>Same as before, let’s start off with some good resources for using Python docstrings and code snippets in VSCode:</p><ul><li><a href="https://code.visualstudio.com/docs/editor/userdefinedsnippets">Snippets in Visual Studio Code</a></li><li><a href="https://www.python.org/dev/peps/pep-0257/">PEP 257 — Docstring Conventions</a></li><li><a href="https://www.programiz.com/python-programming/docstrings">Python Docstrings</a></li><li><a href="https://stackoverflow.com/questions/3898572/what-is-the-standard-python-docstring-format">What is the standard Python docstring format?</a></li><li><a href="http://daouzli.com/blog/docstring.html">The docstrings main formats</a></li><li><a href="https://towardsdatascience.com/30-helpful-python-snippets-that-you-can-learn-in-30-seconds-or-less-69bb49204172?source=search_post---------1">30 Helpful Python Snippets That You Can Learn in 30 Seconds or Less</a></li><li><a href="https://levelup.gitconnected.com/22-python-code-snippets-for-everyday-problems-4c6a216c33ae?source=search_post---------1">22 Python Code Snippets for Everyday Problems</a></li><li><a href="https://levelup.gitconnected.com/22-code-snippets-that-every-python-programmer-must-learn-b7f7ec35e9df?source=search_post---------7">22 Code Snippets That Every Python Programmer Must Learn</a></li></ul><p>To find the Code Snippets for VSCode, you will need to use the Command Palette: press ctrl+shift+p, type snip, select <strong>Preferences: Configure User Snippets</strong>, then open the <strong>snippets.code-snippets</strong> file. Here, you can configure any user snippets you want. This file is fundamentally a JSON file, so you should follow the syntax convention for that.</p><p>Similar to RStudio, VSCode has some defaults. Such as:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b4506f312d25654cd3de3063e0ffccc3/href">https://medium.com/media/b4506f312d25654cd3de3063e0ffccc3/href</a></iframe><p>However, we know that there is a better way to do this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b3fe0cb58d54ccd14741ba6b4b486e2/href">https://medium.com/media/0b3fe0cb58d54ccd14741ba6b4b486e2/href</a></iframe><p>The benefits of this snippet in VSCode are quite similar to the RStudio benefits:</p><ol><li>It’s quick,</li><li>It’s effective,</li><li>It’s easy,</li><li>It’s ready-to-go.</li></ol><h4>!!EASTER EGG!!</h4><p>VSCode renders the docstrings in Markdown format! Meaning that you are able to input <strong>Headings</strong>, code chunks, <em>italics</em>, <strong>bold</strong>, pretty much any Markdown format that you want! This may break the regular docstring conventions (eg. <a href="https://numpydoc.readthedocs.io/en/latest/format.html">Numpy</a>, <a href="https://google.github.io/styleguide/pyguide.html">Google</a>, <a href="https://www.sphinx-doc.org/en/master/#">Sphinx</a>). But for me, that’s okay. It makes my VSCode environment look professional. You’re welcome.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0hGOwThCivCfgJq8D0-KMQ.jpeg" /><figcaption><strong>Docstrings with Markdown Format<br></strong>(image by Author)</figcaption></figure><p>If you wanted the raw files for these Snippets, check them out here:</p><ul><li><a href="https://github.com/chrimaho/code-snippets/blob/97fc70d87902cb55c50b9f543d6b0485f4818015/Python/Snippets_Input.code-snippets">chrimaho/code-snippets</a></li><li><a href="https://github.com/chrimaho/code-snippets/blob/97fc70d87902cb55c50b9f543d6b0485f4818015/Python/Snippets_Output.py">chrimaho/code-snippets</a></li></ul><h3>4. Conclusion</h3><p>Snippets are a fantastic way to quickly and easily build your code in a consistent and aesthetic manner. They are highly configurable and they add a substantial amount of value and efficiency to your coding. By using snippets, your future you will be thanking you. Trust me.</p><h3>5. More Resources</h3><ul><li><a href="https://betterprogramming.pub/10-tips-and-tricks-for-data-scientists-vol-2-5640ccd1aa5e?source=search_post---------1">10 Amazing Tips and Tricks for Data Scientists: Some more helpful code snippets in R and Python</a></li><li><a href="https://betterprogramming.pub/10tips-and-tricks-for-data-scientists-vol-1-a870455325da?source=search_post---------2">10 Tips and Tricks for Data Scientists: Helpful code snippets in R and Python</a></li><li><a href="https://betterprogramming.pub/5-powerful-python-and-r-tricks-for-data-scientists-e35dd9050750?source=search_post---------3">5 Powerful Python and R Tricks for Data Scientist: Helpful code snippets in R and Pythons</a></li><li><a href="https://betterprogramming.pub/10-tips-and-tricks-for-data-scientists-part-3-52ffbddcba26?source=search_post---------4">10 Real-World Tips and Tricks That Data Scientists Would Love: Some more helpful code snippets in Python, R, and more</a></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*aDBANfeYJpGq4k4t" /><figcaption><strong>Plenty More Programming<br></strong>(<a href="https://images.unsplash.com/photo-1518932945647-7a1c969f8be2?ixlib=rb-1.2.1&amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;auto=format&amp;fit=crop&amp;w=1789&amp;q=80">Image</a> by <a href="https://unsplash.com/@mr_vero">Irvan Smith</a> on <a href="https://unsplash.com/">UnSplash</a>)</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9b0359e7ae2d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codex/the-power-of-code-snippets-9b0359e7ae2d">The Power of Code Snippets</a> was originally published in <a href="https://medium.com/codex">CodeX</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mental Models and Social Situations]]></title>
            <link>https://medium.com/better-humans/mental-models-80eb02b0dd70?source=rss-56d03114dd5a------2</link>
            <guid isPermaLink="false">https://medium.com/p/80eb02b0dd70</guid>
            <category><![CDATA[self-reflection]]></category>
            <category><![CDATA[mental-models]]></category>
            <category><![CDATA[networking]]></category>
            <category><![CDATA[personal-development]]></category>
            <category><![CDATA[relationships]]></category>
            <dc:creator><![CDATA[Chris Mahoney]]></dc:creator>
            <pubDate>Tue, 20 Jul 2021 00:32:02 GMT</pubDate>
            <atom:updated>2021-07-20T15:35:05.886Z</atom:updated>
            <content:encoded><![CDATA[<h4>Your mental models can both assist and limit your perceptions of the world, especially when you meet a person for the first time</h4><figure><img alt="A man underneath a chalkboard with gears drawn on it." src="https://cdn-images-1.medium.com/max/1024/1*5xva-H3BMMjEE55xWTLCQw.jpeg" /><figcaption><strong>The Gears Of The Mind</strong><br>(<a href="https://www.shutterstock.com/image-photo/man-gears-on-blackboard-background-356650580">Image</a> by <a href="https://www.shutterstock.com/g/ChristianChan">ChristianChan</a> on <a href="https://www.shutterstock.com/">Shutterstock</a>)</figcaption></figure><p>Our mental models are deeply engrained images of how we see the world and how we react in different scenarios and situations (<a href="#40a4">McShane, Travaglione &amp; Olekalns 2010, p. 91</a>; <a href="#a42e">Senge 2006, p. 164</a>).</p><p>When I meet someone for the first time, my mental models greatly influence my perceptions of them: their words, their expressions, their reactions. And by extension, these mental models then influence my thoughts and actions in that situation: how I behave, how I speak, even my subconscious mannerisms. As a result, that first meeting can make for a pleasant first impression or an embarrassingly regrettable occasion.</p><p>Conversely, I believe that my mental models also limit my perceptions of the other people I meet. This is because I am basing my conclusions on two things: a single, short meeting, and on my filtered model of the world which has been developed over my entire lifetime. Which leaves me with limited inferential flexibility, and the possibility of a prejudicial conclusion (<a href="#37d9">Ormerod 2000</a>, cited in <a href="#9334">Johnson-Laird 2001, p. 436</a>; <a href="#9606">Markman &amp; Gentner 2001, p. 230</a>).</p><p>It is interesting that there are different terms used for ‘<em>mental models.</em>’ For example, ‘cognitive maps’ (<a href="#244d">De Wit &amp; Meyer 2010, p. 31</a>; <a href="#7256">Chermack 2003, p. 412</a>), ‘cognitive schemata’ (<a href="#4fc2">Anderson 1983</a>; <a href="#a805">Pazy 1994, p. 1172</a>; <a href="#3412">Lewis &amp; Durrant 2011, p. 343</a>), ‘knowledge structures’ (<a href="#132e">Lyles &amp; Schwenk 1992, p. 155</a>; <a href="#45db">Walsh 1995, p. 281</a>), and ‘construed reality’ (<a href="#0bbc">Finkelstein &amp; Hambrick 1996</a>).</p><h3>What Does This Actually Mean?</h3><figure><img alt="Person thinking" src="https://cdn-images-1.medium.com/max/1024/1*3V-4lGoV13762iZxnsUCeA.png" /><figcaption><strong>The Question Mark</strong><br>(<a href="https://www.calmclinic.com/storage/images/358/qozo59/main/w1600.png">Image</a> by <a href="https://www.calmclinic.com/about">Micah Abraham</a> on <a href="https://www.calmclinic.com/">CalmClinic</a>)</figcaption></figure><p>For me, personally, I believe that I use a differentiation process of social stereotyping, combined with the attribution process of behavioural observation (<a href="#40a4">McShane, Travaglione &amp; Olekalns 2010, pp. 92–4</a>), to achieve a self-fulfilling prophecy (<a href="#4aa2">Choo 2006, p. 90</a>) of mutual respect and friendship. What that means is that I look at the differences between me and my audience to stereotype them, then I observe patterns in their behaviour to develop attributes and characteristics about them, which then results in a self-fulfilling prophecy that makes me think I have made a new friend. This happens subconsciously — and in the blink of an eye. And to a certain extent, I think that we all tend to do this. In our own way.</p><p>Jackson &amp; Smith (<a href="#c448">1999, p.121</a>) proposed a set of ‘social identity dimensions’, which effectively categorises a number of different types of identities, which we are all somehow subconsciously already aware of. So, there will inherently always be a difference between how I perceive those dimensions, and how another person will also perceive them. My own mental models of how I see the world will help me to understand those differences between me and the other person.</p><p>The Attribution theory provides an explanation about how people make causal explanations and deal with making causal inferences (<a href="#d43b">Kelley 1973, p. 107</a>). I feel like I use this to observe whether a persons’ behaviour is predominately influenced by internal or external factors (<a href="#40a4">McShane, Travaglione &amp; Olekalns 2010, p. 94</a>). My mental models filter my thoughts and actions with the intention to make a respectable first impression, one which emphasises mutual respect and friendship. Therefore, through the filter of my mental models, and with the observance of their behaviour, I can fairly quickly ascertain whether or not they also want to spark a friendship with me.</p><p>In his book titled “<a href="#https://en.wikipedia.org/wiki/How_to_Win_Friends_and_Influence_People">How to Win Friends and Influence People</a>,” Carnegie (<a href="#0231">1999, p. 123</a>) states that:</p><blockquote>If you aspire to be a good conversationalist, be an attentive listener. To be interesting, be interested. Ask questions that other persons will enjoy answering.</blockquote><p>As a result, if I am truly to make a friend during that first meeting, I need to be interested in them. I need to be aware of myself, and my own mental models. And I need to be acutely conscious that my thoughts, words, and actions, will all be observed and perceived by the other person. And they, too, have their own mental models and life experiences.</p><h3>First Example: Meeting a Family Friend</h3><figure><img alt="Two minds merged" src="https://cdn-images-1.medium.com/max/1024/0*0RdSYzibDhePSB9K.jpg" /><figcaption><strong>The Alignment Of Two Minds</strong><br>(<a href="https://cdn.powerofpositivity.com/wp-content/uploads/2020/11/Therapists-Reveal-6-Traits-Of-Psychological-Health.webp">Image</a> by <a href="https://www.powerofpositivity.com/author/lakeisha-e/">Lakeisha Ethans</a>)</figcaption></figure><p>However, the question needs to be asked: ‘so what?’.</p><p>What does this mean about how my mental models assist my perceptions when I meet a person for the first time? For this, I refer to the 11 schemata’s postulated by Pazy (<a href="#a805">1994, p. 1172</a>). In particular, I relate well to one in particular: that of ‘relying on professional background’ (<a href="#a805">Pazy, 1994, p. 1174</a>);. However, I’d take this model one step further to say ‘relying on personal experience.’</p><p>This is because in the past, as a young boy, I have said and done some rather embarrassing things when meeting a person for the first time. For example, I remember when I was perhaps nine or ten years old, and my parents introduced me to a really attractive family friend. She was really cute, however, she was noticeably older than I was. As I had not yet had much life experience, and my mental models were still forming, the encounter turned out to be slightly embarrassing for me.</p><p>“Hello,” I said. And nearly straight away, I blurted out, “You’re really pretty.”</p><p>I said this without thinking of the ramifications of such a statement and regretted it practically straight away.</p><p>She laughed and brushed it off, saying “Aww, aren’t you cute,” and patted me on the head.</p><p>Embarrassed that I would say such a statement not only in public but to such a close family friend, I resolved in the future to not be so blunt when I meet someone for the first time. Subconsciously, this forced me to alter my mental model of the world, to not make such open and personal statements to someone I have only just met. This experience (along with many, many, many others in my life) has shaped me into the person I am today.</p><p>Nowadays, although I may think that a particular lady is very attractive, I don’t vocalise these thoughts because it is not in line with the mental model I use when I meet a person. My mental model allows me to filter my thoughts and actions, allowing both my new acquaintance and myself to have a mutually respectful and beneficial interaction.</p><h3>The Limitations of Mental Models</h3><figure><img alt="Person in box" src="https://cdn-images-1.medium.com/max/612/0*qNVYNspVxmFz9f4o" /><figcaption><strong>Self Limitation </strong><br>(<a href="https://www.istockphoto.com/photo/woman-feeling-boxed-in-by-cube-drawn-around-her-gm909701406-250555158">Image</a> by <a href="https://www.istockphoto.com/portfolio/DNY59?mediatype=photography">DNY59</a> on <a href="https://www.istockphoto.com/">iStockPhotos</a>)</figcaption></figure><p>In contrast, my mental models also limit my perceptions when I am meeting a person for the first time because they restrict the amount of information that is being received by my brain at any one time. I have constructed my mental models of the world to filter through the myriad of external information and stimuli which I receive.</p><p>Johnson-Laird (<a href="#9334">2001, p. 436</a>) explains that this filtering process is a consequence of the limitations of a persons’ working memory and ability to process large amounts of information at once. As described by Magzan (<a href="#19c2">2012</a>), our mental models are frameworks developed over time. They are often hidden to the extent that we are unaware of them and their effects on our behaviour, and once created are fixed and reinforced in our mind, becoming difficult to change.</p><p>Indeed, my mental models do assist me to understand and appreciate a persons’ character. However, at the same time, they limit the amount of information received about the person I am meeting. Furthermore, my mental models have the potential to limit my perceptions of the person themselves.</p><p>That is, what if the person I am meeting doesn’t want to talk about themselves? What if they are particularly introverted and don’t appreciate extended conversations? Or what if they are particularly private and only want to meet me for business purposes? Or perhaps the other person wants me to talk about myself? Or what if they are attracted to me, and due to my own mental model, I don’t vocalise that or provide any signs or mannerisms that I like them back? Or what if the other person is in a rush, and simply doesn’t have the time or inclination to have a long-winded conversation with me?</p><p>This sort of confusion is a limitation and a result of my (potentially wrong) mental models of how an introductory conversation should be conducted. From this, there is potential for me to make an ill-informed, prejudicial conclusion about the person I am meeting. Consequently, when I meet a person for the first time, my mental models limit the amount of information that my brain is receiving during the meeting. It is on this filtered information that I am basing my conclusions about a person, and how openly and friendly I converse with them.</p><h3>Second Example: Meeting a Business Associate</h3><figure><img alt="Two people shaking hands" src="https://cdn-images-1.medium.com/max/612/0*KEf51xujDDuaxBeN" /><figcaption><strong>Intense Communication</strong><br>(<a href="https://www.istockphoto.com/photo/business-is-done-gm1064079264-284494325">Image</a> by <a href="https://www.istockphoto.com/portfolio/PeopleImages?mediatype=photography">PeopleImages</a> on <a href="https://www.istockphoto.com/">iStockPhotos</a>)</figcaption></figure><p>Again, the question needs to be asked: ‘so what?’.</p><p>This time, I will refer to another example from my personal experience, however a more recent one. Last year, I attended a networking event and was introduced to a business associate and customer of the company I work for. I met him toward the end of the function. He was a polite, middle-aged gentleman, not too outspoken, and very knowledgeable about his organisation and about the industry more broadly.</p><p>I asked him questions about himself, about the company he works for, and his experiences over his working career. Questions I thought would be insightful and interesting for him to answer. His answers, though, were short, and the conversation didn’t last long. The event concluded soon after.</p><p>However, in that time I was able to gain a good understanding of him, his experience, and his company. Afterward, I thanked him and went on my way.</p><p>The colleague who introduced us pointed out to me that during the short meeting, the man was leaning slightly away from me and had his body pointed adjacent to mine. My colleague mentioned that this could have been because he was eager to leave, he was disinterested in what I was saying, or that he felt as though his personal space was intruded on by my enthusiasm and constant questioning.</p><p>This was something that I didn’t notice because I was trying to be friendly toward him and find out what interests him. In hindsight, I can see that my mental models filtered out the body-language signals that indicated he didn’t appreciate being asked a barrage of questions. Things always look better in hindsight, and if I could meet him for the first time, again, I perhaps wouldn’t ask so many questions and would tell some stories from my experience that were akin to his experiences.</p><p>As shown, this is an example of how the mental models, which I hold about the world, filter through the information that is being received by my brain. It lead me to believe that the person I met was comfortable talking about himself, when maybe he only wanted to talk about business, or perhaps he was simply tired and wanted to go home for the day.</p><h3>Conclusion</h3><p>Conclusively, my mental models greatly assist my perceptions of a person when I meet them for the first time. They also assist me to make a beneficial first impression by filtering my thoughts and actions, allowing me to share respect with the new friend I have made. However, this filtering process also limits the amount of information that is received by my brain. The filtering limits the information I receive, and my perceptions during the meeting. Therefore, I’m basing my conclusions about the person I am meeting on the relatively short introductory meeting and discussion, which may result in incorrect prejudicial inferences being made about their character.</p><figure><img alt="People meeting" src="https://cdn-images-1.medium.com/max/1024/1*nrqoIkVeYmMC7RmvoUnh7w.jpeg" /><figcaption><strong>Pleased To Meet You</strong><br>(<a href="https://www.shutterstock.com/image-photo/glad-work-you-young-modern-men-1099307402">Image</a> by <a href="https://www.shutterstock.com/g/g-stockstudio">G-Stock Studio</a> on <a href="https://www.shutterstock.com/">Shutterstock</a>)</figcaption></figure><h3>References</h3><p>Anderson, JR 1983, The Architecture of Cognition, Harvard University Press, Cambridge, Massachusetts.</p><p>Carnegie, D 1999, How to Win Friends and Influence People, Harper Collins Publishers, Pymble, Sydney.</p><p>Chermack, TJ 2003, ‘Mental models in decision making and implications for human resource development’, Advances in Developing Human Resources, vol. 5, no. 4, ProQuest database, ISSN: 15234223, DOI: <a href="http://dx.doi.org/10.1177/1523422303257373.">http://dx.doi.org/10.1177/1523422303257373.</a></p><p>Choo, CW 2006, ‘The management of ambiguity: organizations as sense-making communities’, The Knowing Organization: How Organizations Use Information to Construct Meaning, Create Knowledge and Make Decisions, 2nd edn, Sage, London, UK.</p><p>De Wit, B &amp; Meyer, R 2010, Strategy synthesis: resolving strategy paradoxes to create competitive advantage, 3rd edn, Cengage Learning EMEA, Andover.</p><p>Finkelstein, S &amp; Hambrick, DC 1996, Strategic Leadership: Top Executives and Their Effects on Organizations, South-Western College Publications, Boston, Massachusetts.</p><p>Jackson, JW &amp; Smith, ER 1999, ‘Conceptualizing social identity: a new framework and evidence for the impact of different dimensions’, Personality &amp; Social Psychology Bulletin, vol. 25, no. 1, pp. 120–135, Sage Journals database, ISSN: 0146–1672.</p><p>Johnson-Laird, PN 2001, ‘Mental models and deduction’, Trends in Cognitive Sciences, vol. 5, no. 10, pp. 434–42, ScienceDirect database, ISSN: 1364–6613, DOI: 10.1016/S1364–6613(00)01751–4.</p><p>Kelley, HH 1973, ‘The processes of causal attribution’, American Psychologist, vol. 28, no. 2, pp. 107–28, PsycARTICLES database, EBSCOHost, ISSN: 0003–066X, DOI: 10.1037/h0034225.</p><p>Lewis, PA &amp; Durrant, SJ 2011, ‘Overlapping memory relay during sleep builds cognitive schemata’, Trends in Cognitive Sciences, vol. 15, no. 8, pp. 343–51, ScienceDirect database, ISSN: 13646613, DOI: 10.1016/j.tics.2011.06.004.</p><p>Lyles, MA &amp; Schwenk, CR 1992, ‘Top management, strategy and organizational knowledge structures’ Journal of Management Studies, vol. 29, no. 2, pp. 155–74, Business Source Complete database, EBSCOHost, ISSN: 0022–2380, DOI: 10.1111/j.1467–6486.1992.tb00658.x.</p><p>Magzan, M 2012, ‘Mental models for leadership effectiveness: building future different than the past’, Journal of Engineering Management and Competitiveness, vol. 2, no. 2, pp. 57–63, &lt;http://www.tfzr.rs/jemc/files/Vol2No2/V2N22012-03.pdf&gt;.</p><p>Markman, AB &amp; Gentner, D 2001, ‘Thinking’, Annual Review of Psychology, vol. 52, pp. 223–47, Business Source Complete database, EBSCOHost, ISSN: 0066–4308.</p><p>McShane, SL, Travaglione, T &amp; Olekalns, M 2010, ‘Perception and learning in organisations’, in Organisational Behaviour on the Pacific Rim, 3rd edn, McGraw-Hill, North Ryde, NSW, pp. 86–119.</p><p>Ormerod, TC 2000, ‘Mechanisms and strategies for rephrasing’, in W Schaeken &amp; NetLibrary Inc. (eds) Deductive Reasoning and Strategies, L. Erlbaum Associates, Mahwah, New Jersey.</p><p>Pazy, A 1994, ‘Cognitive schemata of professional obsolescence’, Human Relations, vol. 47, no. 10, pp. 1167–99, Sage Journals database, ISSN: 0018–7267.</p><p>Senge, P 2006, The Fifth Discipline: the Art and Practice of Learning Organizations, rev. edn, Doubleday, New York, NY.</p><p>Walsh, JP 1995, ‘Managerial and organizational cognition: notes from a trip down memory lane’, Organiational Science, vol. 6, no. 3, pp. 250–321, Business Source Complete database, EBSCOHost, ISSN: 10477039.</p><p><a href="https://chrimaho.medium.com/">Chris Mahoney - Medium</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=80eb02b0dd70" width="1" height="1" alt=""><hr><p><a href="https://medium.com/better-humans/mental-models-80eb02b0dd70">Mental Models and Social Situations</a> was originally published in <a href="https://betterhumans.pub/">Better Humans</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Reinforcement Learning]]></title>
            <link>https://medium.com/data-science/reinforcement-learning-fda8ff535bb6?source=rss-56d03114dd5a------2</link>
            <guid isPermaLink="false">https://medium.com/p/fda8ff535bb6</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[deep-dives]]></category>
            <category><![CDATA[reinforcement-learning]]></category>
            <category><![CDATA[thoughts-and-theory]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Chris Mahoney]]></dc:creator>
            <pubDate>Sat, 12 Jun 2021 12:26:04 GMT</pubDate>
            <atom:updated>2021-06-17T14:53:36.791Z</atom:updated>
            <content:encoded><![CDATA[<h4><a href="https://towardsdatascience.com/tagged/thoughts-and-theory">Thoughts and Theory</a></h4><h4>A Review of the Historic, Modern, and Future Applications of this Special Form of Machine Learning</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Y0TDuXNyywjqqr5l5GkMQQ.png" /><figcaption>Reinforcement Learning (<a href="https://cdn.iconscout.com/icon/premium/png-512-thumb/reinforcement-learning-2040769-1721120.png">image</a> by Flat-Icons on <a href="https://iconscout.com/">IconScout</a> under license to Chris Mahoney)</figcaption></figure><h3>Contents</h3><p>1. <a href="#b1da">Introduction</a><br>2. <a href="#5554">Historic Developments (pre 1992)</a><br>— 2.1. <a href="#ca1d">Concurrent Developments</a><br> — — 2.1.1. <a href="#703d">Learning by Trial and Error</a><br> — — 2.1.2. <a href="#757c">The Problem of Optimal Control</a><br> — — 2.1.3. <a href="#4b10">Temporal Difference Learning Methods</a><br> — 2.2. <a href="#51b2">Combined Developments</a><br>3. <a href="#7383">Modern Developments (Post 1992)<br></a> — 3.1. <a href="#70c7">Developments in Board Games</a><br> — 3.2. <a href="#c35a">Developments in Computer Games</a><br>4. <a href="#19c2">Current Developments</a><br>5. <a href="#e66f">Future Developments</a><br>6. <a href="#fd37">Conclusion</a><br>7. <a href="#4bed">References</a></p><h3>1. Introduction</h3><p>Reinforcement Learning is not a new concept, but has been developed and matured over 70 years of academic rigour. Fundamentally, Reinforcement Learning is a method of machine learning by which an algorithm can make decisions and take actions within a given environment, and learns what appropriate decisions to make through repeated trial-and-error actions. The academic discourse for Reinforcement Learning pursued three concurrent ‘threads’ of research (trial and error, optimal control, and temporal difference), before being united in the research in the 1990’s. Reinforcement Learning was then able to proceed to mastering the playing of Chess, and of Go, and of countless electronic games. The modern applications of Reinforcement Learning are enabling businesses to optimise, control, and monitor their respective processes, to a phenomenal level of accuracy and finesse. As a result, the future of Reinforcement Learning is both exciting and fascinating, as the research aims to improve the algorithm’s interpretability, accountability and trustworthiness.</p><h3>2. Historic Developments (pre 1992)</h3><p>Sutton &amp; Barto (<a href="#2338">2018</a>) discuss the three ‘threads’ of Reinforcement Learning as being: 1) Learning by trial-and-error; 2) The problem of optimal control; and 3) Temporal difference learning methods. These threads were pursued by researchers independently before becoming intertwined in the 1980’s leading to the concept of Reinforcement Learning as we know it today.</p><h4>2.1. Concurrent Developments</h4><p><strong>2.1.1. Learning by Trial and Error</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wkZMtjhzEStBQSa1rR9Q0w.png" /><figcaption>Thorndike’s Law of Effect (<a href="https://www.researchgate.net/profile/Dyar-Jabbary/publication/329428575_Edward_L_Thorndike_Trial_and_Error_Theory/links/5c3bcfaf458515a4c7247ad8/Edward-L-Thorndike-Trial-and-Error-Theory">Image</a> by <a href="https://www.researchgate.net/profile/Dyar-Jabbary">Dyar Jabbary</a> on <a href="https://www.researchgate.net/">ResearchGate</a>)</figcaption></figure><p>When observing animal intelligence, Thorndike (<a href="#b219">1911</a>) defined trial-and-error as the ‘Law of Effect’ as associated with a feeling of satisfaction or discomfort resulting from a given situation. This concept was integrated into the Machine Learning analogue when Minsky (<a href="#f99b">1954</a>) postulated the use of SNARCs (Stochastic Neural-Analogue Reinforcement Calculators), which was further crystallised when Minsky (<a href="#acc3">1961</a>) addressed the ‘Credit Assignment Problem’; which is how to distribute credit for success among the many decisions that may have been involved in producing it. Research in computational trial-and-error processes was generalised to pattern recognition (<a href="#aa51">Clark &amp; Farley 1955</a>; <a href="#5036">Farley &amp; Clark 1954</a>) before being adapted to supervised learning by using error information to update connection weights (<a href="#b35d">Rosenblatt 1962</a>; <a href="#0b4f">Widrow &amp; Hoff 1960</a>). Due to this blurring of the distinction between traditional trial-and-error, and error-and-loss functions, there were very few publications throughout the 1960’s and 1970’s specifically addressing Reinforcement Learning. One such researcher who was continuing in this area was John Andreae who developed the STELLA system (<a href="#86da">Andreae 1963</a>), which learns through interaction with its environment, and machines with an ‘internal monologue’ (<a href="#7fac">Andreae &amp; Cashin 1969</a>), and then later the machines that can learn from a teacher (<a href="#6cee">Andreae 1977</a>). Unfortunately, as Sutton &amp; Barto (<a href="#2338">2018</a>) discuss, Andreae’s pioneering research was not well known, and did not greatly impact subsequent reinforcement learning research.</p><p><strong>2.1.2. The Problem of Optimal Control</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Mb_CPHFb3j4EiuHk.png" /><figcaption>The Problem of Optimal Control (<a href="https://miro.medium.com/max/3076/1*ZC9qItK9wI0F6BwSVYMQGg.png">Image </a>by <a href="https://pradyyadav.medium.com/">Pradyumna Yadav</a> on <a href="https://medium.com/analytics-vidhya">AnalyticsVidhya</a>)</figcaption></figure><p>The research in to ‘optimal control’ began in the 1950’s, and is defined as “a controller to minimize a measure of a dynamical system’s behaviour over time” (<a href="#2338">Sutton &amp; Barto 2018</a>). Bellman (<a href="#b611">1957a</a>) built upon the work of Hamilton (<a href="#5617">1833</a>, <a href="#4be1">1834</a>) and Jacobi (<a href="#b90b">1866</a>) to develop a method specific for Reinforcement Learning which dynamically defines a functional equation, using a dynamic system’s state, and returns an optimal value function. This “optimal return function” is now often referred to as the Bellman equation, which is technically a class of methods used for solving control problems, and is the main focus of Bellman’s book <em>Dynamic Programming</em> (<a href="#b611">Bellman 1957a</a>). Bellman went on to introduce the Markovian Decision Process (<a href="#83da">1957b</a>) — which he defines as “a discrete stochastic version of the optimal control problem” — which Howard (<a href="#1d73">1960</a>) leveraged to define a policy iteration method for Markovian Decision Processes. Through the 1960’s and 1970’s, there was not much research in the area of ‘optimal control’; however, Bryson (<a href="#0bb1">1996</a>) notes that since the 1980’s, there has been many papers published in areas such as partially observable Markovian Decision Processes, along with its applications, approximation methods, asynchronous methods, and modern treatments of dynamic programming.</p><p><strong>2.1.3. Temporal Difference Learning Methods</strong></p><p>Taking its inspiration from mathematical differentiation, temporal difference learning aims to derive a prediction from a set of known variables. But an important difference is that it does so by bootstrapping from the current estimate of the value function. It is similar to the Monde Carlo method (<a href="#10d5">Hammersley 1964</a>), but it is able to adjust the final predictions in the later iteration stages, before the final outcome is known (which Monte Carlo cannot); as illustrated by Sutton (<a href="#9cc0">1988</a>) with the example where on a given Sunday a forecast is created for the following Saturday, then updating the Saturday forecast on Friday, before the actual Saturday weather is known.</p><p>The origins of temporal difference methods are rooted in animal learning psychology (<a href="#b219">Thorndike 1911</a>), particularly the notion of ‘secondary reinforcers’. Effectively, when a secondary reinforcer (eg. a stimulus) is paired with a primary reinforcer (eg. food or pain), the secondary reinforcing adopts similar properties as the first. Minsky (<a href="#f99b">1954</a>) was the first to realise the importance of temporal difference methods for reinforcement learning, though Samuel (<a href="#161b">1959</a>) seemed to be the second to realise this importance but who did not reference Minsky in his work. Minsky (<a href="#acc3">1961</a>) built upon the work of Samuel (<a href="#161b">1959</a>) and reinforced the importance of this concept to Reinforcement Learning theories. The temporal difference method and the trial-and-error method became entangled when Klopf (<a href="#0367">1972</a>, <a href="#ede0">1975</a>) investigated reinforcement learning in large systems as conceptualised by individual sub-components of the larger system, each with their own excitatory inputs as rewards and inhibitory inputs as punishments, and each could reinforce one another.</p><h4>2.2. Combined Developments</h4><p>Leveraging Klopf’s ideas, Sutton (<a href="#0197">1978a</a>, <a href="#af3e">1978b</a>, <a href="#560b">1978c</a>, <a href="#2e0a">1984</a>) further develops the links to animal learning theory, and further explored the rules by which learning is driven by changes in temporally successive predictions. This work virtually opened the academic floodgates for research into reinforcement learning, as the years following this saw numerous influential developments such as:</p><ul><li>A psychological model of classical conditioning (Barto &amp; Sutton <a href="#7602">1981a</a>, <a href="#51ac">1981b</a>, <a href="#b8eb">1982</a>; Sutton &amp; Barto <a href="#8c25">1981a</a>, <a href="#fbb5">1981b</a>);</li><li>A neuronal model of classical conditioning (<a href="#3cb4">Klopf 1988</a>; <a href="#39d1">Tesauro 1986</a>);</li><li>A temporal-difference model of classical conditioning (Sutton &amp; Barto <a href="#b95a">1987</a>, <a href="#e4d6">1990</a>);</li><li>The ‘Actor-Critic Architecture’ as applied to the pole-balancing problem (<a href="#1330">Barto et al. 1983</a>);</li><li>The extension of ‘Actor-Critic Architecture’ and integration with backpropagation neural network techniques (<a href="#1c4b">Anderson 1986</a>; <a href="#2e0a">Sutton 1984</a>);</li><li>The separation of temporal-difference learning from the control decision and the introduction of the lambda() property (<a href="#9cc0">Sutton 1988</a>).</li></ul><p>The three ‘threads’ of Reinforcement Learning genesis were finally united when Watkins (<a href="#8279">1989</a>) developed Q-Learning (<a href="#8279">Watkins 1989</a>), which was reinforced by Werbos (<a href="#1ce7">1987</a>) who advocated for the convergence of trial-and-error learning and dynamic programming. After this, researchers explored automated electronic implementations of reinforcement learning, and was able to achieve some amazing results.</p><h3>3. Modern Developments (post 1992)</h3><h4>3.1. Developments in Board Games</h4><p>In 1992, Tesauro was able to implement these concepts in his development of the TD-Gammon program (<a href="#d705">Tesauro 1994</a>) which was able to was able to achieve ‘Master Level’ in the game of Backgammon. Research had shifted to attempt to apply this success to the game of chess (Baxter et al. <a href="#ea1a">2000</a>, <a href="#3a4a">2001</a>; <a href="#b96f">Thrun 1995</a>). IBM developed DeepBlue for the sole purpose of playing chess. However, it suffered from the curse of dimensionality, because the algorithm took too much time to calculate, it could not ‘see’ far enough in the future, and it became notorious for very poor opening moves (<a href="#c52b">Szita 2012</a>). Nevertheless, DeepBlue won against a world champion in 1997 (<a href="#e711">King 1997</a>; <a href="#433d">Levy 1997</a>; <a href="#ab52">Newborn 2000</a>; <a href="#e14f">Santo 1997</a>).</p><p>Researchers then pursued a larger and even more complicated game: Go. While many attempts were made to try and learn how to play the game (Bouzy <a href="#5d4f">2006a</a>, <a href="#4e1a">2006b</a>; <a href="#5061">Bouzy &amp; Helmstetter 2004</a>; Coulom <a href="#a9bd">2007a</a>, <a href="#e71e">2007b</a>; <a href="#6110">Dahl 1999</a>; <a href="#0eaa">Gelly &amp; Silver 2008</a>; <a href="#30eb">Gelly et al. 2006</a>; <a href="#398b">Schraudolph et al. 2001</a>; <a href="#71b9">Silver et al. 2008</a>), none were able to win against a world champion until Google’s AlphaGo took the title in 2016 (<a href="#bff9">Borowiec 2016</a>; <a href="#b94f">Moyer 2016</a>). The difference between DeepBlue and AlphaGo was that DeepBlue utilised a parallelised tree-based search methodology with customised hardware advances to effectively use ‘brute force’ to calculate all of the moves required to win the game (<a href="#bff9">Borowiec 2016</a>; <a href="#ab52">Newborn 2000</a>); but this method is not possible in Go as there are simply too many moves and possible combinations that the computational overhead would be impossible. Comparatively, AlphaGo utilised a combination of Monte Carlo simulations, Monte Carlo tree search, Bayesian optimisation, and physically watching the previous matches of world champions (<a href="#fe01">Chen et al. 2018</a>; <a href="#a0cc">Fu 2016</a>; <a href="#66bf">Wheeler 2017</a>), in order to build a strong-enough model, and without needing to perform brute-force calculations of future moves. These developments mark significant advancements in the world of Reinforcement Learning, and is enabling the entire world to see what some of the possibilities are with this particular machine learning technique.</p><h4>3.2. Developments in Computer Games</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*HeeglLFN53ExCZRi" /><figcaption>Atari Games (<a href="https://scontent-syd2-1.xx.fbcdn.net/v/t1.18169-9/14344753_357669837897331_4452619001162145046_n.jpg?_nc_cat=101&amp;ccb=1-3&amp;_nc_sid=e3f864&amp;_nc_ohc=CLFxqaOcxuQAX-Isp3w&amp;_nc_ht=scontent-syd2-1.xx&amp;oh=b1a89d5cef7db2909bf902c037c01d1c&amp;oe=60CA8BFB">image</a> by <a href="https://www.facebook.com/atari2600system">Atari 2600</a> on Facebook)</figcaption></figure><p>As children growing up in the 1980’s and 1990’s, many people played Atari video games. They were released in 1977 and contained 526 games including classics like Pong, Breakout and Space Invaders (<a href="#5cee">Wikipedia 2020</a>). The Atari emulator provided an ideal environment for a Reinforcement Learning algorithm to learn how to play the game (<a href="#9d1e">Hausknecht et al. 2014</a>; <a href="#c1dc">Kaiser et al. 2019</a>; <a href="#f4d8">Mnih et al. 2013</a>) due to its pixel-based display and simple control options. After some time playing the games, the researchers observed some very impressive techniques being used by the algorithm; for example, in Breakout, it was able to drill a hole in order to win the game with less effort. This is indicative of the power of Reinforcement Learning, particularly that it is able to learn certain rules and practices which were not otherwise programmatically told to the model (<a href="#aa00">Berges et al. nd</a>; <a href="#0db0">Patel et al. 2019</a>; <a href="#8340">TwoMinutePapers 2015</a>). Many other computer games have also seen developments with Reinforcement Learning, including games like Snake, Flappy Bird, Angry Birds, and Candy Crush. Modern-day computer games have made substantial advancements since the Atari games, and they provide a much more complex and dynamic environment which provides a multitude of learning opportunities for applications of Reinforcement Learning.</p><h3>4. Current Developments</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/571/1*J10k53qF9IAMA0jDYs-eXA.png" /><figcaption>Traffic Light Automation (<a href="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ-IXKdWEQVI7OS8KykSSsYqhskLg-AqdZ_D7FDOhUKk3WBvrnOcPdhhU66G3ElP1PV_eY&amp;usqp=CAU">image</a> by <a href="https://arxiv.org/pdf/1803.11115.pdf">Xiaoyuan et al.</a> on <a href="https://arxiv.org/">Arxiv</a>)</figcaption></figure><p>While the history of Reinforcement Learning has been fascinating, the applications of Reinforcement Learning in our modern times is really exciting. In a recent article, Garychl (<a href="#8f43">2018</a>) lists some examples of Reinforcement Learning being applied in industry today; including: resource management in computer clusters, traffic light control, robotics, web system configuration, chemistry, advertisement, and games. Lorica (<a href="#639d">2017</a>) consolidates these opportunities in to three main themes of applications, which can quite literally be applied to any business in any industry: to optimize (eg. process planning, yield management, supply chain), to control (eg. autonomous vehicles, factory automation, wind turbine control), and to monitor and maintain (eg. quality control, predictive maintenance, inventory monitoring). All of these applications provide an environment, an action, a response, and a method for optimisation, therefore allow the opportunity for reinforcement learning algorithms to be applied to these specific cases.</p><p>Quite simply, businesses should seek to implement Reinforcement Learning in to their AI strategies when they face any of the below scenarios (<a href="#aa85">Chahill 2017</a>):</p><ul><li>They are using simulations because the system or process is too complex (or too physically hazardous) for teaching machines through trial and error; or</li><li>They are dealing with large state spaces; or</li><li>They are seeking to augment human analysis and domain experts by optimizing operational efficiency and providing decision support.</li></ul><h3>5. Future Developments</h3><p>The future of Reinforcement Learning is a highly subjective discussion, and one in which many people may have quite diverging opinions. There are two main directions for the future of Reinforcement Learning, which can be generalised as follows:</p><ol><li>The future is bright, there is wide adoption and implementation of Reinforcement Learning, and it continues to have a positive impact on humanity; or</li><li>The future is grim, people revolt against the progressive infiltration of Reinforcement Learning in their lives, and the future developments of AI are focussed on techniques other than Reinforcement Learning.</li></ol><p>The phenomenal amount of advancement that has taken place in the world of Reinforcement Learning and the positive impact that it has already had on our society, indicates that this trend will continue and will continue into the future. Godbout (<a href="#2f56">2018</a>) discusses the bright future of Reinforcement Learning, while Kirschte (<a href="#7f0e">2019</a>) discusses that Reinforcement Learning will continue to make substantial advances, and will make the day-to-day lives of businesses easier and more efficient. Of the two options, this bright future is the most likely scenario.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fco19DOJvH_bfPVhV385rQ.jpeg" /><figcaption>Self Driving Cars (<a href="https://www.shutterstock.com/image-photo/young-woman-reading-book-autonomous-car-671755273">image</a> by <a href="https://www.shutterstock.com/g/chombosan">metamorworks</a> on <a href="https://www.shutterstock.com/">Shutterstock</a> under license to Chrism Mahoney)</figcaption></figure><p>However, it would be remiss not to discuss some of the societal apprehension behind the rise of AI, and some of the anxiety resulting from some Hollywood movies about AI. Frye (<a href="#2a59">2019</a>) discusses some concerning issues around AI &amp; specifically Reinforcement Learning, mentioning that Reinforcement Learning is unsafe due to task specification (difficulty in precisely specifying exactly what task the AI Agent is expected to perform), and unsafe exploration (the Agent learns from trial-and-error, implying that it must first make a mistake for it to learn what not to do), which may result in incidents and injuries (or worse) for our fellow citizens. Particularly if we consider the example of self-driving cars. Knight (<a href="#d483">2017</a>) explicitly explores this example, focussing on the Nvidia self-driving car which has learnt to drive not through any programmatic command given to it, but instead through observation of other drivers. While this is an impressive feat unto itself, has the underlying issue to that the creators do not know how or why the computer is making decisions, and this will undoubtedly put people’s lives at risk on the roads. Therefore, there are a couple of improvements which Reinforcement Learning needs to make before it will be widely accepted and adopted by the broader community. Those being in the areas of interpretability to the creators and accountability to the users. Once these two issues are rectified, then Artificial Intelligence and Reinforcement Learning will undoubtedly become more trustworthy.</p><h3>6. Conclusion</h3><p>Reinforcement Learning has come a phenomenally long way since its inception in the 1950’s; and it still has a long way to go in its pathway of development and maturity. From the theoretical and conceptual advancements made up to the 1990’s, Reinforcement Learning has conquered the games of Chess and of Go, and of countless electronic computer games. Reinforcement Learning has also begun to debut in business and in industry and is continuing to prove beneficial and useful in the ever-growing challenge of our modern society. The future of Reinforcement Learning will soon infiltrate our everyday lives in many, many different ways; but not before a few fundamental issues are rectified with its interpretability, accountability, and trustworthiness. Nevertheless, the future of Reinforcement Learning appears to be long and bright, and we will continue to see many great things from this powerful area of Artificial Intelligence.</p><h3>7. References</h3><p>Anderson, C. 1986, <em>Learning and Problem-Solving with Multilayer Connectionist Systems (Adaptive, Strategy Learning, Neural Networks, Reinforcement Learning)</em>,<em> </em>PhD thesis, University of Massachusetts Amherst.</p><p>Andreae, J. 1963, ‘Stella: A Scheme for a Learning Machine’, <em>IFAC Proceedings Volumes</em>, vol. 1, no. 2,<strong> </strong>pp. 497–502, ISSN: 1474–6670, DOI: 10.1016/S1474–6670(17)69682–4.</p><p>Andreae, J. 1977, <em>Thinking with the Teachable Machine</em>, ISBN: Academic Press, London.</p><p>Andreae, J. &amp; Cashin, P. 1969, ‘A Learning Machine with Monologue’, <em>International Journal of Man-Machine Studies</em>, vol. 1, no. 1,<strong> </strong>pp. 1–20, ISSN: 0020–7373, DOI: 10.1016/S0020–7373(69)80008–8.</p><p>Barto, A. &amp; Sutton, R. 1981a, <em>Goal Seeking Components for Adaptive Intelligence: An Initial Assessment</em>.</p><p>Barto, A. &amp; Sutton, R. 1981b, ‘Landmark Learning: An Illustration of Associative Search’, <em>Biological Cybernetics</em>, vol. 42, no. 1,<strong> </strong>pp. 1–8, ISSN: 0340–1200, DOI: 10.1007/BF00335152.</p><p>Barto, A. &amp; Sutton, R. 1982, ‘Simulation of Anticipatory Responses in Classical Conditioning by a Neuron-Like Adaptive Element’, <em>Behavioural Brain Research</em>, vol. 4, no. 3,<strong> </strong>pp. 221–35, ISSN: 0166–4328, DOI: 10.1016/0166–4328(82)90001–8.</p><p>Barto, A., Sutton, R. &amp; Anderson, C. 1983, ‘Neuronlike Adaptive Elements That Can Solve Difficult Learning Control Problems’, <em>IEEE Transactions on Systems, Man, and Cybernetics</em>, vol. SMC-13, no. 5,<strong> </strong>pp. 834–46, ISSN: 0018–9472, DOI: 10.1109/TSMC.1983.6313077.</p><p>Baxter, J., Tridgell, A. &amp; Weaver, L. 2000, ‘Learning to Play Chess Using Temporal Differences’, <em>Machine Learning</em>, vol. 40, no. 3,<strong> </strong>p. 243, ISSN: 0885–6125, DOI: 10.1023/A:1007634325138.</p><p>Baxter, J., Tridgell, A. &amp; Weaver, L. 2001, ‘Reinforcement Learning and Chess’, <em>Machines That Learn to Play Games</em>, pp. 91–116.</p><p>Bellman, R. 1957a, <em>Dynamic Programming</em>, ISBN: 069107951x, Princeton University Press.</p><p>Bellman, R. 1957b, ‘A Markovian Decision Process’, <em>Journal of Mathematics and Mechanics</em>, vol. 6, no. 5,<strong> </strong>pp. 679–84, ISSN: 00959057.</p><p>Berges, V., Rao, P. &amp; Pryzant, R. nd, ‘Reinforcement Learning for Atari Breakout’, <em>Stanford University</em>, &lt;<a href="https://cs.stanford.edu/~rpryzant/data/rl/paper.pdf">https://cs.stanford.edu/~rpryzant/data/rl/paper.pdf</a>&gt;.</p><p>Borowiec, S. 2016, <em>Alphago Seals 4–1 Victory over Go Grandmaster Lee Sedol</em>, The Guardian, viewed 31/May/2020, &lt;<a href="https://www.fbe.hku.hk/f/page/75261/Reading%201_AlphaGo%20seals%204-1%20Victory%20over%20Go%20Grandmaster%20Lee%20Sedol.pdf">https://www.fbe.hku.hk/f/page/75261/Reading%201_AlphaGo%20seals%204-1%20Victory%20over%20Go%20Grandmaster%20Lee%20Sedol.pdf</a>&gt;.</p><p>Bouzy, B. 2006a, ‘Associating Shallow and Selective Global Tree Search with Monte Carlo for Go’, <em>Computers and Games</em>, vol. 3846, pp. 67–80, DOI: 10.1007/11674399_5, Springer Berlin Heidelberg, Berlin, Heidelberg.</p><p>Bouzy, B. 2006b, ‘Move-Pruning Techniques for Monte-Carlo Go’, <em>Advances in Computer Games</em>, vol. 4250, pp. 104–19, DOI: 10.1007/11922155_8.</p><p>Bouzy, B. &amp; Helmstetter, B. 2004, ‘Monte-Carlo Go Developments’, <em>Advances in Computer Games</em>, pp. 159–74, Springer.</p><p>Bryson, A. 1996, ‘Optimal Control’, <em>IEEE Control Systems</em>, vol. 16, no. 3,<strong> </strong>pp. 26–33, ISSN: 1066–033X, DOI: 10.1109/37.506395.</p><p>Chahill, D. 2017, <em>Why Reinforcement Learning Might Be the Best Ai Technique for Complex Industrial Systems</em>, viewed 31/May/2020, &lt;<a href="https://www.bons.ai/blog/ai-reinforcement-learning-strategy-industrial-systems">https://www.bons.ai/blog/ai-reinforcement-learning-strategy-industrial-systems</a>&gt;.</p><p>Chen, Y., Huang, A., Wang, Z., Antonoglou, I., Schrittwieser, J. &amp; Silver, D. 2018, ‘Bayesian Optimization in Alphago’, <em>arXiv.org</em>, ISSN: 2331–8422, &lt;<a href="https://arxiv.org/pdf/1812.06855.pdf">https://arxiv.org/pdf/1812.06855.pdf</a>&gt;.</p><p>Clark, W. &amp; Farley, B. 1955, ‘Generalization of Pattern Recognition in a Self-Organizing System’, <em>Proceedings of the March 1–3, 1955, Western Joint Computer Conference</em>, pp. 86–91, DOI: 10.1145/1455292.1455309, &lt;<a href="https://dl.acm.org/doi/abs/10.1145/1455292.1455309">https://dl.acm.org/doi/abs/10.1145/1455292.1455309</a>&gt;.</p><p>Coulom, R. 2007a, ‘Computing Elo Ratings of Move Patterns in the Game of Go’, <em>Computer Games Workshop</em>, &lt;<a href="https://hal.inria.fr/inria-00149859/document">https://hal.inria.fr/inria-00149859/document</a>&gt;.</p><p>Coulom, R. 2007b, ‘Efficient Selectivity and Backup Operators in Monte-Carlo Tree Search’, in H. Herik, P. Ciancarini &amp; H. Donkers. (eds.), <em>Computers and Games</em>, vol. 4630, pp. 72–83, Springer.</p><p>Dahl, F. 1999, ‘Honte, a Go-Playing Program Using Neural Nets’, <em>Machines that learn to play games</em>, pp. 205–23, &lt;<a href="http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.50.2676&amp;rep=rep1&amp;type=pdf">http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.50.2676&amp;rep=rep1&amp;type=pdf</a>&gt;.</p><p>Farley, B. &amp; Clark, W. 1954, ‘Simulation of Self-Organizing Systems by Digital Computer’, <em>Transactions of the IRE Professional Group on Information Theory</em>, vol. 4, no. 4,<strong> </strong>pp. 76–84, ISSN: 2168–2690, DOI: 10.1109/TIT.1954.1057468.</p><p>Frye, C. 2019, <em>The Dangers of Reinforcement Learning in the Real World</em>, viewed 31/May/2020, &lt;<a href="https://faculty.ai/blog/the-dangers-of-reinforcement-learning-in-the-real-world/">https://faculty.ai/blog/the-dangers-of-reinforcement-learning-in-the-real-world/</a>&gt;.</p><p>Fu, M. 2016, ‘Alphago and Monte Carlo Tree Search: The Simulation Optimization Perspective’, in <em>2016 Winter Simulation Conferences</em>, IEEE, pp. 659–70, &lt;<a href="https://doi-org.ezproxy.lib.uts.edu.au/10.1109/WSC.2016.7822130">https://doi-org.ezproxy.lib.uts.edu.au/10.1109/WSC.2016.7822130</a>&gt;.</p><p>Garychl 2018, <em>Applications of Reinforcement Learning in Real World</em>, viewed 31/May/2020, &lt;<a href="https://towardsdatascience.com/applications-of-reinforcement-learning-in-real-world-1a94955bcd12">https://towardsdatascience.com/applications-of-reinforcement-learning-in-real-world-1a94955bcd12</a>&gt;.</p><p>Gelly, S. &amp; Silver, D. 2008, ‘Achieving Master Level Play in 9 X 9 Computer Go’, in <em>AAAI</em>, vol. 8, pp. 1537–40, &lt;<a href="https://www.aaai.org/Papers/AAAI/2008/AAAI08-257.pdf">https://www.aaai.org/Papers/AAAI/2008/AAAI08-257.pdf</a>&gt;.</p><p>Gelly, S., Wang, Y., Munos, R. &amp; Teytaud, O. 2006, <em>Modiﬁcation of Uct with Patterns in Monte-Carlo Go</em>, &lt;<a href="https://hal.inria.fr/inria-00117266v3/document">https://hal.inria.fr/inria-00117266v3/document</a>&gt;.</p><p>Godbout, C. 2018, <em>The Bright Future of Reinforcement Learning</em>, viewed 31/May/2020, &lt;<a href="https://medium.com/apteo/the-bright-future-of-reinforcement-learning-a66173694f88">https://medium.com/apteo/the-bright-future-of-reinforcement-learning-a66173694f88</a>&gt;.</p><p>Hamilton, W. 1833, <em>On a General Method of Expressing the Paths of Light, &amp; of the Planets, by the Coefficients of a Characteristic Function</em>, ISBN: Printed by P.D. Hardy.</p><p>Hamilton, W. 1834, <em>On the Application to Dynamics of a General Mathematical Method Previously Applied to Optics</em>, ISBN: Printed by P.D. Hardy.</p><p>Hammersley, J. 1964, <em>Monte Carlo Methods</em>, ISBN: Methuen, London.</p><p>Hausknecht, M., Lehman, J., Miikkulainen, R. &amp; Stone, P. 2014, ‘A Neuroevolution Approach to General Atari Game Playing’, <em>IEEE Transactions on Computational Intelligence and AI in Games</em>, vol. 6, no. 4,<strong> </strong>pp. 355–66, ISSN: 1943–068X, DOI: 10.1109/TCIAIG.2013.2294713.</p><p>Howard, R. 1960, ‘Dynamic Programming and Markov Processes’.</p><p>Jacobi, K. 1866, <em>Vorlesungen Über Dynamik Nebst Fünf Hinterlassenen Abhandlungen Desselben Herausgegeben Von A. Clebsch: Unter Beförderung Der Königlich Preussischen Akademie Der Wissenschaften</em>.</p><p>Kaiser, L., Babaeizadeh, M., Milos, P., Osinski, B., Campbell, R., Czechowski, K., Erhan, D., Finn, C., Kozakowski, P., Levine, S., Mohiuddin, A., Sepassi, R., Tucker, G. &amp; Michalewski, H. 2019, ‘Model-Based Reinforcement Learning for Atari’, in <em>ICLR</em>, arXiv.org, <a href="https://arxiv.org/pdf/1903.00374.pdf">https://arxiv.org/pdf/1903.00374.pdf</a>&gt;.</p><p>King, D. 1997, ‘Kasparov Vs. Deeper Blue: The Ultimate Man Vs. Machine Challenge’, <em>Machine Challenge, Trafalgar Square</em>.</p><p>Kirschte, M. 2019, <em>What to Expect from Reinforcement Learning?</em>, viewed 31/May/2020, &lt;<a href="https://towardsdatascience.com/what-to-expect-from-reinforcement-learning-a22e8c16f40c">https://towardsdatascience.com/what-to-expect-from-reinforcement-learning-a22e8c16f40c</a>&gt;.</p><p>Klopf, A. 1972, <em>Brain Function and Adaptive Systems: A Heterostatic Theory</em>.</p><p>Klopf, A. 1975, ‘A Comparison of Natural and Artificial Intelligence’, <em>ACM SIGART Bulletin</em>, no. 52,<strong> </strong>pp. 11–3, ISSN: 0163–5719, DOI: 10.1145/1045236.1045237.</p><p>Klopf, A. 1988, ‘A Neuronal Model of Classical Conditioning’, <em>Psychobiology</em>, vol. 16, no. 2,<strong> </strong>pp. 85–125, ISSN: 0889–6313, DOI: 10.3758/BF03333113.</p><p>Knight, W. 2017, <em>The Dark Secret at the Heart of Ai</em>, viewed 31/May/2020, &lt;<a href="https://www.technologyreview.com/2017/04/11/5113/the-dark-secret-at-the-heart-of-ai/">https://www.technologyreview.com/2017/04/11/5113/the-dark-secret-at-the-heart-of-ai/</a>&gt;.</p><p>Levy, S. 1997, ‘Man Vs. Machine’, <em>Newsweek</em>, vol. 129, no. 18,<strong> </strong>pp. 50–6, ISSN: 00289604.</p><p>Lorica, B. 2017, <em>Practical Applications of Reinforcement Learning in Industry: An Overview of Commercial and Industrial Applications of Reinforcement Learning</em>, viewed 31/May/2020, &lt;<a href="https://www.oreilly.com/radar/practical-applications-of-reinforcement-learning-in-industry/">https://www.oreilly.com/radar/practical-applications-of-reinforcement-learning-in-industry/</a>&gt;.</p><p>Minsky, M. 1954, <em>Theory of Neural-Analog Reinforcement Systems and Its Application to the Brain-Model Problem</em>,<em> </em>PhD thesis, Princeton University.</p><p>Minsky, M. 1961, ‘Steps toward Artificial Intelligence’, <em>Proceedings of the IRE</em>, vol. 49, no. 1,<strong> </strong>pp. 8–30, ISSN: 0096–8390, DOI: 10.1109/JRPROC.1961.287775.</p><p>Mnih, V., Kavukcuoglu, K., Silver, D., Graves, A., Antonoglou, I., Wierstra, D. &amp; Riedmiller, M. 2013, ‘Playing Atari with Deep Reinforcement Learning’, <em>ArXiv.org</em>, &lt;<a href="https://arxiv.org/pdf/1312.5602.pdf">https://arxiv.org/pdf/1312.5602.pdf</a>&gt;.</p><p>Moyer, C. 2016, <em>How Google’s Alphago Beat a Go World Champion</em>, The Atlantic, viewed 31/May/2020, &lt;<a href="https://www.theatlantic.com/technology/archive/2016/03/the-invisible-opponent/475611/">https://www.theatlantic.com/technology/archive/2016/03/the-invisible-opponent/475611/</a>&gt;.</p><p>Newborn, M. 2000, ‘Deep Blue’s Contribution to Ai’, <em>Annals of Mathematics and Artificial Intelligence</em>, vol. 28, no. 1,<strong> </strong>pp. 27–30, ISSN: 1012–2443, DOI: 10.1023/A:1018939819265.</p><p>Patel, D., Hazan, H., Saunders, D., Siegelmann, H. &amp; Kozma, R. 2019, ‘Improved Robustness of Reinforcement Learning Policies Upon Conversion to Spiking Neuronal Network Platforms Applied to Atari Breakout Game’, <em>Neural Networks</em>, vol. 120, pp. 108–15, ISSN: 0893–6080.</p><p>Rosenblatt, F. 1962, <em>Principles of Neurodynamics: Perceptrons and the Theory of Brain Mechanisms</em>, ISBN: Spartan Books, Washington, D.C.</p><p>Samuel, A. 1959, ‘Some Studies in Machine Learning Using the Game of Checkers’, <em>IBM Journal of Research and Development</em>, vol. 3, no. 3,<strong> </strong>pp. 210–29, ISSN: 0018–8646, DOI: 10.1147/rd.33.0210.</p><p>Santo, B. 1997, ‘IBM Tweaks Deep Blue for Man-Machine Rematch’, <em>Electronic Engineering Times</em>, no. 946,<strong> </strong>pp. 111–2, ISSN: 0192–1541.</p><p>Schraudolph, N., Dayan, P. &amp; Sejnowski, T. 2001, ‘Learning to Evaluate Go Positions Via Temporal Difference Methods’, <em>Computational Intelligence in Games</em>, pp. 77–98, Springer, <a href="https://snl.salk.edu/~schraudo/pubs/SchDaySej01.pdf">https://snl.salk.edu/~schraudo/pubs/SchDaySej01.pdf</a>&gt;.</p><p>Silver, D., Sutton, R. &amp; Müller, M. 2008, ‘Sample-Based Learning and Search with Permanent and Transient Memories’, in <em>International Conference on Machine Learning</em>, pp. 968–75.</p><p>Sutton, R. 1978a, <em>Learning Theory Support for a Single Channel Theory of the Brain</em>, ISBN.</p><p>Sutton, R. 1978b, ‘Single Channel Theory: A Neuronal Theory of Learning’, <em>Brain Theory Newsletter</em>, vol. 3, no. 3,<strong> </strong>pp. 72–4.</p><p>Sutton, R. 1978c, <em>A Unified Theory of Expectation in Classical and Instrumental Conditioning</em>, ISBN.</p><p>Sutton, R. 1984, <em>Temporal Credit Assignment in Reinforcement Learning</em>,<em> </em>PhD thesis, ProQuest Dissertations Publishing.</p><p>Sutton, R. 1988, ‘Learning to Predict by the Methods of Temporal Differences’, <em>Machine Learning</em>, vol. 3, no. 1,<strong> </strong>pp. 9–44, ISSN: 0885–6125, DOI: 10.1023/A:1022633531479.</p><p>Sutton, R. &amp; Barto, A. 1981a, ‘An Adaptive Network That Constructs and Uses an Internal Model of Its World’, <em>Cognition and Brain Theory</em>, vol. 4, no. 3,<strong> </strong>pp. 217–46.</p><p>Sutton, R. &amp; Barto, A. 1981b, ‘Toward a Modern Theory of Adaptive Networks: Expectation and Prediction’, <em>Psychological Review</em>, vol. 88, no. 2,<strong> </strong>pp. 135–70, ISSN: 0033–295X, DOI: 10.1037/0033–295X.88.2.135.</p><p>Sutton, R. &amp; Barto, A. 1987, ‘A Temporal-Difference Model of Classical Conditioning’, in <em>Proceedings of the Ninth Annual Conference of the Cognitive Science Society</em>, Seattle, WA, pp. 355–78.</p><p>Sutton, R. &amp; Barto, A. 1990, ‘Time-Derivative Models of Pavlovian Reinforcement’, in M. Gabriel &amp; J. Moore (eds.), <em>Learning and Computational Neuroscience: Foundations of Adaptive Networks</em>, pp. 497–537, The MIT Press, Cambridge, MA.</p><p>Sutton, R. &amp; Barto, A. 2018, <em>Reinforcement Learning: An Introduction</em>, Second edn., ISBN: 9780262039246, The MIT Press, Cambridge, Massachusetts, &lt;<a href="https://web.stanford.edu/class/psych209/Readings/SuttonBartoIPRLBook2ndEd.pdf">https://web.stanford.edu/class/psych209/Readings/SuttonBartoIPRLBook2ndEd.pdf</a>&gt;.</p><p>Szita, I. 2012, ‘Reinforcement Learning in Games’, in M. Wiering &amp; M. van Otterlo (eds.), <em>Reinforcement Learning: State-of-the-Art</em>, pp. 539–77, DOI: 10.1007/978–3–642–27645–3_17, Springer Berlin Heidelberg, Berlin, Heidelberg, &lt;<a href="https://doi.org/10.1007/978-3-642-27645-3_17">https://doi.org/10.1007/978-3-642-27645-3_17</a>&gt;.</p><p>Tesauro, G. 1986, ‘Simple Neural Models of Classical Conditioning’, <em>Biological Cybernetics</em>, vol. 55, no. 2–3,<strong> </strong>pp. 187–200, ISSN: 0340–1200, DOI: 10.1007/BF00341933.</p><p>Tesauro, G. 1994, ‘Td-Gammon, a Self-Teaching Backgammon Program, Achieves Master-Level Play’, <em>Neural computation</em>, vol. 6, no. 2,<strong> </strong>pp. 215–9, ISSN: 0899–7667.</p><p>Thorndike, E. 1911, <em>Animal Intelligence: Experimental Studies</em>, ISBN: 9780765804822, The Macmillan Company.</p><p>Thrun, S. 1995, ‘Learning to Play the Game of Chess’, in <em>Advances in Neural Information Processing Systems</em>, pp. 1069–76, &lt;<a href="http://papers.neurips.cc/paper/1007-learning-to-play-the-game-of-chess.pdf">http://papers.neurips.cc/paper/1007-learning-to-play-the-game-of-chess.pdf</a>&gt;.</p><p>TwoMinutePapers 2015, <em>Google Deepmind’s Deep Q-Learning Playing Atari Breakout</em>, viewed 31/May/2020, &lt;<a href="https://www.youtube.com/watch?v=V1eYniJ0Rnk">https://www.youtube.com/watch?v=V1eYniJ0Rnk</a>&gt;.</p><p>Watkins, C. 1989, <em>Learning from Delayed Rewards</em>,<em> </em>PhD thesis, <a href="https://www.researchgate.net/publication/33784417_Learning_From_Delayed_Rewards">https://www.researchgate.net/publication/33784417_Learning_From_Delayed_Rewards</a>&gt;.</p><p>Werbos, P. 1987, ‘Building and Understanding Adaptive Systems: A Statistical/Numerical Approach to Factory Automation and Brain Research’, <em>IEEE Transactions on Systems, Man, and Cybernetics</em>, vol. 17, no. 1,<strong> </strong>pp. 7–20, ISSN: 0018–9472, DOI: 10.1109/TSMC.1987.289329, &lt;<a href="https://www.aaai.org/Papers/Symposia/Fall/1993/FS-93-02/FS93-02-003.pdf">https://www.aaai.org/Papers/Symposia/Fall/1993/FS-93-02/FS93-02-003.pdf</a>&gt;.</p><p>Wheeler, T. 2017, <em>Alphago Zero — How and Why It Works</em>, viewed 31/May/2020, &lt;<a href="http://tim.hibal.org/blog/alpha-zero-how-and-why-it-works/">http://tim.hibal.org/blog/alpha-zero-how-and-why-it-works/</a>&gt;.</p><p>Widrow, B. &amp; Hoff, M. 1960, ‘Adaptive Switching Circuits’, <em>1960 Wescon Convention Record Part Iv</em>, pp. 94–104, MIT Press, Cambridge, MA.</p><p>Wikipedia 2020, <em>Atari 2600</em>, viewed 31/May/2020, &lt;<a href="https://en.wikipedia.org/wiki/Atari_2600">https://en.wikipedia.org/wiki/Atari_2600</a>&gt;.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fda8ff535bb6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/reinforcement-learning-fda8ff535bb6">Reinforcement Learning</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[I like it  Good work!]]></title>
            <link>https://chrimaho.medium.com/i-like-it-good-work-556f26bdbef?source=rss-56d03114dd5a------2</link>
            <guid isPermaLink="false">https://medium.com/p/556f26bdbef</guid>
            <dc:creator><![CDATA[Chris Mahoney]]></dc:creator>
            <pubDate>Wed, 02 Jun 2021 11:50:51 GMT</pubDate>
            <atom:updated>2021-06-02T11:50:51.367Z</atom:updated>
            <content:encoded><![CDATA[<p>I like it 😊 Good work!</p><p>Perhaps a little philosophical at times. But still a nice Blog. Made me laugh quite a few times!</p><p>If you do choose to publish, I encourage you to remove the parts about &#39;deadline&#39; and &#39;assessment&#39; etc. Mr Joe Public doesn&#39;t need to know about those bits.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=556f26bdbef" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[I like it  Great work!]]></title>
            <link>https://chrimaho.medium.com/i-like-it-great-work-ace83be59d17?source=rss-56d03114dd5a------2</link>
            <guid isPermaLink="false">https://medium.com/p/ace83be59d17</guid>
            <dc:creator><![CDATA[Chris Mahoney]]></dc:creator>
            <pubDate>Wed, 02 Jun 2021 11:37:04 GMT</pubDate>
            <atom:updated>2021-06-02T11:37:04.384Z</atom:updated>
            <content:encoded><![CDATA[<p>I like it 😊 Great work!</p><p>Try adding some headings 😉 It&#39;ll help guide the reader along.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ace83be59d17" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Vanilla Neural Networks in R]]></title>
            <link>https://medium.com/data-science/vanilla-neural-networks-in-r-43b028f415?source=rss-56d03114dd5a------2</link>
            <guid isPermaLink="false">https://medium.com/p/43b028f415</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[neural-network-software]]></category>
            <category><![CDATA[r]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Chris Mahoney]]></dc:creator>
            <pubDate>Fri, 06 Nov 2020 17:30:11 GMT</pubDate>
            <atom:updated>2020-11-06T17:30:11.791Z</atom:updated>
            <content:encoded><![CDATA[<h4>Take a Look Under The Hood of Neural Network Architecture: Design and Build a Neural Network, from Scratch, in <strong>R</strong>, without using any Deep Learning Frameworks or Packages</h4><p>Special thanks to: <a href="https://www.linkedin.com/in/alexjscriven/">Alex Scriven</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/791/0*CXpXUW-LyfJEA7Fg" /><figcaption>Image source: <a href="https://github.com/karanvivekbhargava/vanilla-neural-network">GitHub</a></figcaption></figure><h3>Contents:</h3><p>1. <a href="#b4fb">Introduction</a><br>2. <a href="#fce7">Background</a><br>3. <a href="#9e09">Semantics</a><br>4. <a href="#7ed5">Set Up</a><br>5. <a href="#c655">Get Data</a><br>6. <a href="#556b">Check Data</a><br>7. <a href="http://266e/">Prepare the Data</a><br>8. <a href="#6a04">Instantiate the Network</a><br>9. <a href="#73a7">Initialise the Network</a><br>10. <a href="#fa55">Forward Propagation</a><br>11. <a href="#485a">Calculate the Cost</a><br>12. <a href="#1c42">Backward Propagation</a><br>13. <a href="#5208">Update Model Parameters</a><br>14. <a href="#34e4">Run the Model End-to-End</a><br>15. <a href="#4ed9">Create Prediction</a><br>16. <a href="#9a35">Conclusion</a><br>17. <a href="#cae5">Post Script</a></p><h3>1. Introduction</h3><p>Modern-day Data Science techniques frequently use robust frameworks for designing and building machine learning solutions. In the <a href="https://www.r-project.org/">R</a> community, packages such as the <a href="https://www.tidyverse.org/">tidyverse</a> and the <a href="http://caret.r-forge.r-project.org/">caret</a> packages are frequently referenced; and within <a href="http://127.0.0.1:17287/rmd_output/0/">Python</a>, packages such as <a href="https://numpy.org/">numpy</a>, <a href="https://pandas.pydata.org/">pandas</a>, <a href="https://scikit-learn.org/">sci-kit learn</a> are frequently referenced. There are even some packages that have been built to be used in either language, such as <a href="https://keras.io/">keras</a>, <a href="https://pytorch.org/">pytorch</a>, <a href="https://www.tensorflow.org/">tensorflow</a>. However, the limitation of using these packages is the ‘<em>blackbox</em>’<em> </em>phenomenon, where users do not understand what is happening behind-the-scenes (or ‘under the hood’, if you will). Users know how to use the functions, and can interpret the results, but don’t necessarily know how the packages were able to achieve the results.</p><p>The purpose of this paper is to create a ‘<em>back to basics</em>’ approach to designing Deep Learning solutions. The intention is not to create the most predictive model, nor is it to use the latest and greatest techniques (such as <a href="https://en.wikipedia.org/wiki/Convolutional_neural_network">convolution </a>or <a href="https://en.wikipedia.org/wiki/Recurrent_neural_network">recursion</a>); but the intention is to create a <em>basic </em>neural network, from <em>scratch</em>, using <em>no </em>frameworks, and to <em>walk through</em> the methodology.</p><p><strong>Note</strong>: The word ‘<em>Vanilla</em>’ in ‘Vanilla Neural Networks’ simply refers to the fact it is built from scratch, and does not use any pre-existing frameworks in its construction.</p><h3>2. Background</h3><h4>2.1. Context</h4><p>There are already many websites and blogs which explain how this process is done. Such as <a href="https://machinelearningmastery.com/about/">Jason Brownlee</a>’s article <a href="https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/">How to Code a Neural Network with Backpropagation In Python (from scratch)</a>, and <a href="https://notebooks.azure.com/goldengrape/projects/deeplearning-ai">DeepLearning.ai</a>’s notebook <a href="https://notebooks.azure.com/goldengrape/projects/deeplearning-ai/html/COURSE%201%20Neural%20Networks%20and%20Deep%20Learning/Week%204/Deep%20Neural%20Network%20Application_%20Image%20Classification/dnn_app_utils_v2.py">dnn_app_utils_v2.py</a> (on the <a href="https://notebooks.azure.com/">Microsoft Azure Notebooks</a> network). However, these sources are all written in Python. Which is fine, if that’s what is needed, and there are some very legitimate reasons to use Python over other languages. But this paper will be written in R.</p><p>The R language was chosen for two reasons:</p><ol><li>I am familiar with the language. I can speak Python (along with other languages); I just chose R to show how it can be achieved using this language.</li><li>To prove that there are many different ways to achieve the same outcome. So, while there are sometimes legitimate constraints for choosing one language over another (business legacy, technological availability, system performance, etc), sometimes one language is chosen simply because it is stylistically more preferable.</li></ol><p>Therefore, let’s see how to architect and construct a Vanilla Neural Network in R.</p><h4>2.2. What It’s Not</h4><p>This article does not cover the <em>latest </em>and <em>greatest </em>Deep Learning architectures (like <a href="https://www.tensorflow.org/tutorials/images/cnn">Convolution</a> or <a href="https://www.tensorflow.org/guide/keras/rnn">Recursion</a>). As such the final performance may not be as good as it <em>could </em>be, if these other architectures were used.</p><p>This article does not teach readers about the theoretical mathematical concepts behind how Neural Networks operate. There are plenty of other lectures which teach this information (eg. <a href="https://www.youtube.com/watch?v=RiqWATOoos8">The Math Behind Neural Networks</a>). In fact, this article assumes a lot of knowledge from the Reader about programming, about calculus, and about the basics behind what a Neural Network conceptually is.</p><p>This article does not cover <em>why </em>Neural Networks work the way they do, and the conceptual understanding behind a Feed-Forward architecture. There are plenty of other blogs (eg. <a href="https://medium.com/@purnasaigudikandula/a-beginner-intro-to-neural-networks-543267bda3c8">A Beginner Intro to Neural Networks</a>) and videos (eg. <a href="https://www.youtube.com/playlist?list=PLZHQObOWTQDNU6R1_67000Dx_ZCJB-3pi">Neural Networks series</a>) which cover such information.</p><p>This article doesn’t<em> </em>point the reader to other packages and applications which may already have this information set up and working. Packages like <a href="https://www.rdocumentation.org/packages/tensorflow">tensorflow</a> and <a href="https://www.rdocumentation.org/packages/nnet">nnet</a> already have this covered.</p><p>What this article <em>actually </em>is, is a functional walk-through, how-to piece, for creating a Vanilla Neural Network (a Feed-Forward Network), from scratch, step-by-step, in the R programming language. It contains lots of code, and lots of technical details.</p><h3>3. Semantics</h3><h4>3.1. Layout</h4><p>This article is laid our in such a way to describe how a Neural Network is built from the ground-up. It will walk through the steps to:</p><ol><li>Access and check the data</li><li>Instantiate and Initialise the network</li><li>Run forward propagation</li><li>Compute the cost</li><li>Run backward propagation</li><li>Update the model</li><li>Set up a training method to loop through every thing</li><li>Predict and assess the performance</li></ol><p>In the interest of brevity, the functions defined here will not include all the commentary and validations that should be included in a typical function. They will only include basic steps and prompts. However, the source code for this article (located <a href="https://github.com/chrimaho/VanillaNeuralNetworksInR/blob/master/Functions/functions.R">here</a>) does contain all the appropriate function docstrings and assertions.</p><h4>3.2. Syntax</h4><p>For the most part, the syntax in this article is kept to the <a href="https://www.rdocumentation.org/packages/dplyr">dplyr</a> ‘pipe’ method (which uses the %&gt;% symbol). However, in certain sections the R <a href="https://www.rdocumentation.org/packages/base">base</a> syntax is used (for example, in function declaration lines).</p><p>Throughout the article, many custom functions are written. Each of these are prefixed with the words get, let and set. The definitions of each are given below.</p><ul><li>get_*():<br> — It will get certain attributes of meta-data from the objects which are parsed to this function.<br> — Or will use the information parsed to this function to derive and get other values or parameters.</li><li>set_*():<br> — It will set (or ‘update’) the objects which are parsed to this function.<br> — Is usually used for updating the network during forward and backward propagation processes.</li><li>let_*():<br> — Similar to get, in that it takes other values parsed to this function to derive an outcome, however it will let this value be utilised by another object or function.<br> — Used mainly for the initialisation and activation functions.</li></ul><h3>4. Set Up</h3><h4>4.1. Load Packages</h4><p>The first step is to import the relevant packages. This list includes the the main packages used throughout this process; and the main purpose of which is also listed.</p><p>Note what is listed above about not using existing Deep Learning packages, and yet the tensorflow package is included. Why? Well, this is used for only accessing the data, which will be covered in the next section. The tensorflow package is not used for building and training any networks.</p><pre>library(tensorflow)  #&lt;-- Only used for getting the data<br>library(tidyverse)   #&lt;-- Used for accessing various tools<br>library(magrittr)    #&lt;-- Extends the `dplyr` syntax<br>library(grDevices)   #&lt;-- For plotting the images<br>library(assertthat)  #&lt;-- Function assertions<br>library(roxygen2)    #&lt;-- Documentation is important<br>library(caret)       #&lt;-- Doing data partitioning<br>library(stringi)     #&lt;-- Some string manipulation parts<br>library(DescTools)   #&lt;-- To properly check `is.integer`<br>library(tictoc)      #&lt;-- Time how long different processes take<br>library(docstring)   #&lt;-- Makes viewing the documentation easier<br>library(roperators)  #&lt;-- Conveniently using functions like %+=%<br>library(plotROC)     #&lt;-- For plotting predictions</pre><h3>5. Get Data</h3><h4>5.1. Download Data</h4><p>The dataset to be used is the <a href="https://www.cs.toronto.edu/~kriz/cifar.html">CIFAR-10</a> dataset. It’s chosen for a number of reasons, including:</p><ol><li>The data is on images, which is ideal for Deep Learning purposes;</li><li>There are a decent number of images included (60,000 images in total);</li><li>All images are the same the same size (32x32 pixels);</li><li>The images have been categorised in to 10 different classes; and</li><li>It’s easily accessible through the TensorFlow package.</li></ol><p>The following code chunk has the following process steps:</p><ol><li>Get the data<br> — In order to import the date, it is accessed through the keras element, which contains the suite of datasets, including the cifar10 part.<br> — The load_data() function retrieves the data from the online GitHub repository.</li><li>Extract the second element<br> — The load_package() here returns two different objects:<br> — — 1. The train dataset (containing 50,000 images);<br> — — 2. The Test dataset (containing 10,000 images).<br> — The second element is extracted (by using the extract2(2) function) because only 10,000 images are needed for these purposes.<br> — This article is to show the <em>process </em>of creating Vanilla Neural Networks; if more data is needed at a later stage, it can easily be accessed here.</li><li>Name the parts<br> — The data as downloaded contains two further elements:<br> — — 1. The images themselves (in the form of a 4-Dimensional array);<br> — — 2. The image labels (in the form of a 2-Dimensional, single column array).<br> — This data does not have any names, so therefore the names are set by using the set_names() function.</li></ol><pre># Download Data<br># NOTE:<br># - The first time you run this function, it download everything.<br># - Next time you run it, TensorFlow will load from Cache.<br>cifar &lt;- tf$keras$datasets$cifar10$load_data() %&gt;% <br>    extract2(2) %&gt;% <br>    set_names(c(&quot;images&quot;,&quot;classes&quot;))</pre><h4>5.2. Get Class Definitions</h4><p>One of the challenges behind accessing this data from the TensorFlow package is that the classes are only numeric values (0 to 9) for each type of image. The definitions for these images can be found on GitHub (<a href="https://github.com/EN10/CIFAR#classes">GitHub &gt; EN10 &gt; CIFAR</a>). These classes are defined in the following code chunk.</p><pre># Define classes<br>ClassList &lt;- c(<br>    &quot;0&quot; = &quot;airplane&quot;,<br>    &quot;1&quot; = &quot;automobile&quot;,<br>    &quot;2&quot; = &quot;bird&quot;,<br>    &quot;3&quot; = &quot;cat&quot;,<br>    &quot;4&quot; = &quot;deer&quot;,<br>    &quot;5&quot; = &quot;dog&quot;,<br>    &quot;6&quot; = &quot;frog&quot;,<br>    &quot;7&quot; = &quot;horse&quot;,<br>    &quot;8&quot; = &quot;ship&quot;,<br>    &quot;9&quot; = &quot;truck&quot; <br>)</pre><h3>6. Check Data</h3><h4>6.1. Check Objects</h4><p>It is important to check the data, to ensure that it has been generated correctly, and all the information looks okay. For this, a custom-function is written (get_ObjectAttributes()), the source code for which can be found <a href="https://github.com/chrimaho/VanillaNeuralNetworksInR/blob/master/Functions/functions.R">here</a>. As seen by the following code chunk, the images object is a 4-Dimensional numeric array, with 10,000 images, each 32x32 pixels, and 3 colour chanels. The entire object is over 117 Mb large.</p><pre># Check Images<br>cifar %&gt;% <br>    extract2(&quot;images&quot;) %&gt;% <br>    get_ObjectAttributes(&quot;cifar$images&quot;) %&gt;% <br>    cat()</pre><p>Which prints:</p><pre>Name : cifar$images<br> - Size : 117.2 Mb<br> - Clas : array<br> - Type : integer<br> - Mode : numeric<br> - Dims : 10000x32x32x3</pre><p>When checking the classes object, it is a 2-Dimensional numeric array (with only 1 column), but with the same number of images as the images object (which is to be expected), with the frequency of each class label having exactly 1000 images each. The total size is less than 40 Kb.</p><pre># Check classes<br>cifar %&gt;% <br>    extract2(&quot;classes&quot;) %&gt;% <br>    get_ObjectAttributes(name=&quot;cifar$classes&quot;, print_freq=TRUE) %&gt;% <br>    cat()</pre><p>Which prints:</p><pre>Name : cifar$classes<br> - Size : 39.3 Kb<br> - Clas : matrix,array<br> - Type : integer<br> - Mode : numeric<br> - Dims : 10000x1<br> - Freq :<br>      label Freq<br>   1  0     1000<br>   2  1     1000<br>   3  2     1000<br>   4  3     1000<br>   5  4     1000<br>   6  5     1000<br>   7  6     1000<br>   8  7     1000<br>   9  8     1000<br>   10 9     1000</pre><h4>6.2. Check Images</h4><p>After having gained an appreciation of the size of the objects in memory, it is then worth while to check the actual images themselves. As humans, we understand the actual images and the colours, better than we understand the numbers.</p><p>In order to visualise the images, two custom functions are written, as shown in the following code chunk. These functions take in the data (as a 4-Dimensional array), and visualise the images as a plot.</p><pre>set_MakeImage &lt;- function(image, index=1) {<br>    <br>    # Extract elements<br>    image.r &lt;- image[,,1]<br>    image.g &lt;- image[,,2]<br>    image.b &lt;- image[,,3]<br>    <br>    # Make rgb<br>    image.rgb &lt;- rgb(<br>        image.r, <br>        image.g, <br>        image.b, <br>        maxColorValue=255<br>    )<br>        <br>    # Fix dimensions<br>    dim(image.rgb) &lt;- dim(image.r)<br>    <br>    # Return<br>    return(image.rgb)<br>    <br>}</pre><pre>plt_PlotImage &lt;- function(images, classes, class_list, index=1) {<br>    <br>    # Slice images<br>    image &lt;- images[index,,,]<br>    image %&lt;&gt;% set_MakeImage(index)<br>    lbl &lt;- classes %&gt;% <br>        extract(index) %&gt;% <br>        as.character() %&gt;% <br>        class_list[[.]]<br>    <br>    # Create plot<br>    plot &lt;- ggplot() + <br>        ggtitle(lbl) +<br>        draw_image(image, interpolate=FALSE)<br>    <br>    # Return<br>    return(plot)<br>    <br>}</pre><p>When the function is run on the top 16 images, the following is displayed. As shown, the images are extremely pixelated (which is expected, as they are only 32x32 pixels each), and you can see how each of the images are categorised.</p><pre># Set list<br>lst &lt;- list()</pre><pre># Loop 16 images<br>for (index in 1:16) {<br>    lst[[index]] &lt;- plt_PlotImage(<br>        cifar$images, <br>        cifar$classes, <br>        ClassList,<br>        index)<br>    }</pre><pre># View images<br>plt &lt;- gridExtra::grid.arrange(grobs=lst, ncol=4)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7GvXjGdk2x_YTm_buQRL8A.jpeg" /><figcaption><strong>Figure 1</strong>: Initial Images</figcaption></figure><h3>7. Prepare the Data</h3><p>There are four steps to preparing the data:</p><ol><li>Reclassify</li><li>Split</li><li>Reshape</li><li>Standardise</li></ol><h4>7.1. Reclassify</h4><p>For the purposes of this paper, let’s assume that we are trying to predict whether or not the picture is a car or not. This will require transforming the data in to a binary classification problem, where the Neural Network will be looking to predict a 1 or a 0 from the data. This will mean that the model output will be a probability distribution of scores, which can be easily classified by changing the cutoff variable.</p><p>First step is to reclassify the data so that all the cars have the value 1, and everything else is 0. We know from the classes we defined before that the car’s already have the value 1, which means the transformation will only need to occur for all the other classes.</p><pre># Implement within a pipe<br>cifar[[&quot;classes&quot;]] &lt;- cifar %&gt;%<br>    extract2(&quot;classes&quot;) %&gt;% <br>    (function(classes){<br>        <br>        # View initial classes<br>        classes %&gt;% as.vector %&gt;% head(40) %&gt;% print<br>        <br>        # Reclassify<br>        classes &lt;- ifelse(classes==1,1,0)<br>        <br>        # View reclassified classes<br>        classes %&gt;% as.vector %&gt;% head(40) %&gt;% print<br>        <br>        # Return<br>        return(classes)<br>    })</pre><p>Which prints:</p><pre>[1] 3 8 8 0 6 6 1 6 3 1 0 9 5 7 9 8 5 7 8 6 7 0 4 9 5 2 4 0 9 6<br>[1] 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0</pre><h4>7.2. Split Data</h4><p>The next task is to split the data in to a training and testing set. The reason for doing this is covered elsewhere (such sites such as <a href="https://en.wikipedia.org/wiki/Cross-validation_%28statistics%29">Wikipedia’s Cross-Validation</a>, and <a href="https://developers.google.com/machine-learning/crash-course/training-and-test-sets/splitting-data">Google’s Machine Learning Crash Course: Training and Test Sets Splitting Data</a>).</p><p>Firstly, to get an appreciation of the current split of the data, the following code chunk visualises this data using the ggplot2 package. As show, the data is currently distributed with 90% with the class 0, and the remaining 10% with the class 1.</p><pre># Print Plot<br>cifar %&gt;% <br>    extract(&quot;classes&quot;) %&gt;% <br>    table(dnn=&quot;classes&quot;) %&gt;% <br>    data.frame() %&gt;% <br>    ggplot(aes(classes, Freq, fill=classes)) + <br>        geom_col(colour=&quot;black&quot;) +<br>        geom_label(<br>            aes(label=Freq),<br>            show.legend=FALSE<br>        ) +<br>        scale_y_continuous(breaks=seq(0,10000,1000)) +<br>        theme(panel.grid.minor.y=element_blank()) +<br>        labs(<br>            title=&quot;Count of Each Class&quot;<br>        )</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*89nGze_b10rr-k0u_NzuUg.jpeg" /><figcaption><strong>Figure 2</strong>: Count of Each Class</figcaption></figure><p>To implement this data splitting, we are using the caret::createDataPartition() function. This creates a partition object, which is then used to segregate the cifar data accordingly. The proportion of splitting was arbitrarily chosen at 70% for training, and the remaining for testing. However, it could be justified for this to be 80%; this a is hyperparameter which can be tweaked at a later stage.</p><pre># Set seed for reproducibility<br>set.seed(1234)</pre><pre># Create partition<br>partition &lt;- createDataPartition(cifar$classes, p=0.7, list=FALSE)</pre><pre># Split data<br>trn_img &lt;- cifar$images[partition,,,]<br>tst_img &lt;- cifar$images[-partition,,,]<br>trn_cls &lt;- cifar$classes[partition]<br>tst_cls &lt;- cifar$classes[-partition]</pre><p>After splitting, the data is re-plotted, and the train/test split is easily seen to have achieved an even 70% distribution over the two classes.</p><pre># Print Plot<br>rbind(<br>    trn_cls %&gt;% table,<br>    tst_cls %&gt;% table<br>) %&gt;% <br>    set_rownames(c(&quot;train&quot;,&quot;test&quot;)) %&gt;% <br>    data.frame() %&gt;% <br>    rename_all(str_remove_all, &quot;X&quot;) %&gt;% <br>    rownames_to_column(&quot;data&quot;) %&gt;% <br>    pivot_longer(2:3, names_to=&quot;classes&quot;, values_to=&quot;Freq&quot;) %&gt;% <br>    mutate(<br>        label=paste(data, classes, sep=&quot;: &quot;),<br>        data=factor(data, levels=c(&quot;train&quot;,&quot;test&quot;))<br>    ) %&gt;% <br>    ggplot(aes(classes, Freq, fill=data), position=&quot;dodge&quot;) + <br>        geom_col(colour=&quot;black&quot;, position=&quot;dodge&quot;) +<br>        geom_label(<br>            aes(label=Freq), <br>            position=position_dodge(width=0.9),<br>            show.legend=FALSE<br>        ) +<br>        scale_y_continuous(breaks=seq(0,10000,1000)) +<br>        theme(panel.grid.minor.y=element_blank()) +<br>        labs(<br>            title=&quot;Count of Each Class&quot;,<br>            subtitle=&quot;Split by Train/Test&quot;<br>        )</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8uYX6CZn_1eNmyccZSgrjQ.jpeg" /><figcaption><strong>Figure 3</strong>: Count of Each Class, Split by Train/Test</figcaption></figure><p>Another method for checking that the data has been split properly, is to again run the get_ObjectAttributes() function, as shown in the following code chunk. The information shown here is consistent with the plot above. It is also interesting to note that the Training Image array is 82 Mb large, which will be important to know for later checking the performance of forward propagation.</p><pre>for (name in c(&quot;trn_img&quot;,&quot;tst_img&quot;,&quot;trn_cls&quot;,&quot;tst_cls&quot;)) {<br>    name %&gt;% <br>        get() %&gt;% <br>        get_ObjectAttributes(<br>            name, <br>            if (name %in% c(&quot;trn_cls&quot;,&quot;tst_cls&quot;)) TRUE else FALSE<br>        ) %&gt;% <br>        cat()<br>    if (name != &quot;tst_cls&quot;) cat(&quot;\n&quot;)<br>}</pre><p>Which prints:</p><pre>Name : trn_img<br> - Size : 82 Mb<br> - Clas : array<br> - Type : integer<br> - Mode : numeric<br> - Dims : 7000x32x32x3</pre><pre>Name : tst_img<br> - Size : 35.2 Mb<br> - Clas : array<br> - Type : integer<br> - Mode : numeric<br> - Dims : 3000x32x32x3</pre><pre>Name : trn_cls<br> - Size : 54.7 Kb<br> - Clas : numeric<br> - Type : double<br> - Mode : numeric<br> - Dims : 7000<br> - Freq :<br>     label Freq<br>   1 0     6309<br>   2 1      691</pre><pre>Name : tst_cls<br> - Size : 23.5 Kb<br> - Clas : numeric<br> - Type : double<br> - Mode : numeric<br> - Dims : 3000<br> - Freq :<br>     label Freq<br>   1 0     2691<br>   2 1      309</pre><h4>7.3. Reshape Data</h4><p>For the first input layer of our Neural Network, we want to be a single dimension of nodes. Therefore, it is necessary to reshape the data from a 4-Dimensional array in to a 2-Dimensional array. This process is called Flattening, and more information can be found here: <a href="https://eli.thegreenplace.net/2015/memory-layout-of-multi-dimensional-arrays">Memory Layout of Multi-Dimensional Arrays</a>.</p><p>The implementation of this method can be done quite easily with the array() function, as it has the dim= argument which can be used to specify the dimensions required.</p><p>The desired matrix size should have each <em>image </em>in a new row, and each <em>pixel </em>on a different column. Because each <em>pixel </em>is comprised of the 2nd, 3rd and 4th dimensions, we need to take the <em>product </em>of these three numbers, and use that to specify the desired number of columns. Effectively, we are running the equation: 32×32×3, which equates to having 3072 columns. This equation is implemented programmatically in-line, in the next code chunk.</p><pre># Reshape data<br>trn_img %&lt;&gt;% array(dim=c(<br>    dim(.) %&gt;% extract(1),<br>    dim(.) %&gt;% extract(2:4) %&gt;% prod()<br>))</pre><pre>tst_img %&lt;&gt;% array(dim=c(<br>    dim(.) %&gt;% extract(1),<br>    dim(.) %&gt;% extract(2:4) %&gt;% prod()<br>))</pre><pre>trn_cls %&lt;&gt;% array(dim=c(<br>    length(.),<br>    1<br>))</pre><pre>tst_cls %&lt;&gt;% array(dim=c(<br>    length(.),<br>    1<br>))</pre><p>When checking the object attributes once again, you will see that the image data has been manipulated correctly to have the number of rows as the number of images, and the number of columns as the number of pixels.</p><pre>for (name in c(&quot;trn_img&quot;,&quot;tst_img&quot;,&quot;trn_cls&quot;,&quot;tst_cls&quot;)) {<br>    name %&gt;% <br>        get() %&gt;% <br>        get_ObjectAttributes(name, FALSE) %&gt;% <br>        cat()<br>    if (name != &quot;tst_cls&quot;) cat(&quot;\n&quot;)<br>}</pre><p>Which prints:</p><pre>Name : trn_img<br> - Size : 82 Mb<br> - Clas : matrix,array<br> - Type : integer<br> - Mode : numeric<br> - Dims : 7000x3072</pre><pre>Name : tst_img<br> - Size : 35.2 Mb<br> - Clas : matrix,array<br> - Type : integer<br> - Mode : numeric<br> - Dims : 3000x3072</pre><pre>Name : trn_cls<br> - Size : 54.9 Kb<br> - Clas : matrix,array<br> - Type : double<br> - Mode : numeric<br> - Dims : 7000x1</pre><pre>Name : tst_cls<br> - Size : 23.6 Kb<br> - Clas : matrix,array<br> - Type : double<br> - Mode : numeric<br> - Dims : 3000x1</pre><h4>7.4. Standardise Data</h4><p>The final step to preparing the data is to standardise the data so that all the elements are between 0 and 1. The reason for this is to prevent exploding and vanishing gradients in later steps, as the Neural Network will try to fit to all the peaks and troughs which is caused from having the data in a 0 to 255 value range.</p><p>As documented on the <a href="https://www.tensorflow.org/api_docs/python/tf/keras/datasets/cifar10/load_data">TensorFlow</a> website, the <a href="https://www.cs.toronto.edu/~kriz/cifar.html">CIFAR10</a> data set consists of RGB Image data. And, as documented on <a href="https://en.wikipedia.org/wiki/RGB_color_model">Wikipedia</a>, RGB data are all values between 0 and 255.</p><p>Therefore, what is necessary to do is to divide all the elements by 255, and they will inevitably result in a value between 0 and 1. As the image data is currently in an array, the function in the below code chunk will run as a <em>vectorised function</em> over the entire array, dividing all elements by 255 accordingly.</p><pre>trn_img &lt;- trn_img/255<br>tst_img &lt;- tst_img/255</pre><p>The data is now ready for the network. Next step is to build the network.</p><h3>8. Instantiate the Network</h3><h4>8.1. Define the Architecture</h4><p>Some quick notes on what the Network will <em>actually </em>be:</p><ul><li>Overall architecture is a list.</li><li>Each element of the master list is another list, and these are going to form each of the <em>layers </em>of the overall <em>network</em>.</li><li>The first layer will always be the input layer.</li><li>The last layer will always be the output layer.</li><li>Each of the layers in between will be the hidden layers, and the names for these layers are simply named with numbers.</li><li>Each element of each layer will all be labelled the same, defined as follows:</li><li>— nodz : Number of nodes in this layer.</li><li>— inpt : Input matrix. Aka A_prev. This is a duplicate of the activation of the previous layer, so for large networks this needs to be taken in to consideration.</li><li>— wgts : Weights matrix. Aka W.</li><li>— bias : Bias vector. Aka b.</li><li>— linr : Linear matrix. Aka Z. This is the result of the linear algebra between inpt, wgts and bias.</li><li>— acti : Activation matrix. Aka A. The result of applying an activation function to the linr matrix.</li><li>— acti_func : The activation function used.</li><li>— cost : The overall cost of the model. This is a single value (the overall cost of the model), but is copied to each layer of the model.</li><li>— back_cost : Gradient of the cost vector. Aka dA_cost.</li><li>— back_acti : Gradient of the Activation matrix. Aka dA. The result of differentiation after having applied back propagation. With a given cost function.</li><li>— back_linr : Gradient of the Linear algebra matrix. Aka dZ. The result of backwards linear differentiation back propagation.</li><li>— back_wgts : Gradient of the Weights matrix. Aka dW. Also the result of back-prop.</li><li>— back_bias : Gradient of the Bias vector. Aka db. Also the result of back-prop.</li></ul><h4>8.1. Set the Instantiation Function</h4><p>For the below code chunk, the function set_InstantiateNetwork() is defined. It only has three input arguments, which specify the number of nodes to be used in each layer. Based on this information, the model will be instantiated and returned, ready for initialisation, in the next step.</p><pre>set_InstantiateNetwork &lt;- function(<br>    input=50, <br>    hidden=c(30,20,10), <br>    output=1<br>    ) {</pre><pre>    # Set up<br>    model = list()<br>    layers = c(<br>        &quot;input&quot;,<br>        1:length(hidden),<br>        &quot;output&quot;<br>    )<br>    <br>    # Loop<br>    for (layer in layers) {<br>        <br>        # Make layer<br>        model[[layer]] &lt;- list(<br>            &quot;nodz&quot;      = &quot;&quot;,<br>            &quot;inpt&quot;      = &quot;&quot;,<br>            &quot;wgts&quot;      = &quot;&quot;,<br>            &quot;bias&quot;      = &quot;&quot;,<br>            &quot;linr&quot;      = &quot;&quot;,<br>            &quot;acti&quot;      = &quot;&quot;,<br>            &quot;acti_func&quot; = &quot;&quot;,<br>            &quot;cost&quot;      = &quot;&quot;,<br>            &quot;back_cost&quot; = &quot;&quot;,<br>            &quot;back_acti&quot; = &quot;&quot;,<br>            &quot;back_linr&quot; = &quot;&quot;,<br>            &quot;back_wgts&quot; = &quot;&quot;,<br>            &quot;back_bias&quot; = &quot;&quot; <br>        )<br>        <br>        # Set nodes<br>        if (layer==&quot;input&quot;) {<br>            model[[layer]][[&quot;nodz&quot;]] &lt;- input<br>        } else if (layer==&quot;output&quot;) {<br>            model[[layer]][[&quot;nodz&quot;]] &lt;- output<br>        } else {<br>            layer_index &lt;- layer %&gt;% as.numeric()<br>            model[[layer]][[&quot;nodz&quot;]] &lt;- hidden[layer_index]<br>        }<br>        <br>    }<br>    <br>    # Return<br>    return(model)<br>    <br>}</pre><h4>8.1. Create the Network</h4><p>The below code chunk instantiates the network. The model will be set up with the following number of nodes in each layer:</p><ul><li>The input layer will have 3072 nodes, the same number calculated above.</li><li>Each hidden layer will have a decreasing number of nodes, from 100 down to 20 notes.</li><li>The output layer will have 1 node, because this one node will be a floating point number between 0 and 1, which will be used to predict whether the relevant image is a car or not.</li></ul><pre>network_model &lt;- set_InstantiateNetwork(<br>    input=3072, <br>    hidden=c(100,75,50,30,20), <br>    output=1<br>)</pre><h4>8.1. Visualise the Network</h4><p>There is a very good website available which allows Neural Networks to be visualised: <a href="http://alexlenail.me/NN-SVG/AlexNet.html">http://alexlenail.me/NN-SVG/AlexNet.html</a>. The below image is a representation of the network that has just been created.</p><p>Further visualisation can be conducted once the network is fully initialised and forward propagated. See section <a href="#de5b"><strong>Check Model Shapes</strong></a> for more details.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/877/1*1R57pDTlMUNLPAY4iTJJFw.png" /><figcaption><strong>Figure 4</strong>: Visualisation of the Network</figcaption></figure><h3>9. Initialise the Network</h3><p>There are four steps to initialising the network:</p><ol><li>Set the Weight Initialisation functions</li><li>Set the Layer Initialisation function</li><li>Set the Model Initialisation function</li><li>Run the Initialisation</li></ol><h4>9.1. Weight Initialisation</h4><p>At it’s core, weight initialisation is simply generating a random normal number (with μ=0 and σ=1). However, by only using this randomly generated number, the model gradients are found to be exploding or vanishing when attempting to train deeper neural networks. Therefore, these weights need to be scaled after they are initialised, in order to be robust enough to continue to be trained at deeper layers.</p><p>There are many algorithms that can be used for weight initialisation. Two of the more common ones are the <a href="https://prateekvjoshi.com/2016/03/29/understanding-xavier-initialization-in-deep-neural-networks/">Xavier</a> algorithm, and the <a href="https://medium.com/@prateekvishnu/xavier-and-he-normal-he-et-al-initialization-8e3d7a087528">He</a> algorithm. Some good resources for understanding the details behind these algorithms include:</p><ul><li><a href="https://towardsdatascience.com/weight-initialization-in-neural-networks-a-journey-from-the-basics-to-kaiming-954fb9b47c79">Weight Initialization in Neural Networks</a></li><li><a href="http://proceedings.mlr.press/v9/glorot10a/glorot10a.pdf">Understanding the difficulty of training deep feedforward neural networks</a></li><li><a href="https://arxiv.org/pdf/1502.01852.pdf">Surpassing Human-Level Performance on ImageNet Classification</a></li></ul><p><strong>9.1.1. Xavier Algorithm</strong></p><p>The equation for the Xavier initialisation is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/106/0*Ky01V2AVXfKtVDe5" /><figcaption><strong>Equation 1</strong>: Xavier Initialisation Algorithm</figcaption></figure><p>Where:</p><ul><li><em>nᵢ</em> is the number of nodes coming in to this layer. Also known as “fan-in”.</li><li><em>nᵢ₊₁ </em>is the number of nodes going out of this layer. Also known as “fan-out”.</li></ul><p><strong>9.1.2. He Algorithm</strong></p><p>The equation for the He initialisation is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/52/0*EyhvlCoaEPCiCYsC" /><figcaption><strong>Equation 2</strong>: He Initialisation Algorithm</figcaption></figure><p>Where:</p><ul><li><em>nᵢ</em> is the number of nodes coming in to this layer.</li></ul><h4>9.1. Initialisation Functions</h4><p>For programmatic purposes, these functions are written with the order value as part of the function arguments. This means that the order of magnitude of the equation can be altered at a later stage and used as a hyperparameter.</p><pre>let_InitialiseXavier &lt;- function(nodes_in, nodes_out, order=6) {<br>    <br>    # Do work<br>    numer &lt;- sqrt(order)<br>    denom &lt;- sqrt(nodes_in + nodes_out)<br>    output &lt;- numer/denom<br>    <br>    # Return<br>    return(output)<br>    <br>}</pre><pre>let_InitialiseHe &lt;- function(nodes_in, nodes_out, order=2) {<br>    <br>    # Do work<br>    numer &lt;- order<br>    denom &lt;- nodes_in<br>    output &lt;- sqrt(numer/denom)<br>    <br>    # Return<br>    return(output)<br>    <br>}</pre><h4>9.3. Layer Initialisation</h4><p>The next step is to construct a function that will initialise all the relevant aspects of an individual layer. This step is important because it is where the weight matrices are created, and these must be constructed in a certain manner so as to ensure that the dimensions allow successful forward propagation.</p><p>The steps for the layer construction are as follows:</p><ol><li>Determine the layer names of current layer (layer) and of the previous layer (layer_prev).<br> — Used to access the relevant configurations from the network_model list.</li><li>Determine the number of nodes feeding in to this layer (nodes_in), and the number of nodes feeding out of the current layer (nodes_out).<br> — Used for parse’ing in to the Initialisation Algorithms.</li><li>Create the Weight matrix.<br> — The dimensions of which are as follows:<br> — — 1. Number of Rows is the number of nodes in the previous layer.<br> — — 2. Number of Columns is the number of nodes in the current layer.<br> — Each element is created using the rnorm() function, which generates a random number on the normal curve, with μ=0 and σ=1.</li><li>Determine the Initialisation Algorithm to be used.<br> — The value value parsed in to this function is a lower-case word associated with the relevant algorithm.<br> — It is coerced to a Title capitalisation, and then given the prefix let_Initialise.<br> — The value is then parsed in to the get() function, which then calls the function, and executes the parameters parsed to it.<br> — This is a programmatic way to flexibly call different algorithms, based on values parsed to the function.</li><li>Scale the Weight matrix.<br> — By multiplying each element by the Initialisation Algorithm.</li><li>Create the Bias matrix.<br> — The dimensions of which are as follows:<br> — — 1. The number of rows is the number of nodes in the current layer.<br> — — 2. There is only one column.<br> — Every single element has value 0.</li><li>Apply the Weight and Bias matrix back on to the network_model.</li><li>Return the updated network_model object.</li></ol><p>In order to achieve this, the function arguments used are for the function include:</p><ol><li>The network_model itself.</li><li>The layer_index of the layer (where 1 is the input layer, and each increasing number is each subsequent layer).</li><li>The initialisation_algorithm, which is either the value NA, or the value xavier or he which is the value of the relevant algorithm.</li><li>The initialisation_order, which is an integer value to be used as the numerator in the relevant algorithm.</li></ol><pre>set_InitialiseLayer &lt;- function(<br>    network_model,<br>    layer_index,<br>    initialisation_algorithm=NA,<br>    initialisation_order=6<br>    ) {<br>    <br>    # Get layer names<br>    layer_prev &lt;- names(network_model)[layer_index-1]<br>    layer &lt;- names(network_model)[layer_index]<br>    <br>    # Get number of nodes<br>    if (layer_index == 1) {<br>        nodes_in &lt;- 0 #First layer is &#39;input&#39;<br>    } else {<br>        nodes_in &lt;- network_model %&gt;% <br>            extract2(layer_prev) %&gt;% <br>            extract2(&quot;nodz&quot;)<br>    }<br>    nodes_out &lt;- network_model %&gt;% <br>        extract2(layer) %&gt;% <br>        extract2(&quot;nodz&quot;)<br>    <br>    # Set the seed of reproducibility<br>    set.seed(1234)<br>    <br>    # Initialise weight matrix<br>    w_matrix &lt;- matrix(<br>        data=rnorm(nodes_in * nodes_out), <br>        nrow=nodes_in,<br>        ncol=nodes_out<br>    )<br>    <br>    # Get initialisation algorithm<br>    if (!is.na(initialisation_algorithm)) {<br>        algorithm &lt;- paste0(<br>            &quot;let_Initialise&quot;, <br>            str_to_title(initialisation_algorithm)<br>        )<br>    }<br>    <br>    # Scale weights<br>    if (layer_index != 1) {<br>        if (is.na(initialisation_algorithm)) {<br>            w_matrix &lt;- w_matrix<br>        } else {<br>            w_matrix &lt;- w_matrix * <br>                get(algorithm)(<br>                    nodes_in=nodes_in, <br>                    nodes_out=nodes_out, <br>                    order=initialisation_order<br>                )<br>        }<br>    }<br>    <br>    # Initialise bias matrix<br>    b_matrix &lt;- matrix(<br>        data=network_model %&gt;% <br>            extract2(layer) %&gt;% <br>            extract2(&quot;nodz&quot;) %&gt;% <br>            replicate(0),<br>        nrow=network_model %&gt;% <br>            extract2(layer) %&gt;% <br>            extract2(&quot;nodz&quot;),<br>        ncol=1<br>    )<br>    <br>    # Place data back in to the model<br>    network_model[[layer]][[&quot;wgts&quot;]] &lt;- w_matrix<br>    network_model[[layer]][[&quot;bias&quot;]] &lt;- b_matrix<br>    <br>    # Return<br>    return(network_model)<br>    <br>}</pre><h4>9.4. Model Initialisation</h4><p>The purpose of the set_InitialiseModel() function is to loop through each layer in the network_model object, to initialise them in accordance with the parameters set within the model itself. This function will take the number of nodes (set by the set_InstantiateNetwork() function).</p><p>The function will take only three input parameters:</p><ol><li>The network_model:<br> — The Network, as instantiated by the set_InstantiateNetwork() function.</li><li>The initialisation_algorithm:<br> — The algorithm to be used for the network initialisation.<br> — This should be the values NA, xavier, or he.</li><li>The initialisation_order:<br> — The order value to be used for the numerator of the equation.<br> — This could be a numeric value, or the string layers which signifies that the order should be the number of hidden layers in the network.</li></ol><pre>set_InitialiseModel &lt;- function(<br>    network_model, <br>    initialisation_algorithm=&quot;xavier&quot;, <br>    initialisation_order=&quot;layers&quot;<br>    ) {<br>    <br>    # Redefine &#39;initialisation_order&#39;<br>    if (initialisation_order == &quot;layers&quot;) {<br>        initialisation_order &lt;- get_CountOfElementsWithCondition(<br>            names(network_model), <br>            function(x){is.integer(as.numeric(x))}<br>        )<br>    }<br>    <br>    # Initialise each layer<br>    for (layer_index in 1:length(names(network_model))) {<br>        network_model &lt;- set_InitialiseLayer(<br>            network_model=network_model, <br>            layer_index=layer_index, <br>            initialisation_algorithm=initialisation_algorithm,<br>            initialisation_order=initialisation_order<br>        )<br>    }<br>    <br>    # Return<br>    return(network_model)<br>    <br>}</pre><p>Notice, this function uses the user-defined function get_CountOfElementsWithCondition(). This function allows for the counting of the number of hidden layers in the model. The source code for the function can be found <a href="https://github.com/chrimaho/VanillaNeuralNetworksInR/blob/master/Functions/functions.R">here</a>.</p><h4>9.5. Network Initialisation</h4><p>The below code chunk initialises the network using the function defined above. The method utilised is %&lt;&gt;%, which is defined in the magrittr package, which states that it is used to update a value on the left-hand side by first piping it into the first argument position on the right-hand side, and then assigning the result back to the object on the left hand side.</p><pre>network_model %&lt;&gt;% set_InitialiseModel()</pre><h4>9.6. Check Model Parameters</h4><p>For a quick sanity-check, the below code chunk checks for the number of parameters for the defined model. This again uses a custom function get_ModelParametersCount(), the definition for which can be found in the source code for this article (located <a href="https://github.com/chrimaho/VanillaNeuralNetworksInR/blob/master/Functions/functions.R">here</a>).</p><pre>network_model %&gt;% <br>    get_ModelParametersCount() %&gt;% <br>    format(big.mark=&quot;,&quot;)</pre><p>Which prints:</p><pre>[1] &quot;320,846&quot;</pre><p>There are over 320,000 parameters in this neural network. Every single one of these parameters will need to be trained, and each of them will have an influence on the final outcome. We will continue to walk through how this can be achieved in the following sections.</p><h3>10. Forward Propagation</h3><h4>10.1. The Theory</h4><p>There is a very good website which outlines what actually happens during a matrix multiplication: <a href="http://matrixmultiplication.xyz/">Matrix Multiplication</a>.</p><p>The theory for the forward propagation method is as follows:</p><ol><li>Apply matrix multiplication<br> — 1. The first matrix is the activation matrix from the previous layer.<br> — 2. The second matrix is the weight matrix for the current layer.<br> — 3. The third matrix is the (first) linear activation matrix for the current layer.</li><li>Apply bias matrix<br> — 1. The first matrix is the (first) linear activation of the current layer.<br> — 2. The second matrix is the bias matrix for the current layer.<br> — 3. The third matrix is the (second) linear activation matrix for the current layer.</li><li>Apply activation algorithm<br> — 1. The first matrix is the (second) linear activation matrix for the current layer.<br> — 2. The activation function is determined by the user during the time of the function being run. Can be the relu activation, sigmoid activation, or any of the other activation functions.<br> — 3. The third matrix is the activation matrix for the current layer.</li></ol><p><strong>10.1.1. Step One</strong></p><p>To illustrate, the following matrices shows the first step of the forward propagation, using fictitious numbers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/539/0*67D-5eqiGUxYSI_L" /><figcaption><strong>Figure 5</strong>: Forward Prop, Step One</figcaption></figure><p>The below code shows how this process is implemented programmatically.</p><pre># Declare First matrix<br>matrix_input &lt;- matrix(<br>    data=1:24, <br>    nrow=8, <br>    ncol=3, <br>    byrow=TRUE<br>)</pre><pre># Declare Weight matrix<br>matrix_weight &lt;- matrix(<br>    data=1:15, <br>    nrow=3, <br>    ncol=5, <br>    byrow=TRUE<br>)</pre><pre># Apply matrix manipulation<br>matrix_layer &lt;- matrix_input %*% matrix_weight</pre><p>Which prints:</p><pre>matrix_input:<br>     [,1] [,2] [,3]<br>[1,]    1    2    3<br>[2,]    4    5    6<br>[3,]    7    8    9<br>[4,]   10   11   12<br>[5,]   13   14   15<br>[6,]   16   17   18<br>[7,]   19   20   21<br>[8,]   22   23   24</pre><pre>matrix_weight:<br>     [,1] [,2] [,3] [,4] [,5]<br>[1,]    1    2    3    4    5<br>[2,]    6    7    8    9   10<br>[3,]   11   12   13   14   15</pre><pre>matrix_layer:<br>     [,1] [,2] [,3] [,4] [,5]<br>[1,]   46   52   58   64   70<br>[2,]  100  115  130  145  160<br>[3,]  154  178  202  226  250<br>[4,]  208  241  274  307  340<br>[5,]  262  304  346  388  430<br>[6,]  316  367  418  469  520<br>[7,]  370  430  490  550  610<br>[8,]  424  493  562  631  700</pre><p><strong>10.1.2. Step Two</strong></p><p>The next diagram shows how the bias matrix is applied. As shown, each element in the bias matrix is applied horizontally to each element of the initial matrix. This diagram shows how it works.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/508/0*CojT1k88KTtgsTC-" /><figcaption><strong>Figure 6</strong>: Forward Prop, Step Two</figcaption></figure><p>The below code shows how this process is implemented programmatically.</p><pre># Declare Bias matrix<br>vector_bias &lt;- matrix(1:8, 8, 1)</pre><pre># Apply Bias matrix<br>matrix_biased &lt;- sweep(matrix_layer, 1, vector_bias, &quot;+&quot;)</pre><p>Which prints:</p><pre>matrix_layer:<br>     [,1] [,2] [,3] [,4] [,5]<br>[1,]   46   52   58   64   70<br>[2,]  100  115  130  145  160<br>[3,]  154  178  202  226  250<br>[4,]  208  241  274  307  340<br>[5,]  262  304  346  388  430<br>[6,]  316  367  418  469  520<br>[7,]  370  430  490  550  610<br>[8,]  424  493  562  631  700</pre><pre>vector_bias:<br>     [,1]<br>[1,]    1<br>[2,]    2<br>[3,]    3<br>[4,]    4<br>[5,]    5<br>[6,]    6<br>[7,]    7<br>[8,]    8</pre><pre>matrix_biased:<br>     [,1] [,2] [,3] [,4] [,5]<br>[1,]   47   53   59   65   71<br>[2,]  102  117  132  147  162<br>[3,]  157  181  205  229  253<br>[4,]  212  245  278  311  344<br>[5,]  267  309  351  393  435<br>[6,]  322  373  424  475  526<br>[7,]  377  437  497  557  617<br>[8,]  432  501  570  639  708</pre><p><strong>10.1.3. Step Three</strong></p><p>The activation function is one which is defined at the time of running the function. The algorithm is applied to each element of the initial matrix. In this instance, a simple multiplication of the initial matrix is used.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/635/0*Xu5mU5oV_xoVe0m0" /><figcaption><strong>Figure 7</strong>: Forward Prop, Step Three</figcaption></figure><p>The below code shows how this process is implemented programmatically.</p><pre># Apply Activation function<br>matrix_output &lt;- matrix_biased * (0.01 * matrix_biased)</pre><p>Which prints:</p><pre>matrix_biased:<br>     [,1] [,2] [,3] [,4] [,5]<br>[1,]   47   53   59   65   71<br>[2,]  102  117  132  147  162<br>[3,]  157  181  205  229  253<br>[4,]  212  245  278  311  344<br>[5,]  267  309  351  393  435<br>[6,]  322  373  424  475  526<br>[7,]  377  437  497  557  617<br>[8,]  432  501  570  639  708</pre><pre>matrix_output:<br>        [,1]    [,2]    [,3]    [,4]    [,5]<br>[1,]   22.09   28.09   34.81   42.25   50.41<br>[2,]  104.04  136.89  174.24  216.09  262.44<br>[3,]  246.49  327.61  420.25  524.41  640.09<br>[4,]  449.44  600.25  772.84  967.21 1183.36<br>[5,]  712.89  954.81 1232.01 1544.49 1892.25<br>[6,] 1036.84 1391.29 1797.76 2256.25 2766.76<br>[7,] 1421.29 1909.69 2470.09 3102.49 3806.89<br>[8,] 1866.24 2510.01 3249.00 4083.21 5012.64</pre><h4>10.2. Linear Component</h4><p>When combined together, the linear algebraic function is implemented in just three lines of code, as shown in the below code chunk in the set_LinearForward() function.</p><pre>set_LinearForward &lt;- function(inpt, wgts, bias) {<br>    <br>    # Perform matrix multiplication<br>    linr &lt;- inpt %*% wgts<br>    <br>    # Add bias<br>    linr &lt;- sweep(linr, 2, bias, &quot;+&quot;)<br>    <br>    # Return<br>    return(linr)<br>    <br>}</pre><h4>10.3. Non-Linear Component</h4><p>The real power of Neural Network comes from their Activation function. It is now the network is able to capture the non-linear aspects, and this is what emphasises their predictive power.</p><p>The Activation function can be one of many different algorithms, depending on the purpose of the network. The choice of Activation can be a hyperparameter which is chosen at a later stage. The <a href="https://www.desmos.com/">Desmos</a> website provides an excellent interactive way of reviewing the different types of Activations: <a href="https://www.desmos.com/calculator/rhx5tl8ygi">Activation Functions</a>.</p><p>Each of the Activation functions are defined individually, and each only take one argument for the function, which is a matrix for which the activation will be applied. For the purposes of brevity, four of the more popular activations are provided here; but there are many, many others which can be used. The resources and equations for how to compute these functions are also provided:</p><p><strong>sigmoid:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/119/0*L6sMEfWE0wQIoQp5" /><figcaption><strong>Equation 3</strong>: Sigmoid Activation</figcaption></figure><p>Sources:<br>1. <a href="https://kite.com/python/answers/how-to-calculate-a-logistic-sigmoid-function-in-python">How to calculate a logistic sigmoid function in Python</a><br>2. <a href="https://www.geeksforgeeks.org/implement-sigmoid-function-using-numpy/">Implement sigmoid function using Numpy</a></p><p><strong>relu:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/132/0*goiwHAH3wqhSs3cS" /><figcaption><strong>Equation 4</strong>: Relu Activation</figcaption></figure><p>Sources:<br>1. <a href="https://medium.com/ai%C2%B3-theory-practice-business/a-beginners-guide-to-numpy-with-sigmoid-relu-and-softmax-activation-functions-25b840a9a272">A beginner’s guide to NumPy with Sigmoid, ReLu and Softmax activation functions</a></p><p><strong>softmax:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/139/0*cqDZtY_OE51HkKCA" /><figcaption><strong>Equation 5:</strong> Softmax Activation</figcaption></figure><p>Sources:<br>1. <a href="https://towardsdatascience.com/softmax-activation-function-explained-a7e1bc3ad60">Softmax Activation Function Explained</a></p><p><strong>swish:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/138/0*BjHw-42zm4G8klKm" /><figcaption><strong>Equation 6</strong>: Swish Activation</figcaption></figure><p>Sources:<br>1. <a href="https://arxiv.org/pdf/1710.05941.pdf">Searching for Activation Functions</a><br>2. <a href="https://www.bignerdranch.com/blog/implementing-swish-activation-function-in-keras/">Implementing Swish Activation Function in Keras</a></p><p>These Activation Functions are defined programmatically as follows:</p><pre>let_ActivateSigmoid &lt;- function(linr) {<br>    <br>    # Do work<br>    acti &lt;- 1/(1+exp(-linr))<br>    <br>    # Return<br>    return(acti)<br>    <br>}</pre><pre>let_ActivateRelu &lt;- function(linr) {<br>    <br>    # Do work<br>    acti &lt;- sapply(linr, max, 0) %&gt;% <br>        structure(dim=dim(linr))<br>    <br>    # Return<br>    return(acti)<br>    <br>}</pre><pre>let_ActivateSoftmax &lt;- function(linr) {<br>    <br>    # Do work<br>    expo &lt;- exp(linr)<br>    expo_sum &lt;- sum(exp(linr))<br>    acti &lt;- expo/expo_sum<br>    <br>    # Return<br>    return(acti)<br>    <br>}</pre><pre>let_ActivateSwish &lt;- function(linr, beta=0.1) {<br>    <br>    # Do work<br>    acti &lt;- linr * (beta * linr)<br>    <br>    # Return<br>    return(acti)<br>    <br>}</pre><h4>10.4. Set Forward Propagation Function</h4><p>The set_ForwardProp() function pulls together all of the components mentioned above. It implements the following steps:</p><ol><li>Loop through each layer of the network_model.</li><li>Get the layer name for the current layer.</li><li>Implement a ‘pass-thru’ process for the input layer.</li><li>Extract the relevant information, including:<br> —1. Layer name of the <em>previous </em>layer<br> —2. Activation matrix of the <em>previous </em>layer<br> —3. Weights matrix of the <em>current </em>layer<br> — 4. Bias matrix for the <em>current </em>layer</li><li>Apply linear algebra component.</li><li>Apply the non-linear activation component.</li><li>Note the difference between the activation of the hidden layers and that of the final layer.</li><li>Apply the relevant information back on to the network.</li><li>Return the network_model object.</li></ol><p>In order to implement this process, the function takes only four arguments:</p><ol><li>network_model: The network model to be updated.</li><li>data_in: The 4-Dimensional array of training images, as defined above.</li><li>activation_hidden: The Activation function to be applied to the hidden layers.</li><li>activation_final: The Activation function to be applied to the final (output) layer.</li></ol><pre>set_ForwardProp &lt;- function(<br>    network_model, <br>    data_in, <br>    activation_hidden=&quot;relu&quot;, <br>    activation_final=&quot;sigmoid&quot;<br>    ) {<br>    <br>    # Do work<br>    for (index in network_model %&gt;% names() %&gt;% length() %&gt;% 1:.) {<br>        <br>        # Define layer name<br>        layr &lt;- network_model %&gt;% <br>            names() %&gt;% <br>            extract(index)<br>        <br>        if (layr==&quot;input&quot;) {<br>            <br>            # Pass-thru for &#39;input&#39; layer<br>            network_model[[layr]][[&quot;inpt&quot;]] &lt;- data_in<br>            network_model[[layr]][[&quot;acti&quot;]] &lt;- data_in<br>            <br>        } else {<br>            <br>            # Extract data<br>            prev &lt;- names(network_model)[index-1]<br>            inpt &lt;- network_model %&gt;% <br>                extract2(prev) %&gt;% <br>                extract2(&quot;acti&quot;)<br>            wgts &lt;- network_model %&gt;% <br>                extract2(layr) %&gt;% <br>                extract2(&quot;wgts&quot;)<br>            bias &lt;- network_model %&gt;% <br>                extract2(layr) %&gt;% <br>                extract2(&quot;bias&quot;)<br>            <br>            # Calculate<br>            linr &lt;- set_LinearForward(inpt, wgts, bias)<br>            <br>            # Activate<br>            if (layr==&quot;output&quot;) {<br>                func &lt;- activation_final %&gt;% <br>                    str_to_title() %&gt;% <br>                    paste0(&quot;let_Activate&quot;, .) %&gt;% <br>                    get()<br>                acti &lt;- func(linr)<br>                network_model[[layr]][[&quot;acti_func&quot;]] &lt;- activation_final<br>            } else {<br>                func &lt;- activation_hidden %&gt;% <br>                    str_to_title() %&gt;% <br>                    paste0(&quot;let_Activate&quot;, .) %&gt;% <br>                    get()<br>                acti &lt;- func(linr)<br>                network_model[[layr]][[&quot;acti_func&quot;]] &lt;- activation_hidden<br>            }<br>            <br>            # Apply back to our model<br>            network_model[[layr]][[&quot;inpt&quot;]] &lt;- inpt<br>            network_model[[layr]][[&quot;linr&quot;]] &lt;- linr<br>            network_model[[layr]][[&quot;acti&quot;]] &lt;- acti<br>            <br>        }<br>        <br>    }<br>    <br>    # Return<br>    return(network_model)<br>    <br>}</pre><h4>10.5. Run Forward Propagation</h4><p>The last step of the Forward Propagation process is to actually run the function. In the below code chunk, the tic() and toc() functions are implemented to time how long a process takes to run.</p><pre>tic()<br>network_model %&lt;&gt;% set_ForwardProp(trn_img, &quot;relu&quot;, &quot;sigmoid&quot;)<br>toc()</pre><p>Which prints:</p><pre>7.05 sec elapsed</pre><p>As mentioned above in the <a href="#b95f"><strong>Split Data</strong></a> section, the trn_img object is over 82 Mb large, and the model has over 320,000 parameters. The entire end-to-end process for forward propagation took only 7 seconds to run; which is quite impressive, and is a testament to the power of mathematics.</p><h4>10.6. Check Model Shapes</h4><p>The Network can now be printed using the custom function get_PrintNetwork(). This function returns each layer in an individual box, containing key information like the relevant shapes of the matrices and the activation functions used. The source code for the function can be found <a href="https://github.com/chrimaho/VanillaNeuralNetworksInR/blob/master/Functions/functions.R">here</a>.</p><pre># Print the Network<br>network_model %&gt;% <br>    get_PrintNetwork() %&gt;% <br>    cat()</pre><p>Which prints:</p><pre>+--------------------------------+<br>|      Layer : input             |<br>|      Nodes : 3,072             |<br>| Inpt Shape : 7,000 x 3,072     |<br>| Wgts Shape :     0 x 3,072     |<br>| Outp Shape : 7,000 x 3,072     |<br>| Activation :                   |<br>+--------------------------------+<br>               |<br>               V<br>+--------------------------------+<br>|      Layer : 1                 |<br>|      Nodes : 100               |<br>| Inpt Shape : 7,000 x 3,072     |<br>| Wgts Shape : 3,072 x   100     |<br>| Outp Shape : 7,000 x   100     |<br>| Activation : relu              |<br>+--------------------------------+<br>               |<br>               V<br>+--------------------------------+<br>|      Layer : 2                 |<br>|      Nodes : 75                |<br>| Inpt Shape : 7,000 x   100     |<br>| Wgts Shape : 100   x    75     |<br>| Outp Shape : 7,000 x    75     |<br>| Activation : relu              |<br>+--------------------------------+<br>               |<br>               V<br>+--------------------------------+<br>|      Layer : 3                 |<br>|      Nodes : 50                |<br>| Inpt Shape : 7,000 x    75     |<br>| Wgts Shape : 75    x    50     |<br>| Outp Shape : 7,000 x    50     |<br>| Activation : relu              |<br>+--------------------------------+<br>               |<br>               V<br>+--------------------------------+<br>|      Layer : 4                 |<br>|      Nodes : 30                |<br>| Inpt Shape : 7,000 x    50     |<br>| Wgts Shape : 50    x    30     |<br>| Outp Shape : 7,000 x    30     |<br>| Activation : relu              |<br>+--------------------------------+<br>               |<br>               V<br>+--------------------------------+<br>|      Layer : 5                 |<br>|      Nodes : 20                |<br>| Inpt Shape : 7,000 x    30     |<br>| Wgts Shape : 30    x    20     |<br>| Outp Shape : 7,000 x    20     |<br>| Activation : relu              |<br>+--------------------------------+<br>               |<br>               V<br>+--------------------------------+<br>|      Layer : output            |<br>|      Nodes : 1                 |<br>| Inpt Shape : 7,000 x    20     |<br>| Wgts Shape : 20    x     1     |<br>| Outp Shape : 7,000 x     1     |<br>| Activation : sigmoid           |<br>+--------------------------------+</pre><h3>11. Calculate the Cost</h3><p>Once the forward-propagation part has been completed, it’s then necessary to get a measure for how wrong the model is. This will then be used to update the model parameters during the backward propagation steps.</p><h4>11.1. Set Cost Function</h4><p>The first step is to write the functions to be used for getting the cost for the model. Ultimately, it does not matter how bad the results are for the first round of training; remember, the model was initialised with random numbers. What’s important is that the cost functions should be determining a <em>single </em>value for the cost of the network, and this single function will be what is used for the derivative functions to be used in the backwards propagation steps.</p><p>Note here, that there is a very small epsilon value used (epsi), which effectively adjusts for a perfect prediction made by the model. The reason for this is not that we don’t want the model to predict a perfect value, it’s that we want the model to predict the probability of a perfect value. Furthermore, it’s impossible to take a logarithmic value of a 0, so therefore it’s necessary to adjust this to be <em>just </em>a little bit off-zero.</p><pre>get_ComputeCost &lt;- function(pred, true, epsi=1e-10) {<br>    <br>    # Get number of samples<br>    samp &lt;- length(true)<br>    <br>    # Instantiate totals<br>    total_cost &lt;- 0<br>    <br>    # Loop for each prediction<br>    for (i in 1:samp) {<br>        <br>        # Adjust for perfect predictions<br>        if (pred[i]==1) {pred[i] %&lt;&gt;% subtract(epsi)}<br>        if (pred[i]==0) {pred[i] %&lt;&gt;% add(epsi)}<br>        <br>        # Calculate totals<br>        total_cost &lt;- total_cost - <br>            (<br>                true[i] * log(pred[i]) <br>                + <br>                (1-true[i]) * log(1-pred[i])<br>            )<br>        <br>    }<br>    <br>    # Take an average<br>    cost &lt;- (1/samp) * total_cost<br>    <br>    # Return<br>    return(cost)<br>    <br>}</pre><p>Then, the cost must be applied back on to the network. For this, the exact same value is applied to each layer of the network.</p><pre>set_ApplyCost &lt;- function(network_model, cost) {<br>    <br>    # Apply back to the model<br>    for (layer in network_model %&gt;% names) {<br>        network_model[[layer]][[&quot;cost&quot;]] &lt;- cost<br>    }<br>    <br>    # Return<br>    return(network_model)<br>}</pre><h4>11.2. Run the Cost Function</h4><p>The below code chunk runs the cost functions, utilising the functions defined above.</p><pre>network_model %&lt;&gt;% set_ApplyCost(<br>    get_ComputeCost(network_model[[&quot;output&quot;]][[&quot;acti&quot;]], trn_cls)<br>)</pre><h3>12. Backward Propagation</h3><p>The function of back-propagation is intended to take the cost of the model, and then differentiate each of the Weights and Biases matrices in the network to determine to what extent each and every single parameter in the network contributed to that final cost value. To do this, the process works backwards from the end to the start, following the following steps:</p><ol><li>Differentiate the final cost value</li><li>Differentiate the activation matrices</li><li>Differentiate the linear algebra matrices</li><li>Continue to the previous layer.</li></ol><p>Each step in the back-propagation process requires calculus to derive the values, and the final function is implemented here. As this paper is not intended to show how to derive the equations, but more how to run the functions, the necessary algebraic steps are not included here.</p><h4>12.1. Differentiate Cost</h4><p>As similar to the functions used to get the cost, the cost differential values are first calculated, then applied to each layer of the network.</p><pre>get_DifferentiateCost &lt;- function(pred, true) {<br>    <br>    # Do work<br>    diff_cost &lt;- -(<br>        divide_by(true, pred) - divide_by(1-true, 1-pred)<br>    )<br>    <br>    # Return<br>    return(diff_cost)<br>    <br>}</pre><pre>set_ApplyDifferentiateCost &lt;- function(<br>    network_model, <br>    cost_differential<br>    ) {<br>    <br>    # Do work<br>    for (layer in names(network_model)) {<br>        network_model[[layer]][[&quot;back_cost&quot;]] &lt;- cost_differential<br>        if (layer==&quot;output&quot;) {<br>            network_model[[layer]][[&quot;back_acti&quot;]] &lt;- network_model %&gt;% <br>                extract2(layer) %&gt;% <br>                extract2(&quot;back_cost&quot;) %&gt;% <br>                t()<br>        }<br>    }<br>    <br>    # Return<br>    return(network_model)<br>    <br>}</pre><h4>12.2. Differentiate Activation</h4><p>As each of the activations have their own function, there is also a derivation of that function which can be calculated using calculus.</p><pre>let_BackwardActivateRelu &lt;- function(diff_acti_curr, linr_curr) {<br>    <br>    # Do work<br>    diff_linr_curr &lt;- diff_acti_curr<br>    diff_linr_curr[linr_curr&lt;=0] &lt;- 0<br>    <br>    # Return<br>    return(diff_linr_curr)<br>    <br>}</pre><pre>let_BackwardActivateSigmoid &lt;- function(diff_acti_curr, linr_curr) {<br>    <br>    # Do work<br>    temp &lt;- 1/(1+exp(-linr_curr))<br>    diff_linr_curr &lt;- t(diff_acti_curr) * temp * (1-temp)<br>    <br>    # Return<br>    return(t(diff_linr_curr))<br>    <br>}</pre><h4>12.3. Differentiate Linear</h4><p>Having had defined all this so far, the next step is to combine in to one single function, which can be used once per layer, which runs the necessary back-propagation differentiation functions.</p><p>Notice here that the output is actually a list of three elements. This a key difference between R and python. In R, functions can only output a single element; as compared to python which is able to return multiple elements from each function.</p><pre>get_DifferentiateLinear &lt;- function(<br>    back_linr_curr,<br>    acti_prev,<br>    wgts,<br>    bias<br>    ) {<br>    <br>    # get number of samples<br>    samp &lt;- acti_prev %&gt;% dim %&gt;% extract(2)<br>    <br>    # Differentiate weights<br>    diff_wgts &lt;- 1/samp * (back_linr_curr %*% acti_prev)<br>    <br>    # Differentiate bias<br>    diff_bias &lt;- 1/samp * rowSums(back_linr_curr, dims=1)<br>    <br>    # Differentiate activation<br>    diff_acti_prev &lt;- wgts %*% back_linr_curr<br>    <br>    # Consolidate in to one list<br>    list_linr &lt;- list(<br>        diff_acti_prev, <br>        diff_wgts, <br>        diff_bias<br>    )<br>    <br>    # Return<br>    return(list_linr)<br>    <br>}</pre><h4>12.4. Run Backward Propagation</h4><p>After having defined the differentiation functions, next is necessary to combine these individual functions in to a single, combined, which can be run once per layer.</p><p>First, we will set up the function, and secondly we will run it.</p><p><strong>12.4.1. Set Backward Propagation Function</strong></p><p>The backward propagation function to be defined (set_BackwardProp()) must be designed to run through the following steps:</p><ol><li>Run through each layer in <em>reverse</em> direction. This is necessary because, quite logically, the backward propagation function needs to be run in a backwards direction.</li><li>Extract the layer name.</li><li>Skip the input layer. Again, quite logical, because this layer is at the beginning of the network, and does not need to be back-propagated.</li><li>Extract the name of the previous layer.</li><li>Extract the relevant matrices to be used for subsequent calculations.</li><li>Extract the relevant activation function for this particular layer.</li><li>Set up some empty matrices, which will house the relevant differentiated matrices.</li><li>Differentiate the activation matrix of the <em>current </em>layer</li><li>Differentiate the linear matrices, including:<br> — weight matrix of the current layer.<br> — bias matrix of the current layer.<br> — activation matrix of the previous layer.</li><li>Apply information back in to the relevant places in the network_model.</li><li>Return the updated network_model.</li></ol><pre>set_BackwardProp &lt;- function(network_model) {<br>    <br>    # Loop through each layer in reverse order<br>    for (layr_indx in network_model %&gt;% names() %&gt;% length() %&gt;% 1:. %&gt;% rev) {<br>        <br>        # Get the layer name<br>        layr_curr &lt;- network_model %&gt;% <br>            names() %&gt;% <br>            extract(layr_indx)<br>        <br>        # Skip the &#39;input&#39; layer<br>        if (layr_curr == &quot;input&quot;) next<br>        <br>        # Get the previous layer name<br>        layr_prev &lt;- network_model %&gt;% <br>            names %&gt;% <br>            extract(layr_indx-1)<br>        <br>        # Set up the existing matrices<br>        linr_curr &lt;- network_model %&gt;% <br>            extract2(layr_curr) %&gt;% <br>            extract2(&quot;linr&quot;)<br>        wgts_curr &lt;- network_model %&gt;% <br>            extract2(layr_curr) %&gt;% <br>            extract2(&quot;wgts&quot;)<br>        bias_curr &lt;- network_model %&gt;% <br>            extract2(layr_curr) %&gt;% <br>            extract2(&quot;bias&quot;)<br>        acti_prev &lt;- network_model %&gt;% <br>            extract2(layr_prev) %&gt;% <br>            extract2(&quot;acti&quot;)<br>        diff_acti_curr &lt;- network_model %&gt;% <br>            extract2(layr_curr) %&gt;% <br>            extract2(&quot;back_acti&quot;)<br>        <br>        # Get the activation function<br>        acti_func_back &lt;- network_model %&gt;% <br>            extract2(layr_curr) %&gt;% <br>            extract2(&quot;acti_func&quot;) %&gt;%<br>            str_to_title %&gt;% <br>            paste0(&quot;let_BackwardActivate&quot;, .)<br>        <br>        # Set up the empty matrices<br>        diff_linr_curr &lt;- matrix()<br>        diff_acti_prev &lt;- matrix()<br>        diff_wgts_curr &lt;- matrix()<br>        diff_bias_curr &lt;- matrix()<br>        <br>        # Differentiate activation<br>        diff_linr_curr &lt;- get(acti_func_back)(<br>            diff_acti_curr,<br>            linr_curr<br>        )<br>        <br>        # Differentiate linear<br>        list_linr &lt;- get_DifferentiateLinear(<br>            back_linr_curr=diff_linr_curr,<br>            acti_prev=acti_prev,<br>            wgts=wgts_curr,<br>            bias=bias_curr<br>        )<br>        diff_acti_prev &lt;- list_linr %&gt;% extract2(1)<br>        diff_wgts_curr &lt;- list_linr %&gt;% extract2(2)<br>        diff_bias_curr &lt;- list_linr %&gt;% extract2(3)<br>        <br>        # Apply back to model<br>        network_model[[layr_prev]][[&quot;back_acti&quot;]] &lt;- diff_acti_prev<br>        network_model[[layr_curr]][[&quot;back_linr&quot;]] &lt;- diff_linr_curr<br>        network_model[[layr_curr]][[&quot;back_wgts&quot;]] &lt;- diff_wgts_curr<br>        network_model[[layr_curr]][[&quot;back_bias&quot;]] &lt;- diff_bias_curr<br>        <br>    }<br>    <br>    # Return<br>    return(network_model)<br>    <br>}</pre><p><strong>12.4.2. Run Backward Propagation Function</strong></p><p>Having defined this function, the next step is to run the function. The below code chunk is wrapped with the tic() and toc() functions, in order to determine how much time it took for the function to run.</p><pre>tic()<br>network_model %&lt;&gt;% set_BackwardProp()<br>toc()</pre><p>Which prints:</p><pre>8.84 sec elapsed</pre><p>As shown, it took approximately 9 seconds to run this function. This is quite impressive, considering that there are over 320,000 parameters to be updated (see <a href="#0f41"><strong>Check Model Parameters</strong></a> section).</p><h3>13. Update Model Parameters</h3><h4>13.1. Context</h4><p>After the model parameters have been differenced, it’s then necessary to update the relevant parameters of the network (the weights and biases) before re-training the network again by re-running the forward propagation function.</p><p>The method used to update these parameters is called <a href="https://en.wikipedia.org/wiki/Stochastic_gradient_descent">Stochastic Gradient Descent</a>. For more information, see <a href="https://leon.bottou.org/publications/pdf/nimes-1991.pdf">Stochastic Gradient Learning in Neural Networks</a> or <a href="https://www.sciencedirect.com/science/article/abs/pii/092523129390006O">Backpropagation and Stochastic Gradient Descent Method</a>.</p><p>For sure, there are other methods of implementing neural network optimisation. The literature has been spending a lot of effort in this area. There are algorithms such as <a href="https://towardsdatascience.com/understanding-rmsprop-faster-neural-network-learning-62e116fcf29a">RMSProp</a> and <a href="https://towardsdatascience.com/adam-latest-trends-in-deep-learning-optimization-6be9a291375c">Adam</a>, which implement intelligent methods to achieve a faster convergence, and a more accurate final result. Some good sources for this include <a href="https://heartbeat.fritz.ai/an-empirical-comparison-of-optimizers-for-machine-learning-models-b86f29957050">An Empirical Comparison of Optimizers for Machine Learning Models</a> and <a href="https://medium.com/datadriveninvestor/overview-of-different-optimizers-for-neural-networks-e0ed119440c3">Overview of different Optimizers for Neural Networks</a>. For further enhancement and optimisation, it is important to explore these optimisation options.</p><h4>13.2. Process</h4><p>Nonetheless, the process of implementing Stochastic Gradient Descent is really quite simple:</p><ol><li>Specify a given <em>learning rate</em> between 0 and 1 (usually very small number, for example 0.001).</li><li>Take the differenced <em>weight</em> and <em>bias</em> matrices, and multiply by the <em>learning rate</em> in a <strong>negative</strong> direction.</li><li>Add the updated differenced <em>weight</em> and <em>bias</em> matrices, and add to the original <em>weight</em> and <em>bias</em> matrix.</li><li>Repeat this process layer-by-layer.</li></ol><h4>13.3. Set the Update Model Function</h4><p>To set the function for updating the model parameters, the following steps are used:</p><ol><li>Specify the learning_rate as an argument for the function.</li><li>Loop through each layer in the network in a forward direction.</li><li>Extract the layer name.</li><li>Skip the input layer (as it does not need updating).</li><li>Define the gradient steps for the back_wgts and back_bias matrices.</li><li>Apply the gradient steps to the original wgts and bias matrices.</li><li>Return the updated mode.</li></ol><pre>set_UpdateModel &lt;- function(network_model, learning_rate=0.001) {<br>    <br>    # Do work<br>    for (index in network_model %&gt;% names() %&gt;% length() %&gt;% 1:.) {<br>        <br>        # Get layer name<br>        layr &lt;- network_model %&gt;% <br>            names() %&gt;% <br>            extract(index)<br>        <br>        # Skip &#39;input&#39; layer<br>        if (layr==&quot;input&quot;) next<br>        <br>        # Define gradient steps for the weight<br>        grad_step_wgts &lt;- -1 * <br>            (<br>                learning_rate * network_model[[layr]][[&quot;back_wgts&quot;]]<br>            )</pre><pre>        # Define gradient steps for the bias<br>        grad_step_bias &lt;- -1 * <br>            (<br>                learning_rate * network_model[[layr]][[&quot;back_bias&quot;]]<br>            )<br>        <br>        # Take steps<br>        network_model[[layr]][[&quot;wgts&quot;]] &lt;- network_model %&gt;% <br>            extract2(layr) %&gt;% <br>            extract2(&quot;wgts&quot;) %&gt;% <br>            add(t(grad_step_wgts))<br>        network_model[[layr]][[&quot;bias&quot;]] &lt;- network_model %&gt;% <br>            extract2(layr) %&gt;% <br>            extract2(&quot;bias&quot;) %&gt;% <br>            add(grad_step_bias)<br>        <br>    }<br>    <br>    # Return<br>    return(network_model)<br>    <br>}</pre><h4>13.4. Run the Update Model Function</h4><p>Having defined the function, then we run the model our network.</p><pre>network_model %&lt;&gt;% set_UpdateModel(0.01)</pre><h3>14. Run the Model End-to-End</h3><p>Now, it’s time to bring it all together. Running the model end-to-end essentially means:</p><ol><li>Applying forward propagation.</li><li>Calculate the cost.</li><li>Run backward propagation.</li><li>Update the model parameters.</li><li>Repeat…</li></ol><p>Each time this is repeated is called an <em>Epoch</em>. It is quite typical for networks to be updated over many many Epochs. Sometimes hundreds, sometimes thousands of Epochs; the exact number of Epochs to run is a discretionary decision by the Data Scientist, based on numerous variables.</p><p>There is one additional step to be added in here, and that is <em>batching </em>the data. For this, we can consider within each Epoch, the data will be batched up in to equally divisible groups, and used for subsequent processing. In this instance, the model parameters will be updated after each batch. And when the full amount of data has been run through the model in it’s entirety, this is then considered one Epoch.</p><h3>14.1. Set up the Train Model Function</h3><p>To illustrate this programmatically, the following function is written.</p><p>Notice here, that there are many custom functions included (the source code for each of which can be found <a href="https://github.com/chrimaho/VanillaNeuralNetworksInR/blob/master/Functions/functions.R">here</a>). These functions include:</p><ul><li>get_Modulus()</li><li>get_BatchIndexes()</li><li>get_VerbosityValues()</li><li>get_TimeDifference()</li><li>plt_PlotLearningCurve()</li></ul><p>The steps for this function include:</p><ol><li>Begin the timer.</li><li>Declare what information will be returned.</li><li>Instantiate the network.</li><li>Initialise the network.</li><li>Loop through each Epoch.</li><li>Loop through each batch.</li><li>Subset the data for this particular batch.</li><li>Run forward propagation.</li><li>Calculate the cost.</li><li>Apply the cost to the network.</li><li>Differentiate the cost.</li><li>Run backward propagation.</li><li>Update the model parameters.</li><li>Run through next batch.</li><li>Save the overall cost at the end of the Epoch.</li><li>Print the update at the relevant Epoch number (from the verbosity argument)</li><li>Run through next Epoch.</li><li>Save the updated network.</li><li>Print the learning curve.</li><li>Return the output.</li></ol><pre>let_TrainModel &lt;- function(<br>    x_train,<strong> <br>    </strong>y_train,<br>    input_nodes=dim(x_train)[2], <br>    hidden_nodes=c(100, 50, 10), <br>    output_nodes=1,<br>    initialisation_algorithm=&quot;xavier&quot;,<br>    initialisation_order=&quot;layers&quot;,<br>    activation_hidden=&quot;relu&quot;, <br>    activation_final=&quot;sigmoid&quot;,<br>    batches=get_Modulus(dim(x_train)[1])[4], <br>    epochs=500, <br>    learning_rate=0.001,<br>    verbosity=NA,<br>    print_learning_curve=TRUE<br>    ) {<br>    <br>    # Begin the timer<br>    time_begin &lt;- Sys.time()<br>    <br>    # Set return values<br>    output &lt;- list(<br>        network_model=network_model,<br>        results=list(<br>            cost=vector()<br>            # Can add more, such as accuracy or specificity.<br>        )<br>    )<br>    <br>    # Instantiate<br>    network_model &lt;- set_InstantiateNetwork(<br>        input=input_nodes,<br>        hidden=hidden_nodes, <br>        output=output_nodes<br>    )<br>    <br>    # Initialise<br>    network_model &lt;- set_InitialiseModel(<br>        network_model=network_model, <br>        initialisation_algorithm=initialisation_algorithm, <br>        initialisation_order=initialisation_order<br>    )<br>    <br>    # Loop each epoch<br>    for (epoch in 1:epochs) {<br>        <br>        # Loop each batch<br>        for (batch in 1:batches) {<br>            <br>            # Set indices<br>            batch_indexes &lt;- get_BatchIndexes(<br>                vector=1:dim(x_train)[1], <br>                batches=batches, <br>                batch=batch,<br>                seed=1234<br>            )<br>            <br>            # Set data<br>            x_train_batch &lt;- x_train[batch_indexes,]<br>            y_train_batch &lt;- y_train[batch_indexes]<br>            <br>            # Forward Prop<br>            network_model &lt;- set_ForwardProp(<br>                network_model = network_model, <br>                data_in = x_train_batch, <br>                activation_hidden = activation_hidden, <br>                activation_final = activation_final<br>            )<br>            <br>            # Get cost<br>            cost &lt;- get_ComputeCost(<br>                pred = network_model[[&quot;output&quot;]][[&quot;acti&quot;]], <br>                true = y_train_batch, <br>                epsi = 1e-10<br>            )<br>            <br>            # Apply cost<br>            network_model &lt;- set_ApplyCost(<br>                network_model = network_model, <br>                cost = cost<br>            )<br>            <br>            # Differentiate cost<br>            network_model &lt;- set_ApplyDifferentiateCost(<br>                network_model = network_model, <br>                cost_differential = get_DifferentiateCost(network_model[[&quot;output&quot;]][[&quot;acti&quot;]], y_train_batch)<br>            )<br>            <br>            # Backprop<br>            network_model &lt;- set_BackwardProp(network_model)<br>            <br>            # Update parameters<br>            network_model &lt;- set_UpdateModel(<br>                network_model = network_model, <br>                learning_rate = learning_rate<br>            )<br>            <br>        }<br>            <br>        # Save cost<br>        output[[&quot;results&quot;]][[&quot;cost&quot;]] %&lt;&gt;% c(cost)<br>        <br>        # Print update<br>        if (!is.na(verbosity)) {<br>            if (epoch %in% get_VerbosityValues(epochs, verbosity)){<br>                if (epoch == verbosity) {<br>                    &quot;Learning rate: {}\n&quot; %&gt;% <br>                        str_Format(learning_rate) %&gt;% <br>                        cat()<br>                }<br>                &quot;Epoch {}, Cost: {}, Time: {}\n&quot; %&gt;% <br>                    str_Format(<br>                        epoch, <br>                        round(cost, 5), <br>                        get_TimeDifference(time_begin)<br>                    ) %&gt;% <br>                    cat()<br>            }<br>        }<br>        <br>    }<br>    <br>    # Re-apply back to the output list<br>    output[[&quot;network_model&quot;]] &lt;- network_model<br>    <br>    # Print the results<br>    if (print_learning_curve == TRUE) {<br>        <br>        tryCatch(<br>            expr={<br>                output %&gt;% <br>                    extract2(&quot;results&quot;) %&gt;% <br>                    extract2(&quot;cost&quot;) %&gt;% <br>                    plt_PlotLearningCurve(<br>                        input_nodes=input_nodes, <br>                        hidden_nodes=hidden_nodes, <br>                        output_nodes=output_nodes,<br>                        initialisation_algorithm=<br>                            initialisation_algorithm, <br>                        initialisation_order=initialisation_order,<br>                        activation_hidden=activation_hidden, <br>                        activation_final=activation_final,<br>                        epochs=epochs, <br>                        learning_rate=learning_rate, <br>                        verbosity=verbosity,<br>                        run_time=get_TimeDifference(time_begin)<br>                    ) %&gt;% <br>                    print()<br>            },<br>            warning=function(message){<br>                writeLines(&quot;A Warning occurred:&quot;)<br>                writeLines(message)<br>                return(invisible(NA))<br>            },<br>            error=function(message){<br>                writeLines(&quot;An Error occurred:&quot;)<br>                writeLines(message)<br>                return(invisible(NA))<br>            },<br>            finally={<br>                #Do nothing...<br>            }<br>        )<br>        <br>    }<br>    <br>    # Return<br>    return(output)<br>    <br>}</pre><h4>14.2. Run the Train Model Function</h4><p>Having had set up the training function, let’ now run it.</p><p>Notice here that:</p><ol><li>Only the training data is used (see <a href="#b95f"><strong>Split Data</strong></a> section).</li><li>The He initialisation algorithm is used, with an order of 2.</li><li>The relu activation algorithm is used for the hidden layers, with the sigmoid activation algorithm for the output layer.</li><li>There are 56 batches, and 50 epochs.</li><li>The model results are printed every 10 epochs.</li><li>The learning curve is printed at the end.</li></ol><pre>training_output &lt;- let_TrainModel(<br>    x_train=trn_img, <br>    y_train=trn_cls,<br>    input_nodes=dim(trn_img)[2], <br>    hidden_nodes=c(100,75,50,30,20), <br>    output_nodes=1,<br>    initialisation_algorithm=&quot;he&quot;, <br>    initialisation_order=2,<br>    activation_hidden=&quot;relu&quot;, <br>    activation_final=&quot;sigmoid&quot;,<br>    batches=56, epochs=50, <br>    learning_rate=0.01,<br>    verbosity=10, <br>    print_learning_curve=TRUE<br>)</pre><p>Which prints:</p><pre>Learning rate: 0.01<br>Epoch 10, Cost: 0.32237, Time: 4.86 mins<br>Epoch 20, Cost: 0.30458, Time: 9.58 mins<br>Epoch 30, Cost: 0.28903, Time: 12.23 mins<br>Epoch 40, Cost: 0.29525, Time: 14.87 mins<br>Epoch 50, Cost: 0.29884, Time: 17.43 mins</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W2gp8hknOWkZh3nbttuL6Q.jpeg" /><figcaption><strong>Figure 8</strong>: Learning Curve for Neural Network</figcaption></figure><p>It works! This plot proves that the model is training, and it is continuing to learn and improve its performance over time.</p><p>Notice here that the cost line begins at approximately 0.4, and very quickly decreases to 0.3 after 20 Epochs. In order for it to complete the full 50 Epochs, this took approximately 12 minutes. This can be seen as a success.</p><h4>14.3. Further Experimentation</h4><p>Due to the nature of how these functions have been set up, it is very easy for further experimentation and optimisation.</p><p>It may be reasonable to try:</p><ol><li>A different number of hidden layers, or number of nodes per layer (change the hidden_nodes parameter),</li><li>A different initialisation algorithm, or initialisation order (change the initialisation_alghorithm or initialisation_order parameter),</li><li>A different activation function on the hidden layers (change the activation_hidden parameter),</li><li>A different number of batches per Epoch (change the batches parameter),</li><li>A different number of epochs (change the epochs parameter),</li><li>A different learning rate (change the learning_rate parameter),</li><li>Get more data (recall in the <a href="#64db"><strong>Download Data</strong></a> section, only 10,000 images were downloaded; but there are another 50,000 images which can be used for further training).</li></ol><p>Here is an experimentation of trying some different parameters. Notice how the results also change. This next round of training took 34 minutes to run.</p><pre>training_output &lt;- let_TrainModel(<br>    x_train=trn_img, <br>    y_train=trn_cls,<br>    input_nodes=dim(trn_img)[2],<br>    hidden_nodes=c(100,75,50,30,20), <br>    output_nodes=1,<br>    initialisation_algorithm=&quot;xavier&quot;, <br>    initialisation_order=&quot;layers&quot;,<br>    activation_hidden=&quot;relu&quot;, <br>    activation_final=&quot;sigmoid&quot;,<br>    batches=56, epochs=100, <br>    learning_rate=0.001,<br>    verbosity=20, <br>    print_learning_curve=TRUE<br>)</pre><p>Which prints:</p><pre>Learning rate: 0.001<br>Epoch 20, Cost: 0.33354, Time: 5.4 mins<br>Epoch 40, Cost: 0.29891, Time: 10.26 mins<br>Epoch 60, Cost: 0.30255, Time: 15.2 mins<br>Epoch 80, Cost: 0.29968, Time: 20.84 mins<br>Epoch 100, Cost: 0.29655, Time: 26.09 mins</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dkfdY_f82ZdXZMHmhsDpTA.jpeg" /><figcaption><strong>Figure 9</strong>: Second Learning Curve for Neural Network</figcaption></figure><p>It is then the job of the Data Scientist to identify the optimal configuration of these parameters, in order to achieve a better performance rate. See <a href="https://www.datacamp.com/community/tutorials/parameter-optimization-machine-learning-models">Hyperparameter Optimization in Machine Learning Models</a> for more details.</p><p>However, as this article is not to <em>achieve </em>the best results, but more to <em>show how</em> to achieve said results, and the processes/methodologies involved. Therefore, further experimentation will not be conducted in this paper.</p><h3>15. Create Prediction</h3><p>After having trained the network, next step is to apply it to the test data to see how accurate it can predict unknown data.</p><h4>15.1. Set Up Prediction</h4><p>The first step is to write two custom functions. The first will take in the test data and trained model, and will return a data frame containing the predicted values and the truth values. Then pretty-print the Confusion Matrix in a ggplot-style of output.</p><pre>get_Prediction &lt;- function(<br>    x_test, <br>    y_test, <br>    network_model, <br>    threshold=0.5<br>    ) {<br>    <br>    # Create prediction<br>    predic &lt;- set_ForwardProp(<br>        network_model=network_model, <br>        data_in=x_test, <br>        activation_hidden=&quot;relu&quot;, <br>        activation_final=&quot;sigmoid&quot;<br>    )<br>    <br>    # Extract probabilities<br>    probas &lt;- predic %&gt;% <br>        extract2(&quot;output&quot;) %&gt;% <br>        extract2(&quot;acti&quot;)<br>    <br>    # Define results<br>    result &lt;- data.frame(<br>        probs=probas,<br>        truth=y_test<br>    )<br>    <br>    # Add prdic<br>    result %&lt;&gt;% <br>        mutate(prdic=ifelse(probas&gt;threshold, 1, 0))<br>    <br>    # Return<br>    return(result)<br>    <br>}</pre><pre>plt_ConfusionMatrix &lt;- function(confusion_matrix) {</pre><pre>    # Do work<br>    plot &lt;- confusion_matrix %&gt;% <br>        extract(&quot;table&quot;) %&gt;% <br>        as.data.frame() %&gt;% <br>        rename_all(str_remove_all, &quot;table.&quot;) %&gt;% <br>        rename(&quot;Prediction&quot;=1, &quot;Reference&quot;=2) %&gt;% <br>        mutate(<br>            goodbad = ifelse(<br>                Prediction == Reference, <br>                &quot;good&quot;, <br>                &quot;bad&quot;<br>            )<br>        ) %&gt;%<br>        group_by(Reference) %&gt;% <br>        mutate(prop = Freq/sum(Freq)) %&gt;% <br>        ungroup() %&gt;% <br>        {<br>            ggplot(., <br>                aes(<br>                    x = Reference, <br>                    y = Prediction, <br>                    fill = goodbad, <br>                    alpha = prop<br>                )) +<br>                geom_tile() +<br>                geom_text(<br>                    aes(label = Freq), <br>                    vjust = .5, <br>                    fontface  = &quot;bold&quot;, <br>                    alpha = 1<br>                ) +<br>                scale_fill_manual(<br>                    values=c(good=&quot;green&quot;, bad=&quot;red&quot;)<br>                ) +<br>                scale_x_discrete(<br>                    limits=levels(.$Reference), <br>                    position=&quot;top&quot;<br>                ) +<br>                scale_y_discrete(<br>                    limits=rev(levels(.$Prediction))<br>                ) +<br>                labs(<br>                    title=&quot;Confusion Matrix&quot;,<br>                    subtitle=paste0(<br>                        &quot;For: &#39;&quot;, <br>                        .$Prediction[1], <br>                        &quot;&#39; vs &#39;&quot;, <br>                        .$Prediction[2], <br>                        &quot;&#39;&quot;<br>                    )<br>                )<br>        }<br>    <br>    # Return<br>    return(plot)<br>    <br>}</pre><h4>15.2. Run Prediction</h4><p>The next step is to actually run the prediction. This step is quite self explanatory. The parameters for this function includes the test data, and the trained model.</p><pre># Create prediction<br>Prediction &lt;- get_Prediction(<br>    tst_img, <br>    tst_cls, <br>    training_output[[&quot;network_model&quot;]], <br>    0.1<br>)</pre><h4>15.3. View Prediction</h4><p>Having created this prediction, it’s then necessary to visualise the output. In the same way as the <a href="#5d00"><strong>Check Images</strong></a> section, the following code chunk visualises the top 16 images, and returns a label for each.</p><p>As seen, the model is clearly able to recognise some, but it also makes mistakes on others.</p><pre># Define classes<br>ClassList &lt;- c(&quot;0&quot;=&quot;Not&quot;, &quot;1&quot;=&quot;Car&quot;)</pre><pre># Set list<br>lst &lt;- list()</pre><pre># Loop 16 images<br>for (image in 1:16) {<br>    lst[[image]] &lt;- plt_PlotImage(<br>        cifar$images[-partition,,,], <br>        Prediction[[&quot;prdic&quot;]], <br>        ClassList,<br>        image<br>    )<br>}</pre><pre># View images<br>gridExtra::grid.arrange(grobs=lst, ncol=4)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w3bj6A_fjU_gBGwbLiyk0w.jpeg" /><figcaption><strong>Figure 10</strong>: Predicted Images</figcaption></figure><h4>15.4. Test the Prediction</h4><p>The next step then is to statistically test the data to see how accurate it may be. For this, the confusionMatrix() function (from the caret package) used. From this output, it is possible to select the appropriate metric in order for further optimising the neural network.</p><p>Since this article is onlyabout <em>showing</em> the method, then we will not progress any further with more optimisation here.</p><pre># Set Up<br>ConfusionMatrix &lt;- Prediction %&gt;% <br>    mutate_at(c(&quot;truth&quot;,&quot;prdic&quot;), ~ifelse(.==1,&quot;car&quot;,&quot;not&quot;)) %&gt;% <br>    select(prdic,truth) %&gt;% <br>    table %&gt;% <br>    caret::confusionMatrix()</pre><pre># Print<br>ConfusionMatrix %&gt;% print()</pre><p>Which prints:</p><pre>Confusion Matrix and Statistics</pre><pre>truth<br>prdic  car  not<br>  car  188 1118<br>  not  121 1573<br>                                             <br>               Accuracy : 0.587              <br>                 95% CI : (0.569, 0.605)     <br>    No Information Rate : 0.897              <br>    P-Value [Acc &gt; NIR] : 1                  <br>                                             <br>                  Kappa : 0.079              <br>                                             <br> Mcnemar&#39;s Test P-Value : &lt;0.0000000000000002<br>                                             <br>            Sensitivity : 0.6084             <br>            Specificity : 0.5845             <br>         Pos Pred Value : 0.1440             <br>         Neg Pred Value : 0.9286             <br>             Prevalence : 0.1030             <br>         Detection Rate : 0.0627             <br>   Detection Prevalence : 0.4353             <br>      Balanced Accuracy : 0.5965             <br>                                             <br>       &#39;Positive&#39; Class : car</pre><p>Next step is to plot the confusion matrix. A good source of understanding and interpreting confusion matrices can be found here: <a href="https://en.wikipedia.org/wiki/Confusion_matrix">Confusion Matrix</a>. In this website, it also includes an image (also copied below) for a very good visual understanding.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QbowRQggr1tsbRSI5Tkc0A.png" /><figcaption><strong>Figure 11</strong>: Confusion Matrix Metrics</figcaption></figure><p>When we visualise the confusion matrix of our model, we can see that it successfully predicted a majority of the images. However, the largest number of bad classifications is where it predicted that the image was a car, when it actually was not.</p><pre># Plot Confusion Matrix<br>ConfusionMatrix %&gt;% <br>    plt_ConfusionMatrix()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0MC30-X39bSHlA3Z_80-Qw.jpeg" /><figcaption><strong>Figure 11</strong>: Generated Confusion Matrix</figcaption></figure><p>Another good analytical and plotting tool is to use the ROC Curve (receiver operating characteristics). An ROC Curve is a graphical plot that illustrates the diagnostic ability of a binary classifier system as its discrimination threshold is varied (see <a href="https://en.wikipedia.org/wiki/Receiver_operating_characteristic">Receiver Operating Characteristic</a>).</p><p>Essentially, a model that creates a perfect prediction would have the curve perfectly ‘hugging’ the top-left of this plot. As our model is not doing this, it is clearly obvious that further training and optimisation is necessary. For this, it is necessary to continue experimenting as mentioned in the <a href="#4af2"><strong>Further Experimentation</strong></a> section.</p><pre># Print ROC<br>Prediction %&gt;% <br>    ggplot() +<br>    plotROC::geom_roc(aes(m=probs, d=truth), n.cuts=0) +<br>    plotROC::style_roc(<br>        theme=theme_grey,<br>        ylab=&quot;True Positive Rate&quot;,<br>        xlab=&quot;False Positive Rate&quot;<br>    ) +<br>    theme(<br>        plot.title = element_text(hjust=0.5),<br>        plot.subtitle = element_text(hjust=0.5)<br>    ) + <br>    labs(<br>        title=&quot;ROC Curve&quot;,<br>        y=&quot;True Positive Rate&quot;,<br>        x=&quot;False Positive Rate&quot;<br>    )</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*shxqCawaOvpG62dTp4eUHw.jpeg" /><figcaption><strong>Figure 12</strong>: ROC Curve</figcaption></figure><h3>16. Conclusion</h3><p>This is an effective representation for how to build Vanilla Neural Networks in R. Here, it has been shown how to:</p><ol><li>Access and check the data</li><li>Instantiate and Initialise the network</li><li>Run forward propagation</li><li>Compute the cost</li><li>Run backward propagation</li><li>Update the model</li><li>Set up a training method to loop through every thing</li><li>Predict and assess the performance</li></ol><p>It has also been shown how to do this entirely in R, and without using any pre-defined deep learning framework. For sure, there are other packages that can perform all of the steps, and perhaps in a more computationally efficient manner. But the use of these packages is not the target of this article. Here, the aim is to dispel the ‘blackbox’ phenomenon, and to show how to create these deep learning frameworks from the ground up.</p><p>As shown, the architecture of these feed-forward neural networks effectively use <a href="https://en.wikipedia.org/wiki/Matrix_multiplication">matrix multiplication</a> and <a href="https://en.wikipedia.org/wiki/Differential_calculus">differential calculus</a> in order to adjust the ‘weights’ of a network, and increase it’s predictive power. It does require lots of data, and does require a long time to train and optimise. Furthermore, there are many (many, <em>many</em>) different architectural choices that can be made along the way, all of which are at the discretion of the Data Scientist applying this methodology.</p><p>At the end of the day, this article has proven that deep learning can definitely be achieved in R. Thereby, dispelling the myth that ‘in order to do Deep Learning, you must use Python’. Certainly, there are many successful implementations of Deep Learning in Python; for example: <a href="https://hackernoon.com/building-a-feedforward-neural-network-from-scratch-in-python-d3526457156b">Building a Feedforward Neural Network from Scratch in Python</a>. There’s also example of achieving the same in Swift (<a href="https://www.tooploox.com/blog/deep-neural-networks-in-swift-lessons-learned">Deep Neural Networks in Swift, lessons learned</a>), and in C++ (<a href="https://www.geeksforgeeks.org/ml-neural-network-implementation-in-c-from-scratch/">Neural Network Implementation in C++ From Scratch</a>), and in Java (<a href="https://medium.com/coinmonks/implementing-an-artificial-neural-network-in-pure-java-no-external-dependencies-975749a38114">Implementing an Artificial Neural Network in Pure Java (No external dependencies)</a>). Therefore, the specific language used is effectively irrelevant. What matters is the use case, the environment, the business artifacts, and the language that the Data Scientist feels comfortable in.</p><h3>17. Post Script</h3><p><strong>Acknowledgements</strong>: This report was compiled with some assistance from others. Acknowledgements go to:<br> — <a href="https://www.linkedin.com/in/alexjscriven/">Alex Scriven</a></p><p><strong>Publications</strong>: This report is also published on the following sites:<br> — RPubs: <a href="https://rpubs.com/chrimaho/VanillaNeuralNetworksInR">RPubs/chrimaho/VanillaNeuralNetworksInR</a><br> — GitHub: <a href="https://github.com/chrimaho/VanillaNeuralNetworksInR">GitHub/chrimaho/VanillaNeuralNetworksInR</a><br> — Medium: <a href="https://medium.com/@chrimaho/43b028f415?source=friends_link&amp;sk=f47b3d6f9f539e907d272966fa88bcb8">Medium/chrimaho/VanillaNeuralNetworksInR</a></p><p><strong>Change Log</strong>: This publication was modified on the following dates:<br> — 02/Nov/2020: Original Publication date.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=43b028f415" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/vanilla-neural-networks-in-r-43b028f415">Vanilla Neural Networks in R</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[Exploring Undernourishment: Part 8 — Recommendations and Conclusions]]></title>
            <link>https://medium.com/swlh/exploring-undernourishment-part-8-recommendations-and-conclusions-c5eb682bb3a5?source=rss-56d03114dd5a------2</link>
            <guid isPermaLink="false">https://medium.com/p/c5eb682bb3a5</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-exploration]]></category>
            <category><![CDATA[r]]></category>
            <category><![CDATA[un]]></category>
            <category><![CDATA[r-shiny]]></category>
            <dc:creator><![CDATA[Chris Mahoney]]></dc:creator>
            <pubDate>Tue, 13 Oct 2020 06:37:28 GMT</pubDate>
            <atom:updated>2020-10-17T10:47:48.711Z</atom:updated>
            <content:encoded><![CDATA[<h3>Exploring Undernourishment: Part 8 — Recommendations and Conclusions</h3><h4>A Visual Data Exploration Research Project to Better Understand the Nuances of Our Global Nutrition</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*rc5UEAvxB_rqOl5s.jpg" /><figcaption>Image Source: <a href="http://www.fao.org/3/ca9692en/online/ca9692en.html">Food and Agriculture Organisation of the United Nations</a></figcaption></figure><h3>Contents</h3><p>This is <strong>Part 8 </strong>of an 8-Part research project aiming to better understand the nuances of our global nutrition. It explores this topic through the utilisation of data visualisation and data science techniques. It is complimented by a Web App: <a href="https://chrimaho.shinyapps.io/ExploringUndernourishment/">ExploringUndernourishment</a>, which is freely available to the public.</p><p>Part 1 — <a href="https://medium.com/@chrimaho/exploring-undernourishment-part-1-introduction-and-overview-ff024fa7dd32?source=friends_link&amp;sk=dc47b684aa91157137e18e2bc4e7d8f8">Introduction and Overview</a><strong><br></strong>Part 2 — <a href="https://medium.com/@chrimaho/exploring-undernourishment-part-2-literature-review-a1abfefb86b7?source=friends_link&amp;sk=ee953d379bafa7e6f2bf552fe4d7481f">Literature Review</a><br>Part 3 — <a href="https://medium.com/@chrimaho/exploring-undernourishment-part-3-data-exploration-71f15e5778f3?source=friends_link&amp;sk=5c5c2dc44248109e1c443cb731102773">Data Exploration</a><br>Part 4 — <a href="https://medium.com/@chrimaho/exploring-undernourishment-part-4-research-area-1-general-trend-cdb8ced5b0af?source=friends_link&amp;sk=30922587311cccfd051e40e90e4bf3b8">Research Area 1: General Trend</a><br>Part 5 — <a href="https://medium.com/@chrimaho/exploring-undernourishment-part-5-research-area-2-most-successful-countries-c0afd1504c71?source=friends_link&amp;sk=8da658166ca6d80b3cd9613b26ab02f1">Research Area 2: Most Successful Countries</a><br>Part 6 — <a href="https://medium.com/@chrimaho/exploring-undernourishment-part-6-research-area-3-surprising-trends-962d653a08fc?source=friends_link&amp;sk=d6ba983f77a0f08176d2d916033c4b52">Research Area 3: Surprising Trends</a><br>Part 7 — <a href="https://medium.com/@chrimaho/exploring-undernourishment-part-7-research-area-4-most-influential-indicator-47ba76395f9b?source=friends_link&amp;sk=5eab78561820fe6e0dd5b3ade028e1b5">Research Area 4: Most Influential Indicator</a><br><strong>Part 8 — </strong><a href="https://medium.com/@chrimaho/exploring-undernourishment-part-8-recommendations-and-conclusions-c5eb682bb3a5?source=friends_link&amp;sk=d1b4e31a82e421f6e7924d46732a816f"><strong>Recommendations and Conclusions</strong></a> ← Selected page</p><h3>Recommendations</h3><h4>Recommendation One: Get More Data</h4><p>There is a large number of countries who do not have any data for their Prevalence of Undernourishment. Perhaps this data does exist, or that it can be calculated based on other socio-economic variables; it is recommended to gather this data and add to the repository for further analysis and comparison. Even if it is the case that the data is close to perfect, and there is little room for improvement, it is still important to have a wholistic view of the situation in all countries and regions of the world.</p><h4>Recommendation Two: Focus on Countries with an Upward Trend</h4><p>As there have been a number of countries who have begun to buck the downward trend, and their Prevalence of Undernourishment has started to increase. The focus of the United Nations and of the Food and Agricultural Organisation should be on those countries who have substantially increased their score over time. There should be action-plans to advise and guide the individual Governments, and also plans to educate and inspire the communities to look to perpetuate and improve their own economies.</p><h4>Recommendation Three: Educate on Basic Agricultural Principles</h4><p>As found in the literature review, one of the key deciding factors in improving a communities Prevalence of Undernourishment is education. While having a garden bed in and of itself is not going to substantially improve an individuals’ viability and prosperity, as a collective community having an understanding on the principles of agriculture builds an understanding of self-sustainability and self-maintenance. Perpetually, what this will lead to is an improvement of the economic situation of a community, and thus look to make a positive impact on the dietary adequacy of the citizens. As found in the Analysis, the three most important features were the value of food production, the dietary adequacy, and the protein supply, and by looking to address these within an individual community, they will have a positive impact overall. The fourth most influential feature was Political Stability, which also plays a vital role overall.</p><h3>Conclusion</h3><p>The emphasis that the United Nations has placed on their Sustainable Development Goals, particularly the goal of Zero Hunger, has had a substantial impact on the society, on the Globe in general, and on the individual Countries and their own viability. When analysing the Prevalence of Undernourishment data provided by the Food and Agriculture Organisation of the United Nations, there are many learnings and conclusions which can be drawn from this information.</p><p>At the end of the day, this data and its associated analysis can be used by the Food and Agriculture Organisation to help determine what actions should be taken to address this issue in the future. It is possible to see which countries need the most amount of assistance, and what features or attributes influence those countries the most. Furthermore, this will help to guide and inform the relative countries included, so that they may look to make the appropriate decisions for their best interest and future prosperity and viability.</p><p>Moreover, more broadly, this analysis helps the United Nations to see just how successful they have been in addressing their Sustainable Development Goals in the last two decades. The rest of the society are able to see the level of impact that has already been made in our world, and are able to reflect with confidence that the future trends will be positive and impactful. While there is a bit of a bucking to the trend occurring in recent years, there is some clarity around what can be done to help address this. The level of hope that this brings to us all is inspirational, and beneficial. Which is something that we all need a little bit more of these days.</p><p>By ways of a reminder, the application has been freely published and is openly accessible at any time. For convenience, the links have been copied below.</p><ul><li>Exploration App:<br><a href="https://chrimaho.shinyapps.io/ExploringUndernourishment/">https://chrimaho.shinyapps.io/ExploringUndernourishment/</a></li><li>Source Code:<br><a href="https://github.com/chrimaho/ExploringUndernourishment/">https://github.com/chrimaho/ExploringUndernourishment/</a></li></ul><h3>References</h3><p>Abafita &amp; Kim 2014, ‘Determinants of Household Food Security in Rural Ethiopia: An Empirical Analysis’, <em>Journal of Rural Development</em>, vol. 37, no. 2, pp. 129–57, DOI: 10.22004/ag.econ.196613.</p><p>Food and Agriculture Organisation of the Unites Nations (FAO) 2014, <em>Refinements to the FAO Methodology for estimating the Prevalence of Undernourishment Indicator</em>, viewed 17 May 2020, &lt;<a href="http://www.fao.org/3/a-i4046e.pdf">http://www.fao.org/3/a-i4046e.pdf</a>&gt;.</p><p>FAO 2019, <em>The State of Food Security and Nutrition in the World: Safeguarding Against Economic Slowdowns and Downturns</em>, viewed 16 May 2020, &lt;<a href="http://www.fao.org/3/ca5162en/ca5162en.pdf">http://www.fao.org/3/ca5162en/ca5162en.pdf</a>&gt;.</p><p>FAO 2020a, <em>Food and Agriculture Organisation of the United Nations</em>, viewed 11 May 2020, &lt;<a href="http://www.fao.org/home/en/">http://www.fao.org/home/en/</a>&gt;.</p><p>FAO 2020b, <em>Sustainable Development Goals: Indicator 2.1.1 — Prevalence of undernourishment</em>, viewed 11 May 2020, &lt;<a href="http://www.fao.org/sustainable-development-goals/indicators/2.1.1/en/">http://www.fao.org/sustainable-development-goals/indicators/2.1.1/en/</a>&gt;.</p><p>FAO 2020c, <em>FAOStat</em>, viewed 7 May 2020, &lt;<a href="http://www.fao.org/faostat/en/#data/FS">http://www.fao.org/faostat/en/#data/FS</a>&gt;.</p><p>FAO 2020d, <em>Enhanced Parametric Approach Including In-Depth Thematic Analysis of Underlying Factors and Drivers Behind Food Security and Nutrition Trends</em>, viewed 16 May 2020, &lt;<a href="https://unstats.un.org/sdgs/metadata/files/Metadata-02-01-01.pdf">https://unstats.un.org/sdgs/metadata/files/Metadata-02-01-01.pdf</a>&gt;.</p><p>Fontell &amp; Luchsinger 2011, ‘Sustainable efforts to eradicate Global hunger, undernourishment and malnutrition’, <em>Journal of Global Business Issues</em>, vol. 5, no. 2, pp. 79–83, ProQuest central database.</p><p>Food and Agriculture Organisation of the Unites Nations (FAO) 2020a, <em>Food and Agriculture Organisation of the United Nations</em>, viewed 11 May 2020, &lt;<a href="http://www.fao.org/home/en/">http://www.fao.org/home/en/</a>&gt;.</p><p>Harris-Fry et al. 2015, ‘Socio-economic determinants of household food security and womens dietary diversity in rural Bangladesh: a cross-sectional study’, <em>Journal of Health, Population and Nutrition</em>, vol. 33, ISSN: 16060997, DOI: 10.1186/s41043–015–0022–0.</p><p>Mbolanyi et al. 2017, ‘Determinants of household food security in a rangeland area of Uganda’, <em>African Journal of Rural Development</em>, vol. 2, no. 2, pp. 213–23, ISSN: 2415–2838, DOI: 10.22004/ag.econ.262839.</p><p>Mughal &amp; Fontan-Sers 2020, ‘Cereal production, undernourishment, and food insecurity in South Asia, <em>Review of Development Economics</em>, vol. 24, no. 2, pp. 524–45, Wiley Online Library, &lt;<a href="https://doi-org.ezproxy.lib.uts.edu.au/10.1111/rode.12659">https://doi-org.ezproxy.lib.uts.edu.au/10.1111/rode.12659</a>&gt;.</p><p>United Nations (UN) 2020a, <em>Sustainable Development Goals</em>, viewed 11 May 2020, &lt;<a href="https://www.un.org/sustainabledevelopment/sustainable-development-goals/">https://www.un.org/sustainabledevelopment/sustainable-development-goals/</a>&gt;.</p><p>UN 2020b, <em>Goal 2: Zero Hunger</em>, viewed 11 May 2020, &lt;<a href="https://www.un.org/sustainabledevelopment/hunger/">https://www.un.org/sustainabledevelopment/hunger/</a>&gt;.</p><h3>Read On:</h3><p>Previous section: <a href="https://medium.com/@chrimaho/exploring-undernourishment-part-7-research-area-4-most-influential-indicator-47ba76395f9b?source=friends_link&amp;sk=5eab78561820fe6e0dd5b3ade028e1b5">Research Area 4: Most Influential Indicator</a><br>Next section: -</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c5eb682bb3a5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/exploring-undernourishment-part-8-recommendations-and-conclusions-c5eb682bb3a5">Exploring Undernourishment: Part 8 — Recommendations and Conclusions</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>