Package de.jungblut.math.dense

Examples of de.jungblut.math.dense.DenseDoubleVector


    LogisticRegressionCostFunction cnf = new LogisticRegressionCostFunction(x,
        y, lambda);

    // random init theta
    theta = new DenseDoubleVector(x.getColumnCount() * y.getRowCount());
    for (int i = 0; i < theta.getDimension(); i++) {
      theta.set(i, (random.nextDouble() * 2) - 1d);
    }
    theta = minimizer.minimize(cnf, theta, numIterations, verbose);
  }
View Full Code Here


        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)) });
  }
View Full Code Here

    DoubleVector globalBestPosition = pInput;
    final DoubleVector[] particlePositions = new DoubleVector[numParticles];
    final double[] particlePersonalBestCost = new double[numParticles];
    // we are going to spread the particles a bit
    for (int i = 0; i < numParticles; i++) {
      particlePositions[i] = new DenseDoubleVector(Arrays.copyOf(
          pInput.toArray(), pInput.getLength()));
      for (int j = 0; j < particlePositions[i].getLength(); j++) {
        particlePositions[i].set(j, particlePositions[i].get(j)
            + particlePositions[i].get(j) * random.nextDouble());
      }
View Full Code Here

    public Tuple<Double, DoubleVector> call() throws Exception {
      // loop over all particles and calculate new positions
      for (int particleIndex = range.getStart(); particleIndex < range.getEnd(); particleIndex++) {
        DoubleVector currentPosition = particlePositions[particleIndex];
        DoubleVector currentBest = particlePersonalBestPositions[particleIndex];
        DenseDoubleVector vec = new DenseDoubleVector(dim);
        for (int index = 0; index < vec.getDimension(); index++) {
          double value = (phi * currentPosition.get(index)) // inertia
              + (alpha * random.nextDouble() * (currentBest.get(index) - currentPosition
                  .get(index))) // personal memory
              + (beta * random.nextDouble() * (globalBestPosition.get(index) - currentPosition
                  .get(index))); // group memory
          vec.set(index, value);
        }
        particlePositions[particleIndex] = vec;
        double cost = f.evaluateCost(vec).getCost();
        // check if we have a personal best
        if (cost < particlePersonalBestCost[particleIndex]) {
View Full Code Here

    int length = 0;
    for (DoubleMatrix matrix : matrices) {
      length += matrix.getRowCount() * matrix.getColumnCount();
    }

    DenseDoubleVector v = new DenseDoubleVector(length);
    int index = 0;
    for (DoubleMatrix matrix : matrices) {
      for (int j = 0; j < matrix.getColumnCount(); j++) {
        for (int i = 0; i < matrix.getRowCount(); i++) {
          v.set(index++, matrix.get(i, j));
        }
      }
    }

    return v;
View Full Code Here

  /**
   * Folds a single matrix into a single vector by rows.
   */
  public static DoubleVector foldMatrix(DoubleMatrix mat) {
    DoubleVector vec = new DenseDoubleVector(mat.getRowCount()
        * mat.getColumnCount());
    int index = 0;
    for (int i = 0; i < mat.getRowCount(); i++) {
      for (int j = 0; j < mat.getColumnCount(); j++) {
        vec.set(index++, mat.get(i, j));
      }
    }

    return vec;
  }
View Full Code Here

  @Override
  public DoubleVector minimize(CostFunction f, DoubleVector theta,
      int maxIterations, boolean verbose) {

    DoubleVector zeros = new DenseDoubleVector(theta.getDimension());
    this.x = theta;
    this.grad = zeros;
    this.newX = theta.deepCopy();
    this.newGrad = zeros;
    this.dir = zeros;
View Full Code Here

    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);
View Full Code Here

      if (outcomeMatrix != null) {
        DoubleVector[] outcomeSubArray = ArrayUtils.subArray(outcomeMatrix,
            start, end);
        outcomeMat = new DenseDoubleMatrix(outcomeSubArray);
      }
      DenseDoubleVector bias = DenseDoubleVector.ones(featureSubArray.length);
      DoubleMatrix featureMatrix = sparse ? new SparseDoubleRowMatrix(
          featureSubArray) : new DenseDoubleMatrix(featureSubArray);
      DoubleMatrix featuresWithBias = sparse ? new SparseDoubleRowMatrix(bias,
          featureMatrix) : new DenseDoubleMatrix(bias, featureMatrix);
      batches.add(new Tuple<>(featuresWithBias, outcomeMat));
View Full Code Here

        }
        break;
      }
    }
    double costSum = 0d;
    DoubleVector gradientSum = new DenseDoubleVector(input.getLength());
    try {
      // now collect the results
      for (int i = 0; i < submittedBatches; i++) {
        CostGradientTuple result = completionService.take().get();
        costSum += result.getCost();
        gradientSum = gradientSum.add(result.getGradient());
      }
    } catch (InterruptedException | ExecutionException e) {
      e.printStackTrace();
      // return null so minimizers should fast-fail
      return null;
    }
    // just return an average over the batches
    return new CostGradientTuple(costSum / submittedBatches,
        gradientSum.divide(submittedBatches));
  }
View Full Code Here

TOP

Related Classes of de.jungblut.math.dense.DenseDoubleVector

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.