{"id":15017,"date":"2021-07-30T14:47:46","date_gmt":"2021-07-30T09:17:46","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=15017"},"modified":"2021-07-30T14:47:50","modified_gmt":"2021-07-30T09:17:50","slug":"numpy-softmax","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/numpy-softmax\/","title":{"rendered":"Softmax Function Using Numpy in Python"},"content":{"rendered":"\n<p>Here we are going to learn about the softmax function using the NumPy library in Python. We can implement a softmax function in many frameworks of Python like TensorFlow, scipy, and Pytorch. But, here, we are going to implement it in the NumPy library because we know that NumPy is one of the efficient and powerful libraries.<\/p>\n\n\n\n<p><strong><strong>Softmax is commonly used as an activation function for multi-class classification problems. Multi-class classification problems have a range of values. We need to find the probability of their occurrence.<\/strong><\/strong><\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_74 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/numpy-softmax\/#What_is_softmax_function\" >What is softmax function?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.pythonpool.com\/numpy-softmax\/#How_does_softmax_function_work_using_numpy\" >How does softmax function work using numpy?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/numpy-softmax\/#Benefits_of_softmax_function\" >Benefits of softmax function<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/numpy-softmax\/#Examples_to_Demonstrate_Softmax_Function_Using_Numpy\" >Examples to Demonstrate Softmax Function Using Numpy<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/numpy-softmax\/#Implementing_Softmax_function_in_Python\" >Implementing Softmax function in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/numpy-softmax\/#Softmax_Cross_Entropy_Using_Numpy\" >Softmax Cross Entropy Using Numpy<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/numpy-softmax\/#Code\" >Code<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pythonpool.com\/numpy-softmax\/#Frequently_asked_questions_related_to_the_numpy_softmax_function\" >Frequently asked questions related to the numpy softmax function<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.pythonpool.com\/numpy-softmax\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.pythonpool.com\/numpy-softmax\/#Trending_Right_Now\" >Trending Right Now<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-what-is-softmax-function\"><span class=\"ez-toc-section\" id=\"What_is_softmax_function\"><\/span>What is softmax function?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Softmax is a mathematical function that takes a vector of numbers as an input. It normalizes an input to a probability distribution. The probability for value is proportional to the relative scale of value in the vector.<\/p>\n\n\n\n<p>Before applying the function, the vector elements can be in the range of (-\u221e, \u221e). After applying the function, the value will be in the range of [0,1]. The values will sum up to one so that they can be interpreted as probabilities. <\/p>\n\n\n\n<p><strong> The softmax function formula is given below.<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"865\" height=\"374\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/20210729_223948_0000-1.png\" alt=\"softmax function formula\" class=\"wp-image-15041\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/20210729_223948_0000-1.png 865w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/20210729_223948_0000-1-300x130.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/20210729_223948_0000-1-768x332.png 768w\" sizes=\"(max-width: 865px) 100vw, 865px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_does_softmax_function_work_using_numpy\"><\/span>How does softmax function work using numpy? <span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>If one of the inputs is large, then it turns into a large probability, and if the input is small or negative, then it turns it into a small probability, but it will always remain between the range that is [0,1]<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-benefits-of-softmax-function\"><span class=\"ez-toc-section\" id=\"Benefits_of_softmax_function\"><\/span>Benefits of softmax function<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Softmax classifiers give probability class labels for each while hinge loss gives the margin.<\/li><li>It&#8217;s much easier to interpret <a href=\"https:\/\/en.wikipedia.org\/wiki\/Probability\" target=\"_blank\" rel=\"noreferrer noopener\">probabilities<\/a> rather than margin scores (such as in hinge loss and squared hinge loss).<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-examples-to-demonstrate-softmax-function-using-numpy\"><span class=\"ez-toc-section\" id=\"Examples_to_Demonstrate_Softmax_Function_Using_Numpy\"><\/span>Examples to Demonstrate Softmax Function Using Numpy<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">If we take an input of [0.5,1.0,3.0] the softmax of that is \n[0.02484727, 0.04096623, 0.11135776]<\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Let us run the example in the python compiler.<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; import numpy as np\n&gt;&gt;&gt; a=&#x5B;0.5,1.0,2.0]\n&gt;&gt;&gt; np.exp(a)\/np.sum(np.exp(a))\n<\/pre><\/div>\n\n\n<p class=\"has-medium-font-size\"><strong>The output of the above example is<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">array([0.02484727, 0.04096623, 0.11135776])<\/pre>\n\n\n<div class=\"monsterinsights-inline-popular-posts monsterinsights-inline-popular-posts-kilo monsterinsights-popular-posts-styled\" ><div class=\"monsterinsights-inline-popular-posts-text\"><span class=\"monsterinsights-inline-popular-posts-label\" >Popular now<\/span><span class=\"monsterinsights-inline-popular-posts-border\" ><\/span><span class=\"monsterinsights-inline-popular-posts-border-2\" ><\/span><div class=\"monsterinsights-inline-popular-posts-post\"><a class=\"monsterinsights-inline-popular-posts-title\"  href=\"https:\/\/www.pythonpool.com\/fixed-typeerror-cant-compare-datetime-datetime-to-datetime-date\/\">[Fixed] typeerror can&#8217;t compare datetime.datetime to datetime.date<\/a><\/div><\/div><\/div><p><\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-implementing-softmax-function-in-python\"><span class=\"ez-toc-section\" id=\"Implementing_Softmax_function_in_Python\"><\/span>Implementing Softmax function in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Now we are well about the softmax formula. Here are going to use the NumPy sum() method to calculate our denominator <a href=\"http:\/\/www.pythonpool.com\/python-sum\/\" target=\"_blank\" rel=\"noreferrer noopener\">sum<\/a> and the NumPy exp() method for calculating the exponential of our vector.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\nvector=np.array(&#x5B;6.0,3.0])\nexp=np.exp(vector)\nprobability=exp\/np.sum(exp)\nprint(&quot;Probability distribution is:&quot;,probability)\n<\/pre><\/div>\n\n\n<p>First, we are importing a&nbsp;<strong>NumPy<\/strong>&nbsp;library as np. Secondly, creating a variable named vector. A variable vector holds an array. Thirdly implementing the formula to get the probability distribution.  <\/p>\n\n\n\n<p class=\"has-medium-font-size\" id=\"h-output\"><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Probability distribution is: [0.95257413 0.04742587]<\/pre>\n\n\n<div class=\"monsterinsights-inline-popular-posts monsterinsights-inline-popular-posts-golf monsterinsights-popular-posts-styled\" ><div class=\"monsterinsights-inline-popular-posts-text\"><span class=\"monsterinsights-inline-popular-posts-label\" >Popular now<\/span><span class=\"monsterinsights-inline-popular-posts-border\" ><\/span><span class=\"monsterinsights-inline-popular-posts-border-2\" ><\/span><div class=\"monsterinsights-inline-popular-posts-post\"><a class=\"monsterinsights-inline-popular-posts-title\" style=\"font-size:18px;\" href=\"https:\/\/www.pythonpool.com\/fixed-nameerror-name-unicode-is-not-defined\/\">[Fixed] nameerror: name Unicode is not defined<\/a><\/div><\/div><\/div><p><\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-softmax-cross-entropy-using-numpy\"><span class=\"ez-toc-section\" id=\"Softmax_Cross_Entropy_Using_Numpy\"><\/span>Softmax Cross Entropy Using Numpy<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Using the softmax cross-entropy function, we would measure the difference between the predictions, i.e., the network&#8217;s outputs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-code\"><span class=\"ez-toc-section\" id=\"Code\"><\/span>Code<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\nimport matplotlib.pyplot as plt\ndef sig(x):\n    return 1.0\/(1.0+np.exp(-x))\ndef softmax_cross_entropy(z,y):\n    if y==1:\n        return -np.log(z)\n    else:\n        return -np.log(1-z)\nx=np.arange(-9,9,0.1)\na=sig(x)\nsoftmax1=softmax_cross_entropy(a,1)\nsoftmax2=softmax_cross_entropy(a,0)\nfigure,axis=plt.subplots(figsize=(7,7))\nplt.plot(a,softmax1)\nplt.plot(a,softmax2)\nplt.xlabel(&quot;Cross entropy loss&quot;)\nplt.ylabel(&quot;log loss&quot;)\nplt.show()\n<\/pre><\/div>\n\n\n<p>First, importing a Numpy library and plotting a graph, we are importing a matplotlib library. Next creating a function names <strong>&#8220;sig&#8221;<\/strong> for hypothesis function\/sigmoid function. Creating another function named <strong>&#8220;softmax_cross_entropy&#8221;<\/strong> . <strong>z <\/strong>represents the predicted value, and <strong>y<\/strong> represents the actual value. Next, calculating the sample value for x. And then calculating the probability value. Value of softmax function when y=1 is <strong>-log(z)<\/strong> and when y=0 is <strong>-log(1-z)<\/strong>. So now going to plot the graph. Giving x-label and y-label. plt.show() is used to plot the graph.<\/p>\n\n\n\n<p><strong>Here is the graph is shown for cross-entropy loss\/log loss.<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\" id=\"h-output-1\"><strong>Output<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"554\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Screenshot-115-1024x554.png\" alt=\"Softmax cross entropy \" class=\"wp-image-15043\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Screenshot-115-1024x554.png 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Screenshot-115-300x162.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Screenshot-115-768x416.png 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Screenshot-115.png 1288w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n<div class=\"monsterinsights-inline-popular-posts monsterinsights-inline-popular-posts-alpha monsterinsights-popular-posts-styled\" ><div class=\"monsterinsights-inline-popular-posts-text\"><span class=\"monsterinsights-inline-popular-posts-label\" >Trending<\/span><div class=\"monsterinsights-inline-popular-posts-post\"><a class=\"monsterinsights-inline-popular-posts-title\"  href=\"https:\/\/www.pythonpool.com\/solved-runtimeerror-cuda-error-invalid-device-ordinal\/\">[Solved] runtimeerror: cuda error: invalid device ordinal<\/a><\/div><\/div><\/div><p><\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-frequently-asked-questions-related-to-the-numpy-softmax-function\"><span class=\"ez-toc-section\" id=\"Frequently_asked_questions_related_to_the_numpy_softmax_function\"><\/span>Frequently asked questions related to the numpy softmax function <span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1627567428257\"><strong class=\"schema-faq-question\">1. What is softmax function?<\/strong> <p class=\"schema-faq-answer\">Softmax is a mathematical function that takes a vector of numbers as an input. It normalizes an input to a probability distribution. The probability for value is proportional to the relative scale of value in the vector. <\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1627567464268\"><strong class=\"schema-faq-question\">2. How does softmax work?<\/strong> <p class=\"schema-faq-answer\">If one of the inputs is large, then it turns into a large probability, and if the input is small or negative, then it turns it into a small probability, but it will always remain between the range that is [0,1]<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1627567492099\"><strong class=\"schema-faq-question\">3. Why is softmax good?<\/strong> <p class=\"schema-faq-answer\"> <span style=\"font-size: inherit;\">Softmax classifi<\/span>ers give probability class labels for each,<span style=\"font-size: inherit;\"> while hinge loss gives the margin. It&#8217;s much easier to interpret probabilities than margin scores (such as hinge loss and squared hinge loss).<\/span> <\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1627567524644\"><strong class=\"schema-faq-question\">4. What is the range of vector before applying softmax function?<\/strong> <p class=\"schema-faq-answer\">  Before applying the function, the vector elements can be in the range of (-\u221e, \u221e).  <\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1627567855266\"><strong class=\"schema-faq-question\">5. What is the range of vector after applying softmax function?<\/strong> <p class=\"schema-faq-answer\">  After applying the softmax function, the value will be in the range of [0,1]. <\/p> <\/div> <\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Here we have seen about softmax using Numpy in Python. Softmax is a mathematical function. We can implement the softmax function in many frameworks like Pytorch, <span style=\"text-decoration: underline;\"><a href=\"http:\/\/www.pythonpool.com\/category\/numpy\/\" target=\"_blank\" rel=\"noreferrer noopener\">Numpy<\/a><\/span>, <a href=\"http:\/\/www.pythonpool.com\/category\/tensorflow\/\" target=\"_blank\" rel=\"noreferrer noopener\"><span style=\"text-decoration: underline;\">Tensorflow<\/span><\/a>, and Scipy.<\/p>\n\n\n<div class=\"monsterinsights-widget-popular-posts monsterinsights-widget-popular-posts-charlie monsterinsights-popular-posts-styled monsterinsights-widget-popular-posts-columns-1\"><h2 class=\"monsterinsights-widget-popular-posts-widget-title\"><span class=\"ez-toc-section\" id=\"Trending_Right_Now\"><\/span>Trending Right Now<span class=\"ez-toc-section-end\"><\/span><\/h2><ul class=\"monsterinsights-widget-popular-posts-list\"><li style=\"background-color:#91D2FA;border-color:#CD3034;\"><a href=\"https:\/\/www.pythonpool.com\/fixed-typeerror-cant-compare-datetime-datetime-to-datetime-date\/\"><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" style=\"color:#000000;\">[Fixed] typeerror can&#8217;t compare datetime.datetime to datetime.date<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><\/div><\/div><\/a><\/li><li style=\"background-color:#91D2FA;border-color:#CD3034;\"><a href=\"https:\/\/www.pythonpool.com\/fixed-nameerror-name-unicode-is-not-defined\/\"><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" style=\"color:#000000;\">[Fixed] nameerror: name Unicode is not defined<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><\/div><\/div><\/a><\/li><li style=\"background-color:#91D2FA;border-color:#CD3034;\"><a href=\"https:\/\/www.pythonpool.com\/solved-runtimeerror-cuda-error-invalid-device-ordinal\/\"><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" style=\"color:#000000;\">[Solved] runtimeerror: cuda error: invalid device ordinal<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><\/div><\/div><\/a><\/li><li style=\"background-color:#91D2FA;border-color:#CD3034;\"><a href=\"https:\/\/www.pythonpool.com\/fixed-typeerror-type-numpy-ndarray-doesnt-define-__round__-method\/\"><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" style=\"color:#000000;\">[Fixed] typeerror: type numpy.ndarray doesn&#8217;t define __round__ method<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><\/div><\/div><\/a><\/li><\/ul><\/div><p><\/p>","protected":false},"excerpt":{"rendered":"<p>Here we are going to learn about the softmax function using the NumPy library in Python. We can implement a softmax function in many frameworks &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Softmax Function Using Numpy in Python\" class=\"read-more button\" href=\"https:\/\/www.pythonpool.com\/numpy-softmax\/#more-15017\" aria-label=\"More on Softmax Function Using Numpy in Python\">Read more<\/a><\/p>\n","protected":false},"author":22,"featured_media":15066,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1495],"tags":[4378,4376,4377],"class_list":["post-15017","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy","tag-numpy-softmax-function","tag-softmax-in-numpy","tag-softmax-numpy","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v25.0) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Softmax Function Using Numpy in Python - Python Pool<\/title>\n<meta name=\"description\" content=\"Numpy softmax is a mathematical function that takes a vector of numbers as an input. It normalizes an input to a probability distribution.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pythonpool.com\/numpy-softmax\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Softmax Function Using Numpy in Python\" \/>\n<meta property=\"og:description\" content=\"Here we are going to learn about the softmax function using the NumPy library in Python. We can implement a softmax function in many frameworks of Python\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/numpy-softmax\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-30T09:17:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-07-30T09:17:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Softmax-Function-Using-Numpy-in-Python.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Priyalakshmi\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:site\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Priyalakshmi\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.pythonpool.com\/numpy-softmax\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/numpy-softmax\/\"},\"author\":{\"name\":\"Priyalakshmi\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/83e6e22a6c461cc1a8445bb473db2f20\"},\"headline\":\"Softmax Function Using Numpy in Python\",\"datePublished\":\"2021-07-30T09:17:46+00:00\",\"dateModified\":\"2021-07-30T09:17:50+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/numpy-softmax\/\"},\"wordCount\":694,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/numpy-softmax\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Softmax-Function-Using-Numpy-in-Python.webp\",\"keywords\":[\"numpy softmax function\",\"softmax in numpy\",\"softmax numpy\"],\"articleSection\":[\"Numpy\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.pythonpool.com\/numpy-softmax\/#respond\"]}]},{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\/\/www.pythonpool.com\/numpy-softmax\/\",\"url\":\"https:\/\/www.pythonpool.com\/numpy-softmax\/\",\"name\":\"Softmax Function Using Numpy in Python - Python Pool\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/numpy-softmax\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/numpy-softmax\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Softmax-Function-Using-Numpy-in-Python.webp\",\"datePublished\":\"2021-07-30T09:17:46+00:00\",\"dateModified\":\"2021-07-30T09:17:50+00:00\",\"description\":\"Numpy softmax is a mathematical function that takes a vector of numbers as an input. It normalizes an input to a probability distribution.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.pythonpool.com\/numpy-softmax\/#breadcrumb\"},\"mainEntity\":[{\"@id\":\"https:\/\/www.pythonpool.com\/numpy-softmax\/#faq-question-1627567428257\"},{\"@id\":\"https:\/\/www.pythonpool.com\/numpy-softmax\/#faq-question-1627567464268\"},{\"@id\":\"https:\/\/www.pythonpool.com\/numpy-softmax\/#faq-question-1627567492099\"},{\"@id\":\"https:\/\/www.pythonpool.com\/numpy-softmax\/#faq-question-1627567524644\"},{\"@id\":\"https:\/\/www.pythonpool.com\/numpy-softmax\/#faq-question-1627567855266\"}],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.pythonpool.com\/numpy-softmax\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/numpy-softmax\/#primaryimage\",\"url\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Softmax-Function-Using-Numpy-in-Python.webp\",\"contentUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Softmax-Function-Using-Numpy-in-Python.webp\",\"width\":1200,\"height\":628,\"caption\":\"numpy softmax\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.pythonpool.com\/numpy-softmax\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.pythonpool.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Softmax Function Using Numpy in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.pythonpool.com\/#website\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"name\":\"Python Pool\",\"description\":\"Your One-Stop Python Learning Destination\",\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.pythonpool.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.pythonpool.com\/#organization\",\"name\":\"Python Pool\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"contentUrl\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"width\":452,\"height\":185,\"caption\":\"Python Pool\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/pythonpool\",\"https:\/\/www.youtube.com\/c\/pythonpool\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/83e6e22a6c461cc1a8445bb473db2f20\",\"name\":\"Priyalakshmi\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/8f1aadb3b77a89fe5776d41fb52aec63200d0e68e92c71cb9aafac7fb4456696?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/8f1aadb3b77a89fe5776d41fb52aec63200d0e68e92c71cb9aafac7fb4456696?s=96&d=wavatar&r=g\",\"caption\":\"Priyalakshmi\"}},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/numpy-softmax\/#faq-question-1627567428257\",\"position\":1,\"url\":\"https:\/\/www.pythonpool.com\/numpy-softmax\/#faq-question-1627567428257\",\"name\":\"1. What is softmax function?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Softmax is a mathematical function that takes a vector of numbers as an input. It normalizes an input to a probability distribution. The probability for value is proportional to the relative scale of value in the vector. \",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/numpy-softmax\/#faq-question-1627567464268\",\"position\":2,\"url\":\"https:\/\/www.pythonpool.com\/numpy-softmax\/#faq-question-1627567464268\",\"name\":\"2. How does softmax work?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"If one of the inputs is large, then it turns into a large probability, and if the input is small or negative, then it turns it into a small probability, but it will always remain between the range that is [0,1]\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/numpy-softmax\/#faq-question-1627567492099\",\"position\":3,\"url\":\"https:\/\/www.pythonpool.com\/numpy-softmax\/#faq-question-1627567492099\",\"name\":\"3. Why is softmax good?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\" Softmax classifiers give probability class labels for each, while hinge loss gives the margin. It's much easier to interpret probabilities than margin scores (such as hinge loss and squared hinge loss). \",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/numpy-softmax\/#faq-question-1627567524644\",\"position\":4,\"url\":\"https:\/\/www.pythonpool.com\/numpy-softmax\/#faq-question-1627567524644\",\"name\":\"4. What is the range of vector before applying softmax function?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"  Before applying the function, the vector elements can be in the range of (-\u221e, \u221e).  \",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/numpy-softmax\/#faq-question-1627567855266\",\"position\":5,\"url\":\"https:\/\/www.pythonpool.com\/numpy-softmax\/#faq-question-1627567855266\",\"name\":\"5. What is the range of vector after applying softmax function?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"  After applying the softmax function, the value will be in the range of [0,1]. \",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Softmax Function Using Numpy in Python - Python Pool","description":"Numpy softmax is a mathematical function that takes a vector of numbers as an input. It normalizes an input to a probability distribution.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pythonpool.com\/numpy-softmax\/","og_locale":"en_US","og_type":"article","og_title":"Softmax Function Using Numpy in Python","og_description":"Here we are going to learn about the softmax function using the NumPy library in Python. We can implement a softmax function in many frameworks of Python","og_url":"https:\/\/www.pythonpool.com\/numpy-softmax\/","og_site_name":"Python Pool","article_published_time":"2021-07-30T09:17:46+00:00","article_modified_time":"2021-07-30T09:17:50+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Softmax-Function-Using-Numpy-in-Python.webp","type":"image\/webp"}],"author":"Priyalakshmi","twitter_card":"summary_large_image","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Priyalakshmi","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/numpy-softmax\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/numpy-softmax\/"},"author":{"name":"Priyalakshmi","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/83e6e22a6c461cc1a8445bb473db2f20"},"headline":"Softmax Function Using Numpy in Python","datePublished":"2021-07-30T09:17:46+00:00","dateModified":"2021-07-30T09:17:50+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/numpy-softmax\/"},"wordCount":694,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/numpy-softmax\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Softmax-Function-Using-Numpy-in-Python.webp","keywords":["numpy softmax function","softmax in numpy","softmax numpy"],"articleSection":["Numpy"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/numpy-softmax\/#respond"]}]},{"@type":["WebPage","FAQPage"],"@id":"https:\/\/www.pythonpool.com\/numpy-softmax\/","url":"https:\/\/www.pythonpool.com\/numpy-softmax\/","name":"Softmax Function Using Numpy in Python - Python Pool","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/numpy-softmax\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/numpy-softmax\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Softmax-Function-Using-Numpy-in-Python.webp","datePublished":"2021-07-30T09:17:46+00:00","dateModified":"2021-07-30T09:17:50+00:00","description":"Numpy softmax is a mathematical function that takes a vector of numbers as an input. It normalizes an input to a probability distribution.","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/numpy-softmax\/#breadcrumb"},"mainEntity":[{"@id":"https:\/\/www.pythonpool.com\/numpy-softmax\/#faq-question-1627567428257"},{"@id":"https:\/\/www.pythonpool.com\/numpy-softmax\/#faq-question-1627567464268"},{"@id":"https:\/\/www.pythonpool.com\/numpy-softmax\/#faq-question-1627567492099"},{"@id":"https:\/\/www.pythonpool.com\/numpy-softmax\/#faq-question-1627567524644"},{"@id":"https:\/\/www.pythonpool.com\/numpy-softmax\/#faq-question-1627567855266"}],"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/numpy-softmax\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/numpy-softmax\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Softmax-Function-Using-Numpy-in-Python.webp","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Softmax-Function-Using-Numpy-in-Python.webp","width":1200,"height":628,"caption":"numpy softmax"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/numpy-softmax\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"Softmax Function Using Numpy in Python"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Your One-Stop Python Learning Destination","publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonpool.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonpool.com\/#organization","name":"Python Pool","url":"https:\/\/www.pythonpool.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/","url":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","width":452,"height":185,"caption":"Python Pool"},"image":{"@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/pythonpool","https:\/\/www.youtube.com\/c\/pythonpool"]},{"@type":"Person","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/83e6e22a6c461cc1a8445bb473db2f20","name":"Priyalakshmi","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/8f1aadb3b77a89fe5776d41fb52aec63200d0e68e92c71cb9aafac7fb4456696?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/8f1aadb3b77a89fe5776d41fb52aec63200d0e68e92c71cb9aafac7fb4456696?s=96&d=wavatar&r=g","caption":"Priyalakshmi"}},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/numpy-softmax\/#faq-question-1627567428257","position":1,"url":"https:\/\/www.pythonpool.com\/numpy-softmax\/#faq-question-1627567428257","name":"1. What is softmax function?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"Softmax is a mathematical function that takes a vector of numbers as an input. It normalizes an input to a probability distribution. The probability for value is proportional to the relative scale of value in the vector. ","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/numpy-softmax\/#faq-question-1627567464268","position":2,"url":"https:\/\/www.pythonpool.com\/numpy-softmax\/#faq-question-1627567464268","name":"2. How does softmax work?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"If one of the inputs is large, then it turns into a large probability, and if the input is small or negative, then it turns it into a small probability, but it will always remain between the range that is [0,1]","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/numpy-softmax\/#faq-question-1627567492099","position":3,"url":"https:\/\/www.pythonpool.com\/numpy-softmax\/#faq-question-1627567492099","name":"3. Why is softmax good?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":" Softmax classifiers give probability class labels for each, while hinge loss gives the margin. It's much easier to interpret probabilities than margin scores (such as hinge loss and squared hinge loss). ","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/numpy-softmax\/#faq-question-1627567524644","position":4,"url":"https:\/\/www.pythonpool.com\/numpy-softmax\/#faq-question-1627567524644","name":"4. What is the range of vector before applying softmax function?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"  Before applying the function, the vector elements can be in the range of (-\u221e, \u221e).  ","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/numpy-softmax\/#faq-question-1627567855266","position":5,"url":"https:\/\/www.pythonpool.com\/numpy-softmax\/#faq-question-1627567855266","name":"5. What is the range of vector after applying softmax function?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"  After applying the softmax function, the value will be in the range of [0,1]. ","inLanguage":"en-US"},"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/15017","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/users\/22"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=15017"}],"version-history":[{"count":31,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/15017\/revisions"}],"predecessor-version":[{"id":15082,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/15017\/revisions\/15082"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/15066"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=15017"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=15017"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=15017"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}