示例#1
0
        //Projects corpus
        public SymbolCorpus project(SymbolCorpus control)
        {
            //Create same dimension output Corpus
            SymbolCorpus outputCorpus = new SymbolCorpus();

            outputCorpus.dialogues = new SymbolDialogue[control.dialogues.Length];

            //Process
            SymbolDialogue controlDialogue;
            SymbolSentence controlSentence;

            for (int i = 0; i < control.dialogues.Length; i++)
            {
                controlDialogue = control.dialogues[i];

                //Create same dimension output dialogue
                outputCorpus.dialogues[i]           = new SymbolDialogue();
                outputCorpus.dialogues[i].sentences = new SymbolSentence[control.dialogues[i].sentences.Length];

                for (int j = 0; j < controlDialogue.sentences.Length; j++)
                {
                    controlSentence = controlDialogue.sentences[j];

                    outputCorpus.dialogues[i].sentences[j] = new SymbolSentence();

                    //project corpus, reset state per sentence
                    state = 0;
                    outputCorpus.dialogues[i].sentences[j].symbols = project(controlSentence.symbols);
                }
            }

            return(outputCorpus);
        }
示例#2
0
        public void train(SymbolCorpus trainSet, int cycles)
        {
            Random rdm = new Random();

            AutoStatePredictor predictor = new AutoStatePredictor(symbolSize, stateSize);

            //Make copy of current
            copyInto(this, predictor);

            if (predictor.trainingDepth == 0)
            {
                randomOverride(predictor, rdm);

                //Read and predict analysis
                predictor.testPredict(trainSet);

                //TODO Generate some more randoms and choose best - might not be needed since leap Intensity is so high at the beginning
            }

            //Initialize training variables
            AutoStatePredictor testTable = new AutoStatePredictor(predictor.symbolSize, predictor.stateSize);
            AutoStatePredictor temp;

            double leapIntensity = predictor.lastLeapIntensity * 2;

            if (leapIntensity > 0.9)
            {
                leapIntensity = 0.9;
            }
            for (int i = 0; i < cycles; i++)
            {
                //Mutation
                leapOverride(testTable, predictor, rdm, leapIntensity);

                //Read and predict analysis
                testTable.testPredict(trainSet);

                if (testTable.accuracy > predictor.accuracy)
                {
                    //Best predictor found

                    //Soft change with memory for sucessful mutations intensity (x2 because of random average behaviour)
                    leapIntensity = 0.8 * leapIntensity + 0.2 * (2 * testTable.lastLeapIntensity);

                    if (leapIntensity > 0.9)
                    {
                        leapIntensity = 0.9; //maximum allowed mutation intensity
                    }
                    //Swap predictors for memory usage
                    temp      = predictor;
                    predictor = testTable;
                    testTable = temp;
                }
            }

            copyInto(predictor, this);
        }
示例#3
0
        private void bake(SymbolCorpus trainSet)
        {
            SymbolDialogue dialogue;
            SymbolSentence sentence;
            byte           feed = 0;

            for (int i = 0; i < trainSet.dialogues.Length; i++)
            {
                dialogue = trainSet.dialogues[i];

                //Reset states
                if (deepLayers != null)
                {
                    for (int k = 0; k < deepLayers.Length; k++)
                    {
                        deepLayers[k].state = 0;
                    }
                }
                outputLayer.state = 0;

                for (int j = 0; j < dialogue.sentences.Length; j++)
                {
                    sentence = dialogue.sentences[j];

                    //Predict first symbol
                    feed = 0;
                    if (deepLayers != null)
                    {
                        for (int k = 0; k < deepLayers.Length; k++)
                        {
                            feed = deepLayers[k].process(feed);
                        }
                    }

                    outputLayer.bake(feed, sentence.symbols[0]);


                    //Predict remaining symbols
                    for (int l = 1; l < sentence.symbols.Length; l++)
                    {
                        feed = sentence.symbols[l - 1];
                        if (deepLayers != null)
                        {
                            for (int k = 0; k < deepLayers.Length; k++)
                            {
                                feed = deepLayers[k].process(feed);
                            }
                        }

                        outputLayer.bake(feed, sentence.symbols[l]);
                    }
                }
            }

            outputLayer.finalizeBake();
        }
