Search in sources :

Example 61 with SimpleMatrix

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

the class SentimentCostAndGradient method scaleAndRegularize.

private static double scaleAndRegularize(Map<String, SimpleMatrix> derivatives, Map<String, SimpleMatrix> currentMatrices, double scale, double regCost, boolean activeMatricesOnly, boolean dropBiasColumn) {
    // the regularization cost
    double cost = 0.0;
    for (Map.Entry<String, SimpleMatrix> entry : currentMatrices.entrySet()) {
        SimpleMatrix D = derivatives.get(entry.getKey());
        if (activeMatricesOnly && D == null) {
            // Fill in an emptpy matrix so the length of theta can match.
            // TODO: might want to allow for sparse parameter vectors
            derivatives.put(entry.getKey(), new SimpleMatrix(entry.getValue().numRows(), entry.getValue().numCols()));
            continue;
        }
        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.getKey(), D);
        cost += regMatrix.elementMult(regMatrix).elementSum() * regCost / 2.0;
    }
    return cost;
}
Also used : SimpleMatrix(org.ejml.simple.SimpleMatrix) Map(java.util.Map) TwoDimensionalMap(edu.stanford.nlp.util.TwoDimensionalMap)

Example 62 with SimpleMatrix

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

the class SentimentCostAndGradient method backpropDerivativesAndError.

private void backpropDerivativesAndError(Tree tree, TwoDimensionalMap<String, String, SimpleMatrix> binaryTD, TwoDimensionalMap<String, String, SimpleMatrix> binaryCD, TwoDimensionalMap<String, String, SimpleTensor> binaryTensorTD, Map<String, SimpleMatrix> unaryCD, Map<String, SimpleMatrix> wordVectorD) {
    SimpleMatrix delta = new SimpleMatrix(model.op.numHid, 1);
    backpropDerivativesAndError(tree, binaryTD, binaryCD, binaryTensorTD, unaryCD, wordVectorD, delta);
}
Also used : SimpleMatrix(org.ejml.simple.SimpleMatrix)

Example 63 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 64 with SimpleMatrix

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

the class SentimentCostAndGradient method backpropDerivativesAndError.

