SimpleMatrix randomTransformMatrix() {
   SimpleMatrix binary = new SimpleMatrix(numHid, numHid * 2 + 1);
   // bias column values are initialized zero
   binary.insertIntoThis(0, 0, randomTransformBlock());
   binary.insertIntoThis(0, numHid, randomTransformBlock());
   return binary.scale(op.trainOptions.scalingForInit);
 }
 /** Returns matrices of the right size for either binary or unary (terminal) classification */
 SimpleMatrix randomClassificationMatrix() {
   SimpleMatrix score = new SimpleMatrix(numClasses, numHid + 1);
   // Leave the bias column with 0 values
   double range = 1.0 / (Math.sqrt((double) numHid));
   score.insertIntoThis(0, 0, SimpleMatrix.random(numClasses, numHid, -range, range, rand));
   return score.scale(op.trainOptions.scalingForInit);
 }
Exemple #3
0
  /** Depending on if setZero being true or not the size of the R matrix changes */
  @Test
  public void checkGetRInputSize() {
    int width = 5;
    int height = 10;

    QRDecomposition<DenseMatrix64F> alg = createQRDecomposition();

    SimpleMatrix A = new SimpleMatrix(height, width);
    RandomMatrices.setRandom(A.getMatrix(), rand);

    alg.decompose(A.getMatrix());

    // check the case where it creates the matrix first
    assertTrue(alg.getR(null, true).numRows == width);
    assertTrue(alg.getR(null, false).numRows == height);

    // check the case where a matrix is provided
    alg.getR(new DenseMatrix64F(width, width), true);
    alg.getR(new DenseMatrix64F(height, width), false);

    // check some negative cases
    try {
      alg.getR(new DenseMatrix64F(height, width), true);
      fail("Should have thrown an exception");
    } catch (IllegalArgumentException e) {
    }

    try {
      alg.getR(new DenseMatrix64F(width - 1, width), false);
      fail("Should have thrown an exception");
    } catch (IllegalArgumentException e) {
    }
  }
Exemple #4
0
  /**
   * See if passing in a matrix or not providing one to getQ and getR functions has the same result
   */
  @Test
  public void checkGetNullVersusNot() {
    int width = 5;
    int height = 10;

    QRDecomposition<DenseMatrix64F> alg = createQRDecomposition();

    SimpleMatrix A = new SimpleMatrix(height, width);
    RandomMatrices.setRandom(A.getMatrix(), rand);

    alg.decompose(A.getMatrix());

    // get the results from a provided matrix
    DenseMatrix64F Q_provided = RandomMatrices.createRandom(height, height, rand);
    DenseMatrix64F R_provided = RandomMatrices.createRandom(height, width, rand);

    assertTrue(R_provided == alg.getR(R_provided, false));
    assertTrue(Q_provided == alg.getQ(Q_provided, false));

    // get the results when no matrix is provided
    DenseMatrix64F Q_null = alg.getQ(null, false);
    DenseMatrix64F R_null = alg.getR(null, false);

    // see if they are the same
    assertTrue(MatrixFeatures.isEquals(Q_provided, Q_null));
    assertTrue(MatrixFeatures.isEquals(R_provided, R_null));
  }
    // ecuatia sferei
    // sum from i=1 to m of x(i)^2
    // x(i) is coded with 8 bits
    public double fitness(SimpleMatrix cromosom) {
      int value = 0;
      int n = cromosom.numCols();
      for (int i = 0; i < n; i++) {
        value += Math.pow(cromosom.get(i), 2);
      }

      return value;
    }
  /** Create a transform which will move the specified point to the origin */
  private SimpleMatrix translateToOrigin(int x0, int y0) {

    SimpleMatrix T = SimpleMatrix.identity(3);

    T.set(0, 2, -x0);
    T.set(1, 2, -y0);

    return T;
  }
  private SimpleMatrix computeG(Point3D_F64 epipole, int x0, int y0) {
    double x = epipole.x / epipole.z - x0;
    double y = epipole.y / epipole.z - y0;

    double f = Math.sqrt(x * x + y * y);
    SimpleMatrix G = SimpleMatrix.identity(3);
    G.set(2, 0, -1.0 / f);

    return G;
  }
  /** H0 = H*M P=[M|m] from canonical camera */
  private SimpleMatrix computeHZero(DenseMatrix64F F, Point3D_F64 e2, SimpleMatrix H) {

    Vector3D_F64 v = new Vector3D_F64(.1, 0.5, .2);

    // need to make sure M is not singular for this technique to work
    SimpleMatrix P = SimpleMatrix.wrap(MultiViewOps.canonicalCamera(F, e2, v, 1));

    SimpleMatrix M = P.extractMatrix(0, 3, 0, 3);

    return H.mult(M);
  }
    // ecuatia Rastrigin
    // x(i) is coded with 8 bits
    public double fitness(SimpleMatrix cromosom) {
      int value = 0;
      int n = cromosom.numCols();
      double x1;
      for (int i = 0; i < n; i++) {
        x1 = cromosom.get(i);
        value += (x1 * x1 - 10 * Math.cos(Math.PI * x1) + 10);
      }

      return value;
    }
