当前位置: 首页>>代码示例>>Java>>正文


Java ArrayMath.countNonZero方法代码示例

本文整理汇总了Java中edu.stanford.nlp.math.ArrayMath.countNonZero方法的典型用法代码示例。如果您正苦于以下问题:Java ArrayMath.countNonZero方法的具体用法?Java ArrayMath.countNonZero怎么用?Java ArrayMath.countNonZero使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在edu.stanford.nlp.math.ArrayMath的用法示例。


在下文中一共展示了ArrayMath.countNonZero方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: minimize

import edu.stanford.nlp.math.ArrayMath; //导入方法依赖的package包/类
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);
		}

		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;
	}
 
开发者ID:weizh,项目名称:geolocator-3.0,代码行数:61,代码来源:OWLQN.java

示例2: minimize

import edu.stanford.nlp.math.ArrayMath; //导入方法依赖的package包/类
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);
        }

        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 (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);
            //System.err.println(Arrays.toString(state.newX));
        }

        return state.newX;
    }
 
开发者ID:vietansegan,项目名称:segan,代码行数:45,代码来源:OWLQN.java


注:本文中的edu.stanford.nlp.math.ArrayMath.countNonZero方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。