示例#4
0
        public SymbolCorpus truncanteDialogues(int dialogueCount)
        {
            SymbolCorpus truncated = new SymbolCorpus();

            truncated.dialogues = new SymbolDialogue[dialogueCount];
            for (int i = 0; i < truncated.dialogues.Length; i++)
            {
                truncated.dialogues[i] = dialogues[i];
            }

            return(truncated);
        }
示例#5
0
        public SymbolCorpus textToSymbol(Corpus text)
        {
            SymbolCorpus symbols = new SymbolCorpus();

            symbols.dialogues = new SymbolDialogue[text.dialogues.Length];
            for (int i = 0; i < symbols.dialogues.Length; i++)
            {
                symbols.dialogues[i] = textToSymbol(text.dialogues[i]);
            }

            return(symbols);
        }
示例#6
0
        public Corpus symbolToText(SymbolCorpus symbols)
        {
            Corpus text = new Corpus();

            text.dialogues = new Corpus.Dialogue[symbols.dialogues.Length];

            for (int i = 0; i < text.dialogues.Length; i++)
            {
                text.dialogues[i] = symbolToText(symbols.dialogues[i]);
            }

            return(text);
        }
示例#7
0
        public CorpusResult process(SymbolCorpus input)
        {
            CorpusResult result = new CorpusResult();

            //Create same dimension process Corpus
            result.states                = new SymbolCorpus();
            result.states.dialogues      = new SymbolDialogue[input.dialogues.Length];
            result.hits                  = new SymbolCorpus();
            result.hits.dialogues        = new SymbolDialogue[input.dialogues.Length];
            result.predictions           = new SymbolCorpus();
            result.predictions.dialogues = new SymbolDialogue[input.dialogues.Length];

            //Process
            SymbolDialogue inputDialogue;
            SymbolSentence inputSentence;
            ProcessResult  sentenceResult;

            for (int i = 0; i < input.dialogues.Length; i++)
            {
                inputDialogue = input.dialogues[i];

                //Create same dimension process dialogue
                result.states.dialogues[i]                = new SymbolDialogue();
                result.states.dialogues[i].sentences      = new SymbolSentence[input.dialogues[i].sentences.Length];
                result.hits.dialogues[i]                  = new SymbolDialogue();
                result.hits.dialogues[i].sentences        = new SymbolSentence[input.dialogues[i].sentences.Length];
                result.predictions.dialogues[i]           = new SymbolDialogue();
                result.predictions.dialogues[i].sentences = new SymbolSentence[input.dialogues[i].sentences.Length];

                for (int j = 0; j < inputDialogue.sentences.Length; j++)
                {
                    inputSentence = inputDialogue.sentences[j];

                    result.states.dialogues[i].sentences[j]      = new SymbolSentence();
                    result.hits.dialogues[i].sentences[j]        = new SymbolSentence();
                    result.predictions.dialogues[i].sentences[j] = new SymbolSentence();

                    //predict corpus, reset state per sentence
                    state          = 0;
                    sentenceResult = process(inputSentence.symbols);
                    result.states.dialogues[i].sentences[j].symbols      = sentenceResult.states;
                    result.hits.dialogues[i].sentences[j].symbols        = sentenceResult.hits;
                    result.predictions.dialogues[i].sentences[j].symbols = sentenceResult.predictions;
                }
            }

            return(result);
        }
示例#8
0
            public void StartTrain(SymbolCorpus trainSet)
            {
                this.trainSet = trainSet;
                int threadCount = 2;

                training = true;

                threadStatus = new List <string>();
                for (int i = 0; i < threadCount; i++)
                {
                    threadStatus.Add("");
                }

                best = new Couppy(target.layerStateSizes, target.layerOutputSizes, target.translator);
                Couppy.copyInto(target, best);
                best.outputLayer.reset();
                best.bake(trainSet);
                TrainingUpdated?.Invoke("Layer training started with: " + best.outputLayer.getStats());

                for (int i = 0; i < trainSampleCount.Length; i++)
                {
                    trainSampleCount[i]           = 1;
                    trainAccumulatedAccuracies[i] = best.outputLayer.accuracy;
                }


                for (int i = 0; i < threadCount; i++)
                {
                    //Add thread lock
                    readLocks.Add(new object());
                    trainLocks.Add(new object());
                    //Create thread resource
                    Couppy tester = new Couppy(best.layerStateSizes, best.layerOutputSizes, best.translator);
                    chatBots.Add(tester);
                    //Create and start thread
                    Thread tt = new Thread(ThreadTrain);
                    threads.Add(tt);
                }

                //Start threads after to avoid first cycle conflicts
                for (int i = 0; i < threadCount; i++)
                {
                    threads[i].Start(i);
                }
            }
