示例#1
0
 public final double deviance(double yr, double ym) {
   double y1 = yr == 0 ? .1 : yr;
   switch (_family) {
     case gaussian:
       return (yr - ym) * (yr - ym);
     case binomial:
       return 2 * ((MathUtils.y_log_y(yr, ym)) + MathUtils.y_log_y(1 - yr, 1 - ym));
     case poisson:
       if (yr == 0) return 2 * ym;
       return 2 * ((yr * Math.log(yr / ym)) - (yr - ym));
     case gamma:
       if (yr == 0) return -2;
       return -2 * (Math.log(yr / ym) - (yr - ym) / ym);
     case tweedie:
       double theta =
           _var_power == 1
               ? Math.log(y1 / ym)
               : (Math.pow(y1, 1. - _var_power) - Math.pow(ym, 1 - _var_power))
                   / (1 - _var_power);
       double kappa =
           _var_power == 2
               ? Math.log(y1 / ym)
               : (Math.pow(yr, 2 - _var_power) - Math.pow(ym, 2 - _var_power))
                   / (2 - _var_power);
       return 2 * (yr * theta - kappa);
     default:
       throw new RuntimeException("unknown family " + _family);
   }
 }
示例#2
0
    public final double regularize(double[] u, Regularizer regularization) {
      if (u == null) return 0;
      double ureg = 0;

      switch (regularization) {
        case None:
          return 0;
        case Quadratic:
          for (int i = 0; i < u.length; i++) ureg += u[i] * u[i];
          return ureg;
        case L2:
          for (int i = 0; i < u.length; i++) ureg += u[i] * u[i];
          return Math.sqrt(ureg);
        case L1:
          for (int i = 0; i < u.length; i++) ureg += Math.abs(u[i]);
          return ureg;
        case NonNegative:
          for (int i = 0; i < u.length; i++) {
            if (u[i] < 0) return Double.POSITIVE_INFINITY;
          }
          return 0;
        case OneSparse:
          int card = 0;
          for (int i = 0; i < u.length; i++) {
            if (u[i] < 0) return Double.POSITIVE_INFINITY;
            else if (u[i] > 0) card++;
          }
          return card == 1 ? 0 : Double.POSITIVE_INFINITY;
        case UnitOneSparse:
          int ones = 0, zeros = 0;
          for (int i = 0; i < u.length; i++) {
            if (u[i] == 1) ones++;
            else if (u[i] == 0) zeros++;
            else return Double.POSITIVE_INFINITY;
          }
          return ones == 1 && zeros == u.length - 1 ? 0 : Double.POSITIVE_INFINITY;
        case Simplex:
          double sum = 0, absum = 0;
          for (int i = 0; i < u.length; i++) {
            if (u[i] < 0) return Double.POSITIVE_INFINITY;
            else {
              sum += u[i];
              absum += Math.abs(u[i]);
            }
          }
          return MathUtils.equalsWithinRecSumErr(sum, 1.0, u.length, absum)
              ? 0
              : Double.POSITIVE_INFINITY;
        default:
          throw new RuntimeException("Unknown regularization function " + regularization);
      }
    }
示例#3
0
 @Test
 public void testQuantile() {
   Frame f = null;
   try {
     Frame fr =
         frame(
             ard(
                 ard(1.223292e-02),
                 ard(1.635312e-25),
                 ard(1.601522e-11),
                 ard(8.452298e-10),
                 ard(2.643733e-10),
                 ard(2.671520e-06),
                 ard(1.165381e-06),
                 ard(7.193265e-10),
                 ard(3.383532e-04),
                 ard(2.561221e-05)));
     double[] probs = new double[] {0.001, 0.005, .01, .02, .05, .10, .50, .8883, .90, .99};
     String x =
         String.format("(quantile %%%s %s \"interpolate\")", fr._key, Arrays.toString(probs));
     Val val = Exec.exec(x);
     fr.delete();
     f = val.getFrame();
     Assert.assertEquals(2, f.numCols());
     // Expected values computed as golden values from R's quantile call
     double[] exp =
         ard(
             1.4413698000016206E-13,
             7.206849000001562E-13,
             1.4413698000001489E-12,
             2.882739600000134E-12,
             7.20684900000009E-12,
             1.4413698000000017E-11,
             5.831131148999999E-07,
             3.3669567275300000E-04,
             0.00152780988,
             0.011162408988);
     for (int i = 0; i < exp.length; i++)
       Assert.assertTrue(
           "expected " + exp[i] + " got " + f.vec(1).at(i),
           water.util.MathUtils.compare(exp[i], f.vec(1).at(i), 1e-6, 1e-6));
   } finally {
     if (f != null) f.delete();
   }
 }
