@Override public int getDimension() { return vector.getDimension(); }
@Override public int getDimension() { return this.mainVector.getDimension(); }
/** * Generates a single-row matrix with the given row vector. * * @param row the row vector to set. */ public DenseDoubleMatrix(DoubleVector row) { this(1, row.getDimension()); setRowVector(0, row); }
public RBMCostFunction(DoubleVector[] currentTrainingSet, int batchSize, int numThreads, int numHiddenUnits, ActivationFunction activationFunction, TrainingType type, double lambda, long seed, boolean stochastic) { super(currentTrainingSet, null, batchSize, numThreads, stochastic); this.activationFunction = activationFunction; this.type = type; this.lambda = lambda; this.random = new Random(seed); this.unfoldParameters = MultilayerPerceptronCostFunction .computeUnfoldParameters(new int[]{ currentTrainingSet[0].getDimension(), numHiddenUnits + 1}); }
protected DoubleVector newInstance(DoubleVector v) { if (v.isSparse()) { return new SparseDoubleVector(v.getDimension()); } else { return new DenseDoubleVector(v.getDimension()); } }
static DoubleVector binarize(Random r, DoubleVector v) { for (int j = 0; j < v.getDimension(); j++) { v.set(j, v.get(j) > r.nextDouble() ? 1d : 0d); } return v; }
@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); }
private void scale(DoubleVector a, double b) { for (int i = 0; i < a.getDimension(); i++) { a.set(i, a.get(i) * b); } }
@Override public double calculateLoss(DoubleVector y, DoubleVector hypothesis) { double sum = 0d; for (int col = 0; col < y.getDimension(); col++) { double diff = y.get(col) - hypothesis.get(col); sum += (diff * diff); } return sum; }
void scaleInto(DoubleVector a, DoubleVector b, double c) { for (int i = 0; i < a.getDimension(); i++) { a.set(i, b.get(i) * c); } }
/** * Constructs a new {@link SparseBitVector}. * * @param v the given vector to copy. */ public SparseBitVector(DoubleVector v) { this(v.getDimension()); Iterator<DoubleVectorElement> iterateNonZero = v.iterateNonZero(); while (iterateNonZero.hasNext()) { DoubleVectorElement next = iterateNonZero.next(); set(next.getIndex(), next.getValue()); } }
private void fixDirectionSigns() { if (l1weight > 0) { for (int i = 0; i < dir.getDimension(); i++) { if (dir.get(i) * steepestDescDir.get(i) <= 0) { dir.set(i, 0); } } } }
private void addMultInto(DoubleVector a, DoubleVector b, DoubleVector c, double d) { for (int i = 0; i < a.getDimension(); i++) { a.set(i, b.get(i) + c.get(i) * d); } }
@Override public double calculateLoss(DoubleVector y, DoubleVector hypothesis) { double sum = 0d; for (int col = 0; col < y.getDimension(); col++) { sum += FastMath.abs(y.get(col) - hypothesis.get(col)); } return sum; }
private void addMult(DoubleVector a, DoubleVector b, double c) { for (int i = 0; i < a.getDimension(); i++) { a.set(i, a.get(i) + b.get(i) * c); } }
private double evaluateL1(CostFunction f) { CostGradientTuple evaluateCost = f.evaluateCost(newX); newGrad = evaluateCost.getGradient(); double val = evaluateCost.getCost(); if (l1weight > 0) { for (int i = 0; i < newGrad.getDimension(); i++) { val += Math.abs(newX.get(i)) * l1weight; } } return val; }
private void getNextPoint(double alpha) { addMultInto(newX, x, dir, alpha); if (l1weight > 0) { for (int i = 0; i < x.getDimension(); i++) { if (x.get(i) * newX.get(i) < 0.0) { newX.set(i, 0d); } } } }
@Override public List<VectorDistanceTuple<VALUE>> getNearestNeighbours( DoubleVector vec, int k, double radius) { LimitedPriorityQueue<VectorDistanceTuple<VALUE>> queue = new LimitedPriorityQueue<>( k); HyperRectangle hr = HyperRectangle.infiniteHyperRectangle(vec .getDimension()); getNearestNeighbourInternal(root, vec, hr, radius, k, radius, queue); return queue.toList(); }
/** * @return a log'd matrix that was guarded against edge cases of the * logarithm. */ public static DoubleVector logVector(DoubleVector input) { DenseDoubleVector log = new DenseDoubleVector(input.getDimension()); for (int col = 0; col < log.getDimension(); col++) { log.set(col, guardedLogarithm(input.get(col))); } return log; }
public DoubleVector closestPoint(DoubleVector t) { DoubleVector p = new DenseDoubleVector(t.getDimension()); for (int i = 0; i < t.getDimension(); ++i) { if (t.get(i) <= min.get(i)) { p.set(i, min.get(i)); } else if (t.get(i) >= max.get(i)) { p.set(i, max.get(i)); } else { p.set(i, t.get(i)); } } return p; }