Exemple #10
0
    // ecuatia Rosenbrock
    // x(i) is coded with 8 bits
    public double fitness(SimpleMatrix cromosom) {
      int value = 0;
      int n = cromosom.numRows();
      double x1, x2;
      for (int i = 0; i < n - 1; i++) {
        x1 = cromosom.get(i);
        x2 = cromosom.get(i + 1);
        value += (100 * Math.pow(x2 - Math.pow(x1, 2), 2) + Math.pow(x1 - 1, 2));
      }

      return value;
    }
Exemple #11
0
    // ecuatia Griewank
    // x(i) is coded with 8 bits
    public double fitness(SimpleMatrix cromosom) {
      int sum = 0, product = 1;
      int n = cromosom.numCols();
      double x1;
      for (int i = 0; i < n; i++) {
        x1 = cromosom.get(i);
        sum += x1 * x1;
        product *= Math.cos(x1 / Math.sqrt(i + 1));
      }

      return 1 / 4000 * sum - product + 1;
    }
Exemple #12
0
  @Override
  public double[][] predict(List<PredictionPaper> testDocs) {
    String testData = "lda/test.dat";

    createLdaInputTest(testData, testDocs);
    Utils.runCommand(
        "lib/lda-c-dist/lda inf "
            + " lib/lda-c-dist/settings.txt "
            + "lda/final "
            + testData
            + " lda/output",
        false);

    double[][] gammasMatrix = Utils.readMatrix("lda/output-gamma.dat", false);
    double alpha = Utils.readAlpha("lda/final.other");
    for (int i = 0; i < gammasMatrix.length; i++) {
      for (int j = 0; j < gammasMatrix[i].length; j++) {
        gammasMatrix[i][j] -= alpha;
      }
    }
    SimpleMatrix gammas = new SimpleMatrix(gammasMatrix);
    SimpleMatrix beta = new SimpleMatrix(betaMatrix);
    SimpleMatrix probabilities = gammas.mult(beta);

    double[][] result = new double[probabilities.numRows()][probabilities.numCols()];
    for (int row = 0; row < probabilities.numRows(); row++) {
      for (int col = 0; col < probabilities.numCols(); col++) {
        result[row][col] = probabilities.get(row, col);
      }
    }
    return result;
  }
Exemple #13
0
  public int predictState(int from_state, int number_of_steps) {
    long lStartTime = System.currentTimeMillis();
    int from_bin = getBinNumber((float) from_state);
    SimpleMatrix result = new SimpleMatrix(transition_matrix);
    for (int i = 1; i < number_of_steps; i++) result = result.mult(transition_matrix);

    SimpleMatrix row_result = result.extractVector(true, from_bin);
    //			row_result.print();

    double maxVal = row_result.get(0, 0);
    int maxValIndex = 0;
    for (int i = 1; i < row_result.numCols(); i++) {
      if (row_result.get(0, i) >= maxVal) {
        maxVal = row_result.get(0, i);
        maxValIndex = i;
      }
    }
    long lEndTime = System.currentTimeMillis();
    logger.info(
        ("From: "
            + from_state
            + " In NumberOfSteps: "
            + number_of_steps
            + " MaxProbabilityBin: "
            + maxValIndex
            + "  MostProbablyTo: "
            + getOrgValFromBinNumber(maxValIndex)));
    logger.info(("Time taken for prediction = " + (lEndTime - lStartTime)));
    return getOrgValFromBinNumber(maxValIndex);
  }
  /** Outputs the scores from the tree. Counts the tree nodes the same as setIndexLabels. */
  static int outputTreeScores(PrintStream out, Tree tree, int index) {
    if (tree.isLeaf()) {
      return index;
    }

    out.print("  " + index + ":");
    SimpleMatrix vector = RNNCoreAnnotations.getPredictions(tree);
    for (int i = 0; i < vector.getNumElements(); ++i) {
      out.print("  " + NF.format(vector.get(i)));
    }
    out.println();
    index++;
    for (Tree child : tree.children()) {
      index = outputTreeScores(out, child, index);
    }
    return index;
  }
