예제 #1
0
 public NaiveBayesModel(Context[] parameters, string[] predLabels, IndexHashTable <string> map,
                        string[] outcomeNames)
     : base(parameters, map, outcomeNames)
 {
     outcomeTotals  = InitOutcomeTotals(outcomeNames, parameters);
     evalParameters = new NaiveBayesEvalParameters(parameters, outcomeNames.Length, outcomeTotals,
                                                   predLabels.Length);
     ModelType = ModelType.NaiveBayes;
 }
예제 #2
0
        public void testWithoutCollision() {

            var array = new[] {"4", "7", "5"};

            var arrayIndex = new IndexHashTable<string>(array, 1d);

            for (int i = 0; i < array.Length; i++) {
                Assert.AreEqual(i, arrayIndex[array[i]]);
            }

        }
예제 #3
0
        public QNModelWriter(AbstractModel model)
        {
            object[] data = model.DataStructures;
            cParameters  = (Context[])data[0];
            pmap         = (IndexHashTable <string>)data[1];
            outcomeNames = (string[])data[2];

            QNModel qnModel = (QNModel)model;

            parameters = qnModel.Parameters;
        }
예제 #4
0
        public void testWithoutCollision()
        {
            var array = new[] { "4", "7", "5" };

            var arrayIndex = new IndexHashTable <string>(array, 1d);

            for (int i = 0; i < array.Length; i++)
            {
                Assert.AreEqual(i, arrayIndex[array[i]]);
            }
        }
예제 #5
0
        public PerceptronModelWriter(AbstractModel model)
        {
            object[] data = model.DataStructures;
            this.numOutcomes = model.NumOutcomes;
            PARAMS           = (Context[])data[0];
            IndexHashTable <string> pmap = (IndexHashTable <string>)data[1];

            OUTCOME_LABELS = (string[])data[2];

            PRED_LABELS = new string[pmap.size()];
            pmap.toArray(PRED_LABELS);
        }
예제 #6
0
        public void testWitCollision() {

            var array = new[] {"7", "21", "0"};

            var arrayIndex = new IndexHashTable<string>(array, 1d);

            for (int i = 0; i < array.Length; i++) {
                Assert.AreEqual(i, arrayIndex[array[i]]);
            }

            // has the same slot as as ""
            Assert.AreEqual(-1, arrayIndex["4"]);
        }
예제 #7
0
        public GISModelWriter(AbstractModel model)
        {
            object[] data = model.DataStructures;

            PARAMS = (Context[])data[0];
            IndexHashTable <string> pmap = (IndexHashTable <string>)data[1];

            OUTCOME_LABELS      = (string[])data[2];
            CORRECTION_CONSTANT = (int)data[3];
            CORRECTION_PARAM    = (double)data[4];

            PRED_LABELS = new string[pmap.size()];
            pmap.toArray(PRED_LABELS);
        }
예제 #8
0
        public void testWitCollision()
        {
            var array = new[] { "7", "21", "0" };

            var arrayIndex = new IndexHashTable <string>(array, 1d);

            for (int i = 0; i < array.Length; i++)
            {
                Assert.AreEqual(i, arrayIndex[array[i]]);
            }

            // has the same slot as as ""
            Assert.AreEqual(-1, arrayIndex["4"]);
        }
예제 #9
0
파일: QNModel.cs 프로젝트: qooba/SharpNL
 public QNModel(Context[] parameters, IndexHashTable <string> map, string[] outcomeNames)
     : base(parameters, map, outcomeNames)
 {
 }
