Example usage for edu.stanford.nlp.math ArrayMath countNonZero

List of usage examples for edu.stanford.nlp.math ArrayMath countNonZero

Introduction

In this page you can find the example usage for edu.stanford.nlp.math ArrayMath countNonZero.

Prototype

public static int countNonZero(double[] v) 

Source Link

Usage

From source file:cmu.arktweetnlp.impl.OWLQN.java

public double[] minimize(DiffFunction function, double[] initial, double l1weight, double tol, int m) {

    OptimizerState state = new OptimizerState(function, initial, m, l1weight, quiet);

    if (!quiet) {
        System.err.printf("Optimizing function of %d variables with OWL-QN parameters:\n", state.dim);
        System.err.printf("   l1 regularization weight: %f.\n", l1weight);
        System.err.printf("   L-BFGS memory parameter (m): %d\n", m);
        System.err.printf("   Convergence tolerance: %f\n\n", tol);
        System.err.printf("Iter    n:\tnew_value\tdf\t(conv_crit)\tline_search\n");
        System.err.printf("Iter    0:\t%.4e\t\t(***********)\t", state.value);
    }//from  ww w.  j av a2  s. co  m

    StringBuilder buf = new StringBuilder();
    termCrit.getValue(state, buf);

    for (int i = 0; i < maxIters; i++) {
        buf.setLength(0);
        state.updateDir();
        state.backTrackingLineSearch();

        double termCritVal = termCrit.getValue(state, buf);
        if (!quiet) {
            int numnonzero = ArrayMath.countNonZero(state.newX);
            System.err.printf("Iter %4d:\t%.4e\t%d", state.iter, state.value, numnonzero);
            System.err.print("\t" + buf.toString());

            if (printer != null)
                printer.printWeights();
        }

        //mheilman: I added this check because OWLQN was failing without it sometimes 
        //for large L1 penalties and few features...
        //This checks that the parameters changed in the last iteration.
        //If they didn't, then OWL-QN will try to divide by zero when approximating the Hessian.
        //The ro values end up 0 when the line search ends up trying a newX that equals X (or newGrad and grad).
        //That causes the differences stored in sList and yList to be zero, which eventually causes 
        //values in roList to be zero.  Below, ro values appear in the denominator, and they would 
        //cause the program to crash in the mapDirByInverseHessian() method if any were zero.
        //This only appears to happen once the parameters have already converged.  
        //I suspect that numerical loss of precision is the cause.
        if (arrayEquals(state.x, state.newX)) {
            System.err.println(
                    "Warning: Stopping OWL-QN since there was no change in the parameters in the last iteration.  This probably means convergence has been reached.");
            break;
        }

        if (termCritVal < tol)
            break;

        state.shift();
    }

    if (!quiet) {
        System.err.println();
        System.err.printf("Finished with optimization.  %d/%d non-zero weights.\n",
                ArrayMath.countNonZero(state.newX), state.newX.length);
    }

    return state.newX;
}

From source file:com.gp.extract.twitter.labeler.OWLQN.java