Exemple #15
0
  /** See if the compact format for Q works */
  @Test
  public void checkCompactFormat() {
    int height = 10;
    int width = 5;

    QRDecomposition<DenseMatrix64F> alg = createQRDecomposition();

    SimpleMatrix A = new SimpleMatrix(height, width);
    RandomMatrices.setRandom(A.getMatrix(), rand);

    alg.decompose(A.getMatrix());

    SimpleMatrix Q = new SimpleMatrix(height, width);
    alg.getQ(Q.getMatrix(), true);

    // see if Q has the expected properties
    assertTrue(MatrixFeatures.isOrthogonal(Q.getMatrix(), 1e-6));

    // try to extract it with the wrong dimensions
    Q = new SimpleMatrix(height, height);
    try {
      alg.getQ(Q.getMatrix(), true);
      fail("Didn't fail");
    } catch (RuntimeException e) {
    }
  }
  /** Apply a rotation such that the epipole is equal to [f,0,1)\ */
  private SimpleMatrix rotateEpipole(Point3D_F64 epipole, int x0, int y0) {
    // compute rotation which will set
    // x * sin(theta) + y * cos(theta) = 0

    double x = epipole.x / epipole.z - x0;
    double y = epipole.y / epipole.z - y0;

    double theta = Math.atan2(-y, x);
    double c = Math.cos(theta);
    double s = Math.sin(theta);

    SimpleMatrix R = new SimpleMatrix(3, 3);
    R.setRow(0, 0, c, -s);
    R.setRow(1, 0, s, c);
    R.set(2, 2, 1);

    return R;
  }
Exemple #17
0
 public void rotate(Vector heading, Vector side) {
   matrix =
       matrix.mult(
           new SimpleMatrix(
               new double[][] {
                 {heading.X(), heading.Y(), 0},
                 {side.X(), side.Y(), 0},
                 {0, 0, 1}
               }));
 }
Exemple #18
0
 public void translate(double x, double y) {
   matrix =
       matrix.mult(
           new SimpleMatrix(
               new double[][] {
                 {1, 0, 0},
                 {0, 1, 0},
                 {x, y, 1}
               }));
 }
Exemple #19
0
public class Matrix {

  private SimpleMatrix matrix = SimpleMatrix.identity(3);

  public Matrix() {}

  public Matrix(double[][] matrix) {
    this.matrix = new SimpleMatrix(new double[][] {matrix[0], matrix[1], matrix[2]});
  }

  public void rotate(Vector heading, Vector side) {
    matrix =
        matrix.mult(
            new SimpleMatrix(
                new double[][] {
                  {heading.X(), heading.Y(), 0},
                  {side.X(), side.Y(), 0},
                  {0, 0, 1}
                }));
  }

  public void rotate(double angle) {
    double sin = Math.sin(angle);
    double cos = Math.cos(angle);

    matrix =
        matrix.mult(
            new SimpleMatrix(
                new double[][] {
                  {cos, sin, 0},
                  {-sin, cos, 0},
                  {0, 0, 1}
                }));
  }

  public void translate(double x, double y) {
    matrix =
        matrix.mult(
            new SimpleMatrix(
                new double[][] {
                  {1, 0, 0},
                  {0, 1, 0},
                  {x, y, 1}
                }));
  }

