static string GenerateHeatmap(List <List <string> > Dataset, List <List <Neuronio> > neuronios, LVQ lvq) { for (int p = 0; p < Dataset.Count; p++) { Neuronio bmu = lvq.BMU(neuronios, Dataset[p]).neuron; int line = bmu.row; int column = bmu.column; neuronios[line][column].color += int.Parse(Dataset[p].Last()) + 1; //incrementa a cor de acordo com a classe neuronios[line][column].totalColorsSet++; //soma quantas vezes foi modificado } string heatmap = string.Empty; heatmap += "HEATMAP\n"; for (int q = 0; q < neuronios.Count; q++) //Linhas { for (int j = 0; j < neuronios[q].Count; j++) //Colunas { heatmap += ("" + (float)neuronios[q][j].color / (float)neuronios[q][j].totalColorsSet); //aqui faz o valor ser proximo de qual classe deve pertencer heatmap += ("\t"); } heatmap += ("\n"); } heatmap += ("\n"); heatmap = heatmap.Replace('.', ','); return(heatmap); }
void updateWeight(List <string> DatasetRow, Neuronio winner, ref List <List <Neuronio> > neurons, float radius, double dp, double learningRate) { if (string.IsNullOrEmpty(winner.currentClass)) //se nao possuir classe, atribui classe do row { neurons[winner.row][winner.column].currentClass = DatasetRow.Last(); } for (int i = 0; i < neurons.Count; i++) //Percorre linha da matriz { for (int j = 0; j < neurons[i].Count; j++) //Percorre coluna da matriz { double dist = getNeuronDistance(winner, neurons[i][j]); //Calcula a distancia para o neuronio i,j if (dist <= radius) //Se a distancia eh menor q o raio, deve-se atualizar os pesos { //if (string.IsNullOrEmpty(neurons[i][j].currentClass)) //se nao possuir classe, atribui classe do row // neurons[neurons[i][j].row][neurons[i][j].column].currentClass = DatasetRow.Last(); double gaussian = gaussianMath(dist, dp); for (int w = 0; w < neurons[i][j].pesos.Count; w++) { double lastWeight = neurons[i][j].pesos[w]; double X = double.Parse(DatasetRow[w]); double newWeight = 0; if (winner.currentClass == DatasetRow.Last()) //Eh a mesma classe que a escolhida { newWeight = lastWeight + learningRate * gaussian * (X - lastWeight); } else //Classe diferente, formula diferente { newWeight = lastWeight - learningRate * gaussian * (X - lastWeight); } if (double.IsInfinity(newWeight) || double.IsNaN(newWeight)) { newWeight = 0; } //else if (newWeight > 1) newWeight = 1; //else if (newWeight < 0) newWeight = -1; neurons[i][j].pesos[w] = newWeight; } } } } }
// Verificar se precisa manter essa versao da distancia euclidiana. public double NewGetEuclideanDistance(List <string> trainingLine, Neuronio neuron) { double distance = 0; for (int i = 0; i < trainingLine.Count - 1; i++) { double Training = 0; if (!double.TryParse(trainingLine[i], out Training)) { Console.WriteLine("ERROR: Unable to cast string to double."); } distance += Math.Pow((Training - neuron.pesos[i]), 2); } return(Math.Sqrt(distance)); }
static void ExecuteLVQ(string path) { string fileName = path.Split('/').Last().Split('-')[0]; LVQ _lvq = new LVQ(); List <List <string> > Dataset = _lvq.LoadCSVData(path); List <List <Neuronio> > Neuronios = new List <List <Neuronio> >(); int sizeOfNetwork = GetSizeOfNetwork(GetTotalDistinctClasses(Dataset)); //Define o tamanho N da rede neural int totalEntries = Dataset[0].Count - 1; string CrossValidationErrors = string.Empty; for (int i = 1; i <= 4; i++) //Executa os 4 tipos de R { Console.Write("Iniciando, i = {0}\n", i.ToString()); int StartIndex = 0; List <List <string> > trainingSet, testingSet; List <float> listOfErroAmostral = new List <float>(); int etapa = 1; while (StartIndex < Dataset.Count) //REVER, acredito q esteja no local errado { StartIndex = _lvq.GetDatasets(StartIndex, Dataset, out testingSet, out trainingSet); //inicializa as Constantes float radius = GetRadius(i, sizeOfNetwork); float initial_dp = radius; double t1 = Math.Log10(initial_dp) / 1000; float n0 = 0.1f; int t2 = 1000; Random rnd = new Random(); Neuronios.Clear(); //Inicia uma nova matriz de Neuronios NxN for (int k = 0; k < sizeOfNetwork; k++) //Linhas { Neuronios.Add(new List <Neuronio>()); for (int j = 0; j < sizeOfNetwork; j++) //Colunas { Neuronio neuron = new Neuronio(); neuron.pesos = new List <double>(); neuron.currentClass = string.Empty; neuron.row = k; neuron.column = j; neuron.totalColorsSet = 1; for (int l = 0; l < totalEntries; l++) { neuron.pesos.Add(rnd.NextDouble()); } Neuronios[k].Add(neuron); } } for (int n = 0; n < 500; n++) //numero de iteraçoes para aprendizado { Console.Write("\retapa {0} Iteracao {1} / {2}... {3}%", etapa, n + 1, 500, Math.Round(((float)(n + 1) / (float)500) * 100)); double learningRate = n0 * Math.Pow(Math.E, ((double)-n / (double)t2)); if (learningRate < 0.01f) { learningRate = 0.01f; } double dp = initial_dp * Math.Pow(Math.E, ((double)-n * t1)); _lvq.RunLVQ(Neuronios, trainingSet, radius, dp, learningRate); } List <string> predictions = new List <string>(); for (int x = 0; x < testingSet.Count; x++) { string result = string.Empty; NewDistance prediction = _lvq.BMU(Neuronios, testingSet[x]); predictions.Add(prediction.neuron.currentClass); } // Guarda erro amostral da linha listOfErroAmostral.Add(CrossValidation.erroAmostral(testingSet, predictions)); CrossValidation.prepareConfusionMatrix(Dataset, testingSet, predictions); if (etapa == 10) { Console.WriteLine("Para R = {0}\n", radius); string heatmap = string.Empty; //DEBUG heatmap += "HEATMAP\n"; for (int q = 0; q < sizeOfNetwork; q++) //Linhas { for (int j = 0; j < sizeOfNetwork; j++) //Colunas { if (!string.IsNullOrEmpty(Neuronios[q][j].currentClass)) { heatmap += ("" + Neuronios[q][j].currentClass); } else { heatmap += (" "); } //for (int k = 0; k < totalEntries; k++) // Console.Write(Neuronios[q][j].pesos[k] + " "); heatmap += ("\t"); } heatmap += ("\n"); } heatmap += ("\n"); Console.WriteLine(heatmap); FileSystem.SaveFileContents(heatmap, @"../../Raw Data/Normalized/output/" + fileName + "/", fileName + "-Heatmap-" + i.ToString() + ".txt"); } etapa++; } if (CrossValidation.binaryConfusionMatrix.Count > 0) //Se for matriz binaria ira salvar os dados aqui { FileSystem.SaveFileContents(CrossValidation.GeraMatrizBinaria(), @"../../Raw Data/Normalized/output/" + fileName + "/", fileName + "-Matriz-Binaria-Confusao-" + i.ToString() + ".txt"); } if (CrossValidation.multiClassConfusionMatrix.Count > 0) //Se for matriz multi-classe irá salvar aqui { FileSystem.SaveFileContents(CrossValidation.GeraMatrizMultiClasse(), @"../../Raw Data/Normalized/output/" + fileName + "/", fileName + "-Matriz-MultiClasse-Confusao-" + i.ToString() + ".txt"); } CrossValidationErrors += "Erro de validação cruzada para R = " + GetRadius(i, sizeOfNetwork).ToString() + "\t" + (CrossValidation.erroDeValidacaoCruzada(listOfErroAmostral) * 100) + "%\n"; } FileSystem.SaveFileContents(CrossValidationErrors, @"../../Raw Data/Normalized/output/" + fileName + "/", fileName + "-CROSSVALIDATION REPORT.txt"); }
public NewDistance(Neuronio neuron, double distance, List <string> datasetRow) { this.neuron = neuron; this.distance = distance; this.datasetRow = datasetRow; }
double getNeuronDistance(Neuronio winner, Neuronio neighbor) { return(Math.Sqrt(Math.Pow(winner.row - neighbor.row, 2) + Math.Pow(winner.column - neighbor.column, 2))); }