<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Pol Monroig Company</title>
    <description>The latest articles on DEV Community by Pol Monroig Company (@polcompany).</description>
    <link>https://dev.to/polcompany</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F331892%2F9c0ec902-e212-4d38-8e0c-9d9f695e7c4e.jpg</url>
      <title>DEV Community: Pol Monroig Company</title>
      <link>https://dev.to/polcompany</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/polcompany"/>
    <language>en</language>
    <item>
      <title>The Perfect Activation</title>
      <dc:creator>Pol Monroig Company</dc:creator>
      <pubDate>Thu, 20 Aug 2020 17:02:03 +0000</pubDate>
      <link>https://dev.to/polcompany/the-perfect-activation-33l6</link>
      <guid>https://dev.to/polcompany/the-perfect-activation-33l6</guid>
      <description>&lt;p&gt;It might be too bold to call an activation function perfect, given that the &lt;strong&gt;No Free Lunch Theorem&lt;/strong&gt; of machine learning states that there is no universally perfect machine learning algorithm. Nevertheless, as misleading as the title can be,  I will try to summarize the most widely used activation functions and describe their main differences. &lt;/p&gt;

&lt;h1&gt;
  
  
  Linear (identity)
&lt;/h1&gt;

&lt;p&gt;The linear activation function is essentially no activation at all. &lt;br&gt;
&lt;strong&gt;Overhead:&lt;/strong&gt; fastest, no computation at all &lt;br&gt;
&lt;strong&gt;Performance:&lt;/strong&gt; bad, since it does not enable a non linear transformation &lt;br&gt;
&lt;strong&gt;Advantages:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Differentiable at all points &lt;/li&gt;
&lt;li&gt;Fast execution &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common issues:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Does not provide any non-linear output. &lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;
  
  
  Sigmoid
&lt;/h1&gt;

&lt;p&gt;The Sigmoid activation function is one of the oldest ones. Initially made to mimic the activations in the brain it has been shown to have poor performance on artificial neural networks, nevertheless it is commonly used and a classifier output to transform outputs into class probabilities.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Uses:&lt;/strong&gt; it is commonly used in the output layer of binary classification where we need a probability value between 0 and 1. &lt;br&gt;
&lt;strong&gt;Overhead:&lt;/strong&gt; very expensive because of the exponential term. &lt;br&gt;
&lt;strong&gt;Performance:&lt;/strong&gt; bad on hidden layers, mostly used on output layers &lt;br&gt;
&lt;strong&gt;Advantages:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Outputs are between 0 and 1, that means that values won't explode. &lt;/li&gt;
&lt;li&gt;It is differentiable at every point. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common issues:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Outputs are between 0 and 1, that means outputs might saturate. &lt;/li&gt;
&lt;li&gt;Vanishing gradients are possible. &lt;/li&gt;
&lt;li&gt;Outputs are always positive ( zero centered functions help in a faster convergence). &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Code:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Pytorch 
&lt;/span&gt;&lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;nn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Sigmoid&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; 
&lt;span class="c1"&gt;# Tensorflow 
&lt;/span&gt;&lt;span class="n"&gt;tf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;keras&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;activations&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sigmoid&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F1mt954pdqqsoha16ear1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F1mt954pdqqsoha16ear1.png" alt="Alt Text" width="800" height="444"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Softmax
&lt;/h1&gt;

&lt;p&gt;Generalization of the Sigmoid  function to more than one class, it enables to transform the outputs into multiple probabilities. Used in multiclass classification. &lt;br&gt;
&lt;strong&gt;Uses:&lt;/strong&gt; used in the output layer of a multiclass neural network. &lt;br&gt;
&lt;strong&gt;Overhead:&lt;/strong&gt; similar to Sigmoid, but more overhead caused by more inputs.&lt;br&gt;
&lt;strong&gt;Performance:&lt;/strong&gt; bad on hidden layers, mostly used on output layers &lt;br&gt;
&lt;strong&gt;Advantages:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Unlike Sigmoid, it ensures that outputs are normalized between 0 and 1&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common issues:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Same as Sigmoid.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Code:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Pytorch 
&lt;/span&gt;&lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;nn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Softmax&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dim&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;...)&lt;/span&gt; 
&lt;span class="c1"&gt;# Tensorflow 
&lt;/span&gt;&lt;span class="n"&gt;tf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;keras&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;activations&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;softmax&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Hyperbolic Tangent
&lt;/h1&gt;

&lt;p&gt;Tanh function has the same shape as Sigmoid, in fact is the same but it is mathematically shifted and it works better in most cases. &lt;br&gt;
&lt;strong&gt;Uses:&lt;/strong&gt;  generally used in hidden layers as it outputs between -1 and 1, thus creating normalized outputs, making learning faster. &lt;br&gt;
&lt;strong&gt;Overhead:&lt;/strong&gt; very expensive, since it uses an exponential term. &lt;br&gt;
&lt;strong&gt;Performance:&lt;/strong&gt; similar to Sigmoid but with some added benefits&lt;br&gt;
&lt;strong&gt;Advantages:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Outputs are between -1 and 1, that means that values won't explode. &lt;/li&gt;
&lt;li&gt;It is differentiable at every point.
&lt;/li&gt;
&lt;li&gt;It is zero-centered, unlike Sigmoid. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common issues:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Vanishing gradients. &lt;/li&gt;
&lt;li&gt;Gradients saturation. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Code:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Pytorch 
&lt;/span&gt;&lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;nn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Tanh&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; 
&lt;span class="c1"&gt;# Tensorflow 
&lt;/span&gt;&lt;span class="n"&gt;tf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;keras&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;activations&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;tanh&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fuz2khss64owahi5vkohr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fuz2khss64owahi5vkohr.png" alt="Alt Text" width="800" height="444"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  ReLU
&lt;/h1&gt;

&lt;p&gt;ReLU, also called rectified linear unit is one of the most commonly used activations, both for its computational efficiency and its great performance. Multiple variations have been created to improve its flaws. &lt;br&gt;
&lt;strong&gt;Uses:&lt;/strong&gt; must be used in hidden layers as it provides better performance than tanh and Sigmoid, and is more efficient since it is computationally faster. &lt;br&gt;
&lt;strong&gt;Overhead:&lt;/strong&gt; Almost none, extremely fast. &lt;br&gt;
&lt;strong&gt;Performance:&lt;/strong&gt; great performance, recommended for most cases. &lt;br&gt;
&lt;strong&gt;Advantages:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Adds non-linearity to the network. &lt;/li&gt;
&lt;li&gt;Does not suffer from vanishing gradient. &lt;/li&gt;
&lt;li&gt;Does not saturate. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common issues:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It suffers from dying ReLU&lt;/li&gt;
&lt;li&gt;Not differentiable at x = 0 &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Code:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Pytorch 
&lt;/span&gt;&lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;nn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ReLU&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; 
&lt;span class="c1"&gt;# Tensorflow 
&lt;/span&gt;&lt;span class="n"&gt;tf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;keras&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;activations&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;relu&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fpnwwr2cs5ftohlpfua94.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fpnwwr2cs5ftohlpfua94.png" alt="Alt Text" width="800" height="443"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Leaky Relu
&lt;/h1&gt;