  public Vector transform(Vector point) {
    double tempX =
        (matrix.get(0, 0) * point.X()) + (matrix.get(1, 0) * point.Y() + matrix.get(2, 0));
    double tempY =
        (matrix.get(0, 1) * point.X()) + (matrix.get(1, 1) * point.Y() + matrix.get(2, 1));
    return new Vector(tempX, tempY);
  }
}
 public static SimpleMatrix compDist(
     List<Cluster> kCentroids, SimpleMatrix dataSet, int[] featureSet, String distanceMetric) {
   int dRows = dataSet.numRows();
   int dCols = kCentroids.size();
   int[] features = featureSet;
   SimpleMatrix distMatrix = new SimpleMatrix(dRows, dCols);
   for (int iCentroid = 0; iCentroid < dCols; iCentroid++) {
     Cluster kcenter = kCentroids.get(iCentroid);
     for (int iRows = 0; iRows < dRows; iRows++) {
       double distTemp = 0;
       for (int iFeature = 0; iFeature < features.length; iFeature++) {
         double cX = kcenter.clusterCenter.get(0, features[iFeature]);
         double dX = dataSet.get(iRows, features[iFeature]);
         distTemp = distTemp + Math.pow(cX - dX, 2);
       }
       distMatrix.set(iRows, iCentroid, Math.sqrt(distTemp));
     }
   }
   return distMatrix;
 }
 public static boolean detectConvergence(
     List<Cluster> kCentroids, int[] featureSet, double tolerance) {
   int iCentroids = kCentroids.size();
   boolean toReturn = true;
   for (int i = 0; i < iCentroids; i++) {
     toReturn = true;
     SimpleMatrix current = kCentroids.get(i).clusterCenter.copy();
     SimpleMatrix previous = kCentroids.get(i).clusterPrevious.copy();
     for (int j = 0; j < featureSet.length; j++) {
       toReturn = true;
       // System.out.println(current.get(0, featureSet[j])+"   "+previous.get(0, featureSet[j])+"
       // "+Math.abs(Math.abs(current.get(0, featureSet[j])) - Math.abs(previous.get(0,
       // featureSet[j])))+"   "+tolerance*current.get(0, featureSet[j]));
       if (Math.abs(
               Math.abs(current.get(0, featureSet[j])) - Math.abs(previous.get(0, featureSet[j])))
           > (tolerance * current.get(0, featureSet[j]))) {
         kCentroids.get(i).hasChanged = true;
         // System.out.println("Changes are there");
         break;
       } else {
         kCentroids.get(i).hasChanged = false;
       }
       // System.out.println(kCentroids.get(i).hasChanged);
     }
     // System.out.println(kCentroids.get(i).hasChanged);
     // if(kCentroids.get(i).hasChanged=true) break;
   }
   // Check if I for true
   for (int j = 0; j < iCentroids; j++) {
     // System.out.println(kCentroids.get(j).hasChanged);
     if (kCentroids.get(j).hasChanged) {
       toReturn = false;
       break;
     } else {
       toReturn = true;
     }
   }
   // System.out.println("To reyurn"+toReturn);
   return toReturn;
 }
Exemple #22
0
 public Vector transform(Vector point) {
   double tempX =
       (matrix.get(0, 0) * point.X()) + (matrix.get(1, 0) * point.Y() + matrix.get(2, 0));
   double tempY =
       (matrix.get(0, 1) * point.X()) + (matrix.get(1, 1) * point.Y() + matrix.get(2, 1));
   return new Vector(tempX, tempY);
 }
Exemple #23
0
  private SentimentModel(
      TwoDimensionalMap<String, String, SimpleMatrix> binaryTransform,
      TwoDimensionalMap<String, String, SimpleTensor> binaryTensors,
      TwoDimensionalMap<String, String, SimpleMatrix> binaryClassification,
      Map<String, SimpleMatrix> unaryClassification,
      Map<String, SimpleMatrix> wordVectors,
      RNNOptions op) {
    this.op = op;

    this.binaryTransform = binaryTransform;
    this.binaryTensors = binaryTensors;
    this.binaryClassification = binaryClassification;
    this.unaryClassification = unaryClassification;
    this.wordVectors = wordVectors;
    this.numClasses = op.numClasses;
    if (op.numHid <= 0) {
      int nh = 0;
      for (SimpleMatrix wv : wordVectors.values()) {
        nh = wv.getNumElements();
      }
      this.numHid = nh;
    } else {
      this.numHid = op.numHid;
    }
    this.numBinaryMatrices = binaryTransform.size();
    binaryTransformSize = numHid * (2 * numHid + 1);
    if (op.useTensors) {
      binaryTensorSize = numHid * numHid * numHid * 4;
    } else {
      binaryTensorSize = 0;
    }
    binaryClassificationSize = (op.combineClassification) ? 0 : numClasses * (numHid + 1);

    numUnaryMatrices = unaryClassification.size();
    unaryClassificationSize = numClasses * (numHid + 1);

    rand = new Random(op.randomSeed);

    identity = SimpleMatrix.identity(numHid);
  }
