Ejemplo n.º 1
0
 public ChatbotDialogueView(Couppy bot)
 {
     InitializeComponent();
     this.bot = bot;
     dialogue = new Corpus.Dialogue();
     inputTextBox.TextChanged += InputTextBox_TextChanged;
 }
Ejemplo n.º 2
0
            public Trainer(Couppy target)
            {
                this.target = target;
                checkLock   = new object();
                threads     = new List <Thread>();
                readLocks   = new List <object>();
                trainLocks  = new List <object>();
                chatBots    = new List <Couppy>();

                dnaLength = target.getDnaLength();
                int steps;

                for (steps = 0; Math.Pow(2, steps) < dnaLength; steps++)
                {
                    ;
                }
                steps++; //include last and first
                trainIntensity             = new int[steps];
                trainSampleCount           = new int[steps];
                trainAccumulatedAccuracies = new double[steps];
                for (int i = 0; i < steps; i++)
                {
                    if (i != steps - 1)
                    {
                        trainIntensity[i] = (int)Math.Pow(2, i);
                    }
                    else
                    {
                        trainIntensity[i] = dnaLength;
                    }
                }
            }
Ejemplo n.º 3
0
        public ChatbotView(Couppy chatbot)
        {
            InitializeComponent();
            this.chatbot = chatbot;

            UpdateVisuals();
            MainWindow.controlChanged += UpdateVisuals;
        }
Ejemplo n.º 4
0
        public static Couppy LoadFromFile(string file)
        {
            //Initialize deserializer
            XmlSerializer serializer     = new XmlSerializer(typeof(Couppy));
            FileStream    ReadFileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);
            Couppy        loadedChatbot  = (Couppy)serializer.Deserialize(ReadFileStream);

            ReadFileStream.Close();

            return(loadedChatbot);
        }
Ejemplo n.º 5
0
            public void StopTrain()
            {
                training = false;


                //Pass through all trainLocks to check cleared
                for (int i = 0; i < trainLocks.Count; i++)
                {
                    Monitor.Enter(trainLocks[i]);
                    Monitor.Exit(trainLocks[i]);
                }

                //Copy train result to target
                Couppy.copyInto(best, target);
            }
Ejemplo n.º 6
0
        public TrainView(Couppy chatbot)
        {
            InitializeComponent();
            this.chatbot    = chatbot;
            corpusSelectors = new List <Button>();
            selectedIndex   = -1;

            for (int i = 0; i < MainWindow.corpus.Count; i++)
            {
                Button selector = new Button();
                selector.Background = new SolidColorBrush(Colors.LightYellow);
                GroupBox container = new GroupBox();
                container.Header              = MainWindow.corpus[i].name;
                container.FontSize            = 18;
                container.BorderThickness     = new Thickness(0);
                container.HorizontalAlignment = HorizontalAlignment.Stretch;


                Label stats = new Label();
                stats.FontSize = 14;
                string sts = "";
                sts          += "Dialogue count: " + ("" + MainWindow.corpus[i].getDialogueCount()).PadRight(20) + "   ";
                sts          += "Sentence count: " + ("" + MainWindow.corpus[i].getSentenceCount()).PadRight(20) + "   ";
                sts          += "Character count: " + ("" + MainWindow.corpus[i].getCharCount()).PadRight(20);
                stats.Content = sts;

                container.Content = stats;

                selector.Content     = container;
                selector.DataContext = i;
                selector.Height      = 64;
                selector.Width       = 700;
                selector.FontSize    = 18;
                selector.Click      += Selector_Click;
                selector.Margin      = new Thickness(2);
                selector.HorizontalContentAlignment = HorizontalAlignment.Stretch;

                corpusSelectors.Add(selector);
                corpusPanel.Children.Add(selector);
            }

            timer.Interval = new TimeSpan(0, 0, 1);
            timer.Tick    += timer_tick;

            Couppy.TrainingUpdated += Couppy_TrainingUpdated;
        }