&lt;p&gt;Given that ReLU suffers from the dying relu problem where negative values are rounded to 0. Leaky ReLU tries to diminish the problem by changing the 0 output by a very small value. &lt;br&gt;
&lt;strong&gt;Uses:&lt;/strong&gt; used in hidden layers. &lt;br&gt;
&lt;strong&gt;Overhead:&lt;/strong&gt; same as ReLU&lt;br&gt;
&lt;strong&gt;Performance:&lt;/strong&gt; great performance if the hyperparameter is chosen correctly &lt;br&gt;
&lt;strong&gt;Advantages:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Similar to ReLU and fixes dying ReLU. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common issues:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;New hyperparameter to tune. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Code:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Pytorch 
&lt;/span&gt;&lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;nn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LeakyReLU&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;negative_slope&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;...)&lt;/span&gt; 
&lt;span class="c1"&gt;# Tensorflow 
&lt;/span&gt;&lt;span class="n"&gt;tf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;keras&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;layers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LeakyReLU&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alpha&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;...)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fnsb3l2b2pyntx7k1pcv8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fnsb3l2b2pyntx7k1pcv8.png" alt="Alt Text" width="800" height="443"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Parametric ReLU
&lt;/h1&gt;

&lt;p&gt;Takes the same idea as leaky ReLU but instead of predifining the leaky hyperparemeter, it is added as a parameter that must be learned. &lt;br&gt;
&lt;strong&gt;Uses:&lt;/strong&gt; used in hidden layers. &lt;br&gt;
&lt;strong&gt;Overhead:&lt;/strong&gt; a new parameter must be learned for each PreLU in the network. &lt;br&gt;
&lt;strong&gt;Performance:&lt;/strong&gt; bad on hidden layers, mostly used on output layers &lt;br&gt;
&lt;strong&gt;Advantages:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fixes the need of tuning an hyperparameter &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common issues:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The parameter learned is not guaranteed to be the optimum, and it increases the overhead, so you might as well try some yourself with leaky. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Code:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Pytorch 
&lt;/span&gt;&lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;nn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;PReLU&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="c1"&gt;# Tensorflow 
&lt;/span&gt;&lt;span class="n"&gt;tf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;keras&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;layers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;PReLU&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fnsb3l2b2pyntx7k1pcv8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fnsb3l2b2pyntx7k1pcv8.png" alt="Alt Text" width="800" height="443"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  ELU
&lt;/h1&gt;

&lt;p&gt;The ELU was introduced as another alternative to fix the issues that you can encounter with ReLU. &lt;br&gt;
&lt;strong&gt;Uses:&lt;/strong&gt; used in hidden layers&lt;br&gt;
&lt;strong&gt;Overhead:&lt;/strong&gt; computational expensive, it uses an exponential term&lt;br&gt;
&lt;strong&gt;Performance:&lt;/strong&gt; bad on hidden layers, mostly used on output layers &lt;br&gt;
&lt;strong&gt;Advantages:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Similar to reLU. &lt;/li&gt;
&lt;li&gt;Produces negative outputs. &lt;/li&gt;
&lt;li&gt;Bends smoothly unlike leakyReLU. &lt;/li&gt;
&lt;li&gt;Differentiable at x = 0 &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common issues:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Additional hyperparameter &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Code:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Pytorch 
&lt;/span&gt;&lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;nn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ELU&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; 
&lt;span class="c1"&gt;# Tensorflow 
&lt;/span&gt;&lt;span class="n"&gt;tf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;keras&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;activations&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;elu&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fgc9skcsszpl77o5oxqhm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fgc9skcsszpl77o5oxqhm.png" alt="Alt Text" width="800" height="443"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Other alternatives
&lt;/h1&gt;

&lt;p&gt;There are a lot of activations functions to cover them all in a single post. Here are some: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;SeLU &lt;/li&gt;
&lt;li&gt;GeLU &lt;/li&gt;
&lt;li&gt;CeLU &lt;/li&gt;
&lt;li&gt;Swish &lt;/li&gt;
&lt;li&gt;Mish &lt;/li&gt;
&lt;li&gt;Softplus &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Note: if it ends with LU it usually comes from ReLU.&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Summary
&lt;/h1&gt;

&lt;p&gt;So... having so many choices, which activation should we use? As a &lt;strong&gt;rule of thumb&lt;/strong&gt; you should always try using ReLU in the hidden layers, as it has a great performance with minimal computational overhead. After that (if you have enough computing power) you might want to try with some complex variations of ReLU or similar alternatives. I would never recommend using Sigmoid, Tanh or Sotfmax for any hidden layer. Sigmoid and Softmax should be used whenever we want probabilities outputs for a classification task. Finally, with the current progress and research in deep learning and AI surely new and better functions will appear, so keep an eye out. &lt;/p&gt;

&lt;p&gt;Remember to &lt;strong&gt;try and experiment always&lt;/strong&gt;, you never know which function will work better for a specific task.&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>math</category>
    </item>
    <item>
      <title>Making algorithms work together</title>
      <dc:creator>Pol Monroig Company</dc:creator>
      <pubDate>Thu, 13 Aug 2020 07:40:30 +0000</pubDate>
      <link>https://dev.to/polcompany/making-algorithms-work-together-2m97</link>
      <guid>https://dev.to/polcompany/making-algorithms-work-together-2m97</guid>
      <description>&lt;p&gt;In every machine learning application you may have found that every machine learning algorithm you try has its flaws. As the no free lunch theorem states &lt;em&gt;there is no single learning algorithm in any domain always induces the most accurate learner&lt;/em&gt;. Learning is ill-posed since we do not have all the data and each algorithm converges for a specific subset of the original data. What if there was a way to combine the knowledge of different algorithms?(Spoiler: there is!)&lt;/p&gt;

&lt;h1&gt;
  
  
  Combining base-learners
&lt;/h1&gt;

&lt;p&gt;The idea is to have multiple base learners, each an expert in a subset of the dataset and accurate in it. This way each learner complements each other. There are two aspects we must consider: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We must choose base-learners that &lt;strong&gt;complement&lt;/strong&gt; each other (otherwise we would have unnecessary redundancy). A synonym to this is that they must &lt;strong&gt;not be correlated&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;Each learner must try to have the highest accuracy, we should also find a way to &lt;strong&gt;combine the output&lt;/strong&gt; of each learner to achieve it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fype32gssjine5bwok0v0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fype32gssjine5bwok0v0.png" alt="Alt Text" width="800" height="642"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Diversifying learners
&lt;/h1&gt;

&lt;p&gt;There are multiple ways in which we can diversify each learning algorithm. For instance, we can use &lt;strong&gt;different algorithms&lt;/strong&gt;, we can also try similar algorithms but with &lt;strong&gt;different hyperparameters&lt;/strong&gt; or each algorithm could be trained with a &lt;strong&gt;different data subset&lt;/strong&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  Voting
&lt;/h3&gt;

&lt;p&gt;Voting is the most simple technique in which the output of multiple classifiers is combined using a &lt;strong&gt;linear combination&lt;/strong&gt; (i.e. sum, product, median, weighted sum, maximum, minimum). The main idea behind this method is the we create a voting scheme over high variance and low bias models, thus bias remains small and variance is reduced. An analogy to this is when we have multiple noisy samples of the same data, if we combine them it is much easier to sort out the noise.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bagging
&lt;/h3&gt;

&lt;p&gt;This is variance of the voting method. The difference is that each base-learner is trained on different training sets. To generate each set we draw instances randomly with replacement. By replacing the instances it is possible that many instances are repeated and that some are not used at all. &lt;/p&gt;

&lt;h3&gt;
  
  
  Boosting
&lt;/h3&gt;

