コード例 #1
0
 /// <param name="op">the parameters of the parser</param>
 public DVModel(Options op, IIndex <string> stateIndex, UnaryGrammar unaryGrammar, BinaryGrammar binaryGrammar)
 {
     this.op = op;
     rand    = new Random(op.trainOptions.randomSeed);
     ReadWordVectors();
     // Binary matrices will be n*2n+1, unary matrices will be n*n+1
     numRows = op.lexOptions.numHid;
     numCols = op.lexOptions.numHid;
     // Build one matrix for each basic category.
     // We assume that each state that has the same basic
     // category is using the same transformation matrix.
     // Use TreeMap for because we want values to be
     // sorted by key later on when building theta vectors
     binaryTransform     = TwoDimensionalMap.TreeMap();
     unaryTransform      = Generics.NewTreeMap();
     binaryScore         = TwoDimensionalMap.TreeMap();
     unaryScore          = Generics.NewTreeMap();
     numBinaryMatrices   = 0;
     numUnaryMatrices    = 0;
     binaryTransformSize = numRows * (numCols * 2 + 1);
     unaryTransformSize  = numRows * (numCols + 1);
     binaryScoreSize     = numCols;
     unaryScoreSize      = numCols;
     if (op.trainOptions.useContextWords)
     {
         binaryTransformSize += numRows * numCols * 2;
         unaryTransformSize  += numRows * numCols * 2;
     }
     identity = SimpleMatrix.Identity(numRows);
     foreach (UnaryRule unaryRule in unaryGrammar)
     {
         // only make one matrix for each parent state, and only use the
         // basic category for that
         string childState = stateIndex.Get(unaryRule.child);
         string childBasic = BasicCategory(childState);
         AddRandomUnaryMatrix(childBasic);
     }
     foreach (BinaryRule binaryRule in binaryGrammar)
     {
         // only make one matrix for each parent state, and only use the
         // basic category for that
         string leftState  = stateIndex.Get(binaryRule.leftChild);
         string leftBasic  = BasicCategory(leftState);
         string rightState = stateIndex.Get(binaryRule.rightChild);
         string rightBasic = BasicCategory(rightState);
         AddRandomBinaryMatrix(leftBasic, rightBasic);
     }
 }
コード例 #2
0
 private SentimentModel(TwoDimensionalMap <string, string, SimpleMatrix> binaryTransform, TwoDimensionalMap <string, string, SimpleTensor> binaryTensors, TwoDimensionalMap <string, string, SimpleMatrix> binaryClassification, IDictionary <string,
                                                                                                                                                                                                                                              SimpleMatrix> unaryClassification, IDictionary <string, SimpleMatrix> wordVectors, RNNOptions op)
 {
     this.op = op;
     this.binaryTransform      = binaryTransform;
     this.binaryTensors        = binaryTensors;
     this.binaryClassification = binaryClassification;
     this.unaryClassification  = unaryClassification;
     this.wordVectors          = wordVectors;
     this.numClasses           = op.numClasses;
     if (op.numHid <= 0)
     {
         int nh = 0;
         foreach (SimpleMatrix wv in wordVectors.Values)
         {
             nh = wv.GetNumElements();
         }
         this.numHid = nh;
     }
     else
     {
         this.numHid = op.numHid;
     }
     this.numBinaryMatrices = binaryTransform.Size();
     binaryTransformSize    = numHid * (2 * numHid + 1);
     if (op.useTensors)
     {
         binaryTensorSize = numHid * numHid * numHid * 4;
     }
     else
     {
         binaryTensorSize = 0;
     }
     binaryClassificationSize = (op.combineClassification) ? 0 : numClasses * (numHid + 1);
     numUnaryMatrices         = unaryClassification.Count;
     unaryClassificationSize  = numClasses * (numHid + 1);
     rand     = new Random(op.randomSeed);
     identity = SimpleMatrix.Identity(numHid);
 }
コード例 #3
0
 public DVModel(TwoDimensionalMap <string, string, SimpleMatrix> binaryTransform, IDictionary <string, SimpleMatrix> unaryTransform, TwoDimensionalMap <string, string, SimpleMatrix> binaryScore, IDictionary <string, SimpleMatrix> unaryScore, IDictionary
                <string, SimpleMatrix> wordVectors, Options op)
 {
     this.op = op;
     this.binaryTransform   = binaryTransform;
     this.unaryTransform    = unaryTransform;
     this.binaryScore       = binaryScore;
     this.unaryScore        = unaryScore;
     this.wordVectors       = wordVectors;
     this.numBinaryMatrices = binaryTransform.Size();
     this.numUnaryMatrices  = unaryTransform.Count;
     if (numBinaryMatrices > 0)
     {
         this.binaryTransformSize = binaryTransform.GetEnumerator().Current.GetValue().GetNumElements();
         this.binaryScoreSize     = binaryScore.GetEnumerator().Current.GetValue().GetNumElements();
     }
     else
     {
         this.binaryTransformSize = 0;
         this.binaryScoreSize     = 0;
     }
     if (numUnaryMatrices > 0)
     {
         this.unaryTransformSize = unaryTransform.Values.GetEnumerator().Current.GetNumElements();
         this.unaryScoreSize     = unaryScore.Values.GetEnumerator().Current.GetNumElements();
     }
     else
     {
         this.unaryTransformSize = 0;
         this.unaryScoreSize     = 0;
     }
     this.numRows  = op.lexOptions.numHid;
     this.numCols  = op.lexOptions.numHid;
     this.identity = SimpleMatrix.Identity(numRows);
     this.rand     = new Random(op.trainOptions.randomSeed);
 }
