示例#1
0
 public ConvolutionalChromosome(GeneticProgramm gp, Random random)
 {
     _gp         = gp;
     this.random = random;
     nrp         = gp._nrp;
     crp         = gp._crp;
     drp         = gp._drp;
     convPart    = new ConvStructure(crp, random);
     densePart   = new DenseStructure(nrp, drp, random);
     if (nrp.notRandomSpeed)
     {
         trainConstSpeed = nrp.trainConstSpeedRange[0];
     }
     else
     {
         trainConstSpeed = Convert.ToSingle(random.Next((int)(nrp.trainConstSpeedRange[0] * 1000), (int)(nrp.trainConstSpeedRange[1] * 1000))) / 1000;
     }
     optimizer     = nrp.optimizers[random.Next(nrp.optimizers.Count)];
     loss_function = nrp.loss_functions[random.Next(nrp.loss_functions.Count)];
 }
示例#2
0
        ///////////////////////////////////////////////////////
        // new Process
        public void UpdateAssessmentParams(ConvolutionalChromosome chromosome, GeneticProgramm gp, int epoch)
        {
            instance.Invoke(new Action(() => {
                if (!instance.errDontClearChB.Checked)
                {
                    instance.ErrorTB.Clear();
                }
                if (!instance.chrDontClearChB.Checked)
                {
                    instance.chrOutTB.Clear();
                }
            }));
            ConvolutionalNetwork tempNet = new ConvolutionalNetwork(chromosome, epoch, gp._nrp.networkName);

            ConvolutionalNetwork.CreateNetworkScript(tempNet, chromosome.crp.convActivations, chromosome.drp.denseActivations);
            ConvolutionalNetwork.CreateConvBatFile("temp_convolution.py", @"C:\keras\Directory\scripts\convolutional\genetic", gp._nrp.datasetPath,
                                                   gp._nrp._modelPath, gp._nrp._labelPath, gp._nrp._plotPath, gp._nrp.networkName, epoch, chromosome.indexNumber, 0);

            CreateConvBatProcess();

            chromosome.accuracy    = instance.tempAccuracy;
            chromosome.paramsCount = instance.tempParameters;
        }
