Esempio n. 1
0
  /**
   * Builds L2-regularized regressors for a sequence of regularization parameter lambdas on sparse
   * inputs. Each row of the input represents a feature (instead of a data point), i.e., in
   * column-oriented format. This procedure does not assume the data is normalized or centered.
   *
   * @param attrs the attribute list.
   * @param indices the indices.
   * @param values the values.
   * @param y the targets.
   * @param maxNumIters the maximum number of iterations.
   * @param lambdas the lambdas array.
   * @return L2-regularized regressors.
   */
  public GLM[] buildGaussianRegressors(
      int[] attrs,
      int[][] indices,
      double[][] values,
      double[] y,
      int maxNumIters,
      double[] lambdas) {
    double[] w = new double[attrs.length];
    double intercept = 0;

    GLM[] glms = new GLM[lambdas.length];
    Arrays.sort(lambdas);

    // Backup targets
    double[] rTrain = new double[y.length];
    for (int i = 0; i < rTrain.length; i++) {
      rTrain[i] = y[i];
    }

    // Calculate sum of squares
    double[] sq = new double[attrs.length];
    for (int i = 0; i < values.length; i++) {
      sq[i] = StatUtils.sumSq(values[i]);
    }

    // Compute the regularization path
    for (int g = 0; g < glms.length; g++) {
      double lambda = lambdas[g];

      // Coordinate descent
      final double tl2 = lambda * y.length;
      for (int iter = 0; iter < maxNumIters; iter++) {
        double prevLoss = GLMOptimUtils.computeRidgeLoss(rTrain, w, lambda);

        if (fitIntercept) {
          intercept += OptimUtils.fitIntercept(rTrain);
        }

        doOnePassGaussian(indices, values, sq, tl2, w, rTrain);

        double currLoss = GLMOptimUtils.computeRidgeLoss(rTrain, w, lambda);

        if (verbose) {
          System.out.println("Iteration " + iter + ": " + " " + currLoss);
        }

        if (OptimUtils.isConverged(prevLoss, currLoss, epsilon)) {
          break;
        }
      }

      glms[g] = GLMOptimUtils.getGLM(attrs, w, intercept, LinkFunction.IDENTITY);
    }

    return glms;
  }
Esempio n. 2
0
  /**
   * Builds L2-regularized binary classifiers for a sequence of regularization parameter lambdas on
   * sparse format. Each row of the input represents a feature (instead of a data point), i.e., in
   * column-oriented format. This procedure does not assume the data is normalized or centered.
   *
   * @param attrs the attribute list.
   * @param indices the indices.
   * @param values the values.
   * @param y the targets.
   * @param maxNumIters the maximum number of iterations.
   * @param lambdas the lambdas array.
   * @return L2-regularized classifiers.
   */
  public GLM[] buildBinaryClassifiers(
      int[] attrs,
      int[][] indices,
      double[][] values,
      double[] y,
      int maxNumIters,
      double[] lambdas) {
    double[] w = new double[attrs.length];
    double intercept = 0;

    double[] pTrain = new double[y.length];
    double[] rTrain = new double[y.length];
    OptimUtils.computePseudoResidual(pTrain, y, rTrain);

    // Calculate theta's
    double[] theta = new double[values.length];
    for (int i = 0; i < values.length; i++) {
      theta[i] = StatUtils.sumSq(values[i]) / 4;
    }

    GLM[] glms = new GLM[lambdas.length];
    Arrays.sort(lambdas);

    for (int g = 0; g < glms.length; g++) {
      double lambda = lambdas[g];

      // Coordinate gradient descent
      final double tl2 = lambda * y.length;
      for (int iter = 0; iter < maxNumIters; iter++) {
        double prevLoss = GLMOptimUtils.computeRidgeLoss(pTrain, y, w, lambda);

        if (fitIntercept) {
          intercept += OptimUtils.fitIntercept(pTrain, rTrain, y);
        }

        doOnePassBinomial(indices, values, theta, y, tl2, w, pTrain, rTrain);

        double currLoss = GLMOptimUtils.computeRidgeLoss(pTrain, y, w, lambda);

        if (verbose) {
          System.out.println("Iteration " + iter + ": " + " " + currLoss);
        }

        if (OptimUtils.isConverged(prevLoss, currLoss, epsilon)) {
          break;
        }
      }

      glms[g] = GLMOptimUtils.getGLM(attrs, w, intercept, LinkFunction.LOGIT);
    }

    return glms;
  }
Esempio n. 3
0
  /**
   * Builds an L2-regularized regressor on sparse inputs. Each row of the input represents a feature
   * (instead of a data point), i.e., in column-oriented format. This procedure does not assume the
   * data is normalized or centered.
   *
   * @param attrs the attribute list.
   * @param indices the indices.
   * @param values the values.
   * @param y the targets.
   * @param maxNumIters the maximum number of iterations.
   * @param lambda the lambda.
   * @return an L2-regularized regressor.
   */
  public GLM buildGaussianRegressor(
      int[] attrs, int[][] indices, double[][] values, double[] y, int maxNumIters, double lambda) {
    double[] w = new double[attrs.length];
    double intercept = 0;

    // Initialize residuals
    double[] rTrain = new double[y.length];
    for (int i = 0; i < rTrain.length; i++) {
      rTrain[i] = y[i];
    }

    // Calculate sum of squares
    double[] sq = new double[attrs.length];
    for (int i = 0; i < values.length; i++) {
      sq[i] = StatUtils.sumSq(values[i]);
    }

    // Coordinate descent
    final double tl2 = lambda * y.length;
    for (int iter = 0; iter < maxNumIters; iter++) {
      double prevLoss = GLMOptimUtils.computeRidgeLoss(rTrain, w, lambda);

      if (fitIntercept) {
        intercept += OptimUtils.fitIntercept(rTrain);
      }

      doOnePassGaussian(indices, values, sq, tl2, w, rTrain);

      double currLoss = GLMOptimUtils.computeRidgeLoss(rTrain, w, lambda);

      if (verbose) {
        System.out.println("Iteration " + iter + ": " + " " + currLoss);
      }

      if (OptimUtils.isConverged(prevLoss, currLoss, epsilon)) {
        break;
      }
    }

    return GLMOptimUtils.getGLM(attrs, w, intercept, LinkFunction.IDENTITY);
  }