@Override public double dot(DoubleVector s) { return vector.dot(s); }
@Override public double dot(DoubleVector s) { return vector.dot(s); }
@Override public double dot(DoubleVector s) { return this.mainVector.dot(s); }
private void mapDirectionByInverseHessian() { int count = sList.size(); if (count != 0) { for (int i = count - 1; i >= 0; i--) { alphas[i] = -sList.get(i).dot(dir) / roList.get(i); addMult(dir, yList.get(i), alphas[i]); } DoubleVector lastY = yList.get(count - 1); double yDotY = lastY.dot(lastY); double scalar = roList.get(count - 1) / yDotY; scale(dir, scalar); for (int i = 0; i < count; i++) { double beta = yList.get(i).dot(dir) / roList.get(i); addMult(dir, sList.get(i), -alphas[i] - beta); } } }
private void testDirectionDerivation(CostFunction f) { double dirNorm = FastMath.sqrt(dir.dot(dir)); // if dirNorm is 0, we probably hit the minimum. So we have no gradient to // descent to. if (dirNorm != 0d) { double eps = 1.05e-8 / dirNorm; getNextPoint(eps); double val2 = evaluateL1(f); double numDeriv = (val2 - value) / eps; double deriv = directionDerivation(); LOG.info("GradCheck: expected= " + numDeriv + " vs. " + deriv + "! AbsDiff= " + Math.abs(numDeriv - deriv)); } }
@Override public double measureDistance(DoubleVector vec1, DoubleVector vec2) { double lengthSquaredv1 = vec1.pow(2).sum(); double lengthSquaredv2 = vec2.pow(2).sum(); double dotProduct = vec2.dot(vec1); double denominator = Math.sqrt(lengthSquaredv1) * Math.sqrt(lengthSquaredv2); // correct for floating-point rounding errors if (denominator < dotProduct) { denominator = dotProduct; } return 1.0 - dotProduct / denominator; }
double backoff = 0.5; if (iter == 0) { double normDir = FastMath.sqrt(dir.dot(dir)); alpha = (1 / normDir); backoff = 0.1;
@Override public double measureDistance(DoubleVector vec1, DoubleVector vec2) { double dot = vec1.dot(vec2); double set1Length = vec1.pow(2).sum(); double set2Length = vec2.pow(2).sum(); return 1.0d - (dot / (set1Length + set2Length - dot)); } }
private void shift() { DoubleVector nextS = null; DoubleVector nextY = null; int listSize = sList.size(); if (listSize < m) { nextS = new DenseDoubleVector(x.getDimension()); nextY = new DenseDoubleVector(x.getDimension()); } if (nextS == null) { nextS = sList.get(0); sList.remove(0); nextY = yList.get(0); yList.remove(0); roList.removeAt(0); } addMultInto(nextS, newX, x, -1); addMultInto(nextY, newGrad, grad, -1); double ro = nextS.dot(nextY); sList.add(nextS); yList.add(nextY); roList.add(ro); DoubleVector tmpNewX = newX.deepCopy(); newX = x.deepCopy(); x = tmpNewX; DoubleVector tmpNewGrad = newGrad.deepCopy(); newGrad = grad.deepCopy(); grad = tmpNewGrad; }
double d1 = s.multiply(-1.0d).dot(s); // this is the slope double z1 = red / (1.0 - d1); // initial step is red/(|s|+1) double d2 = df2.dot(s); M = M - 1; i = i + (length < 0 ? 1 : 0); // count epochs d2 = df2.dot(s); M = M - 1; i = i + (length < 0 ? 1 : 0); // count epochs?! d2 = df2.dot(s); final double numerator = (df2.dot(df2) - df1.dot(df2)) / df1.dot(df1); s = s.multiply(numerator).subtract(df2); tmp = df1; df1 = df2; d2 = df1.dot(s); if (d2 > 0) { // new slope must be negative s = df1.multiply(-1.0d); // otherwise use steepest direction d2 = s.multiply(-1.0d).dot(s); d1 = s.multiply(-1.0d).dot(s); z1 = 1.0d / (1.0d - d1); ls_failed = 1; // this line search failed
@Override public DoubleVector predict(DoubleVector feature) { Preconditions.checkArgument(feature.getDimension() == model.getWeights() .getDimension(), "feature dimension must match model weight dimension! Feature: " + feature.getDimension() + " != Model: " + model.getWeights().getDimension()); double result = model.getActivationFunction().apply( feature.dot(model.getWeights())); return new SingleEntryDoubleVector(result); }
@Override public DoubleVector predict(DoubleVector features) { if (features.isSparse()) { SparseDoubleVector tmp = new SparseDoubleVector( features.getDimension() + 1); tmp.set(0, 1d); Iterator<DoubleVectorElement> iterateNonZero = features.iterateNonZero(); while (iterateNonZero.hasNext()) { DoubleVectorElement next = iterateNonZero.next(); tmp.set(next.getIndex() + 1, next.getValue()); } features = tmp; } else { features = new DenseDoubleVector(1d, features.toArray()); } return new DenseDoubleVector(new double[]{SIGMOID.get().apply( features.dot(theta))}); }
@Override protected CostGradientTuple observeExample(FeatureOutcomePair next, DoubleVector weights) { DoubleVector hypothesis = new SingleEntryDoubleVector( activationFunction.apply(next.getFeature().dot(weights))); double cost = lossFunction.calculateLoss(next.getOutcome(), hypothesis); DoubleVector gradient = lossFunction.calculateGradient(next.getFeature(), next.getOutcome(), hypothesis); return new CostGradientTuple(cost, gradient); }
private double directionDerivation() { if (l1weight == 0.0) { return dir.dot(grad); } else { double val = 0.0; for (int i = 0; i < dir.getDimension(); i++) { if (dir.get(i) != 0) { if (x.get(i) < 0) { val += dir.get(i) * (grad.get(i) - l1weight); } else if (x.get(i) > 0) { val += dir.get(i) * (grad.get(i) + l1weight); } else if (dir.get(i) < 0) { val += dir.get(i) * (grad.get(i) - l1weight); } else if (dir.get(i) > 0) { val += dir.get(i) * (grad.get(i) + l1weight); } } } return val; } }