&lt;p&gt;This type of mixer works by using simple learners that actively learn on the mistake of the previous learners, the most commonly and effective use of boosting is called &lt;strong&gt;AdaBoost&lt;/strong&gt;. For AdaBoost to work, the learners must be very weak, otherwise we have a high risk of overfitting. AdaBoost combines the votes based on weights proportional to the accuracy of each base-learner in the training set. &lt;/p&gt;

&lt;h3&gt;
  
  
  Stacking
&lt;/h3&gt;

&lt;p&gt;Stacking is mechanism in which multiple base-learners are trained using the train dataset. After that, a new training data is created by voting the predictions of each base-learner. Finally, another learner (called the mixer) learns from the predictions of the other algorithms. &lt;/p&gt;

&lt;h3&gt;
  
  
  Cascading
&lt;/h3&gt;

&lt;p&gt;This is based on a sequence of classifiers, in which each learner is more complex than the latter, and thus generalize less. We first make a prediction with a simple learner. This learner tries to generalize as well as we can, if the confidence is not sufficient, we continue with the next learner. We continue making predictions until the prediction confidence of a learner is high enough. A common approach to this method is to use a weak algorithm such as linear regression as the first learner and a non-parametric approach for subsequent learners. The idea behind this method is that most samples can be explained with simple rules, and that few exceptions are explained with more complex learners. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: Combining learners is not always the best approach. It increases the speed and it has an execution overhead so you must be sure if it is worth the effort.&lt;/em&gt; &lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Decision trees uncovered</title>
      <dc:creator>Pol Monroig Company</dc:creator>
      <pubDate>Thu, 06 Aug 2020 09:14:04 +0000</pubDate>
      <link>https://dev.to/polcompany/decision-trees-uncovered-5907</link>
      <guid>https://dev.to/polcompany/decision-trees-uncovered-5907</guid>
      <description>&lt;p&gt;If you are a computer scientist I am sure you agree with me when I say that trees are everywhere. And I mean everywhere! It is extremely common to use trees as a basic data structure to improve and define new algorithms in all sorts of domains. Machine learning is no different; decision trees are one of the most used nonparametric methods, it can be used for both classification and regression.&lt;/p&gt;

&lt;p&gt;Decision trees are hierarchical models that work by splitting the input space into smaller regions. A tree is composed of &lt;strong&gt;internal decision nodes&lt;/strong&gt; and &lt;strong&gt;terminal leaves&lt;/strong&gt;. Internal decision nodes implement a &lt;strong&gt;test function&lt;/strong&gt;, this function works by given a set of variables (the most used approach is to use &lt;strong&gt;univariate trees&lt;/strong&gt;, that is trees that test only 1 variable at a given node) we get a discrete output corresponding to which child node we should go next. Terminal nodes correspond to predictions; a classification output might be the corresponding class, and a regression a specific numerical value. A great advantage of decision trees is that they can work using categorical values directly. &lt;/p&gt;

&lt;p&gt;For example, in the following tree, we might want to classify patients that required treatment versus patients that do not require it. Each node makes a decision based on a simple rule, and in each terminal nodes, we have the final prediction. The gini index is a measure of how impure the node is. If the impurity is equal to 0.0, that means we cannot split any further because we have reached a &lt;strong&gt;maximum purity&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F5safv416ocbxb77a5d68.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F5safv416ocbxb77a5d68.png" alt="Alt Text" width="800" height="529"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One of the perks of decision trees, compared to other machine learning algorithms, is that they are extremely easy to understand and have a high degree of interpretability. Just by reading the tree, you can make decisions yourself. On the other hand, decision trees are very sensitive to small variations in the training data, so it usually recommended to apply a boosting method. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: In fact, a decision tree can be transformed into a series of rules that can then be used in a rule-based language such as Prolog.&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Dimensionality reduction
&lt;/h1&gt;

&lt;p&gt;The job of classification and regression trees (&lt;strong&gt;CART&lt;/strong&gt;) is to predict an output based on the possible variables that the input might have; &lt;strong&gt;higher leaves&lt;/strong&gt; tend to divide more important features and &lt;strong&gt;lower leaves&lt;/strong&gt; tend to correspond to less important ones. That is why decision trees are commonly used as a dimensionality reduction technique. By running the CART algorithm you get the importance of each feature for free!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fig3jqq7bb9219t09qk1y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fig3jqq7bb9219t09qk1y.png" alt="Alt Text" width="593" height="411"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Error measures
&lt;/h1&gt;

&lt;p&gt;As any machine learning model, we must ensure to have a correct error function. It has been shown that any of the following error functions tend to perform well: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;MSE (&lt;/strong&gt;regression): it is one of the most common error function on machine learning&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Entropy&lt;/strong&gt; (classification): entropy works by measuring the number of bits needed to encode a class code, based on its probability of occurrence.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Gini index&lt;/strong&gt; (classification): Slightly faster impurity measure than entropy, it tends to isolate the most frequent class in its own branch of the tree, while entropy produces more balanced branches.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Note: Error functions on classification trees are also called impurity measures.&lt;/em&gt; &lt;/p&gt;

&lt;h1&gt;
  
  
  Boosting trees
&lt;/h1&gt;

&lt;p&gt;Decision trees are very good estimators, but sometimes they can perform poorly. Fortunately, there are many ensemble methods to boost their performance.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Random forests&lt;/strong&gt;: bagging/pasting method that works by training multiple decision trees, each with a subset of the dataset. Finally, each tree makes a prediction and they are all aggregated into a single final prediction.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AdaBoost&lt;/strong&gt;: a first base classifier is trained and used to make predictions. Then, a second classifier is trained on the errors that the first one had. This continues on and on until there are no more classifiers to train.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Stacking&lt;/strong&gt;: this idea works by creating a voting mechanism between different classifiers and create a blending classifier that is trained on the predictions of the other classifiers, instead of the data directly.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;The following image represents a stacking ensemble:&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fw9zysh39d8gckg2bfbif.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fw9zysh39d8gckg2bfbif.png" alt="Alt Text" width="800" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>decisiontrees</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>When accuracy is not enough...</title>
      <dc:creator>Pol Monroig Company</dc:creator>
      <pubDate>Wed, 29 Jul 2020 09:35:49 +0000</pubDate>
      <link>https://dev.to/polcompany/when-accuracy-is-not-enough-2hej</link>
      <guid>https://dev.to/polcompany/when-accuracy-is-not-enough-2hej</guid>
      <description>&lt;p&gt;The task of classification has existed long before the invention of machine learning. A problem that may arise when working with different algorithms is the use of an &lt;strong&gt;error function&lt;/strong&gt; that determines if an algorithm is good enough, with classification algorithms it is no different.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Folg2547hcyx52meiff0l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Folg2547hcyx52meiff0l.png" alt="Alt Text" width="636" height="282"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One of the most used metrics applied in these algorithms is the &lt;strong&gt;accuracy metric&lt;/strong&gt;; based on the total number of samples and the predictions made, we return the &lt;strong&gt;percentage of samples&lt;/strong&gt; that were correctly classified. But this method does not always work so well; imagine that we have a total of 1000 samples, and an algorithm called &lt;em&gt;DummyAlgorithm&lt;/em&gt; that tries to classify them in two different classes (A and B). Unfortunately, DummyAlgorithm does not know anything about the data distribution, as a result, it always tells us that a given sample is of type A. Now imagine that all the samples are of class A (you might see where I'm going). In this case, it is easy to see that even though DummyAlgorithm has a 100% accuracy rate, it is not a very good algorithm.&lt;/p&gt;