public double[] minimize(DiffFunction function, double[] initial, double l1weight, double tol, int m) {

    OptimizerState state = new OptimizerState(function, initial, m, l1weight, quiet);

    if (!quiet) {
        System.out.printf("Optimizing function of %d variables with OWL-QN parameters:\n", state.dim);
        System.out.printf("   l1 regularization weight: %f.\n", l1weight);
        System.out.printf("   L-BFGS memory parameter (m): %d\n", m);
        System.out.printf("   Convergence tolerance: %f\n\n", tol);
        System.out.printf("Iter    n:\tnew_value\tdf\t(conv_crit)\tline_search\n");
        System.out.printf("Iter    0:\t%.4e\t\t(***********)\t", state.value);
    }/*from w w w . j  a v  a  2  s .c om*/

    StringBuilder buf = new StringBuilder();
    termCrit.getValue(state, buf);

    for (int i = 0; i < maxIters; i++) {
        buf.setLength(0);
        state.updateDir();
        state.backTrackingLineSearch();

        double termCritVal = termCrit.getValue(state, buf);
        if (!quiet) {
            int numnonzero = ArrayMath.countNonZero(state.newX);
            System.out.printf("Iter %4d:\t%.4e\t%d", state.iter, state.value, numnonzero);
            System.out.print("\t" + buf.toString());

            if (printer != null)
                printer.printWeights();
        }

        //mheilman: I added this check because OWLQN was failing without it sometimes 
        //for large L1 penalties and few features...
        //This checks that the parameters changed in the last iteration.
        //If they didn't, then OWL-QN will try to divide by zero when approximating the Hessian.
        //The ro values end up 0 when the line search ends up trying a newX that equals X (or newGrad and grad).
        //That causes the differences stored in sList and yList to be zero, which eventually causes 
        //values in roList to be zero.  Below, ro values appear in the denominator, and they would 
        //cause the program to crash in the mapDirByInverseHessian() method if any were zero.
        //This only appears to happen once the parameters have already converged.  
        //I suspect that numerical loss of precision is the cause.
        if (arrayEquals(state.x, state.newX)) {
            System.out
                    .println("Warning: Stopping OWL-QN since there was no change in the parameters in the last "
                            + "iteration.  This probably means convergence has been reached.");
            break;
        }

        if (termCritVal < tol)
            break;

        state.shift();
    }

    if (!quiet) {
        System.out.println();
        System.out.printf("Finished with optimization.  %d/%d non-zero weights.\n\n\n",
                ArrayMath.countNonZero(state.newX), state.newX.length);
    }

    return state.newX;
}

From source file:dz.pfe.storm.ressources.cmu.arktweetnlp.impl.OWLQN.java

public synchronized double[] minimize(DiffFunction function, double[] initial, double l1weight, double tol,
        int m) {/*from   w  ww  .j a va2 s . c  o m*/

    OptimizerState state = new OptimizerState(function, initial, m, l1weight, quiet);

    if (!quiet) {
        System.err.printf("Optimizing function of %d variables with OWL-QN parameters:\n", state.dim);
        System.err.printf("   l1 regularization weight: %f.\n", l1weight);
        System.err.printf("   L-BFGS memory parameter (m): %d\n", m);
        System.err.printf("   Convergence tolerance: %f\n\n", tol);
        System.err.printf("Iter    n:\tnew_value\tdf\t(conv_crit)\tline_search\n");
        System.err.printf("Iter    0:\t%.4e\t\t(***********)\t", state.value);
    }

    StringBuilder buf = new StringBuilder();
    termCrit.getValue(state, buf);

    for (int i = 0; i < maxIters; i++) {
        buf.setLength(0);
        state.updateDir();
        state.backTrackingLineSearch();

        double termCritVal = termCrit.getValue(state, buf);
        if (!quiet) {
            int numnonzero = ArrayMath.countNonZero(state.newX);
            System.err.printf("Iter %4d:\t%.4e\t%d", state.iter, state.value, numnonzero);
            System.err.print("\t" + buf.toString());

            if (printer != null)
                printer.printWeights();
        }

        //mheilman: I added this check because OWLQN was failing without it sometimes
        //for large L1 penalties and few features...
        //This checks that the parameters changed in the last iteration.
        //If they didn't, then OWL-QN will try to divide by zero when approximating the Hessian.
        //The ro values end up 0 when the line search ends up trying a newX that equals X (or newGrad and grad).
        //That causes the differences stored in sList and yList to be zero, which eventually causes
        //values in roList to be zero.  Below, ro values appear in the denominator, and they would
        //cause the program to crash in the mapDirByInverseHessian() method if any were zero.
        //This only appears to happen once the parameters have already converged.
        //I suspect that numerical loss of precision is the cause.
        if (arrayEquals(state.x, state.newX)) {
            System.err.println(
                    "Warning: Stopping OWL-QN since there was no change in the parameters in the last iteration.  This probably means convergence has been reached.");
            break;
        }

        if (termCritVal < tol)
            break;

        state.shift();
    }

    if (!quiet) {
        System.err.println();
        System.err.printf("Finished with optimization.  %d/%d non-zero weights.\n",
                ArrayMath.countNonZero(state.newX), state.newX.length);
    }

    return state.newX;
}