예제 #10
0
파일: QNModel.cs 프로젝트: knuppe/SharpNL
 public QNModel(Context[] parameters, IndexHashTable<string> map, string[] outcomeNames)
     : base(parameters, map, outcomeNames) {}
        public AbstractModel TrainModel(int trainIterations, ISequenceStream trainStream, int cutoff,
            bool trainUseAverage) {
            iterations = trainIterations;
            useAverage = trainUseAverage;
            sequenceStream = trainStream;

            info.Append("Trained using Perceptron Sequence algorithm.\n\n");

            var di = new OnePassDataIndexer(new SequenceStreamEventStream(trainStream), cutoff, false);

            trainStream.Reset();

            numSequences = 0;

            while (trainStream.Read() != null) {
                numSequences++;
            }

            outcomeList = di.GetOutcomeList();
            predLabels = di.GetPredLabels();

            pMap = new IndexHashTable<string>(predLabels, 0.7d);

            // Incorporation indexed data for training...


            numEvents = di.GetNumEvents();

            outcomeLabels = di.GetOutcomeLabels();
            oMap = new Dictionary<string, int>();
            for (var i = 0; i < outcomeLabels.Length; i++) {
                oMap.Add(outcomeLabels[i], i);
            }

            outcomeList = di.GetOutcomeList();
            numPreds = predLabels.Length;
            numOutcomes = outcomeLabels.Length;

            if (trainUseAverage) {
                updates = new int[numPreds][][];
                for (var i = 0; i < numPreds; i++) {
                    updates[i] = new int[numOutcomes][];
                    for (var j = 0; j < numOutcomes; j++) {
                        updates[i][j] = new int[3];
                    }
                }
            }

            // done.
            Display("done.\n");

            info.Append("Number of Event Tokens: {0}\n" +
                        "    Number of Outcomes: {1}\n" +
                        "  Number of Predicates: {2}\n", numEvents, numOutcomes, numPreds);

            Display("\tNumber of Event Tokens: " + numEvents);
            Display("\t    Number of Outcomes: " + numOutcomes);
            Display("\t  Number of Predicates: " + numPreds);

            param = new MutableContext[numPreds];
            if (trainUseAverage)
                averageParams = new MutableContext[numPreds];

            allOutcomesPattern = new int[numOutcomes];
            for (var i = 0; i < numOutcomes; i++) {
                allOutcomesPattern[i] = i;
            }

            for (var pi = 0; pi < numPreds; pi++) {
                param[pi] = new MutableContext(allOutcomesPattern, new double[numOutcomes]);
                if (trainUseAverage)
                    averageParams[pi] = new MutableContext(allOutcomesPattern, new double[numOutcomes]);

                for (var aoi = 0; aoi < numOutcomes; aoi++) {
                    param[pi].SetParameter(aoi, 0.0d);
                    if (trainUseAverage)
                        averageParams[pi].SetParameter(aoi, 0.0d);
                }
            }

            Display("Computing model parameters...");
            FindParameters();
            Display("...done.");

            /*************** Create and return the model ******************/

            // ReSharper disable CoVariantArrayConversion
            if (trainUseAverage)
                return new PerceptronModel(averageParams, predLabels, outcomeLabels) {
                    info = info
                };

            return new PerceptronModel(param, predLabels, outcomeLabels) {               
                info = info
            };
            // ReSharper restore CoVariantArrayConversion
        }
        public AbstractModel TrainModel(int trainIterations, ISequenceStream trainStream, int cutoff,
                                        bool trainUseAverage)
        {
            iterations     = trainIterations;
            useAverage     = trainUseAverage;
            sequenceStream = trainStream;

            info.Append("Trained using Perceptron Sequence algorithm.\n\n");

            var di = new OnePassDataIndexer(new SequenceStreamEventStream(trainStream), cutoff, false);

            trainStream.Reset();

            numSequences = 0;

            while (trainStream.Read() != null)
            {
                numSequences++;
            }

            outcomeList = di.GetOutcomeList();
            predLabels  = di.GetPredLabels();

            pMap = new IndexHashTable <string>(predLabels, 0.7d);

            // Incorporation indexed data for training...


            numEvents = di.GetNumEvents();

            outcomeLabels = di.GetOutcomeLabels();
            oMap          = new Dictionary <string, int>();
            for (var i = 0; i < outcomeLabels.Length; i++)
            {
                oMap.Add(outcomeLabels[i], i);
            }

            outcomeList = di.GetOutcomeList();
            numPreds    = predLabels.Length;
            numOutcomes = outcomeLabels.Length;

            if (trainUseAverage)
            {
                updates = new int[numPreds][][];
                for (var i = 0; i < numPreds; i++)
                {
                    updates[i] = new int[numOutcomes][];
                    for (var j = 0; j < numOutcomes; j++)
                    {
                        updates[i][j] = new int[3];
                    }
                }
            }

            // done.
            Display("done.\n");

            info.Append("Number of Event Tokens: {0}\n" +
                        "    Number of Outcomes: {1}\n" +
                        "  Number of Predicates: {2}\n", numEvents, numOutcomes, numPreds);

            Display("\tNumber of Event Tokens: " + numEvents);
            Display("\t    Number of Outcomes: " + numOutcomes);
            Display("\t  Number of Predicates: " + numPreds);

            param = new MutableContext[numPreds];
            if (trainUseAverage)
            {
                averageParams = new MutableContext[numPreds];
            }

            allOutcomesPattern = new int[numOutcomes];
            for (var i = 0; i < numOutcomes; i++)
            {
                allOutcomesPattern[i] = i;
            }

            for (var pi = 0; pi < numPreds; pi++)
            {
                param[pi] = new MutableContext(allOutcomesPattern, new double[numOutcomes]);
                if (trainUseAverage)
                {
                    averageParams[pi] = new MutableContext(allOutcomesPattern, new double[numOutcomes]);
                }

                for (var aoi = 0; aoi < numOutcomes; aoi++)
                {
                    param[pi].SetParameter(aoi, 0.0d);
                    if (trainUseAverage)
                    {
                        averageParams[pi].SetParameter(aoi, 0.0d);
                    }
                }
            }

            Display("Computing model parameters...");
            FindParameters();
            Display("...done.");

            /*************** Create and return the model ******************/

            // ReSharper disable CoVariantArrayConversion
            if (trainUseAverage)
            {
                return new PerceptronModel(averageParams, predLabels, outcomeLabels)
                       {
                           info = info
                       }
            }
            ;

            return(new PerceptronModel(param, predLabels, outcomeLabels)
            {
                info = info
            });
            // ReSharper restore CoVariantArrayConversion
        }
