示例#1
0
        private SimpleMatrix ConcatenateContextWords(SimpleMatrix childVec, IntPair span, IList <string> words)
        {
            // TODO: factor out getting the words
            SimpleMatrix left  = (span.GetSource() < 0) ? dvModel.GetStartWordVector() : dvModel.GetWordVector(words[span.GetSource()]);
            SimpleMatrix right = (span.GetTarget() >= words.Count) ? dvModel.GetEndWordVector() : dvModel.GetWordVector(words[span.GetTarget()]);

            return(NeuralUtils.Concatenate(childVec, left, right));
        }
        public virtual SimpleMatrix GetMentionEmbeddings(Mention m, SimpleMatrix docEmbedding)
        {
            IEnumerator <SemanticGraphEdge> depIterator = m.enhancedDependency.IncomingEdgeIterator(m.headIndexedWord);
            SemanticGraphEdge depRelation = depIterator.MoveNext() ? depIterator.Current : null;

            return(NeuralUtils.Concatenate(GetAverageEmbedding(m.sentenceWords, m.startIndex, m.endIndex), GetAverageEmbedding(m.sentenceWords, m.startIndex - 5, m.startIndex), GetAverageEmbedding(m.sentenceWords, m.endIndex, m.endIndex + 5), GetAverageEmbedding
                                               (m.sentenceWords.SubList(0, m.sentenceWords.Count - 1)), docEmbedding, GetWordEmbedding(m.sentenceWords, m.headIndex), GetWordEmbedding(m.sentenceWords, m.startIndex), GetWordEmbedding(m.sentenceWords, m.endIndex - 1), GetWordEmbedding(m.sentenceWords
                                                                                                                                                                                                                                                                                           , m.startIndex - 1), GetWordEmbedding(m.sentenceWords, m.endIndex), GetWordEmbedding(m.sentenceWords, m.startIndex - 2), GetWordEmbedding(m.sentenceWords, m.endIndex + 1), GetWordEmbedding(depRelation == null ? null : depRelation.GetSource(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ).Word())));
        }
        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);
        }
        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));
        }
        public virtual SimpleMatrix GetPairFeatures(Pair <int, int> pair, Document document, IDictionary <int, IList <Mention> > mentionsByHeadIndex)
        {
            Mention      m1          = document.predictedMentionsByID[pair.first];
            Mention      m2          = document.predictedMentionsByID[pair.second];
            IList <int>  featureVals = PairwiseFeatures(document, m1, m2, dictionaries, conll);
            SimpleMatrix features    = new SimpleMatrix(featureVals.Count, 1);

            for (int i = 0; i < featureVals.Count; i++)
            {
                features.Set(i, featureVals[i]);
            }
            features = NeuralUtils.Concatenate(features, EncodeDistance(m2.sentNum - m1.sentNum), EncodeDistance(m2.mentionNum - m1.mentionNum - 1), new SimpleMatrix(new double[][] { new double[] { m1.sentNum == m2.sentNum && m1.endIndex > m2.startIndex
                                 ? 1 : 0 } }), GetMentionFeatures(m1, document, mentionsByHeadIndex), GetMentionFeatures(m2, document, mentionsByHeadIndex), EncodeGenre(document));
            return(features);
        }
示例#6
0
 public virtual double GetAnaphoricityScore(SimpleMatrix mentionEmbedding, SimpleMatrix anaphoricityFeatures)
 {
     return(Score(NeuralUtils.Concatenate(mentionEmbedding, anaphoricityFeatures), anaphoricityModel));
 }
        /// <summary>
        /// This is the method to call for assigning labels and node vectors
        /// to the Tree.
        /// </summary>
        /// <remarks>
        /// 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.
        /// </remarks>
        public virtual void ForwardPropagateTree(Tree tree)
        {
            SimpleMatrix nodeVector;
            // initialized below or Exception thrown // = null;
            SimpleMatrix classification;

            // initialized below or Exception thrown // = null;
            if (tree.IsLeaf())
            {
                // We do nothing for the leaves.  The preterminals will
                // calculate the classification for this word/tag.  In fact, the
                // recursion should not have gotten here (unless there are
                // degenerate trees of just one leaf)
                log.Info("SentimentCostAndGradient: warning: We reached leaves in forwardPropagate: " + tree);
                throw new AssertionError("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)
                    {
                        log.Info("SentimentCostAndGradient: warning: Non-preterminal nodes of size 1: " + tree);
                        throw new AssertionError("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
                        {
                            log.Info("SentimentCostAndGradient: warning: Tree not correctly binarized: " + tree);
                            throw new AssertionError("Tree not correctly binarized");
                        }
                    }
                }
            }
            SimpleMatrix predictions = NeuralUtils.Softmax(classification.Mult(NeuralUtils.ConcatenateWithBias(nodeVector)));
            int          index       = GetPredictedClass(predictions);

            if (!(tree.Label() is 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(typeof(RNNCoreAnnotations.Predictions), predictions);
            label.Set(typeof(RNNCoreAnnotations.PredictedClass), index);
            label.Set(typeof(RNNCoreAnnotations.NodeVector), nodeVector);
        }
 private SimpleMatrix GetMentionFeatures(Mention m, Document document, IDictionary <int, IList <Mention> > mentionsByHeadIndex)
 {
     return(NeuralUtils.Concatenate(NeuralUtils.OneHot((int)(m.mentionType), 4), EncodeDistance(m.endIndex - m.startIndex - 1), new SimpleMatrix(new double[][] { new double[] { m.mentionNum / (double)document.predictedMentionsByID.Count }, new double
                                                                                                                                                                  [] { mentionsByHeadIndex[m.headIndex].Stream().AnyMatch(null) ? 1 : 0 } })));
 }
 public virtual SimpleMatrix GetAnaphoricityFeatures(Mention m, Document document, IDictionary <int, IList <Mention> > mentionsByHeadIndex)
 {
     return(NeuralUtils.Concatenate(GetMentionFeatures(m, document, mentionsByHeadIndex), EncodeGenre(document)));
 }