private void backpropDerivativesAndError(Tree tree, TwoDimensionalMap<String, String, SimpleMatrix> binaryTD, TwoDimensionalMap<String, String, SimpleMatrix> binaryCD, TwoDimensionalMap<String, String, SimpleTensor> binaryTensorTD, Map<String, SimpleMatrix> unaryCD, Map<String, SimpleMatrix> wordVectorD, SimpleMatrix deltaUp) {
    if (tree.isLeaf()) {
        return;
    }
    SimpleMatrix currentVector = RNNCoreAnnotations.getNodeVector(tree);
    String category = tree.label().value();
    category = model.basicCategory(category);
    // Build a vector that looks like 0,0,1,0,0 with an indicator for the correct class
    SimpleMatrix goldLabel = new SimpleMatrix(model.numClasses, 1);
    int goldClass = RNNCoreAnnotations.getGoldClass(tree);
    if (goldClass >= 0) {
        goldLabel.set(goldClass, 1.0);
    }
    double nodeWeight = model.op.trainOptions.getClassWeight(goldClass);
    SimpleMatrix predictions = RNNCoreAnnotations.getPredictions(tree);
    // If this is an unlabeled class, set deltaClass to 0.  We could
    // make this more efficient by eliminating various of the below
    // calculations, but this would be the easiest way to handle the
    // unlabeled class
    SimpleMatrix deltaClass = goldClass >= 0 ? predictions.minus(goldLabel).scale(nodeWeight) : new SimpleMatrix(predictions.numRows(), predictions.numCols());
    SimpleMatrix localCD = deltaClass.mult(NeuralUtils.concatenateWithBias(currentVector).transpose());
    double error = -(NeuralUtils.elementwiseApplyLog(predictions).elementMult(goldLabel).elementSum());
    error = error * nodeWeight;
    RNNCoreAnnotations.setPredictionError(tree, error);
    if (tree.isPreTerminal()) {
        // below us is a word vector
        unaryCD.put(category, unaryCD.get(category).plus(localCD));
        String word = tree.children()[0].label().value();
        word = model.getVocabWord(word);
        // SimpleMatrix currentVectorDerivative = NeuralUtils.elementwiseApplyTanhDerivative(currentVector);
        // SimpleMatrix deltaFromClass = model.getUnaryClassification(category).transpose().mult(deltaClass);
        // SimpleMatrix deltaFull = deltaFromClass.extractMatrix(0, model.op.numHid, 0, 1).plus(deltaUp);
        // SimpleMatrix wordDerivative = deltaFull.elementMult(currentVectorDerivative);
        // wordVectorD.put(word, wordVectorD.get(word).plus(wordDerivative));
        SimpleMatrix currentVectorDerivative = NeuralUtils.elementwiseApplyTanhDerivative(currentVector);
        SimpleMatrix deltaFromClass = model.getUnaryClassification(category).transpose().mult(deltaClass);
        deltaFromClass = deltaFromClass.extractMatrix(0, model.op.numHid, 0, 1).elementMult(currentVectorDerivative);
        SimpleMatrix deltaFull = deltaFromClass.plus(deltaUp);
        SimpleMatrix oldWordVectorD = wordVectorD.get(word);
        if (oldWordVectorD == null) {
            wordVectorD.put(word, deltaFull);
        } else {
            wordVectorD.put(word, oldWordVectorD.plus(deltaFull));
        }
    } else {
        // Otherwise, this must be a binary node
        String leftCategory = model.basicCategory(tree.children()[0].label().value());
        String rightCategory = model.basicCategory(tree.children()[1].label().value());
        if (model.op.combineClassification) {
            unaryCD.put("", unaryCD.get("").plus(localCD));
        } else {
            binaryCD.put(leftCategory, rightCategory, binaryCD.get(leftCategory, rightCategory).plus(localCD));
        }
        SimpleMatrix currentVectorDerivative = NeuralUtils.elementwiseApplyTanhDerivative(currentVector);
        SimpleMatrix deltaFromClass = model.getBinaryClassification(leftCategory, rightCategory).transpose().mult(deltaClass);
        deltaFromClass = deltaFromClass.extractMatrix(0, model.op.numHid, 0, 1).elementMult(currentVectorDerivative);
        SimpleMatrix deltaFull = deltaFromClass.plus(deltaUp);
        SimpleMatrix leftVector = RNNCoreAnnotations.getNodeVector(tree.children()[0]);
        SimpleMatrix rightVector = RNNCoreAnnotations.getNodeVector(tree.children()[1]);
        SimpleMatrix childrenVector = NeuralUtils.concatenateWithBias(leftVector, rightVector);
        SimpleMatrix W_df = deltaFull.mult(childrenVector.transpose());
        binaryTD.put(leftCategory, rightCategory, binaryTD.get(leftCategory, rightCategory).plus(W_df));
        SimpleMatrix deltaDown;
        if (model.op.useTensors) {
            SimpleTensor Wt_df = getTensorGradient(deltaFull, leftVector, rightVector);
            binaryTensorTD.put(leftCategory, rightCategory, binaryTensorTD.get(leftCategory, rightCategory).plus(Wt_df));
            deltaDown = computeTensorDeltaDown(deltaFull, leftVector, rightVector, model.getBinaryTransform(leftCategory, rightCategory), model.getBinaryTensor(leftCategory, rightCategory));
        } else {
            deltaDown = model.getBinaryTransform(leftCategory, rightCategory).transpose().mult(deltaFull);
        }
        SimpleMatrix leftDerivative = NeuralUtils.elementwiseApplyTanhDerivative(leftVector);
        SimpleMatrix rightDerivative = NeuralUtils.elementwiseApplyTanhDerivative(rightVector);
        SimpleMatrix leftDeltaDown = deltaDown.extractMatrix(0, deltaFull.numRows(), 0, 1);
        SimpleMatrix rightDeltaDown = deltaDown.extractMatrix(deltaFull.numRows(), deltaFull.numRows() * 2, 0, 1);
        backpropDerivativesAndError(tree.children()[0], binaryTD, binaryCD, binaryTensorTD, unaryCD, wordVectorD, leftDerivative.elementMult(leftDeltaDown));
        backpropDerivativesAndError(tree.children()[1], binaryTD, binaryCD, binaryTensorTD, unaryCD, wordVectorD, rightDerivative.elementMult(rightDeltaDown));
    }
}
Also used : SimpleMatrix(org.ejml.simple.SimpleMatrix) SimpleTensor(edu.stanford.nlp.neural.SimpleTensor)

Example 65 with SimpleMatrix

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

the class SentimentCostAndGradient method forwardPropagateTree.

/**
 * This is the method to call for assigning labels and node vectors
 * to the Tree.  After calling this, each of the non-leaf nodes will
 * have the node vector and the predictions of their classes
 * assigned to that subtree's node.  The annotations filled in are
 * the RNNCoreAnnotations.NodeVector, Predictions, and
 * PredictedClass.  In general, PredictedClass will be the most
 * useful annotation except when training.
 */
