|
From: Brendan M. <mc...@us...> - 2006-11-28 20:13:11
|
Update of /cvsroot/jigs/jigs/src/edu/whitman/halfway/util In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv26627/src/edu/whitman/halfway/util Modified Files: MiscUtil.java ExecutionTimer.java Log Message: Index: MiscUtil.java =================================================================== RCS file: /cvsroot/jigs/jigs/src/edu/whitman/halfway/util/MiscUtil.java,v retrieving revision 1.25 retrieving revision 1.26 diff -C2 -d -r1.25 -r1.26 *** MiscUtil.java 11 Nov 2006 02:04:56 -0000 1.25 --- MiscUtil.java 28 Nov 2006 20:13:06 -0000 1.26 *************** *** 261,287 **** public static double distValue = Double.NaN; public static double[] softMaxDistribution(double[] c, double alpha){ double[] e = new double[c.length]; ! ! // first do hard max and subtract of exponents for ! // numerical stability ! double max = MiscUtil.max(c); double d=0; for (int i = 0; i < c.length; i++) { ! e[i] = Math.exp((c[i] - max) / alpha); d += e[i]; } ! //the softMax value, not currently needed ! //double softMax = alpha * Math.log(d) + max; ! distValue = 0; //not needed currently ! double[] p = new double[c.length]; for (int i = 0; i < c.length; i++) { ! p[i] = e[i] / d; ! distValue += p[i] * c[i]; } ! return p; } --- 261,305 ---- public static double distValue = Double.NaN; + public static double softValue = Double.NaN; public static double[] softMaxDistribution(double[] c, double alpha){ + //if(alpha > 1.01){ + // log.warn("Large alphas make the softmax value a bad approx to max (off by a alpha log(length(c)) factor"); + //} double[] e = new double[c.length]; ! ! // first do hard max and subtract off exponents for ! // numerical stability ! double maxOverAlpha = -Double.MAX_VALUE; ! for (int i = 0; i < c.length; i++) { ! double ei = c[i]/alpha; ! e[i] = ei; ! if(ei > maxOverAlpha){ ! maxOverAlpha = ei; ! } ! } ! //System.out.println("alpha = " + alpha); ! //System.out.println("maxOverAlpha = " + maxOverAlpha); ! double d=0; for (int i = 0; i < c.length; i++) { ! e[i] = Math.exp(e[i] - maxOverAlpha); d += e[i]; } + //System.out.println("d = " + d + ", log(d) = " + Math.log(d)); ! softValue = alpha*(Math.log(d) + maxOverAlpha); ! if(alpha > 0){ ! double max = alpha*maxOverAlpha; ! assert softValue >= max -1e-10 : String.format("max = %s, softMax = %s", max, softValue); ! assert softValue <= alpha*maxOverAlpha + alpha*Math.log(c.length) + 1e-10; ! } ! distValue = 0; for (int i = 0; i < c.length; i++) { ! e[i] = e[i] / d; ! distValue += e[i] * c[i]; } ! return e; } *************** *** 295,308 **** } ! public static double[] softMinDistribution(double[] c, double alpha){ ! double[] cp = new double[c.length]; ! for(int i=0; i<cp.length; i++){ ! cp[i] = -1.0 * c[i]; ! } ! double[] dist = softMaxDistribution(cp, alpha); ! distValue *= -1.0; ! return dist; } public static double[] hardMaxDistribution(double[] c){ double max = -Double.MAX_VALUE; --- 313,351 ---- } ! // public static double[] softMinDistribution(double[] c, double alpha){ ! // double[] cp = new double[c.length]; ! // for(int i=0; i<cp.length; i++){ ! // cp[i] = -1.0 * c[i]; ! // } ! // double[] dist = softMaxDistribution(cp, alpha); ! // distValue *= -1.0; ! // return dist; ! // } ! ! public static double[] softMinDistribution(double[] c, double alpha){ ! //testing a better impl ! ! // double[] dist = softMinDistribution(c, alpha); ! // double td = distValue; ! // double sv = softValue; ! ! double[] dist2 = softMaxDistribution(c, -alpha); ! // assert MiscUtil.manhattanNorm(dist, dist2) < dist.length * 1e-8 : ! // String.format("%ndist1=%s\ndist2=%s%n", toString(dist), toString(dist2)); ! // assert MiscUtil.equals(td, distValue, 1e-8) : String.format("td1=%s, td2=%s", td, distValue); ! // assert MiscUtil.equals(sv, softValue, 1e-8) : String.format("sv1=%s, sv2=%s", sv, softValue); ! // ! ! sanityCheckSoftMin(c, alpha, softValue); ! return dist2; } + private static boolean sanityCheckSoftMin(double[] c, double alpha, double softMin){ + double min = min(c); + assert softMin <= min + 1e-8; + assert softMin >= min - alpha*Math.log(c.length) - 1e-10; + return true; + } + public static double[] hardMaxDistribution(double[] c){ double max = -Double.MAX_VALUE; *************** *** 362,377 **** double max = MiscUtil.max(c); double[] minDist = softMinDistribution(c, alpha); double[] maxDist = softMaxDistribution(c, alpha); double minMix = dot(c, minDist); double maxMix = dot(c, maxDist); System.out.printf( "c=%s\t\talpha=%.4g%n"+ ! "\tsoftMaxDist=%s\tmax=%6.4g,\tmix=%6.4g%n" + ! "\tsoftMinDist=%s\tmin=%6.4g,\tmix=%6.4g%n", MiscUtil.toString(c, "%.3g"), alpha, ! MiscUtil.toString(maxDist, "%.3g"), max, maxMix, ! MiscUtil.toString(minDist, "%.3g"), min, minMix ); } --- 405,432 ---- double max = MiscUtil.max(c); double[] minDist = softMinDistribution(c, alpha); + double softMin = softValue; + + //double[] minDist2 = softMinDistribution2(c, alpha); + //double softMin2 = softValue; + double[] maxDist = softMaxDistribution(c, alpha); + double softMax = softValue; double minMix = dot(c, minDist); + //double minMix2 = dot(c, minDist2); + double maxMix = dot(c, maxDist); + + System.out.printf( "c=%s\t\talpha=%.4g%n"+ ! "\tsoftMaxDist =%s\tmax=%6.4g,\tmix=%6.4g,\tsoft=%6.4g%n" + ! "\tsoftMinDist =%s\tmin=%6.4g,\tmix=%6.4g,\tsoft=%6.4g%n", ! //"\tsoftMinDist2=%s\tmin=%6.4g,\tmix=%6.4g,\tsoft=%6.4g%n", MiscUtil.toString(c, "%.3g"), alpha, ! MiscUtil.toString(maxDist, "%.3g"), max, maxMix, softMax, ! MiscUtil.toString(minDist, "%.3g"), min, minMix, softMin ! //MiscUtil.toString(minDist2, "%.3g"), min, minMix2, softMin2 ); } *************** *** 382,386 **** double[][] tests = { { 1, 1, 1 }, { 0.3, 0.8, 1.0 }, { 990, 38, 70 }, { 9, 8.3, 11.3 }, { 0.01, 0.02, 0.015 } }; ! double[] alphas = { 10000, 100, 10, 1, 0.1, 0.01, 0.0001 }; for (double[] c : tests) { for (double alpha : alphas) { --- 437,441 ---- double[][] tests = { { 1, 1, 1 }, { 0.3, 0.8, 1.0 }, { 990, 38, 70 }, { 9, 8.3, 11.3 }, { 0.01, 0.02, 0.015 } }; ! double[] alphas = { 15, 1, 0.1, 0.01, 0.0001 }; for (double[] c : tests) { for (double alpha : alphas) { Index: ExecutionTimer.java =================================================================== RCS file: /cvsroot/jigs/jigs/src/edu/whitman/halfway/util/ExecutionTimer.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** ExecutionTimer.java 19 Nov 2006 22:24:09 -0000 1.5 --- ExecutionTimer.java 28 Nov 2006 20:13:06 -0000 1.6 *************** *** 131,134 **** --- 131,138 ---- } + public String toMicroString(){ + return formatNanoTime(getElapsedTimeNano()); + } + public static String formatTime(long ms) { return formatMicroTime(ms*1000); |