Exemple #1
0
        public MapPopulation(int maxPopulation)
        {
            _maxPopulation   = maxPopulation;
            _populationDeque = new IntervalHeap <Shard>(new ShardComparer());
            _featureMap      = new FeatureMap(NUM_FEATURES, REMAP_FREQUENCY,
                                              NUM_GROUPS_PER_FEATURE);

            _maxWins    = 0;
            _maxFitness = Int32.MinValue;

            _champion_log =
                new RunningIndividualLog(CHAMPION_LOG_FILENAME);
            _fittest_log =
                new RunningIndividualLog(FITTEST_LOG_FILENAME);
            _map_log =
                new FrequentMapLog(ELITE_MAP_FILENAME, _featureMap);
        }
        public FrequentMapLog(string logPath, FeatureMap map)
        {
            _logPath = logPath;
            _map     = map;

            // Create a log for individuals
            using (FileStream ow = File.Open(logPath,
                                             FileMode.Create, FileAccess.Write, FileShare.None))
            {
                string[] dataLabels =
                {
                    "Dimensions",
                    "Map (f1xf2:Size:Individual:Fitness:NormFitness:Feature1:Feature2)"
                };

                WriteText(ow, string.Join(",", dataLabels));
                ow.Close();
            }
        }
Exemple #3
0
        public RandomDirectionEmitter(EmitterParams emParams, FeatureMap featureMap, int numParams)
        {
            _numParams = numParams;
            _params    = emParams;

            NumReleased            = 0;
            _generation            = 0;
            _populationCount       = 0;
            _individualsDispatched = 0;
            _parents    = new List <Individual>();
            _featureMap = featureMap;

            if (_params.PopulationSize == -1)
            {
                _params.PopulationSize = (int)(4.0 + Math.Floor(3.0 * Math.Log(_numParams)));
            }

            reset();
        }
Exemple #4
0
        public List <int> MapFeatures(string[] features)
        {
            var posFeatureIds = new List <int>();

            foreach (var feature in features)
            {
                if (FeatureMap.ContainsKey(feature))
                {
                    posFeatureIds.Add(FeatureMap[feature]);
                }
                else
                {
                    FeatureMap.Add(feature, EntryCount);
                    posFeatureIds.Add(EntryCount);
                    EntryCount++;
                }
            }
            return(posFeatureIds);
        }
    void ShowFeatureMaps()
    {
        FeatureMapLabel.Show();
        FeatureMap.Show();

        FeatureMapI.Show();
        FeatureMapILabel.Show();
        FeatureMapIText.Show();

        FeatureMapJ.Show();
        FeatureMapJLabel.Show();
        FeatureMapJText.Show();

        FeatureMapXText.Show();
        FeatureMapXLabel.Show();
        FeatureMapYText.Show();
        FeatureMapYLabel.Show();

        BiasMap.Show();
        BiasLabel.Show();
    }
    void HideFeatureMaps()
    {
        FeatureMapLabel.Hide();
        FeatureMap.Hide();

        FeatureMapI.Hide();
        FeatureMapILabel.Hide();
        FeatureMapIText.Hide();

        FeatureMapJ.Hide();
        FeatureMapJLabel.Hide();
        FeatureMapJText.Hide();

        FeatureMapXText.Hide();
        FeatureMapXLabel.Hide();
        FeatureMapYText.Hide();
        FeatureMapYLabel.Hide();

        BiasMap.Hide();
        BiasLabel.Hide();
    }
Exemple #7
0
 public override void doFeedForward(Layer prev)
 {
     featureMaps = new FeatureMap[prev.featureMaps.Length];
     filters     = prev.filters;
     for (int i = 0; i < featureMaps.Length; i++)
     {
         featureMaps[i] = new FeatureMap()
         {
             map = Matrix.map(Activation.lrelu, prev.featureMaps[i].map)
         };
         for (int j = 0; j < featureMaps[i].width; j++)
         {
             for (int k = 0; k < featureMaps[i].height; k++)
             {
                 if (float.IsInfinity(featureMaps[i].map.data[j, k]))
                 {
                     Console.WriteLine("ja");
                 }
             }
         }
     }
 }