Ejemplo n.º 7
0
        public void Update()
        {
            chatbots.Clear();;
            chatBotStackPanel.Children.Clear();

            //Load all files in current directory with specified termination
            string[] chatbotFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.cp");

            //Go over all chatbotFiles
            for (int i = 0; i < chatbotFiles.Length; i++)
            {
                //Load each bot
                Couppy loadedChatbot = Couppy.LoadFromFile(chatbotFiles[i]);

                //add bot to list
                chatbots.Add(loadedChatbot);

                //Create some visual
                Button selector = new Button();
                selector.DataContext                = loadedChatbot;
                selector.Width                      = 560;
                selector.HorizontalAlignment        = HorizontalAlignment.Center;
                selector.HorizontalContentAlignment = HorizontalAlignment.Stretch;
                selector.VerticalContentAlignment   = VerticalAlignment.Stretch;
                selector.Background                 = new SolidColorBrush(Colors.LightYellow);
                selector.Click                     += Selector_Click;
                selector.Margin                     = new Thickness(6);

                GroupBox chatBotDescription = new GroupBox();
                chatBotDescription.Header          = loadedChatbot.name;
                chatBotDescription.FontSize        = 18;
                chatBotDescription.BorderThickness = new Thickness(0f);
                //chatBotDescription.Background = new SolidColorBrush(Colors.LightCyan);
                chatBotDescription.HorizontalAlignment = HorizontalAlignment.Stretch;
                chatBotDescription.VerticalAlignment   = VerticalAlignment.Stretch;

                Label details = new Label();
                details.FontSize = 14;
                details.Content  = loadedChatbot.getDescription();

                chatBotDescription.Content = details;
                selector.Content           = chatBotDescription;
                chatBotStackPanel.Children.Add(selector);
            }
        }
Ejemplo n.º 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);
                }
            }
Ejemplo n.º 9
0
        /*
         * public void printCascade()
         * {
         *  Console.WriteLine("Printing cascade");
         *
         *  int topLayer;
         *  int states;
         *  if (deepLayers != null)
         *  {
         *      topLayer = deepLayers.Length;
         *      states = outputLayer.stateSize;
         *
         *      //Set maximum states to all possible combinations between layers
         *      for (int k = 0; k < deepLayers.Length; k++)
         *          states *= deepLayers[k].stateSize;
         *  }
         *  else
         *  {
         *      topLayer = 0;
         *      states = outputLayer.stateSize;
         *  }
         *
         *  byte single;
         *  byte feed;
         *  int iState;
         *  for (int i = 0; i < states; i++)
         *  {
         *      List<byte> output = new List<byte>();
         *
         *      //Set Cascading States
         *      outputLayer.state = (ushort)(i % outputLayer.stateSize);
         *      iState = i / outputLayer.stateSize;
         *      if (deepLayers != null)
         *      {
         *          for (int k = 0; k < deepLayers.Length; k++)
         *          {
         *              deepLayers[k].state = (byte)(iState % deepLayers[k].stateSize);
         *              iState = iState / deepLayers[k].stateSize;
         *          }
         *      }
         *
         *
         *      if (deepLayers != null)
         *      {
         *          feed = deepLayers[deepLayers.Length - 1].state; //Use last state on last layer
         *          for (int k = deepLayers.Length - 1; k >= 0; k--)
         *          {
         *              deepLayers[k].state = feed;
         *              if (k != 0)
         *                  feed = deepLayers[k].predict(deepLayers[k - 1].state);
         *              else
         *                  feed = deepLayers[k].predict(outputLayer.state);
         *          }
         *      }
         *      else
         *          feed = outputLayer.state;
         *
         *      outputLayer.state = feed;
         *      single = outputLayer.process(0);
         *
         *      if (single == 0)
         *          continue;
         *
         *      output.Add(single);
         *
         *      for (int j = 1; j < 128; j++)
         *      {
         *          if (deepLayers != null)
         *          {
         *              feed = deepLayers[deepLayers.Length - 1].state; //Use last state on last layer
         *              for (int k = deepLayers.Length - 1; k >= 0; k--)
         *              {
         *                  deepLayers[k].state = feed;
         *                  if (k != 0)
         *                      feed = deepLayers[k].predict(deepLayers[k - 1].state);
         *                  else
         *                      feed = deepLayers[k].predict(outputLayer.state);
         *              }
         *          }
         *          else
         *              feed = outputLayer.state;
         *
         *          outputLayer.state = feed;
         *          single = outputLayer.process(output[j - 1]);
         *
         *          //Break if AI prints voidChar
         *          if (single == 0)
         *              break;
         *
         *          output.Add(single);
         *      }
         *
         *
         *      string stateString = "";
         *      iState = i;
         *      stateString += iState % outputLayer.stateSize;
         *      iState = i / outputLayer.stateSize;
         *      if (deepLayers != null)
         *      {
         *          for (int k = 0; k < deepLayers.Length; k++)
         *          {
         *              stateString = iState % deepLayers[k].stateSize + " , " + stateString;
         *              iState = iState / deepLayers[k].stateSize;
         *          }
         *      }
         *      Console.WriteLine(stateString + " : " + translator.symbolToText(output.ToArray()));
         *  }
         * }
         */
        public static void copyInto(Couppy original, Couppy destination)
        {
            RestrictedStatePredictor.copyInto(original.outputLayer, destination.outputLayer);
            if (original.deepLayers != null)
            {
                for (int i = 0; i < original.deepLayers.Length; i++)
                {
                    StatePredictor.copyInto(original.deepLayers[i], destination.deepLayers[i]);
                }
            }
            else
            {
                destination.deepLayers = null;
            }

            original.layerStateSizes.CopyTo(destination.layerStateSizes, 0);
            original.layerOutputSizes.CopyTo(destination.layerOutputSizes, 0);
            destination.name          = original.name;
            destination.trainingDepth = original.trainingDepth;
            destination.translator    = original.translator;
        }
        private void Create_Click(object sender, RoutedEventArgs e)
        {
            string name   = nameTextbox.Text;
            int    layers = (int)layerSlider.Value;

            int[] layerStateSizes  = new int[layers];
            int[] layerOutputSizes = new int[layers];
            for (int i = 0; i < layerStateSizes.Length; i++)
            {
                layerStateSizes[i]  = layerStateConfigs[i];
                layerOutputSizes[i] = layerOutputConfigs[i];
            }

            Translator usedTranslator;

            switch (translatorComboBox.SelectedIndex)
            {
            case 0:
                usedTranslator = new CharToSymbolTranslator();
                break;

            case 1:
                usedTranslator = new SyllableToSymbolTranslator();
                break;

            default:
                return;
            }

            layerOutputSizes[layers - 1] = usedTranslator.getSymbolCount();

            Couppy newChatbot = new Couppy(layerStateSizes, layerOutputSizes, usedTranslator);

            newChatbot.name = name;
            newChatbot.SaveToFile(name + ".cp");
            caller.Update();
            Close();
        }
