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()); }
@Override public Vec gradient(final Vec mu) { final Vec grad = new ArrayVec(mu.dim()); for (int k = 0; k < grad.dim(); k++) { final TIntList idxs = classesIdxs.get(k); double val = 0.0; for (final TIntIterator listIter = idxs.iterator(); listIter.hasNext(); ) { final Vec x = ds.data().row(listIter.next()); final double trans = binClassifier.value(x); final double sigmoid = MathTools.sigmoid(trans); val -= (2 * sigmoid - 1) / (mu.get(k) * sigmoid + (1 - mu.get(k)) * (1 - sigmoid)); grad.set(k, val); } } final double norm = VecTools.norm(grad); VecTools.scale(grad, 1 / norm); for (int k = 0; k < grad.dim(); k++) { final double val = VecTools.multiply(laplacian.row(k), mu); grad.adjust(k, val); } return grad; }