示例#4
0
  public static Frame[] shuffleSplitFrame(
      Frame fr, Key[] keys, final double ratios[], final long seed) {
    // Sanity check the ratios
    assert keys.length == ratios.length;
    double sum = ratios[0];
    for (int i = 1; i < ratios.length; i++) {
      sum += ratios[i];
      ratios[i] = sum;
    }
    assert water.util.MathUtils.equalsWithinOneSmallUlp(sum, 1.0);

    // Do the split, into ratios.length groupings of NewChunks
    final int ncols = fr.numCols();
    MRTask mr =
        new MRTask() {
          @Override
          public void map(Chunk cs[], NewChunk ncs[]) {
            Random rng = new Random(seed * cs[0].cidx());
            int nrows = cs[0]._len;
            for (int i = 0; i < nrows; i++) {
              double r = rng.nextDouble();
              int x = 0; // Pick the NewChunk split
              for (; x < ratios.length - 1; x++) if (r < ratios[x]) break;
              x *= ncols;
              // Helper string holder
              ValueString vstr = new ValueString();
              // Copy row to correct set of NewChunks
              for (int j = 0; j < ncols; j++) {
                byte colType = cs[j].vec().get_type();
                switch (colType) {
                  case Vec.T_BAD:
                    break; /* NOP */
                  case Vec.T_STR:
                    ncs[x + j].addStr(cs[j], i);
                    break;
                  case Vec.T_UUID:
                    ncs[x + j].addUUID(cs[j], i);
                    break;
                  case Vec.T_NUM: /* fallthrough */
                  case Vec.T_ENUM:
                  case Vec.T_TIME:
                    ncs[x + j].addNum(cs[j].atd(i));
                    break;
                  default:
                    if (colType > Vec.T_TIME && colType <= Vec.T_TIMELAST)
                      ncs[x + j].addNum(cs[j].atd(i));
                    else throw new IllegalArgumentException("Unsupported vector type: " + colType);
                    break;
                }
              }
            }
          }
        }.doAll(ncols * ratios.length, fr);

    // Build output frames
    Frame frames[] = new Frame[ratios.length];
    Vec[] vecs = fr.vecs();
    String[] names = fr.names();
    Futures fs = new Futures();
    for (int i = 0; i < ratios.length; i++) {
      Vec[] nvecs = new Vec[ncols];
      for (int c = 0; c < ncols; c++) {
        mr.appendables()[i * ncols + c].setDomain(vecs[c].domain());
        nvecs[c] = mr.appendables()[i * ncols + c].close(fs);
      }
      frames[i] = new Frame(keys[i], fr.names(), nvecs);
      DKV.put(frames[i], fs);
    }
    fs.blockForPending();
    return frames;
  }
