MathUtilities { public static double[] softmax(double[] input) { double min = min(input).getSecond(); double denominator = 0d; for (double a : input) { denominator += exp(a - min); } double[] output = new double[input.length]; int i = 0; for (double a : input) { output[i++] = exp(a - min) / denominator; } return output; }  static Pair<Integer, Double> max(double[] array); static Pair<Integer, Integer> max(int[] array); static Pair<Integer, T> max(Collection<T> list); static Pair<Integer, T> max(T[] array); static Pair<T, S> max(Map<T, S> map); static Pair<Integer, Double> min(double[] array); static Pair<Integer, Integer> min(int[] array); static Pair<Integer, T> min(Collection<T> list); static Pair<T, S> min(Map<T, S> map); static Pair<Integer, T> min(T[] array); static List<Double> multiply(List<Double> l, double m); static List<Double> multiply(List<Double> d1, List<Double> d2); static double[] multiply(double[] d1, double[] d2); static double[] multiply(double[] d, double m); static double[] softmax(double[] input); static double[] softmax(double[] input, double[] multiplier); static List<Double> softmax(List<Double> input); static List<Double> softmax(List<Double> input, List<Double> multiplier); static boolean epsilonEquals(double d1, double d2); static double logAdd(double x1, double x2); static double logAdd(double[] doubles); static double logAdd(Collection<Double> doubles); static double lnGamma(double xx); static double lnFactorial(int x); static int binomialCoeffs(int n, int k); static double beta(double x, double w); static double incompleteGammaP(double a, double x); static double incompleteGammaQ(double a, double x); static double erf(double x); static double erfc(double x); final static double EPSILON; }
@Test public void testSoftmax() { List<Double> l = new ArrayList<>(); l.add(1.0); for (double d : softmax(l)) assertEquals(d, 1.0, 0.0); l.add(1.0); for (double d : softmax(l)) assertEquals(d, 0.5, 0.0); l.add(1.0); l.add(1.0); for (double d : softmax(l)) assertEquals(d, 0.25, 0.0); for (double d : softmax(new double[] {10, 100}, new double[] {100, 10})) assertEquals(d, 0.5, 0.0); }