JSFiddle - React, Tailwind, and code Playground

by petabyte

HTML

<script src="https://rawgit.com/nicolaspanel/numjs/893016ec40e62eaaa126e1024dbe250aafb3014b/dist/numjs.min.js"></script>
<h1>
Softmax
</h1>
<blockquote>
  [0.4, 0.3, 0.5]
  <br> e^0.4 / (e^0.4 + e^0.3 + e^0.5)
  <br> e^0.3 / (e^0.4 + e^0.3 + e^0.5)
  <br> e^0.5 / (e^0.4 + e^0.3 + e^0.5)
  <br> [0.332225, 0.300610, 0.3671654]
  <br> 0.332225 + 0.300610 + 0.3671654
  <br>
</blockquote>
Own Softmax Implementation:
<p id="output1">Test </p>
<br> Sum should be equal to 1:
<p id="sum1">Test </p>

NJ Implement:
<p id="output2">Test </p>
<br> Sum should be equal to 1:
<p id="sum2">Test </p>

JavaScript

//Trying to understand softmax 
// this is my own implementation base 
// https://en.wikipedia.org/wiki/Softmax_function
// The problem with softmax for multi-class classification is that
// if the number of output layer is huge this function prove to be costly
function own_softmax(input_array) {
  var a = nj.array(input_array);
  e = a.exp()
  se = e.sum();
  var result = [];
  for (var i = 0; i < e.size; ++i) {
    result[i] = e.get(i) / se;
  }
  return nj.array(result);
}

//this is the nj js library function
function nj_implement(input_array) {
  return nj.softmax(input_array);
}

(
  function() {
    var test_array = [0.4, 0.3, 0.5];
    var my_softmax = own_softmax(test_array)
    var nj_softmax = nj_implement(test_array)
    document.getElementById("output1").innerHTML = my_softmax
    document.getElementById("output2").innerHTML = nj_softmax
    document.getElementById("sum1").innerHTML = my_softmax.sum()
    document.getElementById("sum2").innerHTML = nj_softmax.sum()

  })();