Exemplo n.º 1
0
        /*
         * Dado um padrão de entrada, calcula o neurônio vencedor
         */
        private KohonenNeuron CalculateWinnerNeuron(DataSetObject pattern)
        {
            double        smallestDist = -1;
            KohonenNeuron winner       = new KohonenNeuron(inputLayerSize);

            //Para cada neurônio da camada competitiva
            for (int i = 0; i < competitiveNeuronLength; i++)
            {
                for (int j = 0; j < competitiveNeuronLength; j++)
                {
                    KohonenNeuron neuron = competitiveLayer[i, j];

                    //Calcula a distância euclidiana
                    double dist = neuron.GetEuclideanDistance(pattern);
                    //Primeiro neurônio sempre é o primeiro vencedor
                    if (smallestDist == -1)
                    {
                        smallestDist = dist;
                        winner       = neuron;
                    }
                    //Se a distância for a menor que a distância do último vencedor, temos um novo vencedor
                    if (dist < smallestDist)
                    {
                        smallestDist = dist;
                        winner       = neuron;
                    }
                }
            }

            return(winner);
        }
Exemplo n.º 2
0
        public void Train(Predictor predictor, double[] weightArray, DataSetObject dataSet, double learningRate)
        {
            int weightCount = weightArray.Length;

            double[] tmpWeightArray = new double[weightCount];

            foreach (var item in dataSet.DataSet)
            {
                DataObject data = item.Value;

                double DifferentialPerRow = (double)predictor.GetDifferential(item.Value, weightArray);

                double[] tmpFeature = data.Feature.GetFeature();

                for (int i = 0; i < weightCount; i++)
                {
                    tmpWeightArray[i] += DifferentialPerRow * tmpFeature[i];
                }
            }

            for (int i = 0; i < weightCount; i++)
            {
                weightArray[i] -= (tmpWeightArray[i] / dataSet.RowCount) * learningRate;
            }
        }
Exemplo n.º 3
0
        /*
         * Calcula distância euclidiana do padrão de entrada
         */
        public double GetEuclideanDistance(DataSetObject pattern)
        {
            double dist = 0;

            if (pattern.GetInputLenght() != weights.Length)
            {
                throw new Exception("Incorrect data format!");
            }
            else
            {
                double[] inputWeights = pattern.GetInput();

                if (weights.Length != inputWeights.Length)
                {
                    throw new Exception("Incorrect data format!");
                }
                else
                {
                    for (int x = 0; x < weights.Length; x++)
                    {
                        dist += ((inputWeights[x] - weights[x]) * (inputWeights[x] - weights[x]));
                    }
                }
            }
            return(dist);
        }
Exemplo n.º 4
0
        internal void Train(DataSetObject dataSet)
        {
            _optimizer.Train(_predictor, WeightArray, dataSet);

            _weightHistory.RecordHistory((double[])WeightArray.Clone());
            _costHistory.RecordHistory(_predictor.GetTotalCost(WeightArray, dataSet));
        }
Exemplo n.º 5
0
 public KohonenNeuron RecognizeWinnerNeuron(double[] input)
 {
     if (input.Count() != this.inputLayerSize)
     {
         throw new Exception("Incorrect data format!");
     }
     else
     {
         DataSetObject obj = new DataSetObject(input);
         //Calcula neurônio vencedor
         return(CalculateWinnerNeuron(obj));
     }
 }
Exemplo n.º 6
0
        /// <summary>
        /// MethodOfLeastSquares
        /// </summary>
        /// <param name="value"></param>
        /// <param name="weightArray"></param>
        /// <returns></returns>
        internal override object GetTotalCost(double[] weightArray, DataSetObject dataSet)
        {
            double totalCost = 0;

            foreach (var item in dataSet.DataSet)
            {
                totalCost += Math.Pow((double)GetDifferential(item.Value, weightArray), 2);
            }

            totalCost /= 2 * dataSet.RowCount;

            return(totalCost);
        }
