/* * 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); }
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; } }
/* * 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); }
internal void Train(DataSetObject dataSet) { _optimizer.Train(_predictor, WeightArray, dataSet); _weightHistory.RecordHistory((double[])WeightArray.Clone()); _costHistory.RecordHistory(_predictor.GetTotalCost(WeightArray, dataSet)); }
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)); } }
/// <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); }
/* * 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]); } } }
/// <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); }
/// <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(); }
/* * 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; } } } }
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); }
internal void Train(DataSetObject dataSet) { _hypothesis.Train(dataSet); }
internal void Train(Predictor predictor, double[] weightArray, DataSetObject dataSet) { _optimizer.Train(predictor, weightArray, dataSet, _learningRate); }
abstract internal object GetTotalCost(double[] weightArray, DataSetObject dataSet);