Exemple #8
0
        private void initMap()
        {
            var mapSizer = new LinearMapSizer(_params.Map.StartSize,
                                              _params.Map.EndSize);

            if (_params.Map.Type.Equals("FixedFeature"))
            {
                _featureMap = new FixedFeatureMap(_params.Search.NumToEvaluate,
                                                  _params.Map, mapSizer);
            }
            else if (_params.Map.Type.Equals("SlidingFeature"))
            {
                _featureMap = new SlidingFeatureMap(_params.Search.NumToEvaluate,
                                                    _params.Map, mapSizer);
            }
            else
            {
                Console.WriteLine("ERROR: No feature map specified in config file.");
            }

            _map_log = new FrequentMapLog("logs/elite_map_log.csv", _featureMap);
        }
        public MapSummaryLog(string logPath, FeatureMap map)
        {
            _logPath = logPath;
            _map     = map;

            // Create a log for individuals
            using (FileStream ow = File.Open(logPath,
                                             FileMode.Create, FileAccess.Write, FileShare.None))
            {
                string[] dataLabels =
                {
                    "NumEvaluated",
                    "QD-Score",
                    "MeanNormFitness",
                    "MedianNormFitness",
                    "CellsOccupied",
                    "PercentOccupied",
                    "MaxNormFitness"
                };

                WriteText(ow, string.Join(",", dataLabels));
                ow.Close();
            }
        }
Exemple #10
0
        void ReleaseDesignerOutlets()
        {
            if (FeatureMap != null)
            {
                FeatureMap.Dispose();
                FeatureMap = null;
            }

            if (GetImage != null)
            {
                GetImage.Dispose();
                GetImage = null;
            }

            if (imageLabel != null)
            {
                imageLabel.Dispose();
                imageLabel = null;
            }

            if (ImageViewer != null)
            {
                ImageViewer.Dispose();
                ImageViewer = null;
            }

            if (messageLabel != null)
            {
                messageLabel.Dispose();
                messageLabel = null;
            }

            if (ProcessImage != null)
            {
                ProcessImage.Dispose();
                ProcessImage = null;
            }

            if (ResetImage != null)
            {
                ResetImage.Dispose();
                ResetImage = null;
            }

            if (Root != null)
            {
                Root.Dispose();
                Root = null;
            }

            if (switchSwitch != null)
            {
                switchSwitch.Dispose();
                switchSwitch = null;
            }

            if (textAperture != null)
            {
                textAperture.Dispose();
                textAperture = null;
            }

            if (textThreshold1 != null)
            {
                textThreshold1.Dispose();
                textThreshold1 = null;
            }

            if (textThreshold2 != null)
            {
                textThreshold2.Dispose();
                textThreshold2 = null;
            }
        }
Exemple #11
0
        public override void doFeedForward(Layer prev)
        {
            //throw new NotImplementedException();

            int width  = (prev.featureMaps[0].width - filterWidth + (2 * padding)) / stride + 1;
            int height = (prev.featureMaps[0].height - filterHeight + (2 * padding)) / stride + 1;

            featureMaps = new FeatureMap[prev.filters.Length];

            for (int f = 0; f < prev.filters.Length; f++)
            {
                int mapX = 0;
                int mapY = 0;

                featureMaps[f] = new FeatureMap()
                {
                    map = new Matrix(width, height)
                };


                for (int i = -padding; i < width + padding; i += stride)
                {
                    for (int j = -padding; j < height + padding; j += stride)
                    {
                        float sum = 0;
                        for (int d = 0; d < prev.filters[f].dimensions; d++)
                        {
                            Matrix flipped = prev.filters[f].kernels[d].flip();

                            for (int k = 0; k < prev.filterWidth; k++)
                            {
                                for (int l = 0; l < prev.filterHeight; l++)
                                {
                                    if (!(i + k >= prev.featureMaps[d].width || i + k < 0 || j + l >= prev.featureMaps[d].height || j + l < 0))
                                    {
                                        sum +=
                                            prev.featureMaps[d].map.data[i + k, j + l] *
                                            flipped.data[k, l];
                                    }
                                    if (float.IsInfinity(flipped.data[k, l]) || float.IsNaN(flipped.data[k, l]))
                                    {
                                        Console.WriteLine("moi");
                                    }
                                    if (float.IsInfinity(prev.featureMaps[d].map.data[i + k, j + l]) || float.IsNaN(prev.featureMaps[d].map.data[i + k, j + l]))
                                    {
                                        Console.WriteLine("moi");
                                    }
                                    if (float.IsInfinity(sum) || float.IsNaN(sum))
                                    {
                                        Console.WriteLine("moi");
                                    }
                                }
                            }
                        }

                        featureMaps[f].map.data[mapX, mapY] = sum;

                        if (float.IsInfinity(featureMaps[f].map.data[mapX, mapY]) || float.IsNaN(featureMaps[f].map.data[mapX, mapY]))
                        {
                            Console.WriteLine("jo");
                        }

                        mapY++;
                    }

                    mapX++;
                    mapY = 0;
                }
            }
        }