Ejemplo n.º 11
0
            private void ThreadTrain(object indexObj)
            {
                int index = (int)indexObj;

                Monitor.Enter(trainLocks[index]);

                Random rdm = new Random(index);
                int    currentTrainIndex = trainIntensity.Length - 1;
                int    currentIntensity  = trainIntensity[currentTrainIndex];
                int    usedIntensity;

                setThreadStatus(index, "Train start");
                //while training is in place
                while (training)
                {
                    //Copy from best (if version control is in place, check version)
                    usedIntensity = rdm.Next(currentIntensity) + 1; //Inclusive upper bound, exclusive lower bound
                    setThreadStatus(index, "Waiting read to mutate...");
                    lock (readLocks[index])
                    {
                        setThreadStatus(index, "Reading to mutate...");
                        Couppy.leapOverride(best, chatBots[index], rdm, usedIntensity);
                    }

                    setThreadStatus(index, "Baking started.");
                    chatBots[index].bake(trainSet);

                    setThreadStatus(index, "Waiting lock to check improvement...");
                    lock (checkLock)
                    {
                        //Increment used training intensity sampling count
                        for (int i = currentTrainIndex; i >= 0; i--)
                        {
                            if (currentIntensity <= trainIntensity[i])
                            {
                                trainSampleCount[i]++;
                            }
                        }

                        if (chatBots[index].outputLayer.accuracy > best.outputLayer.accuracy)
                        {
                            //Best found
                            //Tune training intensity
                            //Add incentive to used training Intensity
                            double bestImprovement = trainAccumulatedAccuracies[0] / trainSampleCount[0];
                            int    bestIndex       = 0;
                            double improvement;
                            for (int i = currentTrainIndex; i >= 0; i--)
                            {
                                if (currentIntensity <= trainIntensity[i])
                                {
                                    trainAccumulatedAccuracies[i] += chatBots[index].outputLayer.accuracy - best.outputLayer.accuracy;
                                }
                                improvement = trainAccumulatedAccuracies[i] / trainSampleCount[i];
                                if (improvement > bestImprovement)
                                {
                                    bestImprovement = improvement;
                                    bestIndex       = i;
                                }
                            }
                            //Go through remaining intensitys to check if there is a better one
                            for (int i = currentTrainIndex + 1; i < trainSampleCount.Length; i++)
                            {
                                improvement = trainAccumulatedAccuracies[i] / trainSampleCount[i];
                                if (improvement > bestImprovement)
                                {
                                    bestImprovement = improvement;
                                    bestIndex       = i;
                                }
                            }
                            //Change index to the best expected improvement
                            currentTrainIndex = bestIndex;
                            currentIntensity  = trainIntensity[currentTrainIndex];

                            //if (chatBots[index].trainingDepth % 10 == 0)
                            //    printTrainingIntensityStatus();

                            //Start copy procedure
                            //Lock all read locks
                            for (int i = 0; i < readLocks.Count; i++)
                            {
                                setThreadStatus(index, "Waiting reading locks " + i + " to mutate");
                                Monitor.TryEnter(readLocks[i], -1);
                            }

                            //Copy into best
                            Couppy.copyInto(chatBots[index], best);

                            //This leaks
                            Thread statusPusherThread = new Thread(PushStatus);
                            statusPusherThread.Start("Leap by thread " + index + " : " + best.outputLayer.getStats());

                            //Unlock all read locks
                            for (int i = 0; i < readLocks.Count; i++)
                            {
                                Monitor.Exit(readLocks[i]);
                            }
                            setThreadStatus(index, "All reading locks unlocked");
                        }
                        else
                        {
                            //Failed leap
                            //leapIntensity = 0.99 * leapIntensity + 0.01 * (1f); //Gravity to randomness
                        }
                    }
                }

                Monitor.Exit(trainLocks[index]);
                setThreadStatus(index, "Thread exited");
            }