&lt;p&gt;In this post, we'll learn how we can complement the accuracy metric with other machine learning strategies that do take into account the problem described before. Consequently we'll see a method to avoid such a problem.&lt;/p&gt;

&lt;h1&gt;
  
  
  Definitions
&lt;/h1&gt;

&lt;p&gt;Before going any further, let's define some basic concepts. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Accuracy:&lt;/strong&gt; metric that returns the percentage of correctly classified samples in a dataset &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;True Positives:&lt;/strong&gt; samples that were correctly classified with their respective positive class &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;True Negatives:&lt;/strong&gt; samples that  were correctly classified with their respective negative class&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;False Positives:&lt;/strong&gt; samples that were classified as positives but were negatives &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;False Negatives:&lt;/strong&gt; samples that were classified as negatives but were positives &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Precision:&lt;/strong&gt; accuracy of the true positives (TP / TP + FP)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Recall:&lt;/strong&gt; ratio of positive instances that are correctly classified (TP / TP + FN)&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: when we talk about positives/negatives, we are talking about a specific class&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Confusion Matrix
&lt;/h1&gt;

&lt;p&gt;The confusion matrix creates a division for each of the four possible categorizations. It can be used in multiclass classification. In the following example we are making a binary classification that classifies red dots among other colors. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fh7seoqf0t45tjtw12hmz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fh7seoqf0t45tjtw12hmz.png" alt="Alt Text" width="800" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Precision vs recall tradeoff
&lt;/h1&gt;