Exemple #24
0
  public void rotate(double angle) {
    double sin = Math.sin(angle);
    double cos = Math.cos(angle);

    matrix =
        matrix.mult(
            new SimpleMatrix(
                new double[][] {
                  {cos, sin, 0},
                  {-sin, cos, 0},
                  {0, 0, 1}
                }));
  }
  public static List<Cluster> ReassignCentrids(
      List<Cluster> kCentroids,
      SimpleMatrix distanceMatrix,
      SimpleMatrix dataSet,
      int[] featureSet) {
    List<Cluster> kCentroids_l = kCentroids;
    int[] clusterLoc = new int[dataSet.numRows()];
    for (int iRows = 0; iRows < dataSet.numRows(); iRows++) {
      int clusterNo = 1;
      double minvalue = distanceMatrix.get(iRows, 1);
      for (int iCentroid = 0; iCentroid < kCentroids_l.size(); iCentroid++) {
        // System.out.println(iRows+" "+iCentroid);
        if (distanceMatrix.get(iRows, iCentroid) < minvalue) {
          clusterNo = iCentroid;
          minvalue = distanceMatrix.get(iRows, iCentroid);
        }
      }
      clusterLoc[iRows] = clusterNo;
    }
    // Backup Centroids anc clear current centroids
    for (int i = 0; i < kCentroids_l.size(); i++) {
      kCentroids_l.get(i).backup();
      kCentroids_l.get(i).noPoints = 0;
      Arrays.fill(kCentroids_l.get(i).currPoints, -1);
      Arrays.fill(kCentroids_l.get(i).intIndex, -1);
      // System.out.println("Clusters Backed Up!");
    }
    // printKCentroids(kCentroids_l);
    for (int i = 0; i < clusterLoc.length; i++) {
      int insLoc = kCentroids_l.get(clusterLoc[i]).noPoints;
      // System.out.println("Getting element"+dataSet.get(i, 0));
      kCentroids_l.get(clusterLoc[i]).currPoints[insLoc] = (int) dataSet.get(i, 0);
      kCentroids_l.get(clusterLoc[i]).intIndex[insLoc] = i;
      kCentroids_l.get(clusterLoc[i]).noPoints++;
    }
    // System.out.println(Arrays.toString(clusterLoc));

    // Now Calculate the best representative and give as new centroid values
    for (int i = 0; i < kCentroids.size(); i++) {
      for (int j = 0; j < featureSet.length; j++) {
        double tempAvg = 0;
        int[] travVector = kCentroids_l.get(i).intIndex;
        for (int k = 0; k < travVector.length; k++) {
          if (travVector[k] != -1) {
            tempAvg = tempAvg + dataSet.get(travVector[k], featureSet[j]);
          }
        }
        kCentroids.get(i).clusterCenter.set(0, featureSet[j], tempAvg / kCentroids.get(i).noPoints);
      }
    }
    return kCentroids_l;
  }
