/**
   * Finds the values of a,b,c which minimize
   *
   * <p>sum (a*x(+)_i + b*y(+)_i + c - x(-)_i)^2
   *
   * <p>See page 306
   *
   * @return Affine transform
   */
  private SimpleMatrix computeAffineH(
      List<AssociatedPair> observations, DenseMatrix64F H, DenseMatrix64F Hzero) {
    SimpleMatrix A = new SimpleMatrix(observations.size(), 3);
    SimpleMatrix b = new SimpleMatrix(A.numRows(), 1);

    Point2D_F64 c = new Point2D_F64();
    Point2D_F64 k = new Point2D_F64();

    for (int i = 0; i < observations.size(); i++) {
      AssociatedPair a = observations.get(i);

      GeometryMath_F64.mult(Hzero, a.p1, k);
      GeometryMath_F64.mult(H, a.p2, c);

      A.setRow(i, 0, k.x, k.y, 1);
      b.set(i, 0, c.x);
    }

    SimpleMatrix x = A.solve(b);

    SimpleMatrix Ha = SimpleMatrix.identity(3);
    Ha.setRow(0, 0, x.getMatrix().data);

    return Ha;
  }
  /** 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;
  }
Beispiel #4
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);
  }
}
Beispiel #5
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);
  }
Beispiel #6
0
  /** The traditional way of initializing an empty model suitable for training. */
  public SentimentModel(RNNOptions op, List<Tree> trainingTrees) {
    this.op = op;
    rand = new Random(op.randomSeed);

    if (op.randomWordVectors) {
      initRandomWordVectors(trainingTrees);
    } else {
      readWordVectors();
    }
    if (op.numHid > 0) {
      this.numHid = op.numHid;
    } else {
      int size = 0;
      for (SimpleMatrix vector : wordVectors.values()) {
        size = vector.getNumElements();
        break;
      }
      this.numHid = size;
    }

    TwoDimensionalSet<String, String> binaryProductions = TwoDimensionalSet.hashSet();
    if (op.simplifiedModel) {
      binaryProductions.add("", "");
    } else {
      // TODO
      // figure out what binary productions we have in these trees
      // Note: the current sentiment training data does not actually
      // have any constituent labels
      throw new UnsupportedOperationException("Not yet implemented");
    }

    Set<String> unaryProductions = Generics.newHashSet();
    if (op.simplifiedModel) {
      unaryProductions.add("");
    } else {
      // TODO
      // figure out what unary productions we have in these trees (preterminals only, after the
      // collapsing)
      throw new UnsupportedOperationException("Not yet implemented");
    }

    this.numClasses = op.numClasses;

    identity = SimpleMatrix.identity(numHid);

    binaryTransform = TwoDimensionalMap.treeMap();
    binaryTensors = TwoDimensionalMap.treeMap();
    binaryClassification = TwoDimensionalMap.treeMap();

    // When making a flat model (no symantic untying) the
    // basicCategory function will return the same basic category for
    // all labels, so all entries will map to the same matrix
    for (Pair<String, String> binary : binaryProductions) {
      String left = basicCategory(binary.first);
      String right = basicCategory(binary.second);
      if (binaryTransform.contains(left, right)) {
        continue;
      }
      binaryTransform.put(left, right, randomTransformMatrix());
      if (op.useTensors) {
        binaryTensors.put(left, right, randomBinaryTensor());
      }
      if (!op.combineClassification) {
        binaryClassification.put(left, right, randomClassificationMatrix());
      }
    }
    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);

    unaryClassification = Generics.newTreeMap();

    // When making a flat model (no symantic untying) the
    // basicCategory function will return the same basic category for
    // all labels, so all entries will map to the same matrix
    for (String unary : unaryProductions) {
      unary = basicCategory(unary);
      if (unaryClassification.containsKey(unary)) {
        continue;
      }
      unaryClassification.put(unary, randomClassificationMatrix());
    }
    numUnaryMatrices = unaryClassification.size();
    unaryClassificationSize = numClasses * (numHid + 1);

    // System.err.println("Binary transform matrices:");
    // System.err.println(binaryTransform);
    // System.err.println("Binary classification matrices:");
    // System.err.println(binaryClassification);
    // System.err.println("Unary classification matrices:");
    // System.err.println(unaryClassification);
  }