/* * Método de aprendizado da Rede */ public void Learn(DataSet trainingSet) { if (trainingSet.GetOutputSize() != 0 || trainingSet.GetInputSize() != this.inputLayerSize) { throw new Exception("Incorrect data format!"); } else { List <DataSetObject> dataset = trainingSet.GetList(); //Para cada iteração for (int itN = 0; itN < iterationNumber; itN++) { //Para cada entrada for (int inputN = 0; inputN < trainingSet.Length(); inputN++) { //Calcula neurônio vencedor KohonenNeuron neuro = CalculateWinnerNeuron(dataset[inputN]); //Atualiza pesos do neurônio vencedor e de seus vizinhos List <KohonenNeuron> winners = GetNeighbors(neuro); for (int x = 0; x < winners.Count; x++) { winners[x].UpdateWeights(dataset[inputN], learningRate); } } } } }
/* * 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); }
private int[] FindIndex(KohonenNeuron neuron) { int[] IJ = new int[2]; for (int i = 0; i < competitiveLayer.GetLength(0); i++) { for (int j = 0; j < competitiveLayer.GetLength(1); j++) { if (neuron == competitiveLayer[i, j]) { IJ[0] = i; IJ[1] = j; } } } return(IJ); }
private void BuildNewKohonen(int inputLayerSize, int competitiveNeuronLength, int maximumWeightRange) { this.maximumWeightRange = maximumWeightRange; iterationNumber = 20 * inputLayerSize * competitiveNeuronLength; learningRate = 0.2; neighborhoodRadius = 1; this.inputLayerSize = inputLayerSize; this.competitiveNeuronLength = competitiveNeuronLength; competitiveLayer = new KohonenNeuron[competitiveNeuronLength, competitiveNeuronLength]; for (int i = 0; i < competitiveNeuronLength; i++) { for (int j = 0; j < competitiveNeuronLength; j++) { (competitiveLayer[i, j] = new KohonenNeuron(inputLayerSize, i, j)).RandomizeWeights(maximumWeightRange); } } }
/* * Retorna todos os neurônios no raio de vizinhança do vencedor */ private List <KohonenNeuron> GetNeighbors(KohonenNeuron neuron) { List <KohonenNeuron> list = new List <KohonenNeuron>(); int[] index = FindIndex(neuron); int X = index[0]; int Y = index[1]; for (int i = X - neighborhoodRadius; i <= X + neighborhoodRadius; i++) { for (int j = Y - neighborhoodRadius; j <= Y + neighborhoodRadius; j++) { if (i < competitiveNeuronLength && i >= 0 && j < competitiveNeuronLength && j >= 0) { list.Add(competitiveLayer[i, j]); } } } return(list); }