Exemple #12
0
 public LWClassifier(McoModel mcoModel, string prefixFileName, bool _excludeNullValues)
 {
     model             = (MaltLibModel)mcoModel.GetMcoEntryObject(prefixFileName + ".moo");
     featureMap        = (FeatureMap)mcoModel.GetMcoEntryObject(prefixFileName + ".map");
     excludeNullValues = _excludeNullValues;
 }
Exemple #13
0
        public void Train(int numEpochs, double learningRate, int batchSize)
        {
            double trainingError = 0;
            object olock         = new object();

            for (int i = 0; i < numEpochs; i++)
            {
                trainingError = 0;
                RandomizeInputs();
                int dj = 0; // data index
                for (int j = 0; j < InputDataList.Count / batchSize; j++)
                {
                    dj = j * batchSize;
                    //Stopwatch sw = new Stopwatch();
                    //sw.Start();
                    double[] trainingErr = new double[batchSize];
                    Parallel.For(0, batchSize, (b) =>
                                 //for (int b = 0; b < batchSize; b++)
                    {
                        // do forward pass
                        Matrix res = this.Evaluate(InputDataList[dj + b], b); //res is the self.a in last layer of NN at batch index

                        // compute training error (y-a)^2
                        for (int h = 0; h < res.Rows; h++)
                        {
                            trainingErr[b] += (res.D[h][0] - OutputLabels[dj + b].D[h][0])
                                              * (res.D[h][0] - OutputLabels[dj + b].D[h][0]);
                        }

                        // ----------compute deltas on regular NN layers--------------
                        for (int count = LayerList.Count - 1; count >= 0; count--)
                        {
                            var layer = LayerList[count];
                            if (count == (LayerList.Count - 1))                               // last layer
                            {
                                layer.Delta[b] = (OutputLabels[dj + b] - layer.A[b]).Mul(-1); // for softmax by default
                                if (layer.activationType == ActivationType.SIGMOID)
                                {
                                    layer.Delta[b] = layer.Delta[b].ElementByElementMul(layer.APrime[b]);
                                }
                                if (layer.activationType == ActivationType.RELU)
                                {
                                    for (int m = 0; m < layer.numNeurons; m++)
                                    {
                                        if (layer.Sum[b].D[m][0] < 0)
                                        {
                                            layer.Delta[b].D[m][0] = 0;
                                        }
                                    }
                                }
                            }
                            else  // previous layer
                            {
                                layer.Delta[b] = LayerList[count + 1].W.Transpose() * LayerList[count + 1].Delta[b];
                                //-------apply dropout----------------------
                                if (layer.Dropout < 1.0)
                                {
                                    layer.Delta[b] = layer.Delta[b].ElementByElementMul(layer.DropM[b]);
                                }
                                //--------------------------------------------

                                if (layer.activationType == ActivationType.SIGMOID)
                                {
                                    layer.Delta[b] = layer.Delta[b].ElementByElementMul(layer.APrime[b]);
                                }
                                if (layer.activationType == ActivationType.RELU)
                                {
                                    for (int m = 0; m < layer.numNeurons; m++)
                                    {
                                        if (layer.Sum[b].D[m][0] < 0)
                                        {
                                            layer.Delta[b].D[m][0] = 0;
                                        }
                                    }
                                }
                            }
                            layer.GradB[b] = layer.GradB[b] + layer.Delta[b];
                            if (count == 0)                                                                  // first NN layer connected to CNN last layer via Flatten
                            {
                                layer.GradW[b] = layer.GradW[b] + (layer.Delta[b] * Flatten[b].Transpose()); // flatten = previous output
                            }
                            else
                            {
                                layer.GradW[b] = layer.GradW[b] + (layer.Delta[b] * LayerList[count - 1].A[b].Transpose());
                            }
                        }
                        // compute delta on the output of SS (flat) layer of all feature maps
                        Matrix deltaSSFlat = this.LayerList[0].W.Transpose() * this.LayerList[0].Delta[b];

                        // do reverse flattening and distribute the deltas on
                        // each feature map's SS (SubSampling layer)
                        int index = 0;
                        // last CNN layer
                        foreach (FeatureMap fmp in CNNLayerList[CNNLayerList.Count - 1].FeatureMapList)
                        {
                            fmp.DeltaSS[b] = new Matrix(fmp.OutPutSS[b].Rows, fmp.OutPutSS[b].Cols);
                            for (int m = 0; m < fmp.OutPutSS[b].Rows; m++)
                            {
                                for (int n = 0; n < fmp.OutPutSS[b].Cols; n++)
                                {
                                    fmp.DeltaSS[b].D[m][n] = deltaSSFlat.D[index][0];
                                    index++;
                                }
                            }
                        }
                        // process CNN layers in reverse order, from last layer towards input
                        for (int cnnCount = CNNLayerList.Count - 1; cnnCount >= 0; cnnCount--)
                        {
                            // compute deltas on the C layers - distrbute deltas from SS layer
                            // then multiply by the activation function
                            //foreach (FeatureMap fmp in CNNLayerList[cnnCount].FeatureMapList)
                            //Parallel.For(0, CNNLayerList[cnnCount].FeatureMapList.Count, (k) =>
                            for (int k = 0; k < CNNLayerList[cnnCount].FeatureMapList.Count; k++)
                            {
                                FeatureMap fmp = CNNLayerList[cnnCount].FeatureMapList[k];
                                int indexm     = 0; int indexn = 0;
                                fmp.DeltaCV[b] = new Matrix(fmp.OutPutSS[b].Rows * 2, fmp.OutPutSS[b].Cols * 2);
                                for (int m = 0; m < fmp.DeltaSS[b].Rows; m++)
                                {
                                    indexn = 0;
                                    for (int n = 0; n < fmp.DeltaSS[b].Cols; n++)
                                    {
                                        if (fmp.activationType == ActivationType.SIGMOID)
                                        {
                                            fmp.DeltaCV[b].D[indexm][indexn]         = (1 / 4.0) * fmp.DeltaSS[b].D[m][n] * fmp.APrime[b].D[indexm][indexn];
                                            fmp.DeltaCV[b].D[indexm][indexn + 1]     = (1 / 4.0) * fmp.DeltaSS[b].D[m][n] * fmp.APrime[b].D[indexm][indexn + 1];
                                            fmp.DeltaCV[b].D[indexm + 1][indexn]     = (1 / 4.0) * fmp.DeltaSS[b].D[m][n] * fmp.APrime[b].D[indexm + 1][indexn];
                                            fmp.DeltaCV[b].D[indexm + 1][indexn + 1] = (1 / 4.0) * fmp.DeltaSS[b].D[m][n] * fmp.APrime[b].D[indexm + 1][indexn + 1];
                                            indexn = indexn + 2;
                                        }
                                        if (fmp.activationType == ActivationType.RELU)
                                        {
                                            if (fmp.Sum[b].D[indexm][indexn] > 0)
                                            {
                                                fmp.DeltaCV[b].D[indexm][indexn] = (1 / 4.0) * fmp.DeltaSS[b].D[m][n];
                                            }
                                            else
                                            {
                                                fmp.DeltaCV[b].D[indexm][indexn] = 0;
                                            }
                                            if (fmp.Sum[b].D[indexm][indexn + 1] > 0)
                                            {
                                                fmp.DeltaCV[b].D[indexm][indexn + 1] = (1 / 4.0) * fmp.DeltaSS[b].D[m][n];
                                            }
                                            else
                                            {
                                                fmp.DeltaCV[b].D[indexm][indexn + 1] = 0;
                                            }
                                            if (fmp.DeltaCV[b].D[indexm + 1][indexn] > 0)
                                            {
                                                fmp.DeltaCV[b].D[indexm + 1][indexn] = (1 / 4.0) * fmp.DeltaSS[b].D[m][n];
                                            }
                                            else
                                            {
                                                fmp.DeltaCV[b].D[indexm + 1][indexn] = 0;
                                            }
                                            if (fmp.DeltaCV[b].D[indexm + 1][indexn + 1] > 0)
                                            {
                                                fmp.DeltaCV[b].D[indexm + 1][indexn + 1] = (1 / 4.0) * fmp.DeltaSS[b].D[m][n];
                                            }
                                            else
                                            {
                                                fmp.DeltaCV[b].D[indexm + 1][indexn + 1] = 0;
                                            }
                                            indexn = indexn + 2;
                                        }
                                    }
                                    indexm = indexm + 2;
                                }
                            }

                            //----------compute BiasGrad in current CNN Layer-------
                            foreach (FeatureMap fmp in CNNLayerList[cnnCount].FeatureMapList)
                            {
                                for (int u = 0; u < fmp.DeltaCV[b].Rows; u++)
                                {
                                    for (int v = 0; v < fmp.DeltaCV[b].Cols; v++)
                                    {
                                        lock (olock)
                                        {
                                            fmp.BiasGrad += fmp.DeltaCV[b].D[u][v];
                                        }
                                    }
                                }
                            }
                            //----------compute gradients for pxq kernels in current CNN layer--------
                            if (cnnCount > 0)  // not the first CNN layer
                            {
                                for (int p = 0; p < CNNLayerList[cnnCount - 1].FeatureMapList.Count; p++)
                                //Parallel.For(0, CNNLayerList[cnnCount - 1].FeatureMapList.Count, (p) =>
                                {
                                    for (int q = 0; q < CNNLayerList[cnnCount].FeatureMapList.Count; q++)
                                    {
                                        lock (olock)
                                        {
                                            CNNLayerList[cnnCount].KernelGrads[p, q] = CNNLayerList[cnnCount].KernelGrads[p, q] +
                                                                                       CNNLayerList[cnnCount - 1].FeatureMapList[p].OutPutSS[b].RotateBy90().RotateBy90().Convolution(CNNLayerList[cnnCount].FeatureMapList[q].DeltaCV[b]);
                                        }
                                    }
                                }
                                //---------------this layer is done, now backpropagate to prev CNN Layer----------
                                for (int p = 0; p < CNNLayerList[cnnCount - 1].FeatureMapList.Count; p++)
                                //Parallel.For(0, CNNLayerList[cnnCount - 1].FeatureMapList.Count, (p) =>
                                {
                                    int size = CNNLayerList[cnnCount - 1].FeatureMapList[p].OutPutSS[b].Rows;
                                    CNNLayerList[cnnCount - 1].FeatureMapList[p].DeltaSS[b] = new Matrix(size, size);
                                    //CNNLayerList[cnnCount - 1].FeatureMap2List[p].DeltaSS.Clear();
                                    for (int q = 0; q < CNNLayerList[cnnCount].FeatureMapList.Count; q++)
                                    {
                                        CNNLayerList[cnnCount - 1].FeatureMapList[p].DeltaSS[b] = CNNLayerList[cnnCount - 1].FeatureMapList[p].DeltaSS[b] +
                                                                                                  CNNLayerList[cnnCount].FeatureMapList[q].DeltaCV[b].ConvolutionFull(
                                            CNNLayerList[cnnCount].Kernels[p, q].RotateBy90().RotateBy90());
                                    }
                                }
                            }
                            else  // very first CNN layer which is connected to input
                            {     // has 1xnumFeaturemaps 2-D array of Kernels and Kernel Gradients
                                //----------compute gradient for first layer cnn kernels--------
                                for (int p = 0; p < 1; p++)
                                {
                                    for (int q = 0; q < CNNLayerList[cnnCount].FeatureMapList.Count; q++)
                                    {
                                        lock (olock)
                                        {
                                            CNNLayerList[cnnCount].KernelGrads[p, q] = CNNLayerList[cnnCount].KernelGrads[p, q] +
                                                                                       InputDataList[dj + b].RotateBy90().RotateBy90().Convolution(CNNLayerList[cnnCount].FeatureMapList[q].DeltaCV[b]);
                                        }
                                    }
                                }
                            }
                        }
                    }); // end parallel b loop
                    //sw.Stop();
                    //Console.WriteLine("Time per batch = " + sw.ElapsedMilliseconds);
                    //Stopwatch sw = new Stopwatch();
                    //sw.Start();
                    trainingError += trainingErr.Sum();
                    UpdateKernelsWeightsBiases(learningRate, batchSize);
                    ClearGradients();
                    //sw.Stop();
                    //Console.WriteLine("Time per batch = " + sw.ElapsedMilliseconds);
                }

                if (i % 10 == 0)
                {
                    learningRate = learningRate / 2;  // reduce learning rate
                }
                Console.WriteLine("epoch = " + i.ToString() + " training error = " + trainingError.ToString());
            }
        }