Skip to content

Instantly share code, notes, and snippets.

@cyphunk
Forked from vladimir-ivanov/softmax.js
Last active April 6, 2025 00:13

Revisions

  1. cyphunk revised this gist Dec 18, 2018. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions softmax.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,4 @@
    //
    // Fork with examples for the oneline version by @vladimir-ivanov:
    // Fork & examples for the one-line version by @vladimir-ivanov:
    //let softmax = (arr) => (index) => Math.exp(arr[index]) / arr.map(y => Math.exp(y)).reduce((a, b) => a + b);
    //
    // Also see comments for improvements
  2. cyphunk revised this gist Dec 18, 2018. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions softmax.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,8 @@
    //
    // Fork with examples for the oneline version by @vladimir-ivanov:
    //let softmax = (arr) => (index) => Math.exp(arr[index]) / arr.map(y => Math.exp(y)).reduce((a, b) => a + b);
    //
    // Also see comments for improvements

    function softmax(arr) {
    return arr.map(function(value,index) {
  3. cyphunk revised this gist Jan 15, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion softmax.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    //let softmax = (arr) => (index) => Math.exp(arr[index]) / arr.map(y => Math.exp(y)).reduce((a, b) => a + b);

    function softmax(arr) {
    return arr.forEach(function(value,index) {
    return arr.map(function(value,index) {
    return Math.exp(value) / arr.map( function(y /*value*/){ return Math.exp(y) } ).reduce( function(a,b){ return a+b })
    })
    }
  4. cyphunk revised this gist Jan 15, 2017. 1 changed file with 3 additions and 5 deletions.
    8 changes: 3 additions & 5 deletions softmax.js
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,9 @@
    //let softmax = (arr) => (index) => Math.exp(arr[index]) / arr.map(y => Math.exp(y)).reduce((a, b) => a + b);

    function softmax(arr) {
    arr.forEach(function(value,index) {
    console.log(
    Math.exp(value) / arr.map( function(y /*value*/){ return Math.exp(y) } ).reduce( function(a,b){ return a+b })
    );
    })
    return arr.forEach(function(value,index) {
    return Math.exp(value) / arr.map( function(y /*value*/){ return Math.exp(y) } ).reduce( function(a,b){ return a+b })
    })
    }

    example1=[ 0.9780449271202087,
  5. cyphunk revised this gist Jan 15, 2017. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion softmax.js
    Original file line number Diff line number Diff line change
    @@ -16,6 +16,7 @@ example1=[ 0.9780449271202087,
    0.0006004497990943491,
    0.0004827099328394979,
    0.0001868270628619939 ]
    softmax1=softmax(example1)

    example2= [
    { prob: 0.32289665937423706, cat: '25_32' },
    @@ -25,6 +26,7 @@ example2= [
    { prob: 0.011709162034094334, cat: '15_20' },
    { prob: 0.008010754361748695, cat: '8_13' },
    { last: true, prob: 0.0054732030257582664, cat: '60+' } ].map(function(v){return v.prob})
    softmax2=softmax(example2)

    example3=[ { prob: 0.125, cat: '25_32' },
    { prob: 0.125, cat: '38_43' },
    @@ -33,4 +35,5 @@ example3=[ { prob: 0.125, cat: '25_32' },
    { prob: 0.125, cat: '4_6' },
    { prob: 0.125, cat: '48_53' },
    { prob: 0.125, cat: '60+' },
    { prob: 0.125, cat: '0_2' } ].map(function(v){return v.prob})
    { prob: 0.125, cat: '0_2' } ].map(function(v){return v.prob})
    softmax3=softmax(example3)
  6. cyphunk revised this gist Jan 15, 2017. No changes.
  7. cyphunk revised this gist Jan 15, 2017. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion softmax.js
    Original file line number Diff line number Diff line change
    @@ -24,4 +24,13 @@ example2= [
    { prob: 0.01545996405184269, cat: '48_53' },
    { prob: 0.011709162034094334, cat: '15_20' },
    { prob: 0.008010754361748695, cat: '8_13' },
    { last: true, prob: 0.0054732030257582664, cat: '60+' } ].map(function(v){return v.prob})
    { last: true, prob: 0.0054732030257582664, cat: '60+' } ].map(function(v){return v.prob})

    example3=[ { prob: 0.125, cat: '25_32' },
    { prob: 0.125, cat: '38_43' },
    { prob: 0.125, cat: '15_20' },
    { prob: 0.125, cat: '8_13' },
    { prob: 0.125, cat: '4_6' },
    { prob: 0.125, cat: '48_53' },
    { prob: 0.125, cat: '60+' },
    { prob: 0.125, cat: '0_2' } ].map(function(v){return v.prob})
  8. cyphunk revised this gist Jan 15, 2017. 1 changed file with 27 additions and 1 deletion.
    28 changes: 27 additions & 1 deletion softmax.js
    Original file line number Diff line number Diff line change
    @@ -1 +1,27 @@
    let softmax = (arr) => (index) => Math.exp(arr[index]) / arr.map(y => Math.exp(y)).reduce((a, b) => a + b);
    //let softmax = (arr) => (index) => Math.exp(arr[index]) / arr.map(y => Math.exp(y)).reduce((a, b) => a + b);

    function softmax(arr) {
    arr.forEach(function(value,index) {
    console.log(
    Math.exp(value) / arr.map( function(y /*value*/){ return Math.exp(y) } ).reduce( function(a,b){ return a+b })
    );
    })
    }

    example1=[ 0.9780449271202087,
    0.01590355671942234,
    0.0019390975357964635,
    0.0015482910675927997,
    0.0012942816829308867,
    0.0006004497990943491,
    0.0004827099328394979,
    0.0001868270628619939 ]

    example2= [
    { prob: 0.32289665937423706, cat: '25_32' },
    { prob: 0.15404804050922394, cat: '38_43' },
    { prob: 0.03673655539751053, cat: '4_6' },
    { prob: 0.01545996405184269, cat: '48_53' },
    { prob: 0.011709162034094334, cat: '15_20' },
    { prob: 0.008010754361748695, cat: '8_13' },
    { last: true, prob: 0.0054732030257582664, cat: '60+' } ].map(function(v){return v.prob})
  9. @vladimir-ivanov vladimir-ivanov created this gist Jun 27, 2016.
    1 change: 1 addition & 0 deletions softmax.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    let softmax = (arr) => (index) => Math.exp(arr[index]) / arr.map(y => Math.exp(y)).reduce((a, b) => a + b);