示例#9
0
        //Predicts corpus only to update error
        public void testPredict(SymbolCorpus input)
        {
            //Process
            SymbolDialogue inputDialogue;
            SymbolSentence inputSentence;

            for (int i = 0; i < input.dialogues.Length; i++)
            {
                inputDialogue = input.dialogues[i];

                for (int j = 0; j < inputDialogue.sentences.Length; j++)
                {
                    inputSentence = inputDialogue.sentences[j];

                    //predict corpus, reset state per sentence
                    state = 0;
                    testPredict(inputSentence.symbols);
                }
            }
        }
            public void StartTrain(SymbolCorpus trainSet)
            {
                this.trainSet = trainSet;
                int threadCount = 8;

                training = true;

                best = new HybridStatePredictor(target.symbolSize, target.stateSize);
                HybridStatePredictor.copyInto(target, best);
                best.reset();
                best.testPredict(trainSet);
                Console.WriteLine("Layer training started with: " + best.getStats());

                for (int i = 0; i < trainSampleCount.Length; i++)
                {
                    trainSampleCount[i]           = 1;
                    trainAccumulatedAccuracies[i] = best.accuracy;
                }


                for (int i = 0; i < threadCount; i++)
                {
                    //Add thread lock
                    readLocks.Add(new object());
                    trainLocks.Add(new object());
                    //Create thread resource
                    HybridStatePredictor tester = new HybridStatePredictor(best.symbolSize, best.stateSize);
                    predictors.Add(tester);
                    //Create and start thread
                    Thread tt = new Thread(ThreadTrain);
                    threads.Add(tt);
                }

                //Start threads after to avoid first cycle conflicts
                for (int i = 0; i < threadCount; i++)
                {
                    threads[i].Start(i);
                }
            }
示例#11
0
 public void StartTrain(SymbolCorpus trainSet)
 {
     trainer = new Trainer(this);
     trainer.StartTrain(trainSet);
 }
        public void train2(SymbolCorpus trainSet, double minutes)
        {
            Random rdm = new Random();

            HybridStatePredictor predictor = new HybridStatePredictor(symbolSize, stateSize);

            //Make copy of current
            copyInto(this, predictor);

            if (predictor.trainingDepth == 0)
            {
                randomOverride(predictor, rdm);

                //Read and predict analysis
                predictor.testPredict(trainSet);

                //TODO Generate some more randoms and choose best - might not be needed since leap Intensity is so high at the beginning
            }

            //Initialize training variables
            HybridStatePredictor testTable = new HybridStatePredictor(predictor.symbolSize, predictor.stateSize);
            HybridStatePredictor temp;

            double leapIntensity = predictor.lastLeapIntensity * 2 * 4;

            if (leapIntensity > 0.9)
            {
                leapIntensity = 0.9;
            }

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            Console.Write("Training:");
            int percentil = 1;

            while (stopwatch.ElapsedMilliseconds < 1000 * 60 * minutes)
            {
                if (stopwatch.ElapsedMilliseconds > (1000 * 60 * minutes / 10.0) * percentil)
                {
                    Console.Write("$"); //print training tick
                    percentil++;
                }

                //Mutation
                //leapOverride(testTable, predictor, rdm, leapIntensity);

                //Read and predict analysis
                testTable.testPredict(trainSet);

                if (testTable.accuracy > predictor.accuracy)
                {
                    //Best predictor found

                    //Soft change with memory for sucessful mutations intensity (x2 because of random average behaviour)
                    leapIntensity = 0.8 * leapIntensity + 0.2 * (2 * testTable.lastLeapIntensity * 4);

                    if (leapIntensity > 0.9)
                    {
                        leapIntensity = 0.9; //maximum allowed mutation intensity
                    }
                    //Swap predictors for memory usage
                    temp      = predictor;
                    predictor = testTable;
                    testTable = temp;
                }
            }
            stopwatch.Stop();
            Console.WriteLine();

            copyInto(predictor, this);
        }