Search in sources :

Example 41 with SimpleMatrix

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

the class ConvertMatlabModel method copyWordVector.

/** Will not overwrite an existing word vector if it is already there */
public static void copyWordVector(Map<String, SimpleMatrix> wordVectors, String source, String target) {
    if (wordVectors.containsKey(target) || !wordVectors.containsKey(source)) {
        return;
    }
    log.info("Using wordVector " + source + " for " + target);
    wordVectors.put(target, new SimpleMatrix(wordVectors.get(source)));
}
Also used : SimpleMatrix(org.ejml.simple.SimpleMatrix)

Example 42 with SimpleMatrix

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

the class SentimentCostAndGradient method getTensorGradient.

private static SimpleTensor getTensorGradient(SimpleMatrix deltaFull, SimpleMatrix leftVector, SimpleMatrix rightVector) {
    int size = deltaFull.getNumElements();
    SimpleTensor Wt_df = new SimpleTensor(size * 2, size * 2, size);
    // TODO: combine this concatenation with computeTensorDeltaDown?
    SimpleMatrix fullVector = NeuralUtils.concatenate(leftVector, rightVector);
    for (int slice = 0; slice < size; ++slice) {
        Wt_df.setSlice(slice, fullVector.scale(deltaFull.get(slice)).mult(fullVector.transpose()));
    }
    return Wt_df;
}
Also used : SimpleTensor(edu.stanford.nlp.neural.SimpleTensor) SimpleMatrix(org.ejml.simple.SimpleMatrix)

Example 43 with SimpleMatrix

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

the class AverageDVModels method main.

/**
   * Command line arguments for this program:
   * <br>
   * -output: the model file to output
   * -input: a list of model files to input
   */
public static void main(String[] args) {
    String outputModelFilename = null;
    List<String> inputModelFilenames = Generics.newArrayList();
    for (int argIndex = 0; argIndex < args.length; ) {
        if (args[argIndex].equalsIgnoreCase("-output")) {
            outputModelFilename = args[argIndex + 1];
            argIndex += 2;
        } else if (args[argIndex].equalsIgnoreCase("-input")) {
            for (++argIndex; argIndex < args.length && !args[argIndex].startsWith("-"); ++argIndex) {
                inputModelFilenames.addAll(Arrays.asList(args[argIndex].split(",")));
            }
        } else {
            throw new RuntimeException("Unknown argument " + args[argIndex]);
        }
    }
    if (outputModelFilename == null) {
        log.info("Need to specify output model name with -output");
        System.exit(2);
    }
    if (inputModelFilenames.size() == 0) {
        log.info("Need to specify input model names with -input");
        System.exit(2);
    }
    log.info("Averaging " + inputModelFilenames);
    log.info("Outputting result to " + outputModelFilename);
    LexicalizedParser lexparser = null;
    List<DVModel> models = Generics.newArrayList();
    for (String filename : inputModelFilenames) {
        LexicalizedParser parser = LexicalizedParser.loadModel(filename);
        if (lexparser == null) {
            lexparser = parser;
        }
        models.add(DVParser.getModelFromLexicalizedParser(parser));
    }
    List<TwoDimensionalMap<String, String, SimpleMatrix>> binaryTransformMaps = CollectionUtils.transformAsList(models, model -> model.binaryTransform);
    List<TwoDimensionalMap<String, String, SimpleMatrix>> binaryScoreMaps = CollectionUtils.transformAsList(models, model -> model.binaryScore);
    List<Map<String, SimpleMatrix>> unaryTransformMaps = CollectionUtils.transformAsList(models, model -> model.unaryTransform);
    List<Map<String, SimpleMatrix>> unaryScoreMaps = CollectionUtils.transformAsList(models, model -> model.unaryScore);
    List<Map<String, SimpleMatrix>> wordMaps = CollectionUtils.transformAsList(models, model -> model.wordVectors);
    TwoDimensionalMap<String, String, SimpleMatrix> binaryTransformAverages = averageBinaryMatrices(binaryTransformMaps);
    TwoDimensionalMap<String, String, SimpleMatrix> binaryScoreAverages = averageBinaryMatrices(binaryScoreMaps);
    Map<String, SimpleMatrix> unaryTransformAverages = averageUnaryMatrices(unaryTransformMaps);
    Map<String, SimpleMatrix> unaryScoreAverages = averageUnaryMatrices(unaryScoreMaps);
    Map<String, SimpleMatrix> wordAverages = averageUnaryMatrices(wordMaps);
    DVModel newModel = new DVModel(binaryTransformAverages, unaryTransformAverages, binaryScoreAverages, unaryScoreAverages, wordAverages, lexparser.getOp());
    DVParser newParser = new DVParser(newModel, lexparser);
    newParser.saveModel(outputModelFilename);
}
Also used : LexicalizedParser(edu.stanford.nlp.parser.lexparser.LexicalizedParser) SimpleMatrix(org.ejml.simple.SimpleMatrix) TwoDimensionalMap(edu.stanford.nlp.util.TwoDimensionalMap) Map(java.util.Map) TwoDimensionalMap(edu.stanford.nlp.util.TwoDimensionalMap)

Example 44 with SimpleMatrix

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

the class AverageDVModels method averageBinaryMatrices.

public static TwoDimensionalMap<String, String, SimpleMatrix> averageBinaryMatrices(List<TwoDimensionalMap<String, String, SimpleMatrix>> maps) {
    TwoDimensionalMap<String, String, SimpleMatrix> averages = TwoDimensionalMap.treeMap();
    for (Pair<String, String> binary : getBinaryMatrixNames(maps)) {
        int count = 0;
        SimpleMatrix matrix = null;
        for (TwoDimensionalMap<String, String, SimpleMatrix> map : maps) {
            if (!map.contains(binary.first(), binary.second())) {
                continue;
            }
            SimpleMatrix original = map.get(binary.first(), binary.second());
            ++count;
            if (matrix == null) {
                matrix = original;
            } else {
                matrix = matrix.plus(original);
            }
        }
        matrix = matrix.divide(count);
        averages.put(binary.first(), binary.second(), matrix);
    }
    return averages;
}
Also used : SimpleMatrix(org.ejml.simple.SimpleMatrix)

Example 45 with SimpleMatrix

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

the class DVModel method addRandomBinaryMatrix.

public void addRandomBinaryMatrix(String leftBasic, String rightBasic) {
    if (binaryTransform.get(leftBasic, rightBasic) != null) {
        return;
    }
    ++numBinaryMatrices;
    // scoring matrix
    SimpleMatrix score = SimpleMatrix.random(1, numCols, -1.0 / Math.sqrt((double) numCols), 1.0 / Math.sqrt((double) numCols), rand);
    binaryScore.put(leftBasic, rightBasic, score.scale(op.trainOptions.scalingForInit));
    SimpleMatrix binary;
    if (op.trainOptions.useContextWords) {
        binary = new SimpleMatrix(numRows, numCols * 4 + 1);
        // leave room for bias term
        binary.insertIntoThis(0, numCols * 2 + 1, randomContextMatrix());
    } else {
        binary = new SimpleMatrix(numRows, numCols * 2 + 1);
    }
    SimpleMatrix left = randomTransformMatrix();
    SimpleMatrix right = randomTransformMatrix();
    binary.insertIntoThis(0, 0, left);
    binary.insertIntoThis(0, numCols, right);
    binaryTransform.put(leftBasic, rightBasic, binary.scale(op.trainOptions.scalingForInit));
}
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