Esempio n. 1
0
        public void ProcessDataSet(float[] weights, IndividualDesc desc, float[] inputDS, out float[] outputDS, out float[] errors)
        {
            int populationSize = weights.Length / desc.TotalWeights;
            int setSize        = desc.InputLayerSize + desc.OutputLayerSize;
            int totalSets      = inputDS.Length / setSize;

            outputDS = new float[totalSets * desc.OutputLayerSize];
            GCHandle weightsCHandle = GCHandle.Alloc(weights, GCHandleType.Pinned);

            int[]    hiddenLayers         = desc.HiddenLayersSizes.ToArray();
            GCHandle hiddenLayersCHandle  = GCHandle.Alloc(hiddenLayers, GCHandleType.Pinned);
            GCHandle actFuncsCHandle      = GCHandle.Alloc(desc.ActivationFunctions, GCHandleType.Pinned);
            GCHandle inputDataSetCHandle  = GCHandle.Alloc(inputDS, GCHandleType.Pinned);
            GCHandle outputDataSetCHandle = GCHandle.Alloc(outputDS, GCHandleType.Pinned);

            errors = new float[populationSize];
            GCHandle errorsCHandle   = GCHandle.Alloc(errors, GCHandleType.Pinned);
            Mem      memWeights      = context.CreateBuffer(MemFlags.READ_ONLY | MemFlags.COPY_HOST_PTR, weights.Length * sizeof(float), weightsCHandle.AddrOfPinnedObject());
            Mem      memHiddenLayers = context.CreateBuffer(MemFlags.READ_ONLY | MemFlags.COPY_HOST_PTR, hiddenLayers.Length * sizeof(int), hiddenLayersCHandle.AddrOfPinnedObject());
            Mem      memActFuncs     = context.CreateBuffer(MemFlags.READ_ONLY | MemFlags.COPY_HOST_PTR, desc.ActivationFunctions.Length * sizeof(int), actFuncsCHandle.AddrOfPinnedObject());
            Mem      memInputDS      = context.CreateBuffer(MemFlags.READ_WRITE | MemFlags.COPY_HOST_PTR, inputDS.Length * sizeof(float), inputDataSetCHandle.AddrOfPinnedObject());
            Mem      memOutputDS     = context.CreateBuffer(MemFlags.READ_WRITE, outputDS.Length * sizeof(float) /*, outputDataSetCHandle.AddrOfPinnedObject()*/);
            Mem      memErrors       = context.CreateBuffer(MemFlags.READ_WRITE, populationSize * sizeof(float));

            int argIndex = 0;

            kernelForDataSets.SetArg(argIndex++, memWeights);
            kernelForDataSets.SetArg(argIndex++, weights.Length);
            kernelForDataSets.SetArg(argIndex++, desc.TotalWeights);
            kernelForDataSets.SetArg(argIndex++, desc.InputLayerSize);
            kernelForDataSets.SetArg(argIndex++, desc.OutputLayerSize);
            kernelForDataSets.SetArg(argIndex++, hiddenLayers.Length);
            kernelForDataSets.SetArg(argIndex++, memHiddenLayers);
            kernelForDataSets.SetArg(argIndex++, memActFuncs);
            kernelForDataSets.SetArg(argIndex++, inputDS.Length);
            kernelForDataSets.SetArg(argIndex++, memInputDS);
            kernelForDataSets.SetArg(argIndex++, memOutputDS);
            kernelForDataSets.SetArg(argIndex++, memErrors);
            kernelForDataSets.SetArg(argIndex++, (IntPtr)(desc.MaxLayerSize * sizeof(float)), (IntPtr)null);
            kernelForDataSets.SetArg(argIndex++, (IntPtr)(desc.MaxLayerSize * sizeof(float)), (IntPtr)null);


            IntPtr[] globalWorkSize = new IntPtr[1];
            globalWorkSize[0] = (IntPtr)1;// populationSize;
            IntPtr[] localWorkSize = new IntPtr[1];
            localWorkSize[0] = (IntPtr)1;
            Stopwatch sw = new Stopwatch();

            sw.Start();
            queue.EnqueueNDRangeKernel(kernelForDataSets, 1, null, globalWorkSize, localWorkSize);
            queue.Finish();
            lastTimeResult = sw.ElapsedMilliseconds / 1000.0f;
            sw.Stop();
            queue.EnqueueReadBuffer(memOutputDS, true, 0, outputDS.Length * sizeof(float), outputDataSetCHandle.AddrOfPinnedObject());
            queue.EnqueueReadBuffer(memErrors, true, 0, errors.Length * sizeof(float), errorsCHandle.AddrOfPinnedObject());
        }
Esempio n. 2
0
        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;
        }
Esempio n. 3
0
        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;
        }