&lt;p&gt;As with other metrics, the classifier has to make a decision in which if it wants to learn to have a better precision or a better recall. &lt;em&gt;Sometimes you care more about precision than you care about recall&lt;/em&gt;. For example, if you wish to detect safe for work posts in a social network, you would probably prefer a classifier that rejects many good videos (low recall) but keeps only safe ones (high precision). On the other hand, suppose you train a classifier to detect shoplifters, it is probably better that the classifier has the most recall as possible (the security system will get some false alerts, but almost all shoplifters will get caught. &lt;/p&gt;

&lt;p&gt;Based on this tradeoff we can define a curve called the &lt;strong&gt;precision/recall curve&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ft16pwipspe97wd6jei4t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ft16pwipspe97wd6jei4t.png" alt="Alt Text" width="800" height="385"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  ROC curve
&lt;/h1&gt;

&lt;p&gt;The ROC curve (receiver operating characteristic curve) is a very common tool used with binary classifiers. It is very similar to the precision/recall curve, but it plots the &lt;strong&gt;true positive rate&lt;/strong&gt; against the &lt;strong&gt;false positive rate&lt;/strong&gt;. One way to compare classifiers is to measure the &lt;strong&gt;area under the curve&lt;/strong&gt; (AUC). A perfect classifier will have a AUC equal to 1. A purely random classifier will have a ROC AUC equal to 0.5. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fbo9nz5shg2bs9nttsogk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fbo9nz5shg2bs9nttsogk.png" alt="Alt Text" width="800" height="524"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As the ROC curve and the precision/recall curve are very similar, it might be difficult to choose between them. A common approach is to use the precision/recall curve whenever the positive class is rare and when you care more about the false positives than the false negatives, and the ROC curve otherwise. &lt;/p&gt;

&lt;h1&gt;
  
  
  Solutions
&lt;/h1&gt;

&lt;p&gt;The accuracy problem essentially happens when the data the model is being tested with is unbalanced. To solve this issue there are several approaches. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you have a lot of training data you can discard some of it to create a more balanced data, although your model might generalize worse with less data, this approach must be used in special cases.&lt;/li&gt;
&lt;li&gt;Use a data augmentation technique to increase the data available.&lt;/li&gt;
&lt;li&gt;Use a resampling technique in which you make the training data bigger by using the same data, useful if the data augmentation approach is too complicated.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>machinelearning</category>
      <category>python</category>
      <category>metrics</category>
    </item>
    <item>
      <title>Optimal neural networks</title>
      <dc:creator>Pol Monroig Company</dc:creator>
      <pubDate>Fri, 24 Jul 2020 10:42:59 +0000</pubDate>
      <link>https://dev.to/polcompany/optimal-neural-networks-2l93</link>
      <guid>https://dev.to/polcompany/optimal-neural-networks-2l93</guid>
      <description>&lt;p&gt;Like everything in this world, finding the right path to a high-end goal can become tedious if you don't have the right tools. Each objective and environment has different requirements and must be treated differently. An example of this might be traveling, using a car to go to the grocery shop might be the fastest and most comfortable way to get there. On the other hand, if we want to travel abroad it might be a better idea to get  on an airplane (unless you are one of those who loves driving for hours). &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7emue40akmlpyeqe2c52.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7emue40akmlpyeqe2c52.jpg" alt="Alt Text" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But we are not here to talk about the different types of transportation, we are here to talk about how to improve the training of your neural networks and choosing the best optimizer based on the memory it uses, its complexity and speed. &lt;/p&gt;

&lt;h1&gt;
  
  
  Different optimizers
&lt;/h1&gt;

&lt;p&gt;Training a deep neural network can be very slow, there are multiple ways to improve the speed of convergence. By improving the learning rules of the optimizer we can make the network learn faster (with some computational and memory cost). &lt;/p&gt;

&lt;h3&gt;
  
  
  Simple optimizer SGD
&lt;/h3&gt;

&lt;p&gt;The most simple optimizer out there is a Stochastic Gradient Descent optimizer, this works by calculating the gradient and error through backpropagation and updating the corresponding weights with the learning rate factor. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Speed&lt;/strong&gt;: because it is the most basic implementation it is the fastest &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Memory&lt;/strong&gt;: it is also the one that uses the fewest memory since it only needs to save the gradients of each weight for backpropagation. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Performance&lt;/strong&gt;: it has a very slow convergence but generalizes better than most methods. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Usage&lt;/strong&gt;: this function can be used in pytorch by providing the models parameters (weights) and the learning rate, the rest of the parameters are optional.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;optim&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SGD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lr&lt;/span&gt;&lt;span class="o"&gt;=&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;required&lt;/span&gt; &lt;span class="n"&gt;parameter&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;momentum&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dampening&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;weight_decay&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nesterov&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Momentum optimization
&lt;/h3&gt;

&lt;p&gt;The momentum optimization is a variant of the SGD that incorporates the previous update in the current change as if there is a &lt;strong&gt;momentum&lt;/strong&gt;. This momentum provides a smoothing effect on the training. The value of the momentum is usually between 0.5 and 1.0 &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Speed&lt;/strong&gt;: very fast since it only has an additional multiplication. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Memory&lt;/strong&gt;: this optimization requires a memory  increase, since it needs to save the memory of the weight of the update in the last step. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Performance&lt;/strong&gt;: very useful since it provides an averaging and smooth effect in the trajectory during convergence. It promotes a faster convergence and helps roll past local optima.  It almost always goes faster than SGD. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Usage&lt;/strong&gt;: to activate the momentum, you need to specify its value through the momentum parameter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;optim&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SGD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lr&lt;/span&gt;&lt;span class="o"&gt;=&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;required&lt;/span&gt; &lt;span class="n"&gt;parameter&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;momentum&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dampening&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;weight_decay&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nesterov&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Nesterov accelerated gradient
&lt;/h3&gt;

&lt;p&gt;A variant of the momentum optimization was proposed in which instead of mesuring the gradient at the local position,we  measure it in the direction of the momentum. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Speed&lt;/strong&gt;: an additional sum must be done to apply the momentum to the parameter. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Memory&lt;/strong&gt;: no extra memory is used in this case. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Performance&lt;/strong&gt;: it usually works better than simple momentum since the momentum vector points towards the optimum. In general, it converges faster than the original momentum since we are promoting the movement towards a specific direction. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Usage&lt;/strong&gt;: to apply the use of Nesterov we must set the Nesterov flag to true and add some momentum to the optimizer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;optim&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SGD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lr&lt;/span&gt;&lt;span class="o"&gt;=&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;required&lt;/span&gt; &lt;span class="n"&gt;parameter&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;momentum&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dampening&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;weight_decay&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nesterov&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Adagrad
&lt;/h3&gt;

&lt;p&gt;Adagrad stands for &lt;strong&gt;adaptive learning rate&lt;/strong&gt; and it works by adapting the learning rate depending on where we are located. When we are near a local minimum, Adagrad tries to optimize the learning rate in order to get faster in that direction. A benefit of using this optimizer is that we don't need to concern ourselves too much in tuning the learning rate manually. The learning rate adapts based on all the gradients in the current training. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Speed&lt;/strong&gt;: it is much slower since it needs to multiply a lot of things. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Memory&lt;/strong&gt;: it does not require any additional memory. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Performance&lt;/strong&gt;: in general, it works well for simple quadratic problems, but it often stops too early when training neural networks, since the learning rate gets scaled too much, thus never getting to the minimum. It is not recommended for neural networks but it may be efficient for simpler problems. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Usage&lt;/strong&gt;: Adagrad can be used by providing the default parameters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;optim&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Adagrad&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.01&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lr_decay&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;weight_decay&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;initial_accumulator_value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;eps&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;1e-10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  RMSprop
&lt;/h3&gt;

&lt;p&gt;This is a variant of the Adagrad algorithm that fixes its never converging issue. It does it by accumulating only the gradients from the most recent iterations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Speed&lt;/strong&gt;: it is very similar to Adagrad&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Memory&lt;/strong&gt;: it uses the same memory as Adagrad&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Performance&lt;/strong&gt;: it converges much faster than Adagrad and does not stop before a local minimum, it. It has been used by machine learning researches for a long time before Adam came out. It does not perform very well on very simple problems. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Usage&lt;/strong&gt;: you might notice there is a new hyperparameter, but the default values usually work well, this technique can be combined with a momentum.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;optim&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;RMSprop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.01&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;alpha&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.99&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;eps&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;1e-08&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;weight_decay&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;momentum&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;centered&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Adam
&lt;/h3&gt;

&lt;p&gt;Adam is a relatively new gradient descent optimization method, it stands for adaptive moment estimation. It is a mix between momentum optimization and RMSProp.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Speed&lt;/strong&gt;: the one that costs more since it combines two methods. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Memory&lt;/strong&gt;: the same as RMSprop&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Performance&lt;/strong&gt;: it usually performs better than RMSprop since it a combination of techniques trying to converge faster on the training data. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Usage&lt;/strong&gt;: Adam can be used perfectly with the default parameters, it is even recommended to leave the learning rate as it is since it is an adaptive method that provides an automatic learning rate update.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;optim&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Adam&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.001&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;betas&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.999&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;eps&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;1e-08&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;weight_decay&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amsgrad&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Summary
&lt;/h1&gt;

&lt;p&gt;In the end, which optimization algorithm should you use? It depends, adaptive algorithms are becoming really fancy nowadays but require more &lt;strong&gt;computational power,&lt;/strong&gt; and most of the time more &lt;strong&gt;memory&lt;/strong&gt;. It has been proven that simple SGD has better results on the validation set, as it tends to generalize better, it seems adaptive algorithms try to optimize the training set too much, thus ending with high variance and overfitting the data. The problem with SGD is that it might take a lot of time to reach a minimum, the computational resources needed in total are much higher than the ones needed in adaptive optimizations. So in the end, if you have a lot of computer resources you should consider using SGD with momentum as it tends to generalize better. On the other hand, if your resources, especially &lt;strong&gt;time resources&lt;/strong&gt;, are limited Adam is your best choice.&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Classifying machine learning models</title>
      <dc:creator>Pol Monroig Company</dc:creator>
      <pubDate>Wed, 15 Jul 2020 20:15:58 +0000</pubDate>
      <link>https://dev.to/polcompany/classifying-machine-learning-models-5ba8</link>
      <guid>https://dev.to/polcompany/classifying-machine-learning-models-5ba8</guid>
      <description>&lt;p&gt;Machine learning models come in all shapes and sizes and it might be difficult to create a classification between them, but there is a characteristic inherited in all models that can separate them in two types, parametric models vs non-parametric models.&lt;/p&gt;

&lt;h1&gt;
  
  
  Parametric models
&lt;/h1&gt;

&lt;p&gt;Parametric models are all the ones that give results based on a &lt;strong&gt;set of parameters&lt;/strong&gt;, each parameter is responsible for one of more features and affects each differently. The most basic example of a parametric model is &lt;strong&gt;linear regression&lt;/strong&gt;, where each parameter is multiplied by each feature linearly. Parametric models try to find the probability distribution of the training data and to approximate it using a set of parameters. For parametric models to work we usually assume that the data is drawn from a &lt;strong&gt;probability distribution&lt;/strong&gt; of known form. The &lt;strong&gt;advantage&lt;/strong&gt; of this type of model is that it reduces the problem of estimating a probability density, discriminant, or regression function to estimating a small number of parameters. Its &lt;strong&gt;disadvantage&lt;/strong&gt; is that the distribution assumption may not hold and that might cause a lot of error.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0oou3acs6cdq5ur3ggf5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0oou3acs6cdq5ur3ggf5.png" alt="Alt Text" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Note&lt;/strong&gt;: sometimes a better approach is to use semi-parametric methods, these methods mix different distributions.&lt;/p&gt;

&lt;h1&gt;
  
  
  Non-parametric models
&lt;/h1&gt;

&lt;p&gt;We usually use a non-parametric approach for density estimation, classification, outlier detection and regression. With this type of model we assume that &lt;strong&gt;similar inputs have similar outputs&lt;/strong&gt;. Instances that are similar mean similar things. Based on past data, the algorithm tries to find similar instances, it interpolates their values and gives a result. For this to work non-parametric models required two basic things&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A history of all the seen data (usually O(n) &lt;strong&gt;space and time complexity&lt;/strong&gt;).&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;distance measure&lt;/strong&gt; to compare different instances and assign a similarity level (e.g. Euclidean, Mahalanobis) .&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F9r5cesqhilqeiztcxxfx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F9r5cesqhilqeiztcxxfx.png" alt="Alt Text" width="591" height="393"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The linear complexity is the bottleneck of this method since usually, the training set is bigger than the parameters needed to model the problem.&lt;/p&gt;

&lt;h1&gt;
  
  
  Differences
&lt;/h1&gt;

&lt;p&gt;As a summary, parametric models are &lt;em&gt;faster&lt;/em&gt;, &lt;em&gt;lighter&lt;/em&gt;, and more &lt;em&gt;simple&lt;/em&gt; thus they tend to create less variance error. On the other hand, non-parametric models remember all the training instances and are a more &lt;em&gt;powerful&lt;/em&gt; approach, although much &lt;em&gt;slower&lt;/em&gt;, we need to be careful in order to prevent overfitting. An example of this is when using decision trees, a good &lt;strong&gt;regularization method&lt;/strong&gt; is to use random forests since the results are interpolated between different trees. &lt;/p&gt;

&lt;h1&gt;
  
  
  Examples
&lt;/h1&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Parametric&lt;/th&gt;
&lt;th&gt;Non-parametric&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Linear regression&lt;/td&gt;
&lt;td&gt;Histogram estimator&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Neural networks&lt;/td&gt;
&lt;td&gt;Kernel estimator&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Bayes' estimator&lt;/td&gt;
&lt;td&gt;K-NN&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Support vector machines&lt;/td&gt;
&lt;td&gt;Decision trees&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Linear Discriminant Analysis&lt;/td&gt;
&lt;td&gt;Random forests&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

</description>
      <category>machinelearning</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Subset selection: reducing dimensions</title>
      <dc:creator>Pol Monroig Company</dc:creator>
      <pubDate>Wed, 08 Jul 2020 10:58:17 +0000</pubDate>
      <link>https://dev.to/polcompany/subset-selection-reducing-dimensions-1e9b</link>
      <guid>https://dev.to/polcompany/subset-selection-reducing-dimensions-1e9b</guid>
      <description>&lt;p&gt;As most data scientists know, dimensionality is a &lt;strong&gt;curse&lt;/strong&gt;; although the number of dimensions is not a curse itself but also the quality of each feature in a specific dimension. &lt;strong&gt;Dimensionality reduction&lt;/strong&gt; is a set of techniques that try to transform the input space into a space with fewer dimensions while keeping the meaning and value of the features. In this post, we will journey through a &lt;strong&gt;greedy&lt;/strong&gt; algorithm (greedy in the sense that it does not guarantee to find the optimal answer) that generates a selection of features that try to minimize the model's error.  &lt;/p&gt;

&lt;h1&gt;
  
  
  Feature selection vs Feature extraction
&lt;/h1&gt;

&lt;p&gt;Dimensionality reduction algorithms can be classified as feature selection methods or feature extraction methods. Feature selection methods are interested in reducing the number of initial features to the ones that give us the most information. On the other hand, feature extraction methods are interested in finding a new set of features, different from the initial ones, and with fewer dimensions. &lt;/p&gt;

&lt;h1&gt;
  
  
  Subset selection
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Subset selection&lt;/strong&gt; is a feature selection algorithm that can variate between a forward selection and a backward selection. Both methods consist in finding a subset of the initial features that contain the least number of dimensions that most contribute to accuracy. A naive approach would be to try all the 2^n possible subset combinations but if the number of dimensions is too big it would take forever. Instead, based on a &lt;strong&gt;heuristic&lt;/strong&gt; function (error function) we add or remove features. The performance of subset selection depends highly on the model we choose and our pruning selection algorithm. &lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvwty7xzh1ja2k6qdf6zf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvwty7xzh1ja2k6qdf6zf.png" alt="Alt Text" width="800" height="255"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Forward selection
&lt;/h3&gt;

&lt;p&gt;In forward selection we start with an empty set of features, for each feature that is not in the set we train the model with it and test its performance; we then select the feature with the &lt;strong&gt;least amount of error&lt;/strong&gt;. We continue adding new features for the model to train until the error is low enough or until we have selected a proportion of the total features. &lt;/p&gt;

&lt;h3&gt;
  
  
  Backward selection
&lt;/h3&gt;

&lt;p&gt;Backward selection works in the same way as forward but instead of starting with an empty set and adding features one by one, we start with a full set and remove features one by one. Thus we remove the features that cause &lt;strong&gt;the most error&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>The triple tradeoff</title>
      <dc:creator>Pol Monroig Company</dc:creator>
      <pubDate>Wed, 01 Jul 2020 17:34:11 +0000</pubDate>
      <link>https://dev.to/polcompany/the-triple-tradeoff-38eo</link>
      <guid>https://dev.to/polcompany/the-triple-tradeoff-38eo</guid>
      <description>&lt;p&gt;In the machine learning community two concepts usually popup very often, &lt;em&gt;bias&lt;/em&gt; and &lt;em&gt;variance&lt;/em&gt;. These two concepts provide a representation of the quality of a model in terms of how it adapts to the training data and how it adapts to new data. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmxywvpqsdm960grgqqix.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmxywvpqsdm960grgqqix.png" alt="Alt Text" width="800" height="426"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Bias&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;It is related to a simple, rigid and constrained model, few or poor quality data, underfitting.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Models with high bias tend to miss relevant features in the data that is modeling, this miss of features can cause the model to underfit. This usually happens when the model is too simple and does not have enough capacity to work properly. It can also happen when even though the data is simple enough for the model, it contains too much noise, thus it requires a more complex model. &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Variance&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;It is related to complex, variable, and adaptable models, overfitting.&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;Machine learning models with high variance tend to have a very high sensitivity, they are variable to any small noise and fluctuations that the data can have. This means that it will learn everything in the training set by memory (they are the worst generalizers). This models are too complex and overfit the training dataset. &lt;/p&gt;

&lt;p&gt;All algorithms that learn from data have a tradeoff between variance and bias, the best model is the one that minimizes both, that is, a model that is complex enough to reduce the learning loss and simple enough to be able to generalize properly to new, unseen samples. Based on this tradeoff we can create a third tradeoff called &lt;strong&gt;the triple tradeoff&lt;/strong&gt; (Dietterich 2003). &lt;/p&gt;

&lt;h2&gt;
  
  
  1. The complexity of the model
&lt;/h2&gt;

&lt;p&gt;The first part of the tradeoff takes into account the complexity/capacity of the model, the model has to be complex enough to navigate through the noise and learn the underlying representation. For example, if we take random data samples from the following linear function &lt;code&gt;f(x) = x + noise(x)&lt;/code&gt; , the best model that can model this kind of data is a linear regression model. We need to be careful in not using a too complex model such as a polynomial or a neural network.  &lt;em&gt;As the complexity of the model increases, the generalization error decreases&lt;/em&gt;, but as we have seen before with the bias/variance tradeoff we need to stop increasing the complexity at some point; until we find the &lt;strong&gt;Optimal Model Complexity&lt;/strong&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  2. Amount of training data
&lt;/h2&gt;

&lt;p&gt;The amount of training data is very important, the more data we have, the more information we can learn about the distribution of the samples. &lt;em&gt;As the amount of training data increases the, the generalization error decreases.&lt;/em&gt; This statement is true only up to a point because the noise that we can find in the dataset can confuse the model. &lt;/p&gt;

&lt;h2&gt;
  
  
  3. The generalization error on new data
&lt;/h2&gt;

&lt;p&gt;The generalization error decreases as we keep in check the other two parameters. The capacity of generalization of a machine learning model is the most important thing. When we learn from data, we want the model to be able to apply its knowledge to new situations if the model works perfectly with the training data but generalizes badly, it is useless and we might not even bother working with it.&lt;/p&gt;

</description>
      <category>machinelearning</category>
    </item>
    <item>
      <title>The world of the autoencoders</title>
      <dc:creator>Pol Monroig Company</dc:creator>
      <pubDate>Wed, 24 Jun 2020 17:21:12 +0000</pubDate>
      <link>https://dev.to/polcompany/the-world-of-the-autoencoders-4l34</link>
      <guid>https://dev.to/polcompany/the-world-of-the-autoencoders-4l34</guid>
      <description>&lt;p&gt;Autoencoders are a special kind of neural networks used in unsupervised machine learning, these networks try to learn an efficient representation of a dataset, these representations are called &lt;em&gt;codings&lt;/em&gt; and can be differentiated from the original input in that they usually have a much smaller dimensionality. Autoencoders are used in all kinds of tasks; such as music, text, and image generation. They can also be used for dimensionality reduction or  collaborative filtering recommendation systems . Autoencoders are essentially very powerful &lt;strong&gt;feature detectors&lt;/strong&gt;. One of the most remarkable differences with this type of network is that to the inputs (features) and outputs (labels) are both features. For this reason, it is very common to see symmetrical autoencoders with tied weights. &lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Frutt9zciloexteel71pm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Frutt9zciloexteel71pm.png" alt="Alt Text" width="420" height="677"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This type of networks can sometimes be tricky to train and are very prone to overfitting, as a result many different variations have been developed. &lt;/p&gt;
&lt;h1&gt;
  
  
  Denoising
&lt;/h1&gt;

&lt;p&gt;One of the most simple alternatives is to use a &lt;strong&gt;Denoising Autoencoder&lt;/strong&gt;; this kind of network is made up of a simple autoencoder but the difference is the introduction of some kind of &lt;strong&gt;noise&lt;/strong&gt; to the input data. This way the network has a harder time filtering the data and cleaning it. Although some randomness is introduced to the training. &lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffclz3b3o1kdwz2j58ci5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffclz3b3o1kdwz2j58ci5.png" alt="Alt Text" width="800" height="718"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Variational
&lt;/h1&gt;

&lt;p&gt;This category of neural network is very similar to denoising networks, but they are probabilistic neural networks, so they introduce randomness even after training. This is due to its internal structure. Variational autoencoders apply a &lt;strong&gt;gaussian&lt;/strong&gt; transformation to the codings. This transformation enables to autoencoder to generate new dataset samples, this is called a &lt;strong&gt;generative neural network&lt;/strong&gt;.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Frkzcxgo84z2h4zc8duw1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Frkzcxgo84z2h4zc8duw1.png" alt="Alt Text" width="800" height="861"&gt;&lt;/a&gt; &lt;/p&gt;
&lt;h1&gt;
  
  
  Generative adversarial networks
&lt;/h1&gt;

&lt;p&gt;Generative adversarial networks or GANs for short, have been getting a lot of attention since their release 2014. They are famously unstable but have been shown to generate very good generative data. This autoencoder variation works by creating two networks, a discriminator and a generative network. The generative network works like any other autoencoder trying to generate new data, and the discriminator works like a categorization neural network that tries to distinguish between images from the original dataset and fake images generated with the generator network. &lt;/p&gt;
&lt;h1&gt;
  
  
  Other
&lt;/h1&gt;

&lt;p&gt;There are a lot of autoencoders out there, and there is surely one that fits your needs, other autoencoder examples might be &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sparse AE&lt;/li&gt;
&lt;li&gt;Contractive AE &lt;/li&gt;
&lt;li&gt;Stacked convolutional AE&lt;/li&gt;
&lt;li&gt;Winner Take it All &lt;/li&gt;
&lt;li&gt;Generative stochastic network &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As a personal favorite a really like this NVIDIA generative autoencoder. &lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/9QuDh3W3lOY"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>machinelearning</category>
    </item>
    <item>
      <title>5 git commands that will save your life</title>
      <dc:creator>Pol Monroig Company</dc:creator>
      <pubDate>Wed, 17 Jun 2020 06:58:45 +0000</pubDate>
      <link>https://dev.to/polcompany/5-git-commands-that-will-save-your-life-5hck</link>
      <guid>https://dev.to/polcompany/5-git-commands-that-will-save-your-life-5hck</guid>
      <description>&lt;h1&gt;
  
  
  1. Amend mistakes
&lt;/h1&gt;

&lt;p&gt;We've all been there, your in a rush for your next deadline and you have to deliver your next application, as always you have git to back you up, literally it backs up and versions your data. Unfortunately you've been taking git lightly and so you have committed something horrible, something that was not supposed to be saved, but what will you do? You don't have spare time to ask a question on Stackoverflow... Suddenly you remember a great command, a command that can discard all your changes. &lt;br&gt;
&lt;code&gt;git reset --hard &amp;lt;Commit&amp;gt;&lt;/code&gt;&lt;br&gt;
With this command you remove your commit and move to a selected one. But &lt;strong&gt;beware&lt;/strong&gt;! I will also delete any uncommited changes. &lt;br&gt;
So what happens if you don't want to &lt;strong&gt;lose any changes&lt;/strong&gt;? Well git comes to the rescue again with &lt;br&gt;
&lt;code&gt;git reset --soft HEAD~1&lt;/code&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  2. Find where you are
&lt;/h1&gt;

&lt;p&gt;Have you ever been working on something but have so &lt;strong&gt;many files&lt;/strong&gt; that it's impossible to distinguish between what is useful and what must be deleted? Have you ever wondered where you are? Or who are you? It turns to git can answer to the first two questions. One of the most useful and simple command you'll ever use is &lt;br&gt;
&lt;code&gt;git status&lt;/code&gt;&lt;br&gt;
This command will help you visualize what are your staged files, your uncommited files, and and idea of what have you done in the current commit. &lt;/p&gt;

&lt;h1&gt;
  
  
  3. Go back in time
&lt;/h1&gt;

&lt;p&gt;I have always wanted to go back in time, or at least only if I can come back later to the present. A lot of people don't know it, but git is a time machine, not one of those fancy time machines that can send you to the Jurassic period, but a time machine that can go back inside your code. &lt;br&gt;
&lt;code&gt;git checkout &amp;lt;Commit&amp;gt;&lt;/code&gt;&lt;br&gt;
When you checkout a commit you are essentially travelly to a specific point in time where you wrote that code. You can open any files and navigate through the folders as if you where there. Awesome right? If you change the commit for a branch you can change between branches, but that isn't as fun. &lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F6z7ol8h757hf5cyv4hk3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F6z7ol8h757hf5cyv4hk3.png" alt="Alt Text" width="800" height="216"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  4. Finding differences
&lt;/h1&gt;

&lt;p&gt;Traveling in time is amazing, but sometimes is a little too much, perhaps you just want to see a specific file or wish to compare the same file between different commits. &lt;br&gt;
&lt;code&gt;git diff &amp;lt;OldCommit&amp;gt;...&amp;lt;NewCommit&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;code&gt;git diff &amp;lt;OldCommit&amp;gt;...&amp;lt;NewCommit&amp;gt; -- &amp;lt;file&amp;gt;&lt;/code&gt;&lt;br&gt;
This command does precisely that, it takes two commits and compares their &lt;strong&gt;differences&lt;/strong&gt;. You can only compare a specific file if that is what you want. &lt;/p&gt;

&lt;h1&gt;
  
  
  5. How to read your log?
&lt;/h1&gt;

&lt;p&gt;Having a log is very important, it contains information on any changes you have done, why you have done them, who has done them and when. That is why git has its own log, although learning how to write your log correctly is as important as to how to read it. &lt;br&gt;
Most people use &lt;code&gt;git log&lt;/code&gt;, but it is so simple, so vague, let's add some decoration... &lt;br&gt;
With &lt;code&gt;git log -n &amp;lt;integer&amp;gt;&lt;/code&gt; you can view only a specified number of commits, really helpful if you don't want to see all. &lt;br&gt;
&lt;code&gt;git log --since &amp;lt;Commit&amp;gt; --until &amp;lt;Commit&amp;gt;&lt;/code&gt; &lt;br&gt;
works by filtering any commits between two dates. Finally a very interesting alternative is &lt;br&gt;
&lt;code&gt;git log --grep=&amp;lt;Keyword&amp;gt;&lt;/code&gt;&lt;br&gt;
This commands filters any commands that contain a specific keyword. &lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Git is an immensely powerful tool, it has amazing commands and I have just shown you some. Hope you learned at least one command this day. &lt;/p&gt;

</description>
      <category>git</category>
      <category>github</category>
      <category>bash</category>
    </item>
    <item>
      <title>A tip on unstructured data in AI</title>
      <dc:creator>Pol Monroig Company</dc:creator>
      <pubDate>Mon, 11 May 2020 11:24:04 +0000</pubDate>
      <link>https://dev.to/polcompany/getting-a-24oo</link>
      <guid>https://dev.to/polcompany/getting-a-24oo</guid>
      <description>&lt;p&gt;Training neural networks is not as hard as it used to be 40 years ago, nevertheless, it is as much as art as it is science, thus practice makes the master. Deep learning is a very powerful tool for someone who wants to apply AI to unstructured data, but &lt;strong&gt;unstructured data&lt;/strong&gt; has the tendency to be unstructured, in other words, every piece of data might have a different size and shape. An example of this might be a text, a song, or images. As a programmer, you need to face this difficulty and find solutions. In this post, I'll show you two ways to handle &lt;strong&gt;variable-shaped&lt;/strong&gt; input, when training neural networks. &lt;/p&gt;

&lt;h1&gt;
  
  
  Padding
&lt;/h1&gt;

&lt;p&gt;The most simple way to do it is by padding every input to the same size. It is straightforward since you only need to find the &lt;strong&gt;biggest&lt;/strong&gt; tensor in a batch of data and pad every other tensor in the batch to that size. Mmm, that does not seem very efficient, you are using a lot of empty space, and you are making the training harder for the neural network.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ftp80ai1ngz5s4nygomq6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ftp80ai1ngz5s4nygomq6.png" alt="Alt Text" width="761" height="596"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Bucketing
&lt;/h1&gt;

&lt;p&gt;A better way would be to &lt;strong&gt;sort&lt;/strong&gt; the data in ascending order and create batches that minimize the padding between tensors. This way you make training faster and avoid unused data. You might still encounter an over-padded situation but it is definitely better than the naive solution. A problem with this solution is that the batches you train with will always be the same, which might cause &lt;strong&gt;overfitting&lt;/strong&gt; but it shouldn't be much of an issue. &lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F72roth8c94mmvyl9blmw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F72roth8c94mmvyl9blmw.png" alt="Alt Text" width="761" height="596"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;This is a simple tip I wanted to share for deep learning enthusiasts. Comment your favorite way to handle variable-shaped data! &lt;/p&gt;

</description>
      <category>python</category>
      <category>ai</category>
      <category>deeplearnin</category>
      <category>machinelearning</category>
    </item>
    <item>
      <title>How to run anything in your web server?</title>
      <dc:creator>Pol Monroig Company</dc:creator>
      <pubDate>Fri, 01 May 2020 08:16:24 +0000</pubDate>
      <link>https://dev.to/polcompany/how-to-run-anything-in-your-web-server-4im8</link>
      <guid>https://dev.to/polcompany/how-to-run-anything-in-your-web-server-4im8</guid>
      <description>&lt;p&gt;Web applications are all around us, they allow us to communicate in unimaginable manners. Best of all, it is straightforward to set up your own web site and post any content you like. But what happens when you wish to run more complex stuff in your web server, maybe you need a fast application that replies to millions of requests, maybe you are just curious and wish to test your self in the web jungle, maybe you are in quarantine and don't have anything better to do. Whatever the reason (I don't judge you) this is the post for you, here you'll learn the basics of CGI programming.  &lt;/p&gt;