Exemple #26
0
 /** @print the transition_matrix */
 public int printTransition_matrix() {
   if (transition_matrix == null) {
     logger.info(("The model has not been initialized yet"));
     return -1;
   }
   transition_matrix.print();
   //			System.out.println("The Transition Probability Matrix is:");
   //			for(int i = 0; i< num_of_states; i++)
   //			{
   //				for(int j = 0; j<num_of_states;j++)
   //					System.out.print(transition_matrix[i][j] + "   ");
   //				System.out.println();
   //			}
   return 0;
 }
  private static DenseMatrix64F multByExtract(
      int operationType, D1Submatrix64F subA, D1Submatrix64F subB, D1Submatrix64F subC) {
    SimpleMatrix A = subA.extract();
    SimpleMatrix B = subB.extract();
    SimpleMatrix C = subC.extract();

    if (operationType > 0) return A.mult(B).plus(C).getMatrix();
    else if (operationType < 0) return C.minus(A.mult(B)).getMatrix();
    else return A.mult(B).getMatrix();
  }
  /**
   * Compute rectification transforms for the stereo pair given a fundamental matrix and its
   * observations.
   *
   * @param F Fundamental matrix
   * @param observations Observations used to compute F
   * @param width Width of first image.
   * @param height Height of first image.
   */
  public void process(DenseMatrix64F F, List<AssociatedPair> observations, int width, int height) {

    int centerX = width / 2;
    int centerY = height / 2;

    MultiViewOps.extractEpipoles(F, epipole1, epipole2);
    checkEpipoleInside(width, height);

    // compute the transform H which will send epipole2 to infinity
    SimpleMatrix R = rotateEpipole(epipole2, centerX, centerY);
    SimpleMatrix T = translateToOrigin(centerX, centerY);
    SimpleMatrix G = computeG(epipole2, centerX, centerY);

    SimpleMatrix H = G.mult(R).mult(T);

    // Find the two matching transforms
    SimpleMatrix Hzero = computeHZero(F, epipole2, H);

    SimpleMatrix Ha = computeAffineH(observations, H.getMatrix(), Hzero.getMatrix());

    rect1.set(Ha.mult(Hzero).getMatrix());
    rect2.set(H.getMatrix());
  }
Exemple #29
0
  public void trainMarkovChainModel(double[] input) {
    int input_size = input.length;
    int from_state, to_state;
    Integer[] count = new Integer[num_of_states];
    for (int i = 0; i < num_of_states; i++) count[i] = 0;

    // Loop through the input and record the number of transitions
    for (int i = 0; i < input_size - 2; i++) {
      from_state = getBinNumber(input[i]);
      to_state = getBinNumber(input[i + 1]);
      //				Increment entry by 1
      //				transition_matrix[from_state][to_state]++;
      transition_matrix.set(from_state, to_state, transition_matrix.get(from_state, to_state) + 1);

      count[from_state]++;
      // COMMENT THIS
      logger.info(
          "FromVal:"
              + input[i]
              + " FromBin: "
              + from_state
              + " ToVal:"
              + input[i + 1]
              + " ToBin: "
              + to_state
              + " TransitionCount: "
              + transition_matrix.get(from_state, to_state)
              + " TotalCount: "
              + count[from_state]);
    }

    // Calculate the transition probability matrix
    for (int i = 0; i < num_of_states; i++) {
      for (int j = 0; j < num_of_states; j++) {
        if (count[i] == 0) {
          //						transition_matrix[i][j]
          transition_matrix.set(i, j, 0);
        } else {
          //						transition_matrix[i][j]/=count[i];
          transition_matrix.set(i, j, transition_matrix.get(i, j) / count[i]);
        }
      }
    }
  }
Exemple #30
0
  private void checkDecomposition(int height, int width, boolean compact) {
    QRDecomposition<DenseMatrix64F> alg = createQRDecomposition();

    SimpleMatrix A = new SimpleMatrix(height, width);
    RandomMatrices.setRandom(A.getMatrix(), rand);

    assertTrue(alg.decompose(A.copy().getMatrix()));

    int minStride = Math.min(height, width);

    SimpleMatrix Q = new SimpleMatrix(height, compact ? minStride : height);
    alg.getQ(Q.getMatrix(), compact);
    SimpleMatrix R = new SimpleMatrix(compact ? minStride : height, width);
    alg.getR(R.getMatrix(), compact);

    // see if Q has the expected properties
    assertTrue(MatrixFeatures.isOrthogonal(Q.getMatrix(), 1e-6));

    //        UtilEjml.print(alg.getQR());
    //        Q.print();
    //        R.print();

    // see if it has the expected properties
    DenseMatrix64F A_found = Q.mult(R).getMatrix();

    EjmlUnitTests.assertEquals(A.getMatrix(), A_found, 1e-6);
    assertTrue(Q.transpose().mult(A).isIdentical(R, 1e-6));
  }