public TestForm(decimal[,] neuronsValue, List <Stat> inputStatistics, NetworkSettings inputSettings, MapSettings inputMapSettings, int parameterNumber, List <TestForm> a) { InitializeComponent(); this.neuronsValue = neuronsValue; this.inputStatistics = inputStatistics; this.inputSettings = inputSettings; this.inputMapSettings = inputMapSettings; this.parameterNumber = parameterNumber; this.a = a; Make(); }
private void loadMeasurements_Click(object sender, EventArgs e) { if (ListDelete.Items.Count != 0) { dataGridView2.Rows.Clear(); bestKohonen = new NetworkSettings(Convert.ToInt32(dimensionOfVector.Text), Convert.ToInt32(numberOfNeurons.Text), Convert.ToInt32(numberOfTrainingCycles.Text), Convert.ToInt32(xMap.Text), Convert.ToInt32(yMap.Text), Convert.ToInt32(rangeOfLearning.Text), TrainData.Train.Count, weights, normalise, Convert.ToDecimal(alfa.Text)); decimal fuhler = decimal.MaxValue; Task <decimal> task; for (int i = 0; i <= Convert.ToInt32(sampleSize.Text); i++) { numberOfNeurons.Text = (Convert.ToInt32(xMap.Text) * Convert.ToInt32(yMap.Text)).ToString(); LoadFromDB(); NetworkSettings ns = new NetworkSettings(Convert.ToInt32(dimensionOfVector.Text), Convert.ToInt32(numberOfNeurons.Text), Convert.ToInt32(numberOfTrainingCycles.Text), Convert.ToInt32(xMap.Text), Convert.ToInt32(yMap.Text), Convert.ToInt32(rangeOfLearning.Text), TrainData.Train.Count, weights, normalise, Convert.ToDecimal(alfa.Text)); decimal error = 0.0M; LearningNetwork finalWeightVectors = new LearningNetwork(); task = new Task <decimal>(finalWeightVectors.TrainingOfKohonenLayer, ns); task.Start(); task.Wait(); //error = finalWeightVectors.TrainingOfKohonenLayer(ref normalise, ref weights, ns); dataGridView2.Rows.Add(); int k = dataGridView2.RowCount; dataGridView2[1, k - 1].Value = Math.Round(task.Result, 3); dataGridView2[0, k - 1].Value = k; if (fuhler > error) { bestKohonen = ns; fuhler = error; } } int count = dataGridView2.RowCount; decimal averageError = 0.0M; for (int i = 0; i < count; i++) { averageError += Convert.ToDecimal(dataGridView2[1, i].Value); } averageError /= count; dataGridView2[1, count - 1].Value = averageError; dataGridView2[0, count - 1].Value = Localization.MyStrings.Average; } else { MessageBox.Show(Localization.MyStrings.AddParameter); } }
//public event changeXYHendler changeXY; public ParentForMaps(decimal[,] neuronsValue, List <Stat> inputStatistics, NetworkSettings inputSettings, List <string> nameParam) { if (!string.IsNullOrEmpty(Properties.Settings.Default.Languages)) { System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo(Properties.Settings.Default.Languages); System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.GetCultureInfo(Properties.Settings.Default.Languages); } InitializeComponent(); this.neuronsValue = neuronsValue; this.inputStatistics = inputStatistics; this.inputSettings = inputSettings; this.nameParam = nameParam; textBoxHexSide.Text = "15"; textBoxXOffset.Text = "5"; textBoxtYOffset.Text = "5"; textBoxPenWidth.Text = "2"; }
public decimal[,] OperationOfKohonenLayer(decimal[,] normalizingData, decimal[,] outputNeurons, decimal[,] inputData, NetworkSettings ns) { decimal[,] valueOfNeurons = new decimal[ns.DimentionOfVector, ns.NumberOfNeurons]; int[] numberOfNeuronWinner = new int[ns.NumberOfVectors]; //номер нейрона победителя decimal[] minEuclideanDistance = new decimal[ns.NumberOfVectors]; //минимальное евклидово расстояние int[] numberOfWins = new int[ns.NumberOfNeurons]; //количество побед //numberOfWins = null; for (int i = 0; i < ns.NumberOfVectors; i++) { minEuclideanDistance[i] = decimal.MaxValue; } for (int j = 0; j < ns.NumberOfVectors; j++) //нахождение евклидова расстояния между входным вектором и нейронами { decimal[] euclideanDistance = new decimal[ns.NumberOfNeurons]; //евклидова метрика for (int k = 0; k < ns.NumberOfNeurons; k++) { for (int i = 0; i < ns.DimentionOfVector; i++) { euclideanDistance[k] += (normalizingData[i, j] - outputNeurons[i, k]) * (normalizingData[i, j] - outputNeurons[i, k]); } euclideanDistance[k] = Convert.ToDecimal(Math.Sqrt(Convert.ToDouble(euclideanDistance[k]))); if (minEuclideanDistance[j] > euclideanDistance[k]) { minEuclideanDistance[j] = euclideanDistance[k]; numberOfNeuronWinner[j] = k; } } numberOfWins[numberOfNeuronWinner[j]]++; for (int m = 0; m < ns.DimentionOfVector; m++) { valueOfNeurons[m, numberOfNeuronWinner[j]] += inputData[m, j]; } } for (int m = 0; m < ns.DimentionOfVector; m++) { for (int k = 0; k < ns.NumberOfNeurons; k++) { if (numberOfWins[k] != 0) { valueOfNeurons[m, k] /= numberOfWins[k]; valueOfNeurons[m, k] = Math.Round(valueOfNeurons[m, k], 2); } } } for (int i = 0; i < ns.NumberOfNeurons; i++) { if ((valueOfNeurons[0, i]) != 0) { for (int j = 0; j < ns.DimentionOfVector; j++) { //valueOfNeurons.Rows[i][j] = Math.Round((Convert.ToDouble(valueOfNeurons.Rows[i][j]) / Convert.ToDouble(valueOfNeurons.Rows[i][0])), 2); valueOfNeurons[j, i] = (valueOfNeurons[j, i]); } } else { for (int j = 0; j < ns.DimentionOfVector; j++) { valueOfNeurons[j, i] = -1; } } } return(valueOfNeurons); }
//ref decimal[,] normalizingData, ref decimal[,] outputNeurons, NetworkSettings ns, decimal alfa public decimal TrainingOfKohonenLayer(object arg) { decimal[,] normalizingData = ((NetworkSettings)arg).Normalise; decimal[,] outputNeurons = ((NetworkSettings)arg).Weights; NetworkSettings ns = ((NetworkSettings)arg); Random rand = new Random((int)(DateTime.Now.Ticks)); //DataTable finalWeightVectors = new DataTable(); int currentNumberOfTrainingCycles; //номер цикла обучения int[] numberOfNeuronWinner = new int[ns.NumberOfVectors]; decimal[] potenctal = new decimal[ns.NumberOfNeurons]; int[] winnersCount = new int[ns.NumberOfNeurons]; decimal pmin = 0.75M; int number; decimal functionOfSpeedTraining = 0.9M; //функция (коэфициент) скорости обучения decimal functionNeighborhood; // функция соседства decimal networkError = 0.1M; //погрешность сети (квантования) decimal alfaZero = ns.Alfa; decimal deltaZero = Convert.ToDecimal(ns.RangeOfLearning); for (int i = 0; i < ns.NumberOfNeurons; i++) { potenctal[i] = 1.0M; } for (currentNumberOfTrainingCycles = 0; currentNumberOfTrainingCycles < ns.NumberOfTrainingCycles; currentNumberOfTrainingCycles++) { List <int> numbers = new List <int>(); for (int k = 0; k < ns.NumberOfNeurons; k++) { winnersCount[k] = 0; } decimal alfa = alfaZero * (1 - (currentNumberOfTrainingCycles / (decimal)ns.NumberOfTrainingCycles)); // Коэффициент скорости обучения decimal delta = deltaZero * (1 - (currentNumberOfTrainingCycles / (decimal)ns.NumberOfTrainingCycles)); // Вычисляет радиус обучения используемые функцией decimal[] minEuclideanDistance = new decimal[ns.NumberOfVectors]; //минимальное евклидово расстояние for (int i = 0; i < ns.NumberOfVectors; i++) { minEuclideanDistance[i] = decimal.MaxValue; numbers.Add(i); } for (int j = 0; j < ns.NumberOfVectors; j++) //нахождение евклидова расстояния между входным вектором и нейронами { number = RandomValue.Random(rand, numbers.Count - 1); int numberRow = number; number = numbers[number]; numbers.RemoveAt(numberRow); for (int k = 0; k < ns.NumberOfNeurons; k++) { if (potenctal[k] > pmin) { decimal euclideanDistance = 0; //евклидова метрика for (int i = 0; i < ns.DimentionOfVector; i++) { euclideanDistance += (normalizingData[i, number] - outputNeurons[i, k]) * (normalizingData[i, number] - outputNeurons[i, k]); } euclideanDistance = Convert.ToDecimal(Math.Sqrt((Convert.ToDouble(euclideanDistance)))); if (minEuclideanDistance[number] > euclideanDistance) { minEuclideanDistance[number] = euclideanDistance; numberOfNeuronWinner[number] = k; } } } winnersCount[numberOfNeuronWinner[number]]++; // Сколько раз одержал нейрон победу ( итератор) //Цикл изменяющий потенциал for (int i = 0; i < ns.NumberOfNeurons; i++) { if (numberOfNeuronWinner[number] == i) { potenctal[numberOfNeuronWinner[number]] -= pmin; } else if (potenctal[i] >= 1.0M) { potenctal[i] = 1.0M; } else { potenctal[i] += 1 / (decimal)ns.NumberOfNeurons; } } int axisX = numberOfNeuronWinner[number] % ns.XMap; int axisY = numberOfNeuronWinner[number] / ns.YMap; Elements elements = new Elements(ns.NumberOfNeurons); int neuronNumber = axisY * ns.YMap + axisX; elements.neighbours[neuronNumber].value = true; ///Матрица поиска соседей /// for (int i = 1; i <= Convert.ToInt32(delta); i++) { for (int z = 0; z < ns.NumberOfNeurons; z++) { if (true) { if ((elements.neighbours[z].value == true) && (elements.neighbours[z].depth == i - 1)) { if ((z / ns.YMap == ns.YMap - 1) || (z / ns.YMap == 0)) { for (int l = 0; l < ns.XMap; l++) { int row = z / ns.YMap; int neuronNumbers = row * ns.YMap + l; if (!elements.neighbours[neuronNumbers].value) { elements.neighbours[neuronNumbers].value = true; elements.neighbours[neuronNumbers].depth = i; } } } if (z + 1 < ns.YMap * ns.XMap - 1) { if ((z % ns.XMap == ns.XMap - 1) && (z - ns.XMap - 1 > 0)) { if (!elements.neighbours[z - ns.XMap - 1].value) { elements.neighbours[z - ns.XMap - 1].value = true; elements.neighbours[z - ns.XMap - 1].depth = i; } } else if (!elements.neighbours[z + 1].value) { elements.neighbours[z + 1].value = true; elements.neighbours[z + 1].depth = i; } } if (z - 1 >= 0) { if ((z % ns.XMap == 0) && (!elements.neighbours[z + ns.XMap - 1].value)) { elements.neighbours[z + ns.XMap - 1].value = true; elements.neighbours[z + ns.XMap - 1].depth = i; } else if (!elements.neighbours[z - 1].value) { elements.neighbours[z - 1].value = true; elements.neighbours[z - 1].depth = i; } } if ((z + ns.YMap < ns.YMap * ns.XMap - 1) && (!elements.neighbours[z + ns.YMap].value)) { elements.neighbours[z + ns.YMap].value = true; elements.neighbours[z + ns.YMap].depth = i; } if ((z + ns.YMap + 1 < ns.YMap * ns.XMap - 1) && (!elements.neighbours[z + ns.YMap + 1].value)) { elements.neighbours[z + ns.YMap + 1].value = true; elements.neighbours[z + ns.YMap + 1].depth = i; } if ((z + ns.YMap - 1 < ns.YMap * ns.XMap - 1) && (!elements.neighbours[z + ns.YMap - 1].value)) { elements.neighbours[z + ns.YMap - 1].value = true; elements.neighbours[z + ns.YMap - 1].depth = i; } if ((z - ns.YMap >= 0) && (!elements.neighbours[z - ns.YMap].value)) { elements.neighbours[z - ns.YMap].value = true; elements.neighbours[z - ns.YMap].depth = i; } if ((z - ns.YMap + 1 >= 0) && (!elements.neighbours[z - ns.YMap + 1].value)) { elements.neighbours[z - ns.YMap + 1].value = true; elements.neighbours[z - ns.YMap + 1].depth = i; } if ((z - ns.YMap - 1 >= 0) && (!elements.neighbours[z - ns.YMap - 1].value)) { elements.neighbours[z - ns.YMap - 1].value = true; elements.neighbours[z - ns.YMap - 1].depth = i; } } } } } for (int i = 0; i < ns.NumberOfNeurons; i++) { if (elements.neighbours[i].value) { for (int m = 0; m < ns.DimentionOfVector; m++) //изменение вектора весов нейронов из радиуса обучения { functionOfSpeedTraining = alfa * Convert.ToDecimal(Math.Exp( Convert.ToDouble( -((normalizingData[m, number] - outputNeurons[m, i]) * (normalizingData[m, number] - outputNeurons[m, i]) / (2 * delta * delta))))); outputNeurons[m, i] = outputNeurons[m, i] + functionOfSpeedTraining * (normalizingData[m, number] - outputNeurons[m, i]); } } } } if (currentNumberOfTrainingCycles % 200 == 0) { decimal errors = 0.0M; for (int j = 0; j < ns.NumberOfVectors; j++) //нахождение евклидова расстояния между входным вектором и нейронами { for (int i = 0; i < ns.DimentionOfVector; i++) { errors += (normalizingData[i, j] - outputNeurons[i, numberOfNeuronWinner[j]]) * (normalizingData[i, j] - outputNeurons[i, numberOfNeuronWinner[j]]); } } errors = errors / ns.NumberOfVectors; } } for (int i = 0; i < ns.NumberOfNeurons; i++) { for (int j = 0; j < ns.DimentionOfVector; j++) { outputNeurons[j, i] = Convert.ToDecimal(Math.Round(Convert.ToDouble(outputNeurons[j, i]), 5)); } } decimal error = 0.0M; for (int j = 0; j < ns.NumberOfVectors; j++) //нахождение евклидова расстояния между входным вектором и нейронами { for (int i = 0; i < ns.DimentionOfVector; i++) { error += (normalizingData[i, j] - outputNeurons[i, numberOfNeuronWinner[j]]) * (normalizingData[i, j] - outputNeurons[i, numberOfNeuronWinner[j]]); } } return(error / ns.NumberOfVectors); }