&lt;h1&gt;
  
  
  Common Gateway Interface
&lt;/h1&gt;

&lt;p&gt;A common gateway interface or CGI is the most common way to run scripts in the cloud. Commonly you would send &lt;strong&gt;POST&lt;/strong&gt;/&lt;strong&gt;GET&lt;/strong&gt; requests to your web server and get a response in HTML that is displayed by your web browser. On the other hand, when you configure your server with a CGI every post request will instead execute a specific file in your server. This file can be anything, from a &lt;strong&gt;Python&lt;/strong&gt; script to a &lt;strong&gt;C++&lt;/strong&gt; binary (as long as your server can run it). This script will also get a response of course, in fact, you can run an entire web page on CGI; although you can create every component of your page manually it is easier to use some &lt;strong&gt;framework&lt;/strong&gt; (e.g. Pistache, Wt for C++). &lt;/p&gt;

&lt;h1&gt;
  
  
  Simple setup with Apache
&lt;/h1&gt;

&lt;p&gt;Before trying to send an HTTP request to the specific file we want to execute we need to set up the web server, for this, we'll use Apache. &lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1 Locate your CGI directory
&lt;/h3&gt;

&lt;p&gt;Scripts can only be executed in a specific directory, otherwise, you could run everything... Thus, the first we need to do is locate it, in any of the following directories &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;/etc/apache2/httpd.conf&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;/etc/apache2/apache2.conf&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;/etc/httpd/httpd.conf&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;/etc/httpd/conf/httpd.conf&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Inside your config file, you'll need to find the &lt;strong&gt;ScriptAlias&lt;/strong&gt; variable, this variable defines the location of your CGI files (you can change it if you want) &lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2 Writing your CGI program
&lt;/h3&gt;

