Skip to content

Commit

Permalink
Merge pull request #198 from SunYatong/2.0.0
Browse files Browse the repository at this point in the history
modify softmax function for mathematical robustness.
  • Loading branch information
SunYatong committed Oct 8, 2017
2 parents 7c22c99 a520e67 commit 38d81ad
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions core/src/main/java/net/librec/math/algorithm/Maths.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 141,21 @@ protected double gaussian(double x, double mu, double sigma) {
}

/**
* logistic function g(x)
* softmax function
*
* @param x given parameter x
* @return value of logistic function g(x)
* @throws Exception if error occurs
* @param x given array
* @return output array of softmax function
* @throws Exception if error occurs
*/
public static double[] softmax(double[] x) throws Exception {
double[] expx = new double[x.length];

double max = x[0];
for(int i=1;i<x.length;i )
max = Math.max(x[i],max);

for (int i = 0; i < x.length; i ) {
expx[i] = Math.exp(x[i]);
expx[i] = Math.exp(x[i] - max);
}
return norm(expx);
}
Expand Down

0 comments on commit 38d81ad

Please sign in to comment.