<?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 Pius Gadosey on Medium]]></title>
        <description><![CDATA[Stories by Pius Gadosey on Medium]]></description>
        <link>https://medium.com/@kwaogad?source=rss-ef5428ca172b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*0qiX2sXVs7jV7_a8</url>
            <title>Stories by Pius Gadosey on Medium</title>
            <link>https://medium.com/@kwaogad?source=rss-ef5428ca172b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 01:18:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kwaogad/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[A beginner’s guide to NumPy with Sigmoid, ReLu and Softmax activation functions]]></title>
            <link>https://medium.com/ai%C2%B3-theory-practice-business/a-beginners-guide-to-numpy-with-sigmoid-relu-and-softmax-activation-functions-25b840a9a272?source=rss-ef5428ca172b------2</link>
            <guid isPermaLink="false">https://medium.com/p/25b840a9a272</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[softmax]]></category>
            <category><![CDATA[sigmoid]]></category>
            <category><![CDATA[relu]]></category>
            <category><![CDATA[numpy]]></category>
            <dc:creator><![CDATA[Pius Gadosey]]></dc:creator>
            <pubDate>Mon, 19 Aug 2019 07:40:47 GMT</pubDate>
            <atom:updated>2019-08-19T07:40:47.042Z</atom:updated>
            <content:encoded><![CDATA[<p>Deep learning has caught up very fast with AI enthusiasts and has been spreading like wildfire in the past few years. The number of researchers in the field is growing and more application developers do not want to be left out either. With its growth comes the need to provide different methods and libraries to make it easier and more accessible to everyone. Major programming languages are being introduced while the already existing ones are constantly updated to add deep learning functionalities One of the biggest communities today is the Python community and one of the most popular packages used with python is the NumPy library. In this post, we will look at a brief introduction to the NumPy library and how to use its packages to implement Sigmoid, ReLu and Softmax functions in python. These are the most widely used activation functions in building artificial neural networks for deep learning</p><h4>Why NumPy?</h4><p>NumPy is the main package for scientific computations in python and has been a major backbone of Python applications in various computational, engineering, scientific, statistical, image processing, etc fields. NumPy was built from 2 earlier libraries: Numeric and Numarray. [1]<br>Most deep learning algorithms make use of several numpy operations and functions. This is because compared with pure python syntax, NumPy computations are faster. NumPy for instance makes use of vectorization that enables the elimination of unnecessary loops in a code structure, hence reducing latency in execution of code. The following is an example of vectorization for a 1-d array NumPy dot operation, which is mentioned later in the article:</p><pre><strong>#with vectorization</strong><em><br>import numpy as np #always remember to import numpy</em><strong><br></strong>import time<br>t0 = time.process_time()<br>#array1<br>tmatrix1 = [1,2,3,4,5]<br>#array2<br>tmatrix2 = [6,7,8,9,10]<br>#dot matrix<br>np.dot(tmatrix1,tmatrix2)<br>t1 = time.process_time()<br>print (&quot;dot operation = &quot; + str(dot) + &quot;\n Computation time = &quot; + str(1000*(t1 - t0)) + &quot;ms&quot;)<strong><br>output:</strong><br>  dot operation = 130  Computation time = <strong>0.17037400000008418ms</strong></pre><pre><strong>#without vectorization</strong><br>t0 = time.process_time()<br>dot = 0<br>for i in range(len(tmatrix1)):<br>  %time dot+= tmatrix1[i]*tmatrix2[i]<br>t1 = time.process_time()<br>print (&quot;dot operation = &quot; + str(dot) + &quot;\n Computation time = &quot; + str(1000*(t1 - t0)) + &quot;ms&quot;)</pre><pre><strong>output:<br></strong>dot operation = 130<br> Computation time = <strong>7.590736999999681ms</strong></pre><p>It can be seen that without vetorizing our code, computation time for a simple loop could be over 40x slower. Now, let’s get back to the basics and see how to do simple numply definitions and operations</p><p><strong>Creating vectors and matrices: </strong>Vectors (1-d arrays) and matrices (n-d arrays) are among the most basic mathematical data structures used in machine/deep learning learning.</p><pre><em>import numpy as np</em></pre><pre><strong><em>#create a 1-d array of 1,2,3</em></strong><em><br>mvector = np.array([1,2,3])<br>print(mvector)</em></pre><pre><strong>output:</strong> [1 2 3]</pre><pre><strong><em>#create a 2x3 matrix/2-d array</em></strong></pre><pre><em>mmatrix= np.array([[1,2,3],[4,5,6]])<br>print(mmatrix)</em></pre><pre><strong>output:</strong> [[1 2 3]</pre><pre>        [4 5 6]]</pre><p><strong>Getting properties of a numpy array/matrix</strong></p><pre><strong>#print array type</strong><br>print(type(mmatrix))<br><strong>output:</strong> &lt;class ‘numpy.ndarray’&gt;</pre><pre><strong>#print array shape</strong></pre><pre>print(mmatrix.shape)<br><strong>output:</strong> (2, 3)<br>#print array size<br>print(mmatrix.size)<br><strong>output:</strong> 6</pre><pre><strong>#print smallest number in a matrix</strong><br>print(mmatrix.min())<br><strong>output:</strong> 1<br>#print biggest number<br>print(mmatrix.max())<br><strong>output:</strong> 6</pre><p><strong>Common Mathematical operations</strong></p><pre><strong>#Adding, subtracting and multiplying</strong><br>#array1<br>mmatrix1 = np.array([[1,2,3],[4,5,6]])<br>#array2<br>mmatrix2 = np.array([[6,7,8],[8,9,10]])</pre><pre><strong>#add matrices</strong><br>addmatrix = np.add(mmatrix1,mmatrix2)<br>print(‘addmatrix \n’, addmatrix)</pre><pre><strong>#subtract matrices</strong><br>submatrix = np.subtract(mmatrix1,mmatrix2)<br>print(‘submatrix \n’, submatrix)</pre><pre><strong>#multiply matrices</strong><br>mulmatrix = np.multiply(mmatrix1,mmatrix2)<br>print(‘mulmatrix \n’, mulmatrix)</pre><pre><strong>#Calculating dot products</strong><br>#array1<br>dmatrix1 = np.array([[1,2,3],[4,5,6]])<br>#array2<br>dmatrix2 = np.array([[6,7],[8,9],[9,10]])<br>print(‘array1 \n’, dmatrix1)<br>print(‘array2 \n’, dmatrix2)<br>#dot matrix<br>dotmatrix = np.dot(dmatrix1,dmatrix2)<br>print(‘dotmatrix \n’, dotmatrix)</pre><pre><strong>#Finding the exponential of a matrix</strong><br>mexp = np.exp(mmatrix)<br>print(mexp)</pre><pre><strong>Outputs:</strong><br>addmatrix   <br>[[ 7  9 11]  <br>[12 14 16]] <br>submatrix   <br>[[-5 -5 -5]  <br>[-4 -4 -4]] <br>mulmatrix   <br>[[ 6 14 24]  <br>[32 45 60]]<br>dotmatrix <br>[[ 49  55]<br>[118 133]]<br>matrix exponential  <br>[[  2.71828183   7.3890561   20.08553692]  [ 54.59815003 148.4131591  403.42879349]]</pre><p><strong>Common Statistical operations</strong></p><pre><strong>#print variance of a matrix</strong><br>print(np.var(mmatrix))<br><strong>output:</strong>2.9166666666666665</pre><pre><strong>#print mean of a matrix</strong><br>print(np.mean(mmatrix))<br><strong>output:</strong>3.5</pre><pre><strong>#print standard deviation of a matrix</strong><br>print(np.std(mmatrix))<br><strong>output:</strong>1.707825127659933</pre><p><strong>Reshaping Arrays</strong></p><pre><strong><em>#Reshape mmatrix from a 2x3 array  to a 3x2 array</em></strong></pre><pre><em>rematrix = mmatrix.reshape(3,2)<br>print(rematrix.shape)</em></pre><pre><strong>output:</strong>(3, 2)</pre><pre><strong><em>#flatten mmatrix to 1-d array</em></strong><em><br>flmatrix = mmatrix.flatten()<br>print(flmatrix)</em></pre><pre><strong>output:</strong>[1 2 3 4 5 6]</pre><p>Now let’s define three major activation functions used in deep learning and have a look at simple ways of implementing them with numpy</p><ol><li><strong>The Sigmoid Activation Function</strong></li></ol><p>Using a mathematical definition, the sigmoid function [2] takes any range real number and returns the output value which falls in the range of 0 to 1. Based on the convention, the output value is expected to be in the range of -1 to 1. The sigmoid function produces an “S” shaped curve (figure 1). Mathematically, sigmoid is represented as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/976/1*-aDRUctmGhwjXVF0LS8Zqw.jpeg" /><figcaption>Equation 1. The Sigmoid function</figcaption></figure><p><strong>Properties of the Sigmoid Function</strong></p><ul><li>The sigmoid function takes in real numbers in any range and returns a real-valued output.</li><li>The first derivative of the sigmoid function will be non-negative (greater than or equal to zero) or non-positive (less than or equal to Zero).</li><li>It appears in the output layers of the Deep Learning architectures, and is used for predicting probability based outputs and has been successfully implemented in binary classification problems, logistic regression tasks as well as other neural network applications.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/568/1*DtwGZO8CcRhEa4hgObry0A.png" /><figcaption>figure 1. The sigmoid function produces as ‘S’ shape</figcaption></figure><p>Now let’s see how to easily implement sigmoid easily using numpy</p><pre><strong><em>#sigmoid function</em></strong></pre><pre><em>def sigmoid(X):<br>   return 1/(1+np.exp(-X))</em></pre><pre><strong>#Example with mmatrix defined above</strong><br>sigmoid(mmatrix)</pre><pre><strong>output:<br></strong>array([[0.73105858, 0.88079708, 0.95257413],<br>       [0.98201379, 0.99330715, 0.99752738]])</pre><p><strong>2. ReLu</strong></p><p>The Rectified linear unit (ReLu) [3] activation function has been the most widely used activation function for deep learning applications with state-of-the-art results. It usually achieves better performance and generalization in deep learning compared to the sigmoid activation function.</p><p><strong>Properties of the ReLu Function</strong></p><ul><li>The main idea behind the ReLu activation function is to perform a threshold operation to each input element where values less than zero are set to zero (figure 2).</li></ul><p>Mathematically it is defined by:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/976/1*ugxPV_0OL-LBN95LHZofUQ.jpeg" /><figcaption>Equation 2. The ReLu function</figcaption></figure><p>And is shown as :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/283/1*QufKYf2Xq7xPvkIpO4wBNA.jpeg" /></figure><p>Implementing the ReLu function in NumPy is very straight forward:</p><pre><strong><em>#ReLu function</em></strong></pre><pre><em>def relu(X):<br>   return np.maximum(0,X)</em></pre><pre><strong>#Example with mmatrix defined above</strong><br>relu(mmatrix)</pre><pre><strong>output:<br></strong>array([[1, 2, 3],        [4, 5, 6]])</pre><p><strong>3. The Softmax Function</strong></p><p>The Softmax function is used for prediction in multi-class models where it returns probabilities of each class in a group of different classes, with the target class having the highest probability. The calculated probabilities are then helpful in determining the target class for the given inputs.</p><p><strong>Properties of the Softmax Function</strong></p><ul><li>The Softmax function produces an output which is a range of values between 0 and 1, with the sum of the probabilities been equal to 1.</li><li>The Softmax function is computed using the relationship:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/976/1*48FpDngytN34rvVlnw0ojA.jpeg" /><figcaption>Equation 3. The Softmaxfunction</figcaption></figure><ul><li>The main difference between the Sigmoid and Softmax functions is that Sigmoid is used in binary classification while the Softmax is used for multi-class tasks</li></ul><p>Softmax in NumPy:</p><pre><strong><em>#softmax function</em></strong></pre><pre><em>def softmax(X):<br>    expo = np.exp(X)<br>    expo_sum = np.sum(np.exp(X))<br>    return expo/expo_sum</em></pre><pre><strong>#Example with mmatrix defined above</strong><br>print (softmax(mmatrix))</pre><pre><strong>output:<br></strong> [[0.00426978 0.01160646 0.03154963]  [0.08576079 0.23312201 0.63369132]]</pre><p><strong>Summary</strong></p><p>This post is an introduction to simple NumPy operations and provides a guide to writing simple activation functions common in deep learning using the numpy package. For more information, you may visit the NumPy official website (<a href="https://www.numpy.org/">https://www.numpy.org</a>).</p><p>If you liked this post, kindly CLAP and FOLLOW ME for more future posts. All comments are welcome</p><p><strong>Bibliography</strong></p><p>[1] [Online]. Available: <a href="https://www.numpy.org/devdocs/about.html">https://www.numpy.org/devdocs/about.html</a>.<br>[2] C. M. Jun Han, “The influence of the sigmoid function parameters on the speed of backpropagation learning,” in From Natural to Artificial Neural Computation, 1995. <br>[3] R. S. M. A. M. R. J. D. &amp;. H. S. S. Richard H. R. Hahnloser, “Digital selection and analogue amplification coexist in a cortex-inspired silicon circuit,” Nature, vol. 405, p. 947–951, 2000.</p><p>[<a href="https://en.wikipedia.org/wiki/Softmax_function#CITEREFGoodfellowBengioCourville2016">4] Goodfellow, Bengio &amp; Courville 2016</a>, pp. 183–184:</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=25b840a9a272" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ai%C2%B3-theory-practice-business/a-beginners-guide-to-numpy-with-sigmoid-relu-and-softmax-activation-functions-25b840a9a272">A beginner’s guide to NumPy with Sigmoid, ReLu and Softmax activation functions</a> was originally published in <a href="https://medium.com/ai%C2%B3-theory-practice-business">AI³ | Theory, Practice, Business</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>