示例#3
0
        public GeneticProgramm CollectConv_NN_Params()
        {
            List <String> convActivations = new List <string>();

            if (reluConvChb.Checked)
            {
                convActivations.Add("relu");
            }
            if (softmaxConvChb.Checked)
            {
                convActivations.Add("softmax");
            }
            if (lReluConvChb.Checked)
            {
                convActivations.Add("Leaky ReLU");
            }
            if (softsignConvChb.Checked)
            {
                convActivations.Add("softsign");
            }
            if (sigmoidConvChb.Checked)
            {
                convActivations.Add("sigmoid");
            }
            if (eluConvChb.Checked)
            {
                convActivations.Add("elu");
            }
            if (seluConvChb.Checked)
            {
                convActivations.Add("selu");
            }
            if (softplusConvChb.Checked)
            {
                convActivations.Add("softplus");
            }
            if (tanhConvChb.Checked)
            {
                convActivations.Add("tang");
            }
            if (PReLUConvChB.Checked)
            {
                convActivations.Add("PReLu");
            }
            if (TReLUConvChB.Checked)
            {
                convActivations.Add("TReLU");
            }

            List <String> denseActivations = new List <string>();

            if (reluDenseChb.Checked)
            {
                denseActivations.Add("relu");
            }
            if (softmaxDenseChb.Checked)
            {
                denseActivations.Add("softmax");
            }
            if (lReluDenseChb.Checked)
            {
                denseActivations.Add("Leaky ReLU");
            }
            if (softsignDenseChb.Checked)
            {
                denseActivations.Add("softsign");
            }
            if (sigmoidDenseChb.Checked)
            {
                denseActivations.Add("sigmoid");
            }
            if (eluDenseChb.Checked)
            {
                denseActivations.Add("elu");
            }
            if (seluDenseChb.Checked)
            {
                denseActivations.Add("selu");
            }
            if (softplusDenseChb.Checked)
            {
                denseActivations.Add("softplus");
            }
            if (tanhDenseChb.Checked)
            {
                denseActivations.Add("tang");
            }
            if (PReLUDenseChB.Checked)
            {
                denseActivations.Add("PReLU");
            }
            if (ThReLUDenseChB.Checked)
            {
                denseActivations.Add("TReLU");
            }

            List <String> optimazers = new List <string>();

            if (SGD_OptChB.Checked)
            {
                optimazers.Add("SGD");
            }

            List <String> lossFunctions = new List <string>();

            if (categorical_crossentropyChB.Checked)
            {
                lossFunctions.Add("categorical_crossentropy");
            }

            List <int> callbacks_indexes = new List <int>();

            if (modelCPChB.Checked)
            {
                callbacks_indexes.Add(0);
            }


            var nrp = new NetworkRandomParams(constSpeedChB.Checked,
                                              new[] { float.Parse(minConstSpeedTB.Text, System.Globalization.CultureInfo.InvariantCulture),
                                                      float.Parse(maxConstSpeedTB.Text, System.Globalization.CultureInfo.InvariantCulture) },
                                              datasetPathTB.Text, modelsPathTB.Text, labelsPathTB.Text, plotsPathTB.Text,
                                              networkNameTB.Text, optimazers, lossFunctions, (int)learningEpochsNUD.Value, (int)batchSizeNUD.Value);

            var crp = new ConvRandomParams((int)ConvLayersNumbNUD.Value,
                                           (int)ConvFiltersNUD.Value,
                                           convActivations.Count,
                                           convActivations,
                                           new[] { (int)slidingWindow1NUD.Value, (int)slidingWindow2NUD.Value },
                                           convDropoutChB.Checked,
                                           (int)convDropoutNUD.Value);
            var drp = new DenseRandomParams((int)DenseLayersNumbNUD.Value,
                                            (int)DenseNeuronsNUD.Value,
                                            nrp.networkOutputNumb, // Support.GetOutputNumb(datasetPath);
                                            denseActivations.Count,
                                            denseActivations,
                                            denseDropoutChB.Checked,
                                            (int)denseDropoutNUD.Value);
            var gp = new GeneticProgramm(nrp, crp, drp, (int)geneticEpochsNUD.Value, new[] { (int)copySelNUD.Value, (int)crossSelNUD.Value, (int)mutateRateNUD.Value },
                                         (int)popolationCountNUD.Value, (int)mutateRateNUD.Value, (float)accPriorityNUD.Value, (float)memPriorityNUD.Value,
                                         EstimatorCB.SelectedIndex, (int)instance.percentNUD.Value);

            return(gp);
        }