コード例 #4
0
        /// <summary>The traditional way of initializing an empty model suitable for training.</summary>
        public SentimentModel(RNNOptions op, IList <Tree> trainingTrees)
        {
            this.op = op;
            rand    = new Random(op.randomSeed);
            if (op.randomWordVectors)
            {
                InitRandomWordVectors(trainingTrees);
            }
            else
            {
                ReadWordVectors();
            }
            if (op.numHid > 0)
            {
                this.numHid = op.numHid;
            }
            else
            {
                int size = 0;
                foreach (SimpleMatrix vector in wordVectors.Values)
                {
                    size = vector.GetNumElements();
                    break;
                }
                this.numHid = size;
            }
            TwoDimensionalSet <string, string> binaryProductions = TwoDimensionalSet.HashSet();

            if (op.simplifiedModel)
            {
                binaryProductions.Add(string.Empty, string.Empty);
            }
            else
            {
                // TODO
                // figure out what binary productions we have in these trees
                // Note: the current sentiment training data does not actually
                // have any constituent labels
                throw new NotSupportedException("Not yet implemented");
            }
            ICollection <string> unaryProductions = Generics.NewHashSet();

            if (op.simplifiedModel)
            {
                unaryProductions.Add(string.Empty);
            }
            else
            {
                // TODO
                // figure out what unary productions we have in these trees (preterminals only, after the collapsing)
                throw new NotSupportedException("Not yet implemented");
            }
            this.numClasses      = op.numClasses;
            identity             = SimpleMatrix.Identity(numHid);
            binaryTransform      = TwoDimensionalMap.TreeMap();
            binaryTensors        = TwoDimensionalMap.TreeMap();
            binaryClassification = TwoDimensionalMap.TreeMap();
            // When making a flat model (no symantic untying) the
            // basicCategory function will return the same basic category for
            // all labels, so all entries will map to the same matrix
            foreach (Pair <string, string> binary in binaryProductions)
            {
                string left  = BasicCategory(binary.first);
                string right = BasicCategory(binary.second);
                if (binaryTransform.Contains(left, right))
                {
                    continue;
                }
                binaryTransform.Put(left, right, RandomTransformMatrix());
                if (op.useTensors)
                {
                    binaryTensors.Put(left, right, RandomBinaryTensor());
                }
                if (!op.combineClassification)
                {
                    binaryClassification.Put(left, right, RandomClassificationMatrix());
                }
            }
            numBinaryMatrices   = binaryTransform.Size();
            binaryTransformSize = numHid * (2 * numHid + 1);
            if (op.useTensors)
            {
                binaryTensorSize = numHid * numHid * numHid * 4;
            }
            else
            {
                binaryTensorSize = 0;
            }
            binaryClassificationSize = (op.combineClassification) ? 0 : numClasses * (numHid + 1);
            unaryClassification      = Generics.NewTreeMap();
            // When making a flat model (no symantic untying) the
            // basicCategory function will return the same basic category for
            // all labels, so all entries will map to the same matrix
            foreach (string unary in unaryProductions)
            {
                unary = BasicCategory(unary);
                if (unaryClassification.Contains(unary))
                {
                    continue;
                }
                unaryClassification[unary] = RandomClassificationMatrix();
            }
            numUnaryMatrices        = unaryClassification.Count;
            unaryClassificationSize = numClasses * (numHid + 1);
        }
コード例 #5
0
 // Maps from basic category to the matrix transformation matrices for
 // binary nodes and unary nodes.
 // The indices are the children categories.  For binaryTransform, for
 // example, we have a matrix for each type of child that appears.
 // score matrices for each node type
 // cache these for easy calculation of "theta" parameter size
 // we just keep this here for convenience
 // the seed we used to use was 19580427
 /// <exception cref="System.IO.IOException"/>
 /// <exception cref="System.TypeLoadException"/>
 private void ReadObject(ObjectInputStream @in)
 {
     @in.DefaultReadObject();
     identity = SimpleMatrix.Identity(numRows);
 }