Ejemplo n.º 12
0
        public static void leapOverride(Couppy original, Couppy destination, Random rdm, int intensity)
        {
            //Copy original to destination
            Couppy.copyInto(original, destination);
            destination.outputLayer.reset();

            destination.trainingDepth             = original.trainingDepth + 1;
            destination.outputLayer.trainingDepth = original.trainingDepth + 1;
            if (original.deepLayers != null)
            {
                for (int i = 0; i < destination.deepLayers.Length; i++)
                {
                    destination.deepLayers[i].reset();
                    destination.deepLayers[i].trainingDepth = original.deepLayers[i].trainingDepth + 1;
                }
            }

            int changeCount = intensity;

            //Make changes
            int            layer;
            int            index;
            StatePredictor layerPredictor;
            int            dnaLength = original.getDnaLength();

            for (int i = 0; i < changeCount; i++)
            {
                index = rdm.Next(dnaLength);

                layer = -1;
                if (index < original.outputLayer.stateTransitionTable.Length)
                {
                    layer = destination.layerStateSizes.Length - 1;
                }
                else
                {
                    index -= original.outputLayer.stateTransitionTable.Length;
                    for (int k = 0; k < original.deepLayers.Length; k++)
                    {
                        if (index < original.deepLayers[k].table.Length)
                        {
                            layer = k;
                            break;
                        }
                        else
                        {
                            index -= original.deepLayers[k].table.Length;
                        }
                    }
                }

                if (layer == destination.layerStateSizes.Length - 1)
                {
                    destination.outputLayer.stateTransitionTable[index] = (ushort)rdm.Next(destination.outputLayer.stateSize); //New random state at index
                }
                else
                {
                    //Choose random table index for change
                    layerPredictor = destination.deepLayers[layer];

                    //Check if index is output or state
                    if (index % 2 == 0)
                    {
                        layerPredictor.table[index] = (byte)rdm.Next(layerPredictor.outputSymbolSize); //New random output at index
                    }
                    else
                    {
                        layerPredictor.table[index] = (byte)rdm.Next(layerPredictor.stateSize); //New random state at index
                    }
                }
            }
        }