Exemplo n.º 7
0
        /*
         * Dado um padrão de entrada e uma variável de aprendizado, atualiza os pesos do neurônio
         */
        public void UpdateWeights(DataSetObject pattern, double learningRate)
        {
            if (pattern.GetInputLenght() != weights.Length)
            {
                throw new Exception("Incorrect data format!");
            }
            else
            {
                double[] inputWeights = pattern.GetInput();

                for (int i = 0; i < inputWeights.Length; i++)
                {
                    weights[i] += learningRate * (inputWeights[i] - weights[i]);
                }
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// MethodOfMaximumLikehood
        /// </summary>
        /// <param name="weightArray"></param>
        /// <param name="dataSet"></param>
        /// <returns></returns>
        internal override object GetTotalCost(double[] weightArray, DataSetObject dataSet)
        {
            double totalCost = 0;

            foreach (var item in dataSet.DataSet)
            {
                FeatureObject tmpFeature = item.Value.Feature;

                double predictResult = (double)Predict(tmpFeature, weightArray);
                double label         = (double)item.Value.Label.Label;

                totalCost += label * Math.Log10(predictResult) - (1 - label) * Math.Log(1 - predictResult);
            }

            totalCost /= dataSet.RowCount;

            return(totalCost);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Data initialize
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_SetData_Click(object sender, RoutedEventArgs e)
        {
            string filePath = null;

            OpenFileDialog openFileDialog = new OpenFileDialog();

            if (openFileDialog.ShowDialog() == true)
            {
                filePath = openFileDialog.FileName;

                _dataSet = new DataSetObject(filePath);
            }

            _something = new Something((KindOfSomething)Enum.Parse(typeof(KindOfSomething), comboBox_Filter.Text)
                                       , double.Parse(textBox_LearningRate.Text), _dataSet.FeatureCount);

            _drawObjector = new DrawObjector();
        }
Exemplo n.º 10
0
        /*
         * Método de aprendizado da Rede
         */
        public void Learn(DataSet trainingSet)
        {
            if (trainingSet.GetOutputSize() != this.outputLayerSize || trainingSet.GetInputSize() != this.inputLayerSize)
            {
                throw new Exception("Incorrect data format!");
            }
            else
            {
                bool learned = false;
                List <DataSetObject> patterns = trainingSet.GetList();
                for (int it = 1; learned == false && it <= maxIterationNumber; it++)
                {
                    //VAI APRESENTANDO OS PADRÕES, DE FORMA ALTERNADA, ATÉ QUE TODOS ATINJAM O ERRO REQUERIDO.
                    int allPatternOK = 0;
                    for (int p = 0; p < patterns.Count; p++)
                    {
                        DataSetObject pattern = patterns[p];

                        //INJETA O PADRÃO NA ENTRADA DA REDE.
                        ApplyPattern(pattern.GetInput());

                        //PROPAGA
                        DoPropagation();

                        if (CalculateStopError(pattern.GetTargetOutput()) > this.Error)
                        {
                            DoBackPropagation(pattern.GetTargetOutput());
                        }
                        else
                        {
                            allPatternOK++;
                        }

                        if (allPatternOK == patterns.Count)
                        {
                            learned = true;
                        }

                        iterationNumber = it;
                    }
                }
            }
        }
Exemplo n.º 11
0
        public Int32 OneHotLeaveOneOutStep(DistanceFunction distance, KernelFunction kernel, Window window, Int32 controlIndex)
        {
            Double maxClassValue = Double.MinValue;
            Int32  index         = Int32.MinValue;

            for (Int32 i = 0; i < _classCount; i++)
            {
                Int32 currentClass = i;

                DataSetObject[] currentClassRows = new DataSetObject[_dataSet.Length - 1];
                Int32           newRowsIndex     = 0;
                for (Int32 j = 0; j < _dataSet.Length; j++) //ОпТиМиЗаЦиЯ
                {
                    if (j == controlIndex)
                    {
                        continue;
                    }

                    var rowObject = _dataSet[j];

                    currentClassRows[newRowsIndex] = new DataSetObject(rowObject.Features, rowObject.Label == currentClass ? 1 : 0);
                    newRowsIndex++;
                }

                var result = new DataSet(currentClassRows)
                             .GetPredictkNN(_dataSet[controlIndex].Features, kernel, distance, window);

                if (result > maxClassValue)
                {
                    maxClassValue = result;
                    index         = i;
                }
            }

            return(index);
        }
Exemplo n.º 12
0
 internal void Train(DataSetObject dataSet)
 {
     _hypothesis.Train(dataSet);
 }
Exemplo n.º 13
0
 internal void Train(Predictor predictor, double[] weightArray, DataSetObject dataSet)
 {
     _optimizer.Train(predictor, weightArray, dataSet, _learningRate);
 }
Exemplo n.º 14
0
 abstract internal object GetTotalCost(double[] weightArray, DataSetObject dataSet);