produces a double between 0 and 1 based on the given array x and the index that must be between (0 and x.length) - Java Collection Framework

Java examples for Collection Framework:Array Length

Description

produces a double between 0 and 1 based on the given array x and the index that must be between (0 and x.length)

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int index = 2;
        double[] x = new double[] { 34.45, 35.45, 36.67, 37.78, 37.0000,
                37.1234, 67.2344, 68.34534, 69.87700 };
        System.out.println(softmax(index, x));
    }//from   ww w .j  av a  2 s.  c  o  m

    /**
     * produces a double between 0 and 1 based on the given array x
     * and the index that must be between (0 and x.length)
     * For further information see also:
     * http://www-ccs.ucsd.edu/matlab/toolbox/nnet/softmax.html
     */
    public static double softmax(int index, double[] x) {
        double sum = 0;
        for (int i = 0; i < x.length; i++) {
            sum = sum + Math.exp(x[i]);
        }
        return (Math.exp(x[index]) / sum);
    }
}

Related Tutorials