Exemplo n.º 1
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);
            }
Exemplo n.º 2
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);
                }
            }
Exemplo n.º 3
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");
            }
Exemplo n.º 4
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
                    }
                }
            }
        }