示例#4
0
        // ReSharper disable once InconsistentNaming
        public static void TrainConvolutionalNN()
        {
            Support.pointParamsList.Clear();
            Support.pointAssesList.Clear();
            instance.currentTask++;
            instance.Invoke(new Action((() =>
            {
                instance.curTaskLabel.Text = instance.currentTask.ToString();
                instance.AssesGenZedGraph.Invalidate();
                instance.ParamsZedGraph.Invalidate();
                instance.accZG.Invalidate();

                instance.currentTaskPB.Minimum = 0;
                instance.currentTaskPB.Maximum = (int)instance.geneticEpochsNUD.Value + 1;
                instance.currentTaskPB.Value = 0;
                instance.currentTaskPB.Step = 1;
            })));



            string dateDirectory = Support.CreateDateTimeDirectory(@"C:\keras\Directory\reports", 0);
            string timeDirectory = Support.CreateDateTimeDirectory(dateDirectory, 1);

            File.Create(timeDirectory + @"\report.txt").Close();

            int             maxParams = 0;
            GeneticProgramm gp        = instance.geneticProgramms.Dequeue();

            if (!instance.genDontClearChB.Checked)
            {
                instance.Invoke(new Action(instance.textBox9.Clear));
            }
            instance.population = new List <ConvolutionalChromosome>(gp._populationSize);

            while (instance.population.Count != instance.population.Capacity)
            {
                instance.population.Add(new ConvolutionalChromosome(gp, random));
                instance.population[instance.population.Count - 1].name        = "Chromosome " + instance.population.Count.ToString();
                instance.population[instance.population.Count - 1].indexNumber = instance.population.Count;
            }

            for (int i = 0; i < instance.population.Count; i++)
            {
                instance.tempAccuracy = instance.tempParameters = 0;
                instance.WriteAdds(0, i);
                instance.UpdateAssessmentParams(instance.population[i], gp, 0);
                File.AppendAllText(@"C:\keras\Directory\Assessment_Statistic.txt",
                                   instance.population[i].accuracy.ToString() + "_" + instance.population[i].paramsCount + Environment.NewLine);
                if (instance.population[i].paramsCount > maxParams)
                {
                    maxParams = instance.population[i].paramsCount;
                }
                Support.DrawParamsGraph(instance.ParamsZedGraph, instance.population[i].accuracy, instance.population[i].paramsCount, maxParams);
                Support.DrawAccGraph(instance.accZG, 0, instance.population[0]._gp._genEpochs, instance.population[i].accuracy);
            }

            Support.UpdateAssesment(instance.population);
            for (int z = 0; z < instance.population.Count; z++)
            {
                Support.DrawAssesGraph(instance.AssesGenZedGraph, 0, instance.population[z]._gp._genEpochs, instance.population[z].assessment, z);
            }

            instance.SortChromosome(instance.population, gp._assessmentIndex, gp._percent);
            //instance.population = SortChromosome(instance.population, gp._assessmentIndex, gp._percent);
            //instance.population.Sort(new ChromosomeComparer4((int)instance.percentNUD.Value));
            //instance.population.Sort(new ChromosomeComparer2());
            //instance.eliteChromosomes.Add(new ConvolutionalNetwork(instance.population[0]));
            instance.WriteConvGeneticOutput(0);
            File.WriteAllLines(timeDirectory + @"\report.txt", instance.textBox9.Lines);
            instance.Invoke(new Action(instance.currentTaskPB.PerformStep));
            // Evolve

            for (int i = 1; i <= gp._genEpochs; i++)
            {
                for (int m = 1; m < instance.population.Count; m++)
                {
                    // ReSharper disable once CompareOfFloatsByEqualityOperator
                    if (instance.population[m].assessment == 0)
                    {
                        string buff = instance.population[m].name;
                        instance.population[m] = new ConvolutionalChromosome(gp, random)
                        {
                            name = buff
                        };
                    }
                    else
                    {
                        if (new Random().Next() < 5)
                        {
                            string buff = instance.population[m].name;
                            instance.population[m] = new ConvolutionalChromosome(gp, random)
                            {
                                name = buff
                            };
                        }
                        else
                        {
                            instance.population[m].MutateConvolutional(gp._crp, gp._mutateRate);
                            instance.population[m].MutateDense(gp._drp, gp._mutateRate);
                        }
                    }
                }


                for (int n = 1; n < instance.population.Count; n++)
                {
                    instance.population[n].assessment = instance.population[n].accuracy = instance.population[n].paramsCount = 0;
                    instance.tempAccuracy             = instance.tempParameters = 0;
                    instance.WriteAdds(i, n);
                    instance.UpdateAssessmentParams(instance.population[n], gp, i);
                    File.AppendAllText(@"C:\keras\Directory\Assessment_Statistic.txt",
                                       instance.population[n].accuracy.ToString() + "_" + instance.population[n].paramsCount + Environment.NewLine);
                    if (instance.population[n].paramsCount > maxParams)
                    {
                        maxParams = instance.population[n].paramsCount;
                    }
                    Support.DrawParamsGraph(instance.ParamsZedGraph, instance.population[n].accuracy, instance.population[n].paramsCount, maxParams);
                }

                Support.UpdateAssesment(instance.population);
                for (int z = 0; z < instance.population.Count; z++)
                {
                    Support.DrawAssesGraph(instance.AssesGenZedGraph, i, instance.population[z]._gp._genEpochs, instance.population[z].assessment, z);
                }

                instance.SortChromosome(instance.population, gp._assessmentIndex, gp._percent);
                //instance.population.Sort(new ChromosomeComparer4((int)instance.percentNUD.Value));
                //population.Sort(new ChromosomeComparer((float)memPriorityNUD.Value, (float)accPriorityNUD.Value));
                //instance.population.Sort(new ChromosomeComparer2());
                //instance.eliteChromosomes.Add(new ConvolutionalNetwork(instance.population[0]));
                instance.WriteConvGeneticOutput(i);
                File.WriteAllLines(timeDirectory + @"\report.txt", instance.textBox9.Lines);
                instance.Invoke(new Action(instance.currentTaskPB.PerformStep));
            }

            Image image = new Bitmap(instance.AssesGenZedGraph.GetImage());

            image.Save(timeDirectory + @"\asses.png");
            image = new Bitmap(instance.ParamsZedGraph.GetImage());
            image.Save(timeDirectory + @"\params.png");
            image = new Bitmap(instance.accZG.GetImage());
            image.Save(timeDirectory + @"\acc.png");
        }