<?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 Pedro Borges on Medium]]></title>
        <description><![CDATA[Stories by Pedro Borges on Medium]]></description>
        <link>https://medium.com/@pedrohbtp?source=rss-482df4c176c4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*CjtQAdr_N-A_WBR7Raa7yQ.jpeg</url>
            <title>Stories by Pedro Borges on Medium</title>
            <link>https://medium.com/@pedrohbtp?source=rss-482df4c176c4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 06:09:47 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pedrohbtp/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[AI: OpenAI Gym]]></title>
            <link>https://medium.com/@pedrohbtp/ai-openai-gym-7096516ec422?source=rss-482df4c176c4------2</link>
            <guid isPermaLink="false">https://medium.com/p/7096516ec422</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[reinforcement-learning]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Pedro Borges]]></dc:creator>
            <pubDate>Thu, 12 Sep 2019 19:50:40 GMT</pubDate>
            <atom:updated>2019-09-12T19:50:40.986Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*B9JDpC6lx_1cxeWg.jpg" /></figure><p>OpenAI has been on the news for some time. The media attention of OpenAI is deeply related with the interest in Reinforcement Learning (RL) applications.</p><p>In the graph below we see the google searches related with RL and OpenAI. That is not a coincidence since OpenAI offers a tool used by many in the field, which is the OpenAI Gym.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9FmBIJmw2Ygjd_Ep6jEq3w.png" /><figcaption>Google Trends interest on OpenAI and Reinforcement Learning over the past 5 years</figcaption></figure><p>In this article I go over what is the Gym and how to use it in your applications.</p><h4>What OpenAI Gym is</h4><p>As stated on their website, the main purpose of the gym is provide environments so that people can try their own Reinforcement Learning algorithms against them.</p><p>The difficulty of RL task can greatly vary depending on the observation space. For instance, in the classic <a href="https://en.wikipedia.org/wiki/Breakout_(video_game)">Breakout</a> game, one can choose to give the algorithm the position of the paddle and the position of the ball. On the other hand, it is also possible to give simply the entire image of each frame, as it was it was done by <a href="https://www.youtube.com/watch?v=V1eYniJ0Rnk">Google DeepMind’s team</a>. The latter is a much harder task.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5khyRs3e9Mm1yptO-XBw1A.png" /><figcaption>Breakout game</figcaption></figure><p>For that matter, it is important to have an environment against which people can compare the results of their algorithms. The Gym offers exactly that.</p><h4>How to use the Gym</h4><p>You can install the library by doing</p><pre>pip install gym</pre><p>It has several pre-made environments that you can play. A list of the available environments can be seen on <a href="https://gym.openai.com/envs/#classic_control">their website.</a> Probably the most famous of the envs is the <a href="https://gym.openai.com/envs/CartPole-v1/">CartPole</a>. Most of the RL libraries that you find online use it as an example.</p><p>The environments offer a simple standard interface that is composed by:</p><p><strong>observation, reward, is_done, info = env.step(action)</strong></p><p>This method performs the action given and then observes the resulting state returning it to you together with the any reward that was received. It also returns a true or false variable that represents if it reached an end state. For example, if you won or lost your game and there is no other action you can take. The last object is used to hold some additional info for that environment</p><p><strong>observation = env.reset()</strong></p><p>Resets the environment to the initial state and returns it to you.</p><p><strong>env.render()</strong></p><p>Renders the environment so you can visualize what is happening. In the case of the CartPole, it will create a window just like the one below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qkLPmuLIKIArlE_GvYI-bw.png" /><figcaption>CartPole environment</figcaption></figure><h4>How to implement your custom environment to use with RL libraries</h4><p>You might find several interesting RL libraries on github with some state of the art algorithms you want to try such as <a href="https://github.com/hill-a/stable-baselines">Stable Baselines</a>. A lot of these libraries standardized on using the the OpenAI Gym interface, therefore, to effectively use them, you will have to adapt your custom environment.</p><p>To adapt, you have to implement a class that inherits from the base gym.env class and then implement the methods <em>step</em>, <em>reset</em> and <em>render.</em></p><p>The class below can be used to guide your efforts.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/860a9ffd6c2653d83a2445e897ffa3b9/href">https://medium.com/media/860a9ffd6c2653d83a2445e897ffa3b9/href</a></iframe><h4>Final Thoughts</h4><p>OpenAI Gym is a very useful standard for Reinforcement Learning.</p><p>It allows to easier comparison of algorithms using their available interfaces.</p><p>If you are building a Reinforcement Learning framework, following the Gym standard makes it easier for people to interface with your tool and not have to care about millions of ways someone might decide to interact with environments.</p><blockquote><em>Let me know what you thought about the post. Check me out on any of these places:</em></blockquote><blockquote><a href="https://www.pedro-torres.com/">pedro-torres.com</a></blockquote><blockquote><a href="https://www.linkedin.com/in/pedrohbtp/">LinkedIn</a></blockquote><blockquote><a href="https://twitter.com/pebtp">Twitter</a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7096516ec422" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[AI: Monte Carlo Tree Search (MCTS)]]></title>
            <link>https://medium.com/@pedrohbtp/ai-monte-carlo-tree-search-mcts-49607046b204?source=rss-482df4c176c4------2</link>
            <guid isPermaLink="false">https://medium.com/p/49607046b204</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[games]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[Pedro Borges]]></dc:creator>
            <pubDate>Thu, 22 Aug 2019 13:16:08 GMT</pubDate>
            <atom:updated>2019-09-08T18:08:43.401Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*2WEDTcMRm5TVpBHs.jpg" /></figure><p>Monte Carlo Tree search is a fancy name for one Artificial Intelligence algorithm used specially in games. Alpha Go reportedly used this algorithm with a combination of Neural Network. MCTS has been used in many other applications before that.</p><p>Here I explain what algorithm is, and how it works.</p><h4>What is Monte Carlo Tree Search</h4><p>MCTS, like the name says, is a way of searching a tree. Within this tree, its nodes represent the states and the arc between nodes represents the choice that would take from one state to the other.</p><p>The image below shows the game of Tic-Tac-toe and how we can represent its possible states as a tree.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*GAmoT7b38V3c4B0g.png" /><figcaption>Example of a state tree for tic-tac-toe. <a href="https://commons.wikimedia.org/wiki/File:Tic-tac-toe-game-tree.svg">Source</a></figcaption></figure><p>The <em>Monte Carlo </em>part of the name is what differs from other kinds of tree searches. <em>Monte Carlo </em>algorithms are a broder class of probabilistic algorithms. That means that the algorithm normally returns only an approximation of the actual result. But in several cases it is proven to converge to the real result when ran up to infinity.</p><p>The classical example of a Monte Carlo algorithm is the one used to approximate the number <em>Pi</em>. For that, draw a square with each side of size 1 and inscribe a circle in the square. The area of the square should be equal 1 while the area of the inscribed circle should be equal to <em>pi*(1/2)²</em>. Then the ratio between the area of the square and the area of the circle should be 4/<em>pi</em>. Therefore, to estimate <em>PI</em>, we can place points at random within the region following a uniform distribution. The ratio of the points that fall inside the circle over the total points will give the approximation of the ratio of areas of the two figures. That in turn will give the approximation of the number <em>PI</em>.</p><p>The figure bellow is a representation of the algorithm to estimate Pi, but using only a quarter of circle.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/0*7XC3dtcV3DbTuC4V.gif" /><figcaption>Estimation of PI by using the ratio of areas of the square and circle using a Monte Carlo method. <a href="https://en.wikipedia.org/wiki/Monte_Carlo_method">Source</a></figcaption></figure><h4>How MCTS works</h4><p>The algorithm can be described in 3 steps:</p><ol><li>Selection: From the current state of your game, go down your tree of states of already explored states. There are some ways of choosing which of the already explored states to be chosen, such as the Upper Confidence Bount (UCT), but it will not be discussed here. When we reach a state that we have not been to before, we go into the expansion phase</li><li>Rollout: From this state we have not seen before, simulate the successive states until a Win/Draw/Loss has been reached. In a pure Monte Carlo method, the state chosen to be explored next can be picked at random. Therefore we can continue choosing random moves until the game reaches an end. After reaching the end we have to do the backpropagation of the result.</li><li>Backpropagation: This is performed after we finished one rollout, so after we reach a state of Win/Draw/Loss. We update the value of Win/Loss/Draw of any state that we have been through until we have reached this terminal state.</li></ol><p>We can execute the above steps for as long as we have time. Then when it is time to choose a move, we can choose the one with the highest Win ratio.</p><p>The picture below shows an example of one iteration of the steps above</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/808/0*xJe9U3ApBsAPyanR.png" /><figcaption>One iteration of MCTS. <a href="https://en.wikipedia.org/wiki/Monte_Carlo_tree_search">Source</a></figcaption></figure><p>In the picture, white nodes represent that it is a state that it is player 1’s turn. Black nodes represent the converse, where it would be player 2’s turn. The number in each node represent the number of wins over the total number of rollouts that have been through that node.</p><h4>Implementations</h4><p>If you would like to try, I recommend checking one of these libraries</p><p>Python: <a href="https://github.com/int8/monte-carlo-tree-search">https://github.com/int8/monte-carlo-tree-search</a></p><p>Javascript: <a href="https://github.com/dbravender/mcts">https://github.com/dbravender/mcts</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=49607046b204" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deep Learning: Introduction to PyTorch]]></title>
            <link>https://medium.com/deeplearningbrasilia/deep-learning-introduction-to-pytorch-5bd39421c84?source=rss-482df4c176c4------2</link>
            <guid isPermaLink="false">https://medium.com/p/5bd39421c84</guid>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[pytorch]]></category>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Pedro Borges]]></dc:creator>
            <pubDate>Wed, 19 Dec 2018 15:30:43 GMT</pubDate>
            <atom:updated>2018-12-19T15:30:43.974Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*myMaRWYYzlFYnLfRyqAC3w.png" /></figure><p>PyTorch is an AI framework developed by Facebook. Its code is available on <a href="https://github.com/pytorch/pytorch">GitHub</a> and at the current time has more than <em>22k </em>stars. It has been gaining a lot of momentum since 2017 and is in a steady adoption growth. To understand this trend more and put it in perspective, we can see the picture below containing Google trends data comparing the searches for Tensorflow and PyTorch.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xv-ncxIlFW-vFwFxEyA8Vg.png" /><figcaption>Worldwide searches for tensorflow and PyTorch on google from 1/1/15 to 12/13/18</figcaption></figure><p>It is surely a framework worth learning. Here I discuss some of its components and what you would need to know to get started. First, I introduce how to define your models. Then how to create custom data loaders. Then how to train by updating the weights of your network. Finally, I briefly add a note about tensors in PyTorch.</p><h3>Defining your network</h3><p>PyTorch has a standard way for you to create your own models. The entire definition should stay inside an object that is a child of the class <strong>nn.Module</strong><em>. </em>Inside this class, there are only two methods that must be implemented. These methods are <strong><em>__init__</em></strong> and <strong><em>forward.</em></strong> To give a basic idea of how to use these, I show below an example of an implementation of a network containing 3 linear layers combined by 2 RELU layers.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d348b438c9169e3f63a84e810cf7f2c0/href">https://medium.com/media/d348b438c9169e3f63a84e810cf7f2c0/href</a></iframe><p>I give a little more explanation on each of the methods is as follows:</p><ul><li><strong>__init__</strong></li></ul><p>As in other python classes, the <strong>__init__ </strong>method is used to define the classe’s attributes and populate any value that you would want upon instantiation.</p><p>In the PyTorch context, you should always call the <strong>super() </strong>method to initialize the parent class. Beyond that, you can define all the layers that have optimizable parameters. The definition of layers do not need to be in order of which they will be used in the network. That is because you are just specifying each layer and not how it is connected to others at the moment.</p><ul><li><strong>forward</strong></li></ul><p>This is the method where you tell how the layers are connected. You can see from the example above that it is where you call the layers that you defined inside the <strong>__init__<em> </em></strong>and then return a value which represents the output of your network.</p><p>It is worth noticing that there are some other functions applied inside the forward method that are not defined in the <strong>__init__</strong><em> </em>method but can traditionally be regarded as layers. Take a look for instance at the <strong>F.relu </strong>function. We did not define that in the <strong>__init__</strong><em> </em>method because it does not have any trainable parameters. In other words, the <strong>F.relu </strong>function will always give the same output if you give it the same input. Training the network does not affect its behavior. So as a rule of thumb, you can put inside the <strong>forward </strong>method all the layers that do not have any weights to be updated. On the other hand, you should put all the layers that have weights to be updated inside the <strong>__init__</strong>.</p><h3>Loading your data: Dataset and Data Loaders</h3><p><strong>Dataset</strong> and <strong>Data loaders </strong>are the tools in PyTorch can define how to access your data. This is specially interesting when your data is distributed over several files. For instance, if you have several images in some directory structure, you can personalize the way you access it with the <strong>Dataset </strong>class.</p><p>The code below gives a basic example of how to define your own <strong>Dataset </strong>class and then loop over your data accessing the elements.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6929a39c19e2a55d3cbc50705c6b1c63/href">https://medium.com/media/6929a39c19e2a55d3cbc50705c6b1c63/href</a></iframe><p>There are 3 requires methods we need to implement in the <strong>Dataset</strong> class.</p><ul><li><strong>__init__</strong></li></ul><p>In the initialization, you should put your directories information and other things that would allow to access it. In my example I am loading the data from a csv file. That does not have to be the case. You can simply load a list of file names, where each filename represents a data point. Inside the <strong>__init__ </strong>you do not have to already load your data.</p><ul><li><strong>__len__</strong></li></ul><p>In this method, you should implement a way to get the entire size of your dataset. For instance, if you have a set of images in some directories, you have to implement a way of counting the total number of files that makes your data. In my basic example, I simply get the length of my dataframe. However, that can be arbitrarily complicated depending on how your data is stored.</p><ul><li><strong>__getitem__</strong></li></ul><p>This is where you implement how to get a single item from your dataset. For instance, if you have several images, here is where you would load your image from disk into memory and serve it as what the method returns.</p><p>In order to more efficiently access your dataset, we use the <strong>DataLoader </strong>class. This class simply reads a batch of data at a time in parallel while optionally shuffling your data. All these operations are very important for efficient training.</p><h3>Training: Updating the Network Weights</h3><p>You have your defined your model and your dataset. Now we need create something that allows your network to learn. In PyTorch, this is performed using what is called an optimizer. The optimizer goes over all your weights and update them for you. That means that it automatically accesses all the layers you defined in the <strong>__init__ </strong>method of your network class and updates them using a certain learning rate and algorithm depending on the kind of optimizer you choose. More information about it can be found <a href="https://pytorch.org/docs/stable/optim.html">here.</a></p><p>The optimizer must have a criterion to use for optimization. This is where you need to define your loss function. There are several different loss types implemented. You can check it better <a href="https://pytorch.org/docs/stable/nn.html#torch.nn.CTCLoss">here.</a></p><p>Below I give a basic example on how to instantiate your network, choose an optimizer, then loop over the data and update its weights.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9688d794264766cc244fc1bb0b44244b/href">https://medium.com/media/9688d794264766cc244fc1bb0b44244b/href</a></iframe><h3>A note about Tensors and Gradients</h3><p>Everything that happens under the hood in these neural network libraries are tensor operations. PyTorch is no different. Tensors are like arrays but can be of any dimension. That means that an one dimension array is a tensor. A matrix is also a tensor. The representation of an image like <em>WxHx3</em>, where 3 is for RGB, is also a tensor.</p><p>On its core, the library is about creating operations on tensors and calculating the gradients on these operations. I explained previously in <a href="https://medium.com/deeplearningbrasilia/deep-learning-the-learning-part-a2f68c6319a2">this post</a> why we care about calculating gradients for neural networks.</p><p>Therefore, PyTorch keeps track of all the operations performed on your tensors and makes it easy for you to calculate their. An important thing to notice is that it will only record the gradients of the leaves and not the intermediate tensors. That happens because of the way that the gradients should be calculated for Neural Networks. The weights that are optimized are always leaves of the computational graph, so we only need to care about them.</p><h3>Summarizing</h3><p>Whenever you are working with PyTorch, the steps you should follow are these:</p><ol><li>Define your Network class by placing the layers with weights that can be updated inside the <strong>__init__ </strong>method. Then define how the data flows through the layers inside the <strong>forward </strong>method.</li><li>Define how your data should be loaded using the <strong>Dataset </strong>class. Then use <strong>DataLoader </strong>class to loop over your data.</li><li>Choose an optimizer and a loss function. Loop over your training data and let the optimizer update the weights of your network.</li></ol><p>Creating good models might take a lot of practice and experience. With this in mind there is an excellent library based on PyTorch that already implements lots of things to you. I would highly recommend looking into it. It is called <a href="https://www.fast.ai/">Fastai</a>.</p><p>After you train your model, the next step is putting it somewhere people can use it. If you want to know a little more about putting your model into production, I created an article that explains it more. You can access it <a href="https://medium.com/deeplearningbrasilia/deep-learning-deploying-an-ai-application-with-flask-e23a546f32d1">here.</a></p><blockquote>Let me know what you thought about the post. Check me out on any of these places:</blockquote><blockquote><a href="https://www.pedro-torres.com/"><em>pedro-torres.com</em></a></blockquote><blockquote><a href="https://www.linkedin.com/in/pedrohbtp/"><em>LinkedIn</em></a></blockquote><blockquote><a href="https://twitter.com/pebtp"><em>Twitter</em></a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5bd39421c84" width="1" height="1" alt=""><hr><p><a href="https://medium.com/deeplearningbrasilia/deep-learning-introduction-to-pytorch-5bd39421c84">Deep Learning: Introduction to PyTorch</a> was originally published in <a href="https://medium.com/deeplearningbrasilia">deeplearningbrasilia</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deep Learning: Deploying an AI application with Flask]]></title>
            <link>https://medium.com/deeplearningbrasilia/deep-learning-deploying-an-ai-application-with-flask-e23a546f32d1?source=rss-482df4c176c4------2</link>
            <guid isPermaLink="false">https://medium.com/p/e23a546f32d1</guid>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[rest-api]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <dc:creator><![CDATA[Pedro Borges]]></dc:creator>
            <pubDate>Thu, 15 Nov 2018 15:38:18 GMT</pubDate>
            <atom:updated>2018-11-15T15:38:18.680Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9MDey9lisJqBRl3-Tr_W0g.png" /></figure><blockquote><em>I tried introducing how to deploy an AI application. Please leave a comment about it or about something else that you would want me to write next. Follow me and show some love clapping if you like it!<br></em><a href="https://www.linkedin.com/in/pedrohbtp/"><em>LinkedIn</em></a><em> <br></em><a href="https://twitter.com/pebtp"><em>Twitter</em></a><em><br></em><a href="https://pedrohbtp.github.io/"><em>Github</em></a></blockquote><p>You created your Deep Learning model. You had excellent results in your tests. But what do you do with this model? How would you allow other people to use it?</p><p>The most common answer to this question is that you want to create a web server to let others use your model over the internet. This can easily be done with Flask. Here I give an example of that. Then I show some options you have to really scale your application using some serverless architectures.</p><p>The Gif below shows a working example of an application I created using what I explain in this post. You can go to the website and try it out. The repository is available <a href="https://github.com/pedrohbtp/pytorch-zappa-serverless"><strong>here</strong></a><strong>.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5-BzV7PuyVzGTaKb1CTHmA.gif" /><figcaption>Example from <a href="https://pedrohbtp.github.io/language-model/">https://pedrohbtp.github.io/language-model/</a></figcaption></figure><h3>What is Flask and how to use it</h3><p><a href="http://flask.pocoo.org/"><strong>Flask</strong></a><strong> </strong>is<strong> </strong>a very popular python library to create web servers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f1Y3VkRlSXwctGZ0g4szSQ.png" /></figure><p>Flask is a lean framework. You don’t have to setup many things to get a web server running. The example below lets you create your own server. The only thing you need is python installed and then install flask using pip:</p><ul><li><em>pip install Flask</em></li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c6fa7e649bc07f9329677775b4289d88/href">https://medium.com/media/c6fa7e649bc07f9329677775b4289d88/href</a></iframe><p>The code above will start your server and make the <em>/test </em>route available. Any other route will not return anything. You can copy the code above and test it on your own computer. Change what is within the <em>test() </em>function so it returns something more interesting.</p><h4>Adding AI Libraries</h4><p>Since Flask lives within the Python world, you can install any other library you would want. For instance, you want to use PyTorch, TensorFlow or any other Deep Learning library you like, you can install normally and import in your flask application. To illustrate that, I created a github repository to serve PyTorch programs.You can access it <a href="https://github.com/pedrohbtp/pytorch-zappa-serverless"><strong>here</strong></a><strong>.</strong></p><p>Beyond plain Pytorch, the the repository comes with the necessary files to upload the server to AWS Lambda using <strong>Zappa.</strong></p><h3>What is Zappa and how to use it</h3><p>You now created your Flask application that is running on a single machine. How do you handle the case where several users would want to access your API at the same time? <br>One answer to that would be to put your application running on a stronger machine. Or maybe several different machines.<br>The downside of that is that you would have to take care of the servers yourself. That means that you would have to monitor and check if they are always running and would need to scale it manually to accommodate higher demands.</p><p>An approach that solves these issues is called <strong>Serverless Acrchiteture. <br></strong>I wrote a small article discussing what it is and what is the pricing model. You can check it <a href="https://medium.com/@pedrohbtp/serverless-infrastructures-b2bc62b4e3af"><strong>here</strong></a>.</p><p><a href="https://github.com/Miserlou/Zappa"><strong>Zappa</strong></a><strong> </strong>is a tool that allows you to deploy python serverless inside the AWS ecosystem. It makes it easy to setup all you need to make endpoints available using AWS Lambda and API Gateway. This means that it would work similarly to plain flask in your machine. Zappa deploys your application to the cloud and makes the endpoints and routes available.</p><p>The application that is showcased on the Gif on the top part of this post was deployed to AWS using Zappa.</p><h3>Other Serverless Frameworks</h3><p>Zappa is not the only tool to offer serverless deployments. Some of them that are worth mentioning are:</p><ul><li><a href="https://github.com/serverless/serverless">Serverless</a></li><li><a href="https://algorithmia.com/">algorithmia.com</a></li><li><a href="http://zeit.co/">zeit.co</a></li></ul><p>Algorithmia is specially interesting because it is focused on Machine Learning Applications. It would be good to check all of these and see which would fit better your use case.</p><h3>Final thoughts</h3><p>Creating a good AI model is the first step. If you want to create a product and be usable, you need to find a way to deploy your application somewhere.</p><p>Flask is a great framework to do so since it is a Python library that can be easily integrated with other famous libraries like TensorFlow, Keras and PyTorch. The next step is then to think about scalability. We turn to some serverless tools like Zappa. <br>With that done, you can have a widely available AI application.</p><blockquote>Let me know what you thought about the post. Show some love clapping. Check me out on any of these networks:</blockquote><blockquote><a href="https://www.linkedin.com/in/pedrohbtp/"><em>LinkedIn</em></a></blockquote><blockquote><a href="https://twitter.com/pebtp"><em>Twitter</em></a></blockquote><blockquote><a href="https://pedrohbtp.github.io/"><em>Github</em></a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e23a546f32d1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/deeplearningbrasilia/deep-learning-deploying-an-ai-application-with-flask-e23a546f32d1">Deep Learning: Deploying an AI application with Flask</a> was originally published in <a href="https://medium.com/deeplearningbrasilia">deeplearningbrasilia</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deep Learning: The Learning Part]]></title>
            <link>https://medium.com/deeplearningbrasilia/deep-learning-the-learning-part-a2f68c6319a2?source=rss-482df4c176c4------2</link>
            <guid isPermaLink="false">https://medium.com/p/a2f68c6319a2</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Pedro Borges]]></dc:creator>
            <pubDate>Fri, 09 Nov 2018 14:45:11 GMT</pubDate>
            <atom:updated>2018-11-09T14:45:11.223Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>I tried introducing how learning happens on a Neural Network. Please leave a comment about it or about something else that you would want me to write next. Follow me and show some love clapping if you like it!<br><a href="https://www.linkedin.com/in/pedrohbtp/">LinkedIn</a><br><a href="https://twitter.com/pebtp">Twitter</a></blockquote><p>You started working with <strong>Deep Learning. </strong>You have a problem to solve. You go and choose a framework like<strong> </strong>Keras, TensorFlow, Pytorch. Then you build your network, give it examples and let it learns. Magically, it does really learn. But what does it mean to train a network? What is it performing under the hood?</p><p>The short answer for that is that the network learns because we update its internal weights to minimize the a loss function using the gradients calculated by a technique called <strong>Back Propagation.</strong></p><p>Here I give an introduction on what this means. I divided in three steps: <strong>Forward Pass, Back Propagation </strong>and <strong>Weights Update.</strong></p><h3>Forward Pass</h3><p>The first step during training is to feed the network with an example. That means that we give the network some data and evaluate its output.</p><p>The image below shows how given a data, we can get a prediction. There is a small difference however between the normal prediction and how we do it during training. During training we calculate a loss function and not only the prediction itself.<br>This loss function is normally based on the output of the network. Taking for instance the network below for dogs and cats, we could tell the network that if it predicted wrongly, we would sum the value of 1 to a counter. If it predicted correctly, we would sum the value of 0. Then the resulting sum is the value of our loss function.</p><p>Normally other types of loss functions are used such as <a href="https://en.wikipedia.org/wiki/Cross_entropy">Cross Entropy</a>. There are several different types and you can very well try your custom one.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*hGVRq6n4xcKsNP7G.gif" /><figcaption>Getting a prediction on a network. Image from <a href="https://www.analyticsindiamag.com/most-common-activation-functions-in-neural-networks-and-rationale-behind-it/">analyticsindiamag.com</a></figcaption></figure><p>After you calculate your loss function, you need a way to tune your network parameters so you minimize this loss. We do that by calculating gradients using <strong>Back Propagation </strong>and then updating these weights based on the gradients using <strong>Gradient Descent.</strong></p><h3>Back Propagation</h3><p>Back Propagation is the way that we calculate the gradients of the Network so we can update the weights. <br>I previously wrote an article about <a href="https://medium.com/@pedrohbtp/deep-learning-convolutional-neural-networks-7b48bde10c0">Convolutional Neural Networks</a> and another one about <a href="https://medium.com/@pedrohbtp/deep-learning-recurrent-neural-networks-f9482a24d010">Recurrent Neural Networks</a> and how these architectures work. While they serve different applications — one is normally used with image and the other used with sequence data — the learning algorithm uses the same technique. They all use <strong>Back Propagation.</strong></p><p><strong>Back Propagation </strong>allows us to go layer by layer on the network and calculate its gradients with respect to the inputs and the weights. That means that you can start from the output layer where you have calculated your loss, then calculate the gradients for the layer right before the output one, then calculate the gradients for the layer before that one and so on. All that in a sequential manner.</p><p>This sequential calculation is possible because of the <a href="https://en.wikipedia.org/wiki/Chain_rule">Chain Rule</a>.</p><p>The image below shows how the gradients of the loss with respect to the inputs can be calculated using the gradient of the loss with respect to the output of the function using Chain Rule.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PvClWcKYvfa5fRBaGzIK6Q.png" /><figcaption>Image showing how the gradients of the input of a function can be expressed using the gradient of the output. Image from <a href="https://kratzert.github.io/2016/02/12/understanding-the-gradient-flow-through-the-batch-normalization-layer.html">kratzert</a></figcaption></figure><h3>How it is actually computed</h3><p>We know from above that we need to update the weights based on the gradients of the network with relation to the error. However that starts to look complicated when we see that some networks have millions of weights to be updated. Using the traditional representation of Networks with nodes and arcs, where each arc is a single weight, makes it very hard to understand the calculations.</p><p><strong>Computational Graphs </strong>are a way of organizing the thoughts while abstracting the number of weights in the network so we can calculate the gradients in a cleaner way.</p><p>Instead of representing the network as we traditionally do as a lot of little nodes and arcs, where each component could represent a single scalar number, we represent the network in terms of operations that the input goes through. That is independent on the number of inputs or the number of weights.</p><p>The picture below shows a network with a linear layer that is concatenated with a ReLU non-linear activation and then connected to a softmax output.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pKLcwWqrJsK7GWvBseC4lA.png" /><figcaption>Neuron Representation (Left) and Computational Graph Representation (Right) for the same network. Image from <a href="http://www.zemris.fer.hr/~ssegvic/du/lab1en.shtml">zemris.fer</a></figcaption></figure><p>Both left and right representations are equivalent. The distinction between them is that the left represents the exact number of inputs, weights and outputs. On the other hand, the same computational graph can be used even if there are thousands of inputs and weights. Therefore, it is a more general representation. In practice, computational graphs are used in all the libraries for Neural Networks such as Pytorch and TensorFlow.</p><p>The picture below shows how to compute the gradients using the computational graph from before using the chain rule.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q07bpcCXhoUvLiiSS7ZnCw.png" /><figcaption>How the gradients of each layer as calculated. Image from <a href="http://www.zemris.fer.hr/~ssegvic/du/lab1en.shtml">zemris.fer</a></figcaption></figure><p>Notice that the last layer was a modified to calculate the loss instead of only giving the prediction. The function in this case is the negative log likelihood, but we could have chosen something else.</p><p>If we look closely to the equalities in each of the layers, we can see that they are always expanded in two terms. One term is the derivative of the loss with respect to the output of the current layer and the other is the derivative of the current layer with respect to the variable we are interested in. That is all based on the Chain Rule.</p><p>That is the beauty of this algorithm. It makes possible to go layer by layer and calculate the derivatives.</p><h3><strong>Updating the weights</strong></h3><p>We have the gradients, but how do we update the weights?</p><p>Updating is based on a technique called <a href="https://en.wikipedia.org/wiki/Gradient_descent">Gradient Descent</a>. It tells us that we can minimize a function by updating its parameters using the gradients.</p><p>The equation below shows how to update the weights on our network.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/414/0*czaX7dqHFxY2MLsA.png" /><figcaption>Equation for weight update form <a href="https://hmkcode.github.io/ai/backpropagation-step-by-step/">hmkcode</a></figcaption></figure><p>Notice that there is a parameter called Learning Rate. That is the only parameter on the equation that is chosen by us. It determines how much we update the weights based on the gradients. If it is too big, we might never really minimize the loss because it can make it bounce from one side to the other. If it is too small, it might take too much time to reach a good value. There are several techniques used to choose good learning rates which we will not discuss here.</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/0*QwE8M4MupSdqA3M4.png" /><figcaption>Image from <a href="https://towardsdatascience.com/gradient-descent-in-a-nutshell-eaf8c18212f0">towardsdatascience.com</a> showing the effects of too big and small learning rates.</figcaption></figure><p>We then need to keep performing the entire operation for several examples until the loss is good enough for us.</p><p>The entire process from <strong>forward pass</strong>, <strong>backward pass</strong> and <strong>weights update</strong> is in the picture below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/572/0*Xjpn8YRfdNgbSZum.gif" /><figcaption>Entire process of training a network from <a href="http://www.cnblogs.com/daniel-D/archive/2013/06/03/3116278.html">daniel-D</a></figcaption></figure><p>And that is how a network learns.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a2f68c6319a2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/deeplearningbrasilia/deep-learning-the-learning-part-a2f68c6319a2">Deep Learning: The Learning Part</a> was originally published in <a href="https://medium.com/deeplearningbrasilia">deeplearningbrasilia</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deep Learning: Style Transfer]]></title>
            <link>https://medium.com/deeplearningbrasilia/deep-learning-style-transfer-39337b4b70b6?source=rss-482df4c176c4------2</link>
            <guid isPermaLink="false">https://medium.com/p/39337b4b70b6</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Pedro Borges]]></dc:creator>
            <pubDate>Thu, 01 Nov 2018 14:11:50 GMT</pubDate>
            <atom:updated>2018-11-01T15:22:48.841Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><em>I tried giving the intuition on how Style Transfer works. Please leave a comment about it or about something else that you would want me to write next. Follow me and show some love clapping if you like it</em></blockquote><p><strong>Style Transfer </strong>is an application of neural networks that is fairly recent. It gained its popularity by showing outstanding transformation of photos based on artist paintings. The original work is described in <a href="https://arxiv.org/abs/1508.06576">Gatys et al (2015).</a></p><p>The image displayed below shows several examples of this paper where a picture is transformed to something that is painting like.</p><p>Here I try to give the basic intuition on how this technique works.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VvQVUKUnsTAe6s4xJ8X62A.png" /><figcaption>Style transfer examples from <a href="https://arxiv.org/abs/1508.06576">Gatys et al (2015).</a></figcaption></figure><h3>Breaking down Style Transfer</h3><h4>Select Base Neural Network</h4><p>The first thing to keep in mind to implement this technique is to get an pre-trained network. It is common to choose an architecture that was trained on the famous <a href="http://www.image-net.org/">ImageNet</a> dataset. For instance, one could choose to use a VGG16 network available on any programming language.</p><p>The importance of getting a well trained network is that its inner weights are the responsible for guiding the image transformation.</p><p>In case we use a non-trained network, this technique would simply not work. It would transform the input image into non-interpretable noise.</p><h4>Loss Function</h4><p>After choosing the model, we have to switch the way that its loss function is calculated. That is the biggest difference between this technique an other applications such as classification.</p><p>On traditional applications, the loss is calculated on the last layer. <br>Let’s use as an example the traditional task of classifying the image on whether there is an object present or not. During training, we feed an image to the network and on the last layer it tells us if it thinks whether the object is present. We then update all the network starting from the last layer to the first one in order to minimize this classification error using the famous back-propagation algorithm.</p><p>For style transfer, we calculate loss based on intermediate layers. Therefore, we do not use only the value from the last layer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yBwImXzxurctOVj7y4RYvg.png" /><figcaption>Representation of intermediate layers from <a href="https://arxiv.org/abs/1508.06576">Gatys et al (2015).</a></figcaption></figure><p>The steps to calculate the loss for style transfer are as follows:</p><ul><li>Feed the <strong>style</strong> picture once in the network and save the activations on the intermediate layers</li><li>Feed the <strong>content</strong> picture once in the network and save the activations on the intermediate layers</li><li>Feed an <strong>input </strong>as a random noise image to be transformed and get the activations for that image on the same layers as we got for the <strong>style </strong>and <strong>content </strong>images.</li><li>Using these activation values for the <strong>style, content </strong>and <strong>input </strong>images on these different layers, get a function that should output a number representing the loss</li></ul><p>The tricky thing on this process is what is the form of this loss function. That is one of the big contributions of <a href="https://arxiv.org/abs/1508.06576">Gatys et al (2015).</a> I will not into the calculation, but you can check how they combine the style and content loss in its methods section.</p><p>It is worth noticing that you could try creating your own loss function and checking if it works. The function described on the paper is not necessarily the only one that should work.</p><h4>Updating the image</h4><p>Another key point of this technique is what gets updated during training. Traditionally on classification applications, we would update the weights of the network applying back-propagation. Conversely, for Style Transfer we want to update the input image and not the weights of the network.</p><p>To update the input image, we <strong>freeze all the weights of the network</strong>. That means that we tell the algorithm to not update the layers. Instead, we tell it to propagate the loss all the way back to the input image and update this input so that we minimize our function designed previously.</p><p>We can let this process run until there is a satisfactory result. At the end, the input image that could have been purely noise, becomes something that contains the <strong>content </strong>picture — a building for instance — as if it was a painting based on the <strong>style </strong>image.</p><h3>Final Thoughts</h3><p>What was discussed here is the basic technique for style transfer trying to give an intuition on how it works without going too in depth on the math.<br>Since 2015 there were several other interesting applications and improvements based on this.</p><p>If you liked, follow me and leave some claps. Tell me in the comments what you would want to know next.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=39337b4b70b6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/deeplearningbrasilia/deep-learning-style-transfer-39337b4b70b6">Deep Learning: Style Transfer</a> was originally published in <a href="https://medium.com/deeplearningbrasilia">deeplearningbrasilia</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deep Learning: Convolutional Neural Networks]]></title>
            <link>https://medium.com/deeplearningbrasilia/deep-learning-convolutional-neural-networks-7b48bde10c0?source=rss-482df4c176c4------2</link>
            <guid isPermaLink="false">https://medium.com/p/7b48bde10c0</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Pedro Borges]]></dc:creator>
            <pubDate>Thu, 25 Oct 2018 20:21:37 GMT</pubDate>
            <atom:updated>2018-10-25T20:21:37.614Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>I tried giving the intuition and basic components of how CNNs work. Please leave a comment about it or about something else that you would want me to write next. Follow me and show some love clapping if you like it</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*4VDck-B6jZyinDdN.png" /></figure><p><strong>Convolutional Neural Networks (CNNs) </strong>are the Neural Networks tools for problems related with images. It can also be used in other domains, but it is responsible for the big leap in advancements with everything related to image processing. It is part of several camera related programs we use every day. <br>For instance, It is used for detecting a human face is on your picture, identifying who are the people present and more.<br>Besides these applications, they are widely used in other things such as automatic detection of deseases on medical images, image processing in self-driving cars, satellite image applications.</p><p>In this post, I explain its basic components and try to give some intuition of its inner workings.</p><h3>Building Blocks</h3><h4>Convolution</h4><p>What makes the CNNs distinct when compared to other architectures is the convolutional layers it has.</p><p>The convolution is simply a set of multiplications and sums. Let’s imagine that the input to our network is an image so we can illustrate the operations.<br>When we say we perform a convolution on this image, what happens is that we slide a little windows over the image. Each of the the cells of the window contains a number. When the window is on top of a set of pixels, we multiply the corresponding value of the pixel with the corresponding value of the cell and sum everything together.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/390/0*GwuNZzzKEIp0xfW6.gif" /><figcaption>Convolution visualization</figcaption></figure><p>The convolutional layers are layers that use this operation. Each layer can have a distinct window size and a step-size corresponding to how much the window slides to calculate its next output.</p><p>The idea of convolution on images is not used only in the domain of neural networks. Traditionally, these convolutions would be filters that are applied to an image. The distinct window with its weights is called kernels. There are several different types of kernels used to transform images such as edge detection, blurring, sharpening ones.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Hy8TUFFuso032ipxAEOdvg.png" /><figcaption>Original image (Left). Image after passing through an edge detection convolution (Right)</figcaption></figure><p>When the convolution is used in the inner layers of the network, there might not be an easy interpretation on what it does with the image. The inner convolutional layers might transform the signal in a way that does not necessarily make sense for a human.</p><h4>Subsampling</h4><p>Subsampling is used to reduce the image size or the internal representation of the image in intermediary layers.</p><p>This is specially important for image applications. We can understand why when we remember that the input to the network is the value for each pixel. An image of 320x240 has a total of 76800 pixels. If we increase the resolution , for instance to 1280x720, the image ends up with 921600 pixels. On top of that, the CNNs tend to be very deep. Carrying that much information through so many layers would make its use unfeasible.</p><p>One of the most common methods of subsampling is called Max Pooling.</p><p>It uses a sliding window like explained previously for convolution. The difference is that the window returns the activation with the highest value. So the operation is simpler compared with the convolution.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/570/0*Yz-ZerkuKSMqlTXv.png" /><figcaption>Max Pooling example</figcaption></figure><h3>How to use a CNN</h3><p>A CNN might have several layers and contain millions of internal weights that are automatically tuned during training. That makes these Neural Networks very computationally intensive to train.</p><p>To mitigate this issue, it is common to use a technique called transfer learning. That means to use a pre-trained Network and continue its training on your specific dataset.</p><p>There are several different CNN architectures implemented in lots of programming languages on the Internet.</p><h3>Famous Architectures</h3><p>There are several variations of the most different ways of combining the different building blocks that constitute a CNN. I will not get into details of the architectures, but some that are used are:</p><ul><li>VGG 16</li><li>RestNet</li><li>Inception</li><li>DenseNet</li></ul><h3>Final Thoughts</h3><p>The CNN is the Neural Network tool that attracted attention to neural networks due to the amazing performance increase in image processing tasks that were previously reaching a plateau. Since then, they are used in the most varied situations that have any relation with images. <br>Every year there is a new technique and application showing more impressive results using CNNs.</p><p>I hope I helped giving a basic understanding on how these networks work. Follow me and clap if you liked the post. Please comment below about which subject you would like to get more information.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7b48bde10c0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/deeplearningbrasilia/deep-learning-convolutional-neural-networks-7b48bde10c0">Deep Learning: Convolutional Neural Networks</a> was originally published in <a href="https://medium.com/deeplearningbrasilia">deeplearningbrasilia</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deep Learning: Recurrent Neural Networks]]></title>
            <link>https://medium.com/deeplearningbrasilia/deep-learning-recurrent-neural-networks-f9482a24d010?source=rss-482df4c176c4------2</link>
            <guid isPermaLink="false">https://medium.com/p/f9482a24d010</guid>
            <category><![CDATA[recurrent-neural-network]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <dc:creator><![CDATA[Pedro Borges]]></dc:creator>
            <pubDate>Thu, 18 Oct 2018 13:44:02 GMT</pubDate>
            <atom:updated>2018-10-18T13:54:12.183Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Recurrent Neural Networks</strong> (RNNs) are the Neural Network tools for problems that deal with sequential data.<br>It became increasingly more popular due to their great results in Natural Language Processing (NLP).<br>Within NLP they are used for the most varied tasks like translation, text classification, automatic text generation.</p><p>Here, I briefly explain the basic structure of RNNs</p><h3>Basic Building Block</h3><p>RNNs are normally shown like the picture below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SKGAqkVVzT6co-sZ29ze-g.png" /><figcaption>Representation of RNN both in folded and unfolded forms</figcaption></figure><p>In the picture, there are some distinct components, from which the most important are:</p><ul><li><strong>x: </strong>The input. It can be a word in a sentence or some other type of sequential data</li><li><strong>O: </strong>The output. For instance, what the network thinks the next word on a sentence should be given the previous words</li><li><strong>h: </strong>The main block of the RNN. It contains the weights and the activation functions of the network</li><li><strong>V: </strong>Represents the communication from one time-step to the other.</li></ul><p>The folded and unfolded representations of the network in the picture are equivalent. It is sometimes useful to unfold the network to get a better understanding of what is happening at each step.</p><p>One very important aspect to notice is that, even though the unfolded version shows several <strong>h </strong>blocks, the <strong>h </strong>block used is always the same. <br>The <strong>h </strong>block sends its output back to itself.<br>It keeps doing that until it is told to stop.</p><p>The simplest kind of <strong>h </strong>block is in the picture below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*biau76XDRvQG-yWARY-lLg.png" /><figcaption>Representation of simple RNN block</figcaption></figure><p>This block simply combines the input — a word for instance — with the output of itself from the previous time-step and passes it though a <strong>tanh </strong>activation to get the output of the current time-step.</p><p>To illustrate how it works, we can think about the problem of predicting the next word of a phrase. Let’s take the phrase “<em>Bananas are” </em>as an example. The task should be to give us what is the third word.</p><p>On the first time-step, we feed the network the word <em>Bananas</em>. That word will go through the <strong>h </strong>block. Then the output of the <strong>h </strong>block will go inside itself along with the next word <em>are</em>. At this point, the network stops feeding itself and gives us what it thinks should be the next word. In this case, it could say <em>yellow.</em></p><h3>Long short-term memory (LSTM) and Gated Recurrent Unit (GRU)</h3><p>The basic building block for RNN shown above suffers from some problems. One of the most important is the inability to retain information when the sequence given is long. It forgets information that was supplied several time-steps ago. That limits the learning performance. So people created some architectures to tackle that. The most popular are <strong>Long short-term memory </strong>(LSTM) and <strong>Gated Recurrent Unit </strong>(GRU)</p><p>These two are sibling architectures which are used in the great majority of applications. The distinction of these two with relation to the basic one is what the <strong>h </strong>block contains. I will not go in detail about each one, but the pictures below show the inner components of both LSTM and GRU.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f8716Sl6BMPUkNHattRWiQ.png" /><figcaption>LSTM block</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aAStXOSlt2mtXYORAHYliw.png" /><figcaption>GRU block</figcaption></figure><h3>Deep RNN</h3><p>It is possible to concatenate the building blocks we have seen previously to create a longer network. The picture below show the representation of what would be a network with two RNN layers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/522/1*CRRtMrhFeUN-zlWo9DsH0w.png" /><figcaption>2 layer RNN</figcaption></figure><p>Each of these will be unfolded several times when you want to train or perform inference on the network. Then, it is not common to concatenate many of these RNN blocks since it would turn the network too computationally demanding to be used in practice.</p><p>The architectures tend to use up to 3 concatenated RNN layers, but it is rare to use much more than that.</p><h3>Back Propagation Through Time (BPTT)</h3><p>The <strong>Back Propagation </strong>algorithm used to train neural networks receives a special name in the case of RNNs. It works the same way, which is applying the chain rule on the network and updating the weights going from last to first layer.</p><p>It receives this special name because there are not really distinct layers that are updated, but the same one that is updated at each time-step.</p><h3>Final Thoughts</h3><p>Compared to other architectures like the famous Convolutional Neural Networks (CNN) widely used in computer vision, the RNNs are simpler. They contain fewer blocks and layers. However they might still require a lot of computational power due to the way they need to be unrolled.</p><p>Finally, even though RNNs have proven themselves by achieving incredible performance in sequence related tasks — in special NLP applications — I do believe we will still have some great improvements in the next couple of years.</p><blockquote>If you liked, the post, follow me, and give it a clap. Let me know what else you would like me to write about</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f9482a24d010" width="1" height="1" alt=""><hr><p><a href="https://medium.com/deeplearningbrasilia/deep-learning-recurrent-neural-networks-f9482a24d010">Deep Learning: Recurrent Neural Networks</a> was originally published in <a href="https://medium.com/deeplearningbrasilia">deeplearningbrasilia</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Serverless Infrastructures]]></title>
            <link>https://medium.com/@pedrohbtp/serverless-infrastructures-b2bc62b4e3af?source=rss-482df4c176c4------2</link>
            <guid isPermaLink="false">https://medium.com/p/b2bc62b4e3af</guid>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[aws-lambda]]></category>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Pedro Borges]]></dc:creator>
            <pubDate>Thu, 11 Oct 2018 20:55:41 GMT</pubDate>
            <atom:updated>2018-10-11T20:55:41.934Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/940/0*r7v99ghaqNKoRj1b" /></figure><p><strong>Serveless</strong> infrastructures are the solution for implementing true auto-scaling and tying your costs with how much your application is used.</p><p>The traditional path of putting software into production is to test on a development computer and then try to find a server to host your code. Sometimes it can be a server maintained by the organization itself. Where you buy all the hardware and install the software there.<br>What has been very popular is to hire cloud resources from companies like Amazon, Google, IBM and so on to run your code. <br>Normally these hired services charge by the hour that you use a specified type of cloud computer. If there is code or not running, you are charged for using the machine.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i3WV7sGbUEcVSNa3hp0hcw.png" /><figcaption>AWS EC2 pricing</figcaption></figure><p>The great problem with that approach is that it does not scale easily. If you have a lot of traffic only in determined periods of the day, you might have to hire a more capable machine that costs more. Most of the time, the machine will be underused.</p><p>A new trend has showed up in the last couple of years that is a step ahead of charging for a machine. <br>You get charged only by the execution of the code. For instance, if you have a lot of traffic during an hour of your day, it will charge elastically for that hour and not for the other ones with no requests. That approach is called <strong>Serverless.</strong></p><h3>AWS Lambdas</h3><p>The product following the serverless architecture I am the most familiar with is the Lambdas Service offered by AWS.</p><p>Its <a href="https://aws.amazon.com/lambda/pricing/">pricing</a> is based on the amount of time the code is executed on the machine.</p><p>Let’s say, for instance that you have a function that receives an image over a POST request and stores this image on AWS S3. If each call takes 1ms and in one day you have 1000 calls, then you would be charged for only 1 second of execution time. The entire rest of the day would not be put in your bill.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OmvwvPy9kYhcBEjy_gYNOA.png" /><figcaption>AWS Lambda Pricing</figcaption></figure><p>It is possible to perform a multitude of tasks with this tool ranging from internal code for your application to exposing a web server for your public API.</p><h3>Google Cloud Function and Microsoft Azure Functions</h3><p>Both <a href="https://cloud.google.com/functions/pricing">Google Cloud Function</a> and <a href="https://azure.microsoft.com/en-us/pricing/details/functions/">Microsoft Azure Functions</a> follow the same philosophy of AWS Lambda and could be an alternative in case you do not use the AWS environment</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E8WEFBr5XhQmYpf3r3eGLg.png" /><figcaption>Google Cloud Function</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*utZfz7UnR7MeD17xzclnFA.png" /><figcaption>Microsoft Azure Pricing</figcaption></figure><h3>Final Thoughts</h3><p>These serverless architectures are growing beyond services like AWS Lambda. For instance, AWS recently released a database service called Aurora Serveless which scales your database so you don’t have to care about doing it yourself.</p><p>I am a big advocate of this model. It helps you tie your costs with how much your application is being used. <br>I do believe it is here to stay and it will expand significantly in the next years.</p><p><em>I would love to learn more with you. <br>Leave a comment below with your thoughts and also ideas on what you would want be to write about!</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b2bc62b4e3af" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Upverter — My experience designing PCBs]]></title>
            <link>https://medium.com/@pedrohbtp/upverter-my-experience-with-designing-pcbs-38b2c70605fb?source=rss-482df4c176c4------2</link>
            <guid isPermaLink="false">https://medium.com/p/38b2c70605fb</guid>
            <category><![CDATA[pcb]]></category>
            <category><![CDATA[electronics]]></category>
            <category><![CDATA[hardware]]></category>
            <dc:creator><![CDATA[Pedro Borges]]></dc:creator>
            <pubDate>Mon, 10 Sep 2018 18:12:37 GMT</pubDate>
            <atom:updated>2018-09-10T18:14:02.207Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://upverter.com/">Upverter</a> is an entirely online Printed Circuit Board software. I have used it since around 2016 to design something like 4 boards. I am not associated with Upverter in any form, but I do believe it is a very useful software worth mentioning.<br>In this article I am going to briefly discuss about its characteristics and my personal opinions.</p><h3>Pricing</h3><p><a href="https://upverter.com/pricing/">Upverter is currently <em>Free</em></a>. <br>This aspect is specially important if you want to develop commercial products. It is common that companies offer a limited free version of a software where you could only use it to open source/open hardware products. <br>In the beginning Upverter had distinct pricing plans with different features. It has since changed its business model to make the use of the software free. <br>The current source of revenue for the company seems to be their Concierge services. It is possible to order <a href="https://upverter.com/features/layout-concierge/">board layout</a>, <a href="https://upverter.com/features/parts-concierge/">components footprint and schematic</a>.</p><h3>Collaborating</h3><p>One of Upverter’s most interesting characteristic is that you can work with others at the same time in the same design. For instance, two people can work with the same schematics. While one deals with a DC-DC converter, the other can work on a sensor. It is just like a Google Docs for PCBs. For me that has been a large productivity upgrade.</p><p>Besides that, it is possible to check and fork the several public designs created by the community.</p><h3>Verified Parts Database</h3><p>There is a great parts database. For most of the common parts, there is no need to design the schematics or footprints. Their parts search is generally good. A verified parts will be cleared labeled. <br>I have never had problems with their verified parts, which takes a big weight off the shoulders not having to double check every component.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HZoEwwI67PQEQz12ilfUOQ.png" /></figure><h3>PCB Layout</h3><p>Their layout tools are generally easy to use. It is possible to change between metric and imperial system. They offer good default values for the vias and traces and it is possible to create complex multi-layered designs.<br>The one thing that might be missing is the functionality to auto-trace the board. However it is understandable that they will not offer that in the future since that part of their business model is to offer board layout for hire.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Msy3PhVivk1_WMmnTC9RVg.png" /></figure><h3>The issues</h3><p>There are surely some bugs in Upverter. <br>The program might crash and reload when moving too many parts at the same time. For example, I would select several components and try to drag them and it would make the browser to refresh.<br>The connection with their servers seem a little unstable sometimes. Upverter normally saves the design on every change. It happens, however, that it cannot save after some time. It shows on the screen when was the last checkpoint, so you need to make sure it was saved before you close the design.<br>There are some other bugs such as when moving a component in the schematics, the wire connected to it might move in a weird way. It would sometimes even the connection.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Wh8yozZkXWoNfotrvKHPhw.png" /></figure><p>When you get used to these bugs, they don’t bother you as much.</p><h3>TLDR</h3><p>Upverter is free a colaborative PCB running in your browser. It has a newer feel compared to other circuit software. It has a large database of verified parts ready to use which might make you more confident about your design. It has some bugs, but overall I like it a lot and highly recommend it.</p><p>If someone is interested I could give my perspective in some other aspect of it.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=38b2c70605fb" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>