protected static double[] smooth(List<double[]> toSmooth){ double[] smoothed = new double[toSmooth.get(0).length]; for(double[] thisArray:toSmooth){ ArrayMath.pairwiseAddInPlace(smoothed,thisArray); } ArrayMath.multiplyInPlace(smoothed,1/((double) toSmooth.size() )); return smoothed; }
/** * Makes the values in this array sum to 1.0. Does it in place. * If the total is 0.0 or NaN, throws an RuntimeException. */ public static void normalize(float[] a) { float total = sum(a); if (total == 0.0f || Double.isNaN(total)) { throw new RuntimeException("Can't normalize an array with sum 0.0 or NaN"); } multiplyInPlace(a, 1.0f/total); // divide each value by total } public static void L2normalize(float[] a) {
double[] applyInitialHessian(double[] x, StringBuilder sb) { switch (scaleOpt) { case SCALAR: sb.append('I'); ArrayMath.multiplyInPlace(x, gamma); break; case DIAGONAL: sb.append('D'); if (d != null) { // Check sizes if (x.length != d.length) { throw new IllegalArgumentException("Vector of incorrect size passed to applyInitialHessian in QNInfo class"); } // Scale element-wise for (int i = 0; i < x.length; i++) { x[i] = x[i] / (d[i]); } } break; } return x; }
public static void L2normalize(double[] a) { double total = L2Norm(a); if (total == 0.0 || Double.isNaN(total)) { if (a.length < 100) { throw new RuntimeException("Can't normalize an array with sum 0.0 or NaN: " + Arrays.toString(a)); } else { double[] aTrunc = new double[100]; System.arraycopy(a, 0, aTrunc, 0, 100); throw new RuntimeException("Can't normalize an array with sum 0.0 or NaN: " + Arrays.toString(aTrunc) + " ... "); } } multiplyInPlace(a, 1.0/total); // divide each value by total }
/** * Makes the values in this array sum to 1.0. Does it in place. * If the total is 0.0 or NaN, throws an RuntimeException. */ public static void normalize(double[] a) { double total = sum(a); if (total == 0.0 || Double.isNaN(total)) { throw new RuntimeException("Can't normalize an array with sum 0.0 or NaN: " + Arrays.toString(a)); } multiplyInPlace(a, 1.0/total); // divide each value by total }
public static void L2normalize(float[] a) { float total = L2Norm(a); if (total == 0.0 || Float.isNaN(total)) { if (a.length < 100) { throw new RuntimeException("Can't normalize an array with sum 0.0 or NaN: " + Arrays.toString(a)); } else { float[] aTrunc = new float[100]; System.arraycopy(a, 0, aTrunc, 0, 100); throw new RuntimeException("Can't normalize an array with sum 0.0 or NaN: " + Arrays.toString(aTrunc) + " ... "); } } multiplyInPlace(a, 1.0/total); // divide each value by total }
public static void L1normalize(double[] a) { double total = L1Norm(a); if (total == 0.0 || Double.isNaN(total)) { if (a.length < 100) { throw new RuntimeException("Can't normalize an array with sum 0.0 or NaN: " + Arrays.toString(a)); } else { double[] aTrunc = new double[100]; System.arraycopy(a, 0, aTrunc, 0, 100); throw new RuntimeException("Can't normalize an array with sum 0.0 or NaN: " + Arrays.toString(aTrunc) + " ... "); } } multiplyInPlace(a, 1.0/total); // divide each value by total } public static void L2normalize(double[] a) {
/** * Standardize values in this array, i.e., subtract the mean and divide by the standard deviation. * If standard deviation is 0.0, throws a RuntimeException. */ public static void standardize(double[] a) { double m = mean(a); if (Double.isNaN(m)) { throw new RuntimeException("Can't standardize array whose mean is NaN"); } double s = stdev(a); if (s == 0.0 || Double.isNaN(s)) { throw new RuntimeException("Can't standardize array whose standard deviation is 0.0 or NaN"); } addInPlace(a, -m); // subtract mean multiplyInPlace(a, 1.0/s); // divide by standard deviation }
private void computeDir(double[] dir, double[] fg) throws SQNMinimizer.SurpriseConvergence { System.arraycopy(fg, 0, dir, 0, fg.length); int mmm = sList.size(); double[] as = new double[mmm]; double[] factors = new double[dir.length]; for (int i = mmm - 1; i >= 0; i--) { as[i] = roList.get(i) * ArrayMath.innerProduct(sList.get(i), dir); plusAndConstMult(dir, yList.get(i), -as[i], dir); } // multiply by hessian approximation if (mmm != 0) { double[] y = yList.get(mmm - 1); double yDotY = ArrayMath.innerProduct(y, y); if (yDotY == 0) { throw new SQNMinimizer.SurpriseConvergence("Y is 0!!"); } double gamma = ArrayMath.innerProduct(sList.get(mmm - 1), y) / yDotY; ArrayMath.multiplyInPlace(dir, gamma); }else if(mmm == 0){ //This is a safety feature preventing too large of an initial step (see Yu Schraudolph Gunter) ArrayMath.multiplyInPlace(dir,epsilon); } for (int i = 0; i < mmm; i++) { double b = roList.get(i) * ArrayMath.innerProduct(yList.get(i), dir); plusAndConstMult(dir, sList.get(i), cPosDef*as[i] - b, dir); plusAndConstMult(ArrayMath.pairwiseMultiply(yList.get(i),sList.get(i)),factors,1,factors); } ArrayMath.multiplyInPlace(dir, -1); }
/** * Returns the Jensen Shannon divergence (information radius) between * a and b, defined as the average of the kl divergences from a to b * and from b to a. */ public static double jensenShannonDivergence(double[] a, double[] b) { double[] average = pairwiseAdd(a, b); multiplyInPlace(average, .5); return .5 * klDivergence(a, average) + .5 * klDivergence(b, average); }
/** * Samples a single position in the sequence. * Does not modify the sequence passed in. * returns the score of the new label for the position to sample * @param sequence the sequence to start with * @param pos the position to sample. * @param temperature the temperature to control annealing */ private Pair<Integer, Double> samplePositionHelper(SequenceModel model, int[] sequence, int pos, double temperature) { double[] distribution = model.scoresOf(sequence, pos); if (temperature!=1.0) { if (temperature==0.0) { // set the max to 1.0 int argmax = ArrayMath.argmax(distribution); Arrays.fill(distribution, Double.NEGATIVE_INFINITY); distribution[argmax] = 0.0; } else { // take all to a power // use the temperature to increase/decrease the entropy of the sampling distribution ArrayMath.multiplyInPlace(distribution, 1.0/temperature); } } ArrayMath.logNormalize(distribution); ArrayMath.expInPlace(distribution); int newTag = ArrayMath.sampleFromDistribution(distribution, random); double newProb = distribution[newTag]; return new Pair<>(newTag, newProb); }
System.arraycopy(thisFunc.derivativeAt(x,x,batchSize),0,thisGrad,0,thisGrad.length); System.arraycopy(thisFunc.HdotVAt(x,x,thisGrad,batchSize),0,thisHx,0,thisHx.length); ArrayMath.multiplyInPlace(thisHx,hessScale);
ArrayMath.multiplyInPlace(derivative, (1.0/trainingBatch.size())); value += regCost; ArrayMath.multiplyInPlace(currentParams, op.trainOptions.regCost); ArrayMath.pairwiseAddInPlace(derivative, currentParams);
means[data[i][j]] += values[i][j]; ArrayMath.multiplyInPlace(means, 1.0 / this.size());
/** * computeDir() * * This function will calculate an approximation of the inverse hessian based * off the seen s,y vector pairs. This particular approximation uses the BFGS * update. */ private void computeDir(double[] dir, double[] fg, double[] x, QNInfo qn, Function func, StringBuilder sb) throws SurpriseConvergence { System.arraycopy(fg, 0, dir, 0, fg.length); int mmm = qn.size(); double[] as = new double[mmm]; for (int i = mmm - 1; i >= 0; i--) { as[i] = qn.getRho(i) * ArrayMath.innerProduct(qn.getS(i), dir); plusAndConstMult(dir, qn.getY(i), -as[i], dir); } // multiply by hessian approximation qn.applyInitialHessian(dir, sb); for (int i = 0; i < mmm; i++) { double b = qn.getRho(i) * ArrayMath.innerProduct(qn.getY(i), dir); plusAndConstMult(dir, qn.getS(i), as[i] - b, dir); } ArrayMath.multiplyInPlace(dir, -1); if (useOWLQN) { // step (2) in Galen & Gao 2007 constrainSearchDir(dir, fg, x, func); } }
protected static double[] smooth(List<double[]> toSmooth){ double[] smoothed = new double[toSmooth.get(0).length]; for(double[] thisArray:toSmooth){ ArrayMath.pairwiseAddInPlace(smoothed,thisArray); } ArrayMath.multiplyInPlace(smoothed,1/((double) toSmooth.size() )); return smoothed; }
/** * Makes the values in this array sum to 1.0. Does it in place. * If the total is 0.0 or NaN, throws an RuntimeException. */ public static void normalize(double[] a) { double total = sum(a); if (total == 0.0 || Double.isNaN(total)) { throw new RuntimeException("Can't normalize an array with sum 0.0 or NaN: " + Arrays.toString(a)); } multiplyInPlace(a, 1.0/total); // divide each value by total }
/** * Makes the values in this array sum to 1.0. Does it in place. * If the total is 0.0 or NaN, throws an RuntimeException. */ public static void normalize(double[] a) { double total = sum(a); if (total == 0.0 || Double.isNaN(total)) { throw new RuntimeException("Can't normalize an array with sum 0.0 or NaN: " + Arrays.toString(a)); } multiplyInPlace(a, 1.0/total); // divide each value by total }
/** * Returns the Jensen Shannon divergence (information radius) between * a and b, defined as the average of the kl divergences from a to b * and from b to a. */ public static double jensenShannonDivergence(double[] a, double[] b) { double[] average = pairwiseAdd(a, b); multiplyInPlace(average, .5); return .5 * klDivergence(a, average) + .5 * klDivergence(b, average); }
/** * Returns the Jensen Shannon divergence (information radius) between * a and b, defined as the average of the kl divergences from a to b * and from b to a. */ public static double jensenShannonDivergence(double[] a, double[] b) { double[] average = pairwiseAdd(a, b); multiplyInPlace(average, .5); return .5 * klDivergence(a, average) + .5 * klDivergence(b, average); }