Search in sources :

Example 16 with SimpleMatrix

use of org.ejml.simple.SimpleMatrix in project CoreNLP by stanfordnlp.

the class AverageDVModels method averageUnaryMatrices.

public static Map<String, SimpleMatrix> averageUnaryMatrices(List<Map<String, SimpleMatrix>> maps) {
    Map<String, SimpleMatrix> averages = Generics.newTreeMap();
    for (String name : getUnaryMatrixNames(maps)) {
        int count = 0;
        SimpleMatrix matrix = null;
        for (Map<String, SimpleMatrix> map : maps) {
            if (!map.containsKey(name)) {
                continue;
            }
            SimpleMatrix original = map.get(name);
            ++count;
            if (matrix == null) {
                matrix = original;
            } else {
                matrix = matrix.plus(original);
            }
        }
        matrix = matrix.divide(count);
        averages.put(name, matrix);
    }
    return averages;
}
Also used : SimpleMatrix(org.ejml.simple.SimpleMatrix)

Example 17 with SimpleMatrix

use of org.ejml.simple.SimpleMatrix in project CoreNLP by stanfordnlp.

the class NeuralUtils method paramsToVector.

/**
   * Given a sequence of iterators over the matrices, builds a vector
   * out of those matrices in the order given.  Asks for an expected
   * total size as a time savings.  AssertionError thrown if the
   * vector sizes do not exactly match.
   */
@SafeVarargs
public static double[] paramsToVector(int totalSize, Iterator<SimpleMatrix>... matrices) {
    double[] theta = new double[totalSize];
    int index = 0;
    for (Iterator<SimpleMatrix> matrixIterator : matrices) {
        while (matrixIterator.hasNext()) {
            SimpleMatrix matrix = matrixIterator.next();
            int numElements = matrix.getNumElements();
            //System.out.println(Integer.toString(numElements)); // to know what matrices are
            for (int i = 0; i < numElements; ++i) {
                theta[index] = matrix.get(i);
                ++index;
            }
        }
    }
    if (index != totalSize) {
        throw new AssertionError("Did not entirely fill the theta vector: expected " + totalSize + " used " + index);
    }
    return theta;
}
Also used : SimpleMatrix(org.ejml.simple.SimpleMatrix)

Example 18 with SimpleMatrix

use of org.ejml.simple.SimpleMatrix in project CoreNLP by stanfordnlp.

the class NeuralUtils method softmax.

/**
   * Applies softmax to all of the elements of the matrix.  The return
   * matrix will have all of its elements sum to 1.  If your matrix is
   * not already a vector, be sure this is what you actually want.
   */
public static SimpleMatrix softmax(SimpleMatrix input) {
    SimpleMatrix output = new SimpleMatrix(input);
    for (int i = 0; i < output.numRows(); ++i) {
        for (int j = 0; j < output.numCols(); ++j) {
            output.set(i, j, Math.exp(output.get(i, j)));
        }
    }
    double sum = output.elementSum();
    // will be safe, since exp should never return 0
    return output.scale(1.0 / sum);
}
Also used : SimpleMatrix(org.ejml.simple.SimpleMatrix)

Example 19 with SimpleMatrix

use of org.ejml.simple.SimpleMatrix in project CoreNLP by stanfordnlp.

the class NeuralUtils method oneHot.

public static SimpleMatrix oneHot(int index, int size) {
    SimpleMatrix m = new SimpleMatrix(size, 1);
    m.set(index, 1);
    return m;
}
Also used : SimpleMatrix(org.ejml.simple.SimpleMatrix)

Example 20 with SimpleMatrix

use of org.ejml.simple.SimpleMatrix in project CoreNLP by stanfordnlp.

the class NeuralUtils method paramsToVector.

/**
   * Given a sequence of iterators over the matrices, builds a vector
   * out of those matrices in the order given.  The vector is scaled
   * according to the <code>scale</code> parameter.  Asks for an
   * expected total size as a time savings.  AssertionError thrown if
   * the vector sizes do not exactly match.
   */
@SafeVarargs
public static double[] paramsToVector(double scale, int totalSize, Iterator<SimpleMatrix>... matrices) {
    double[] theta = new double[totalSize];
    int index = 0;
    for (Iterator<SimpleMatrix> matrixIterator : matrices) {
        while (matrixIterator.hasNext()) {
            SimpleMatrix matrix = matrixIterator.next();
            int numElements = matrix.getNumElements();
            for (int i = 0; i < numElements; ++i) {
                theta[index] = matrix.get(i) * scale;
                ++index;
            }
        }
    }
    if (index != totalSize) {
        throw new AssertionError("Did not entirely fill the theta vector: expected " + totalSize + " used " + index);
    }
    return theta;
}
Also used : SimpleMatrix(org.ejml.simple.SimpleMatrix)

Aggregations

SimpleMatrix (org.ejml.simple.SimpleMatrix)52 Tree (edu.stanford.nlp.trees.Tree)8 Map (java.util.Map)7 DeepTree (edu.stanford.nlp.trees.DeepTree)5 TwoDimensionalMap (edu.stanford.nlp.util.TwoDimensionalMap)5 SimpleTensor (edu.stanford.nlp.neural.SimpleTensor)4 LexicalizedParser (edu.stanford.nlp.parser.lexparser.LexicalizedParser)4 Pair (edu.stanford.nlp.util.Pair)4 IdentityHashMap (java.util.IdentityHashMap)4 Mention (edu.stanford.nlp.coref.data.Mention)3 BufferedWriter (java.io.BufferedWriter)3 File (java.io.File)3 FileWriter (java.io.FileWriter)3 ArrayList (java.util.ArrayList)3 CoreLabel (edu.stanford.nlp.ling.CoreLabel)2 Embedding (edu.stanford.nlp.neural.Embedding)2 ParserQuery (edu.stanford.nlp.parser.common.ParserQuery)2 RerankingParserQuery (edu.stanford.nlp.parser.lexparser.RerankingParserQuery)2 FileFilter (java.io.FileFilter)2 Bone (com.jme3.animation.Bone)1