public void forwardPropagateTree(Tree tree) {
    // initialized below or Exception thrown // = null;
    SimpleMatrix nodeVector;
    // initialized below or Exception thrown // = null;
    SimpleMatrix classification;
    if (tree.isLeaf()) {
        // degenerate trees of just one leaf)
        throw new ForwardPropagationException("We should not have reached leaves in forwardPropagate");
    } else if (tree.isPreTerminal()) {
        classification = model.getUnaryClassification(tree.label().value());
        String word = tree.children()[0].label().value();
        SimpleMatrix wordVector = model.getWordVector(word);
        nodeVector = NeuralUtils.elementwiseApplyTanh(wordVector);
    } else if (tree.children().length == 1) {
        throw new ForwardPropagationException("Non-preterminal nodes of size 1 should have already been collapsed");
    } else if (tree.children().length == 2) {
        forwardPropagateTree(tree.children()[0]);
        forwardPropagateTree(tree.children()[1]);
        String leftCategory = tree.children()[0].label().value();
        String rightCategory = tree.children()[1].label().value();
        SimpleMatrix W = model.getBinaryTransform(leftCategory, rightCategory);
        classification = model.getBinaryClassification(leftCategory, rightCategory);
        SimpleMatrix leftVector = RNNCoreAnnotations.getNodeVector(tree.children()[0]);
        SimpleMatrix rightVector = RNNCoreAnnotations.getNodeVector(tree.children()[1]);
        SimpleMatrix childrenVector = NeuralUtils.concatenateWithBias(leftVector, rightVector);
        if (model.op.useTensors) {
            SimpleTensor tensor = model.getBinaryTensor(leftCategory, rightCategory);
            SimpleMatrix tensorIn = NeuralUtils.concatenate(leftVector, rightVector);
            SimpleMatrix tensorOut = tensor.bilinearProducts(tensorIn);
            nodeVector = NeuralUtils.elementwiseApplyTanh(W.mult(childrenVector).plus(tensorOut));
        } else {
            nodeVector = NeuralUtils.elementwiseApplyTanh(W.mult(childrenVector));
        }
    } else {
        StringBuilder error = new StringBuilder();
        error.append("SentimentCostAndGradient: Tree not correctly binarized:\n   ");
        error.append(tree);
        error.append("\nToo many top level constituents present: ");
        error.append("(" + tree.value());
        for (Tree child : tree.children()) {
            error.append(" (" + child.value() + " ...)");
        }
        error.append(")");
        throw new ForwardPropagationException(error.toString());
    }
    SimpleMatrix predictions = NeuralUtils.softmax(classification.mult(NeuralUtils.concatenateWithBias(nodeVector)));
    int index = getPredictedClass(predictions);
    if (!(tree.label() instanceof CoreLabel)) {
        log.info("SentimentCostAndGradient: warning: No CoreLabels in nodes: " + tree);
        throw new AssertionError("Expected CoreLabels in the nodes");
    }
    CoreLabel label = (CoreLabel) tree.label();
    label.set(RNNCoreAnnotations.Predictions.class, predictions);
    label.set(RNNCoreAnnotations.PredictedClass.class, index);
    label.set(RNNCoreAnnotations.NodeVector.class, nodeVector);
}
Also used : SimpleMatrix(org.ejml.simple.SimpleMatrix) SimpleTensor(edu.stanford.nlp.neural.SimpleTensor) CoreLabel(edu.stanford.nlp.ling.CoreLabel) RNNCoreAnnotations(edu.stanford.nlp.neural.rnn.RNNCoreAnnotations) Tree(edu.stanford.nlp.trees.Tree)

Aggregations

SimpleMatrix (org.ejml.simple.SimpleMatrix)156 Test (org.junit.Test)30 Layer (org.gitia.froog.layer.Layer)14 Test (org.junit.jupiter.api.Test)13 Map (java.util.Map)12 ArrayList (java.util.ArrayList)11 TransferFunction (org.gitia.froog.transferfunction.TransferFunction)10 Tree (edu.stanford.nlp.trees.Tree)9 TwoDimensionalMap (edu.stanford.nlp.util.TwoDimensionalMap)9 List (java.util.List)8 LexicalizedParser (edu.stanford.nlp.parser.lexparser.LexicalizedParser)7 Pair (edu.stanford.nlp.util.Pair)6 EmbeddingExtractor (edu.stanford.nlp.coref.neural.EmbeddingExtractor)5 Embedding (edu.stanford.nlp.neural.Embedding)5 DeepTree (edu.stanford.nlp.trees.DeepTree)5 CoreLabel (edu.stanford.nlp.ling.CoreLabel)4 SimpleTensor (edu.stanford.nlp.neural.SimpleTensor)4 DVModel (edu.stanford.nlp.parser.dvparser.DVModel)4 Logsig (org.gitia.froog.transferfunction.Logsig)4 Ignore (org.junit.Ignore)4