コード例 #1
0
ファイル: Population.cs プロジェクト: deezy135/Neural-Network
        public Population(int size, IndividualDesc desc, ComputeDevice device, float[] dataSet, bool isTimeSeries, int seed = 0)
        {
            populationSize = size;
            if (seed == 0)
            {
                randomGenerator = new Random();
            }
            else
            {
                randomGenerator = new Random(seed);
            }
            if (desc.Updated == false)
            {
                desc.UpdateProperties();
            }
            indDesc         = desc;
            usingTimeSeries = isTimeSeries;
            genomeLength    = indDesc.TotalWeights;

            genotypes = new float[populationSize * genomeLength];
            for (int i = 0; i < genotypes.Length; ++i)
            {
                genotypes[i] = Convert.ToSingle(randomGenerator.NextDouble()) * 2 - 1;
            }
            phenotypes = new Individual[populationSize];
            for (int i = 0; i < phenotypes.Length; ++i)
            {
                phenotypes[i].Fitness = 0;
                phenotypes[i].Offset  = i * genomeLength;
            }

            inputData = dataSet;

            computeDevice = device;

            maxFitness = -float.MaxValue;
            avgFitness = 0.0f;

            eliteRatio    = 0.1f;
            mutationRatio = 0.05f;
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: deezy135/Neural-Network
        private void buildNetworkB_Click(object sender, EventArgs e)
        {
            if (!checkSelectedInputOutputCells())
            {
                return;
            }
            if (computeDeviceCB.SelectedIndex == -1)
            {
                MessageBox.Show("Пожалуйста, выберите устройство");
                return;
            }

            IndividualDesc indDescTS = new IndividualDesc(selectedInputRows.Length / 2, 1);

            optionsForm.GetStructure(out indDescTS.HiddenLayersSizes, out indDescTS.ForHidden, out indDescTS.ForOutput);
            indDescTS.UpdateProperties();

            IndividualDesc indDescDS = new IndividualDesc(selectedInputColumns.Length, selectedOutputColumns.Length);

            optionsForm.GetStructure(out indDescDS.HiddenLayersSizes, out indDescDS.ForHidden, out indDescDS.ForOutput);
            indDescDS.UpdateProperties();

            MessageBox.Show("Количество весов: " + indDescTS.TotalWeights + " " + indDescDS.TotalWeights);
            float[][] table;
            float[][] predictionTable;
            float[]   dataSet;

            float normalizationMin, normalizationMax;

            optionsForm.GetDataOptions(out normalizationMin, out normalizationMax);
            copyAndNormalizeDataFromDGV(normalizationMin, normalizationMax, out table, out minValues, out maxValues);
            int setSize   = selectedInputColumns.Length + selectedOutputColumns.Length;
            int totalSets = selectedInputRows.Length;

            dataSet = new float[setSize * totalSets];
            for (int iRow = 0; iRow < totalSets; ++iRow)
            {
                for (int iCol = 0; iCol < setSize; ++iCol)
                {
                    dataSet[iRow * setSize + iCol] = table[iCol][iRow];
                }
            }

            dataFromFile.Clear();
            dataFromNetwork.Clear();
            graphCB.Items.Clear();

            float avgError = 0.0f;

            float[] errors    = new float[selectedInputColumns.Length + 1];
            float   totalTime = 0.0f;

            float[] seconds        = new float[selectedInputColumns.Length + 1];
            bool[]  stagnationStop = new bool[selectedInputColumns.Length + 1];
            predictionTable = new float[setSize][];
            float[] predictedDataSet = null;

            Population[] populations = new Population[selectedInputColumns.Length + 1];

            device.Initialize(computeDeviceCB.SelectedIndex);
            //device.SetWorkGroupSize(Convert.ToInt32(workGroupSizeNUD.Value));

            Random random = new Random(Convert.ToInt32(seedNUD.Value));

            float gpgpuTime = 0.0f;

            for (int iPop = 0; iPop < populations.Length; ++iPop)
            {
                bool  stopCondition   = false;
                float maxFitness      = -float.MaxValue;
                int   epochs          = 0;
                int   lastImprovement = 0;
                int   stagnationValue = Convert.ToInt32(stagnationTSNUD.Value);
                float errorThreshold  = Convert.ToSingle(errorThresholdTSNUD.Value);

                if (iPop < selectedInputColumns.Length)
                {
                    populations[iPop] = new Population(1024, indDescTS, device, table[iPop], true, random.Next());
                }
                else
                {
                    populations[iPop] = new Population(1024, indDescDS, device, dataSet, false, random.Next());
                    //stagnationValue = Convert.ToInt32(stagnationDSNUD.Value);
                    //errorThreshold = Convert.ToSingle(errorThresholdDSNUD.Value);
                }
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                while (!stopCondition)
                {
                    ++epochs;
                    populations[iPop].Epoch();
                    gpgpuTime += device.GetLastTimeResult();
                    float curFitness = populations[iPop].GetMaxFitness();
                    if (maxFitness < curFitness)
                    {
                        maxFitness      = curFitness;
                        lastImprovement = epochs;
                    }
                    if (epochs > (lastImprovement + stagnationValue) || -curFitness < errorThreshold)
                    {
                        stagnationStop[iPop] = epochs > (lastImprovement + stagnationValue);
                        stopCondition        = true;
                    }
                }
                seconds[iPop] = stopwatch.ElapsedMilliseconds / 1000.0f;
                stopwatch.Stop();

                if (iPop < selectedInputColumns.Length)
                {
                    populations[iPop].PredictTimeSeries(Convert.ToInt32(predictionSizeNUD.Value));
                    predictionTable[iPop] = populations[iPop].GetOutputTimeSeries();
                    graphCB.Items.Add(inputOutputDGV.Columns[selectedInputColumns[iPop]].HeaderText);
                    dataFromFile.Add(table[iPop]);
                    dataFromNetwork.Add(predictionTable[iPop]);
                }
                else
                {
                    if (predictedDataSet == null)
                    {
                        predictedDataSet = new float[Convert.ToInt32(predictionSizeNUD.Value) * setSize];
                        for (int iCol = 0; iCol < selectedInputColumns.Length; ++iCol)
                        {
                            for (int iRow = totalSets; iRow < predictionTable[iCol].Length; ++iRow)
                            {
                                predictedDataSet[(iRow - totalSets) * setSize + iCol] = predictionTable[iCol][iRow];
                            }
                        }
                    }
                    populations[iPop].PredictDataSet(predictedDataSet);
                    float[] predictedOutput          = populations[iPop].GetOutputTimeSeries();
                    int     preditedOutputColumnSize = predictedOutput.Length / selectedOutputColumns.Length;
                    for (int iCol = 0; iCol < selectedOutputColumns.Length; ++iCol)
                    {
                        float[] predictedOutputColumn = new float[preditedOutputColumnSize];
                        Array.Copy(predictedOutput, iCol * preditedOutputColumnSize, predictedOutputColumn, 0, preditedOutputColumnSize);
                        predictionTable[iPop + iCol] = predictedOutputColumn;
                        graphCB.Items.Add(inputOutputDGV.Columns[selectedOutputColumns[iCol]].HeaderText);
                        dataFromFile.Add(table[iPop + iCol]);
                        dataFromNetwork.Add(predictionTable[iPop + iCol]);
                    }
                }

                errors[iPop] = -maxFitness;
                avgError    += errors[iPop] / errors.Length;
                totalTime   += seconds[iPop];
            }

            // Записываем прогнозные данные в таблицу "Результат"
            denormalizeAndCopyDataToDGV(predictionTable, normalizationMin, normalizationMax, minValues, maxValues);
            foreach (DataGridViewColumn column in predictionDGV.Columns)
            {
                column.SortMode = DataGridViewColumnSortMode.NotSortable;
                column.DefaultCellStyle.Format = "0.######";
            }

            // Вывод графиков
            graphCB.SelectedIndex     = 0;
            tabControl1.SelectedIndex = 1;

            resultMessage = "Ошибки факторов: \n";
            int factorCount = selectedInputColumns.Length;

            for (int i = 0; i < errors.Length; ++i)
            {
                resultMessage += (i < factorCount ? "Вход №" + (i + 1) :
                                  "Вых. №" + (i - factorCount + 1)) + ": " + (i + 1 - (i < factorCount ? 0 : factorCount) < 10 ? "\t\t" : "\t")
                                 + errors[i].ToString("0.000") + " за " + seconds[i].ToString("0.000") + " сек. "
                                 + "(Достигнута " + (stagnationStop[i] ? "стагнация" : "ошибка") + ")\n";
            }
            resultMessage += "Средняя ошибка: \t" + avgError.ToString("0.000") + " за "
                             + totalTime.ToString("0.000") + " сек.\n";
            resultMessage += "Вычисления: " + gpgpuTime.ToString("0.000") + " сек.\n";
            MessageBox.Show(resultMessage, "Результаты", MessageBoxButtons.OK, MessageBoxIcon.Information);
            showResultMessageB.Enabled = true;
        }