示例#5
0
 /** Re-do the TwoDim table generation with updated model. */
 public TwoDimTable generateSummary(Key train, int iter) {
   String[] names =
       new String[] {
         "Family",
         "Link",
         "Regularization",
         "Number of Predictors Total",
         "Number of Active Predictors",
         "Number of Iterations",
         "Training Frame"
       };
   String[] types = new String[] {"string", "string", "string", "int", "int", "int", "string"};
   String[] formats = new String[] {"%s", "%s", "%s", "%d", "%d", "%d", "%s"};
   if (_parms._lambda_search) {
     names =
         new String[] {
           "Family",
           "Link",
           "Regularization",
           "Lambda Search",
           "Number of Predictors Total",
           "Number of Active Predictors",
           "Number of Iterations",
           "Training Frame"
         };
     types = new String[] {"string", "string", "string", "string", "int", "int", "int", "string"};
     formats = new String[] {"%s", "%s", "%s", "%s", "%d", "%d", "%d", "%s"};
   }
   _output._model_summary =
       new TwoDimTable("GLM Model", "summary", new String[] {""}, names, types, formats, "");
   _output._model_summary.set(0, 0, _parms._family.toString());
   _output._model_summary.set(0, 1, _parms._link.toString());
   String regularization = "None";
   if (_parms._lambda != null
       && !(_parms._lambda.length == 1 && _parms._lambda[0] == 0)) { // have regularization
     if (_parms._alpha[0] == 0) regularization = "Ridge ( lambda = ";
     else if (_parms._alpha[0] == 1) regularization = "Lasso (lambda = ";
     else
       regularization =
           "Elastic Net (alpha = " + MathUtils.roundToNDigits(_parms._alpha[0], 4) + ", lambda = ";
     regularization =
         regularization
             + MathUtils.roundToNDigits(_parms._lambda[_output._best_lambda_idx], 4)
             + " )";
   }
   _output._model_summary.set(0, 2, regularization);
   int lambdaSearch = 0;
   if (_parms._lambda_search) {
     lambdaSearch = 1;
     _output._model_summary.set(
         0,
         3,
         "nlambda = "
             + _parms._nlambdas
             + ", lambda_max = "
             + MathUtils.roundToNDigits(_lambda_max, 4)
             + ", best_lambda = "
             + MathUtils.roundToNDigits(_output.bestSubmodel().lambda_value, 4));
   }
   int intercept = _parms._intercept ? 1 : 0;
   if (_output.nclasses() > 2) {
     _output._model_summary.set(0, 3 + lambdaSearch, _output.bestSubmodel().beta.length);
   } else {
     _output._model_summary.set(0, 3 + lambdaSearch, beta().length);
   }
   _output._model_summary.set(0, 4 + lambdaSearch, Integer.toString(_output.rank() - intercept));
   _output._model_summary.set(0, 5 + lambdaSearch, Integer.valueOf(iter));
   _output._model_summary.set(0, 6 + lambdaSearch, train.toString());
   return _output._model_summary;
 }
  /** Compute statistics about this model on all nodes */
  public void computeStats() {
    if (!get_params()._diagnostics) return;
    float[][] rate = get_params()._adaptive_rate ? new float[units.length - 1][] : null;

    if (get_params()._autoencoder && get_params()._sparsity_beta > 0) {
      for (int k = 0; k < get_params()._hidden.length; k++) {
        mean_a[k] = 0;
        for (int j = 0; j < avg_activations[k].size(); j++) mean_a[k] += avg_activations[k].get(j);
        mean_a[k] /= avg_activations[k].size();
      }
    }

    for (int y = 1; y < units.length; y++) {
      mean_rate[y] = rms_rate[y] = 0;
      mean_bias[y] = rms_bias[y] = 0;
      mean_weight[y] = rms_weight[y] = 0;
      for (int u = 0; u < biases[y - 1].size(); u++) {
        mean_bias[y] += biases[y - 1].get(u);
      }
      if (rate != null) rate[y - 1] = new float[get_weights(y - 1).raw().length];
      for (int u = 0; u < get_weights(y - 1).raw().length; u++) {
        mean_weight[y] += get_weights(y - 1).raw()[u];
        if (rate != null) {
          //            final float RMS_dx =
          // (float)Math.sqrt(ada[y-1][2*u]+(float)get_params().epsilon);
          //            final float invRMS_g =
          // (float)(1/Math.sqrt(ada[y-1][2*u+1]+(float)get_params().epsilon));
          final float RMS_dx =
              MathUtils.approxSqrt(
                  get_ada_dx_g(y - 1).raw()[2 * u] + (float) get_params()._epsilon);
          final float invRMS_g =
              MathUtils.approxInvSqrt(
                  get_ada_dx_g(y - 1).raw()[2 * u + 1] + (float) get_params()._epsilon);
          rate[y - 1][u] =
              RMS_dx
                  * invRMS_g; // not exactly right, RMS_dx should be from the previous time step ->
                              // but close enough for diagnostics.
          mean_rate[y] += rate[y - 1][u];
        }
      }

      mean_bias[y] /= biases[y - 1].size();

      mean_weight[y] /= get_weights(y - 1).size();
      if (rate != null) mean_rate[y] /= rate[y - 1].length;

      for (int u = 0; u < biases[y - 1].size(); u++) {
        final double db = biases[y - 1].get(u) - mean_bias[y];
        rms_bias[y] += db * db;
      }
      for (int u = 0; u < get_weights(y - 1).size(); u++) {
        final double dw = get_weights(y - 1).raw()[u] - mean_weight[y];
        rms_weight[y] += dw * dw;
        if (rate != null) {
          final double drate = rate[y - 1][u] - mean_rate[y];
          rms_rate[y] += drate * drate;
        }
      }
      rms_bias[y] = MathUtils.approxSqrt(rms_bias[y] / biases[y - 1].size());
      rms_weight[y] = MathUtils.approxSqrt(rms_weight[y] / get_weights(y - 1).size());
      if (rate != null) rms_rate[y] = MathUtils.approxSqrt(rms_rate[y] / rate[y - 1].length);
      //        rms_bias[y] = (float)Math.sqrt(rms_bias[y]/biases[y-1].length);
      //        rms_weight[y] = (float)Math.sqrt(rms_weight[y]/weights[y-1].length);
      //        if (rate != null) rms_rate[y] = (float)Math.sqrt(rms_rate[y]/rate[y-1].length);

      // Abort the run if weights or biases are unreasonably large (Note that all input values are
      // normalized upfront)
      // This can happen with Rectifier units when L1/L2/max_w2 are all set to 0, especially when
      // using more than 1 hidden layer.
      final double thresh = 1e10;
      unstable |=
          mean_bias[y] > thresh
              || isNaN(mean_bias[y])
              || rms_bias[y] > thresh
              || isNaN(rms_bias[y])
              || mean_weight[y] > thresh
              || isNaN(mean_weight[y])
              || rms_weight[y] > thresh
              || isNaN(rms_weight[y]);
    }
  }