&lt;p&gt;There are many ways to write a CGI program but remember it needs to deliver a response to the user, the response will be the text that write in the &lt;strong&gt;Standard Output&lt;/strong&gt;. A simple program would be the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#!/dir/to/python/env
# script.py / script.cgi 
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Content-type: text/html&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# write text content type(e.g. text/plain, image/gif)
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;&amp;lt;h1&amp;gt;This is a text header&amp;lt;/h1&amp;gt;&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# write the content 
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3 Set permissions
&lt;/h3&gt;

&lt;p&gt;For apache to execute the script for you, you should first set permissions for it to run it &lt;br&gt;
&lt;code&gt;chmod a+x script.py&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4 Test it
&lt;/h3&gt;

&lt;p&gt;Now to try your new CGI script, it is as easy as sending an HTTP request, for simplicity you can use &lt;strong&gt;curl&lt;/strong&gt;. &lt;br&gt;
&lt;code&gt;curl example.com/cgi-bin/script.py&lt;/code&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Extra
&lt;/h1&gt;

&lt;p&gt;There are two things you may be wondering by now. First of all, how can I use the data that was sent using a POST request from my python file? The data that is sent using a post request is saved in Linux &lt;strong&gt;environment variables&lt;/strong&gt;, so it is a simple as reading them, probably in something like QUERY or PATH. Another thing that can be easily spotted is that every time a user sends a request a new process is created in the server, so if you have a lot of requests it is easy for the server to collapse. An alternative to CGI is FastCGI, it enables you to execute a single process that processes each request. A better idea would be to code your own web API, but that is beyond the scope of this post!&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;At this point you should have successfully executed your app in the cloud, if that was not the case, contact me and I'll help you as I can. &lt;/p&gt;

</description>
      <category>backend</category>
      <category>webdev</category>
      <category>linux</category>
    </item>
  </channel>
</rss>