예제 #13
0
 public PerceptronModel(Context[] parameters, IndexHashTable<string> map, string[] outcomeNames)
     : base(parameters, map, outcomeNames) {
     ModelType = ModelType.Perceptron;
 }
예제 #14
0
 protected AbstractModel(Context[] parameters, string[] predLabels, IndexHashTable<string> map, string[] outcomeNames) {
     this.map = map;
     this.outcomeNames = outcomeNames;
     evalParameters = new EvalParameters(parameters, outcomeNames.Length);
 }
예제 #15
0
        private AbstractModel(string[] predLabels, string[] outcomeNames) {
            map = new IndexHashTable<string>(predLabels, 0.7d);

            this.outcomeNames = outcomeNames;
        }
        public virtual AbstractModel trainModel(int iterations, SequenceStream sequenceStream, int cutoff,
                                                bool useAverage)
        {
            this.iterations     = iterations;
            this.sequenceStream = sequenceStream;
            DataIndexer di = new OnePassDataIndexer(new SequenceStreamEventStream(sequenceStream), cutoff, false);

            numSequences = 0;
            foreach (Sequence <Event> s in sequenceStream)
            {
                numSequences++;
            }
            outcomeList = di.OutcomeList;
            predLabels  = di.PredLabels;
            pmap        = new IndexHashTable <string>(predLabels, 0.7d);

            display("Incorporating indexed data for training...  \n");
            this.useAverage = useAverage;
            numEvents       = di.NumEvents;

            this.iterations = iterations;
            outcomeLabels   = di.OutcomeLabels;
            omap            = new Dictionary <string, int?>();
            for (int oli = 0; oli < outcomeLabels.Length; oli++)
            {
                omap[outcomeLabels[oli]] = oli;
            }
            outcomeList = di.OutcomeList;

            numPreds    = predLabels.Length;
            numOutcomes = outcomeLabels.Length;
            if (useAverage)
            {
                updates = RectangularArrays.ReturnRectangularIntArray(numPreds, numOutcomes, 3);
            }

            display("done.\n");

            display("\tNumber of Event Tokens: " + numEvents + "\n");
            display("\t    Number of Outcomes: " + numOutcomes + "\n");
            display("\t  Number of Predicates: " + numPreds + "\n");


            parameters = new MutableContext[numPreds];
            if (useAverage)
            {
                averageParams = new MutableContext[numPreds];
            }

            allOutcomesPattern = new int[numOutcomes];
            for (int oi = 0; oi < numOutcomes; oi++)
            {
                allOutcomesPattern[oi] = oi;
            }

            for (int pi = 0; pi < numPreds; pi++)
            {
                parameters[pi] = new MutableContext(allOutcomesPattern, new double[numOutcomes]);
                if (useAverage)
                {
                    averageParams[pi] = new MutableContext(allOutcomesPattern, new double[numOutcomes]);
                }
                for (int aoi = 0; aoi < numOutcomes; aoi++)
                {
                    parameters[pi].setParameter(aoi, 0.0);
                    if (useAverage)
                    {
                        averageParams[pi].setParameter(aoi, 0.0);
                    }
                }
            }
            modelDistribution = new double[numOutcomes];

            display("Computing model parameters...\n");
            findParameters(iterations);
            display("...done.\n");

            /// <summary>
            ///************* Create and return the model ***************** </summary>
            string[] updatedPredLabels = predLabels;

            /*
             *  String[] updatedPredLabels = new String[pmap.size()];
             *  for (String pred : pmap.keySet()) {
             *    updatedPredLabels[pmap.get(pred)]=pred;
             *  }
             */
            if (useAverage)
            {
                return(new PerceptronModel(averageParams, updatedPredLabels, outcomeLabels));
            }
            else
            {
                return(new PerceptronModel(parameters, updatedPredLabels, outcomeLabels));
            }
        }
예제 #17
0
 public PerceptronModel(Context[] parameters, IndexHashTable <string> map, string[] outcomeNames)
     : base(parameters, map, outcomeNames)
 {
     ModelType = ModelType.Perceptron;
 }
예제 #18
0
 public PerceptronModel(Context[] parameters, string[] predLabels, IndexHashTable <string> pmap,
                        string[] outcomeNames) : base(parameters, predLabels, pmap, outcomeNames)
 {
     modelType = ModelTypeEnum.Perceptron;
 }