public Vec optimizeColumn(final FuncC1 func, final Vec codingColumn) {
    final Vec mu = new ArrayVec(codingColumn.dim());
    for (int i = 0; i < mu.dim(); i++) {
      final double code = codingColumn.get(i);
      if (code == 1.0) mu.set(i, 1.0);
      else if (code == -1.0) mu.set(i, 0.0);
      else mu.set(i, 0.5);
    }

    double error = 100500;
    while (error > 1e-3) {
      final Vec muPrev = VecTools.copy(mu);
      final Vec gradient = func.gradient(mu);
      VecTools.incscale(mu, gradient, -step);

      for (int i = 0; i < mu.dim(); i++) {
        final double code = codingColumn.get(i);
        final double val = mu.get(i);
        if (code == 1.0 || val > 1.0) {
          mu.set(i, 1.0);
        } else if (code == -1.0 || val < 0) {
          mu.set(i, 0);
        }
      }
      System.out.println(mu);
      error = VecTools.norm(VecTools.subtract(muPrev, mu));
    }

    return new ArrayVec(codingColumn.dim());
  }
 public CMLMetricOptimization(
     final VecDataSet ds,
     final BlockwiseMLLLogit target,
     final Mx S,
     final double c,
     final double step) {
   this.ds = ds;
   this.target = target;
   this.step = step;
   this.classesIdxs = MCTools.splitClassesIdxs(target.labels());
   this.laplacian = VecTools.copy(S);
   VecTools.scale(laplacian, -1.0);
   for (int i = 0; i < laplacian.rows(); i++) {
     final double diagElem = VecTools.sum(S.row(i));
     laplacian.adjust(i, i, diagElem);
   }
   this.c = c;
 }
Пример #3
0
  public void backward() {
    Mx cnc = null;
    if (bias_b != 0) {
      cnc = leftContract(output);
    } else {
      cnc = VecTools.copy(output);
    }

    difference = MxTools.multiply(MxTools.transpose(cnc), activations);
    for (int i = 0; i < difference.dim(); i++) {
      difference.set(i, difference.get(i) / activations.rows());
    }

    input = MxTools.multiply(cnc, weights);

    rectifier.grad(activations, activations);
    for (int i = 0; i < input.dim(); i++) {
      input.set(i, input.get(i) * activations.get(i));
      if (dropoutFraction > 0) {
        input.set(i, input.get(i) * dropoutMask.get(i));
      }
    }
  }
Пример #4
0
  public void forward() {
    if (bias != 0) {
      activations = leftExtend(input);
    } else {
      activations = VecTools.copy(input);
    }

    output = MxTools.multiply(activations, MxTools.transpose(weights));
    rectifier.value(output, output);

    if (dropoutFraction > 0) {
      if (isTrain) {
        dropoutMask = getDropoutMask();

        for (int i = 0; i < output.dim(); i++) {
          output.set(i, output.get(i) * dropoutMask.get(i));
        }
      } else {
        for (int i = 0; i < output.dim(); i++) {
          output.set(i, output.get(i) * (1 - dropoutFraction));
        }
      }
    }
  }