Search in sources :

Example 6 with SimpleMatrix

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

the class SentimentCostAndGradient method scaleAndRegularize.

private static double scaleAndRegularize(TwoDimensionalMap<String, String, SimpleMatrix> derivatives, TwoDimensionalMap<String, String, SimpleMatrix> currentMatrices, double scale, double regCost, boolean dropBiasColumn) {
    // the regularization cost
    double cost = 0.0;
    for (TwoDimensionalMap.Entry<String, String, SimpleMatrix> entry : currentMatrices) {
        SimpleMatrix D = derivatives.get(entry.getFirstKey(), entry.getSecondKey());
        SimpleMatrix regMatrix = entry.getValue();
        if (dropBiasColumn) {
            regMatrix = new SimpleMatrix(regMatrix);
            regMatrix.insertIntoThis(0, regMatrix.numCols() - 1, new SimpleMatrix(regMatrix.numRows(), 1));
        }
        D = D.scale(scale).plus(regMatrix.scale(regCost));
        derivatives.put(entry.getFirstKey(), entry.getSecondKey(), D);
        cost += regMatrix.elementMult(regMatrix).elementSum() * regCost / 2.0;
    }
    return cost;
}
Also used : SimpleMatrix(org.ejml.simple.SimpleMatrix) TwoDimensionalMap(edu.stanford.nlp.util.TwoDimensionalMap)

Example 7 with SimpleMatrix

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

the class SentimentCostAndGradient method computeTensorDeltaDown.

private static SimpleMatrix computeTensorDeltaDown(SimpleMatrix deltaFull, SimpleMatrix leftVector, SimpleMatrix rightVector, SimpleMatrix W, SimpleTensor Wt) {
    SimpleMatrix WTDelta = W.transpose().mult(deltaFull);
    SimpleMatrix WTDeltaNoBias = WTDelta.extractMatrix(0, deltaFull.numRows() * 2, 0, 1);
    int size = deltaFull.getNumElements();
    SimpleMatrix deltaTensor = new SimpleMatrix(size * 2, 1);
    SimpleMatrix fullVector = NeuralUtils.concatenate(leftVector, rightVector);
    for (int slice = 0; slice < size; ++slice) {
        SimpleMatrix scaledFullVector = fullVector.scale(deltaFull.get(slice));
        deltaTensor = deltaTensor.plus(Wt.getSlice(slice).plus(Wt.getSlice(slice).transpose()).mult(scaledFullVector));
    }
    return deltaTensor.plus(WTDeltaNoBias);
}
Also used : SimpleMatrix(org.ejml.simple.SimpleMatrix)

Example 8 with SimpleMatrix

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

the class SentimentModel method randomTransformMatrix.

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);
}
Also used : SimpleMatrix(org.ejml.simple.SimpleMatrix)

Example 9 with SimpleMatrix

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

the class SentimentModel method randomClassificationMatrix.

/**
   * Returns matrices of the right size for either binary or unary (terminal) classification
   */
SimpleMatrix randomClassificationMatrix() {
    SimpleMatrix score = new SimpleMatrix(numClasses, numHid + 1);
    double range = 1.0 / (Math.sqrt((double) numHid));
    score.insertIntoThis(0, 0, SimpleMatrix.random(numClasses, numHid, -range, range, rand));
    // bias column goes from 0 to 1 initially
    score.insertIntoThis(0, numHid, SimpleMatrix.random(numClasses, 1, 0.0, 1.0, rand));
    return score.scale(op.trainOptions.scalingForInit);
}
Also used : SimpleMatrix(org.ejml.simple.SimpleMatrix)

Example 10 with SimpleMatrix

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

the class SentimentModel method readWordVectors.

void readWordVectors() {
    Embedding embedding = new Embedding(op.wordVectors, op.numHid);
    this.wordVectors = Generics.newTreeMap();
    //    for (String word : rawWordVectors.keySet()) {
    for (String word : embedding.keySet()) {
        // TODO: factor out unknown word vector code from DVParser
        wordVectors.put(word, embedding.get(word));
    }
    String unkWord = op.unkWord;
    SimpleMatrix unknownWordVector = wordVectors.get(unkWord);
    wordVectors.put(UNKNOWN_WORD, unknownWordVector);
    if (unknownWordVector == null) {
        throw new RuntimeException("Unknown word vector not specified in the word vector file");
    }
}
Also used : SimpleMatrix(org.ejml.simple.SimpleMatrix) Embedding(edu.stanford.nlp.neural.Embedding)

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