private void button1_Click(object sender, EventArgs e) { double dou = 0; listBox1.Items.Clear(); for (int i = 0; i < 10; i++) { //Коэффициент обучение сети n = Convert.ToDouble(textBox_n.Text); //Объем обучающей выборки countRow = Convert.ToInt32(textBox_X.Text); numHidden = Convert.ToInt32(textBox_C.Text); countIter = Convert.ToInt32(textBox1.Text); // чтение входных данных x = ReaderWriter.ReadMatrix(filename, countRow, numInput); double[] d = ReaderWriter.ReadVector(filename, countRow, numInput); D = new double[countRow, numOutput]; RecD(d); //Инициализация центроид centroids = ReaderWriter.ReadMatrixC(filename, countRow, numInput, numHidden, d); rn = new RBFNetwork(countRow, numInput, numHidden, numOutput, x, centroids, n, temp, er); rn.countIter = countIter; //Инициализация весов rn.SetWeight(); // Обучение сети rn.TrainNetwork(x, centroids, countRow, numInput, numHidden, numOutput, D); er = rn.errTrain; countRow = 30; // чтение входных данных x = ReaderWriter.ReadMatrix("iris_test.txt", countRow, numInput); d = ReaderWriter.ReadVector("iris_test.txt", countRow, numInput); D = new double[countRow, numOutput]; RecD(d); // Тестирование сети errTest = rn.TestNetwork(x, D, countRow, numInput, numOutput, numHidden); dou += errTest; listBox1.Items.Add(errTest); } listBox1.Items.Add("Result: " + dou / 10); }
private void button_Sample_Click(object sender, EventArgs e) { label_err.Text = "Learning error: "; label_Test.Text = "Testing error: "; n = Convert.ToDouble(textBox_n.Text); //Объем обучающей выборки countRow = Convert.ToInt32(textBox_X.Text); numHidden = Convert.ToInt32(textBox_C.Text); countIter = Convert.ToInt32(textBox1.Text); x = ReaderWriter.ReadMatrix(filename, countRow, numInput); double[] d = ReaderWriter.ReadVector(filename, countRow, numInput); D = new double[countRow, numOutput]; RecD(d); //Ин центроид centroids = ReaderWriter.ReadMatrixC(filename, countRow, numInput, numHidden, d); Console.WriteLine(centroids); rn = new RBFNetwork(countRow, numInput, numHidden, numOutput, x, centroids, n, temp, er); // Console.WriteLine(rn); rn.countIter = countIter; //Ин весов rn.SetWeight(); rn.TrainNetwork(x, centroids, countRow, numInput, numHidden, numOutput, D); // Console.WriteLine(rn); er = rn.errTrain; label_err.Text = "Learning error: " + er; temp = rn.temp; // button_Sample.Enabled = false; button_test.Enabled = true; }
public RBFNetwork TrainNetwork(Matrix x, Matrix centroids, int countRow, int numInput, int numHidden, int numOutput, double[,] d) { R = centroids.Row - 1; List <double> sko = new List <double>(); double e = 0; double eSum = 0; double[] error = new double[countRow]; //105 double[,] newX = new double[countRow, numOutput]; double[,] newC = new double[centroids.Row, centroids.Column]; double[,] newSi = new double[centroids.Row, centroids.Column]; double m = numOutput; double p = countRow; bool flg = true; double sum = 0; double[,] gradient = new double[numHidden, numOutput]; int t = 0; //k-means SetCentroidsWin(x, centroids); Sigmoida(centroids); double[,] u = new double[countRow, numHidden]; //3 double[,] f = new double[countRow, numHidden]; //3 Matrix y = new Matrix(countRow, numOutput); //3 double temp1 = 0; int r = countIter; while (r != 0) { r--; eSum = 0; //Обучение сети for (int ti = 0; ti < x.Row; ti++) { for (int q = 0; q < y.Column; q++) { e = 0; for (int i = 0; i < centroids.Row; i++) //3 { sum = 0; for (int j = 0; j < centroids.Column; j++) //4 { sum += Math.Pow(x.Get(ti, j) - centroids.Get(i, j), 2) / (Math.Pow(sigmoida[i, j], 2)); //newC[i,j] = x.Get(ti, j) - centroids.Get(i, j)/ (Math.Pow(sigmoida[i, j], 2)); //newSi[i, j] = x.Get(ti, j) - centroids.Get(i, j) / (Math.Pow(sigmoida[i, j], 3)); } u[ti, i] = sum; f[ti, i] = Math.Exp(-0.5 * u[ti, i]); temp1 = 0; } for (int w = 0; w < weight.Column; w++) { temp1 += weight.Get(q, w) * f[ti, q]; } //Console.WriteLine(temp1); //выход y.Set(ti, q, temp1); //разность newX[ti, q] = y.Get(ti, q) - d[ti, q]; //for (int i = 0; i < centroids.Row; i++) //3 //{ // for (int j = 0; j < centroids.Column; j++) //4 // { // centroids.Set(i, j, centroids.Get(i, j) - n * newX[ti, q] * f[ti, q] * weight.Get(i, q) * (x.Get(ti, j) - centroids.Get(i, j) / (Math.Pow(sigmoida[i, j], 2)))); // // sigmoida[i, j] = sigmoida[i, j] - n * newX[ti, q] * f[ti, q] * weight.Get(i, q) * ((x.Get(ti, j) - centroids.Get(i, j) / (Math.Pow(sigmoida[i, j], 3)))); // } //} for (int k = 0; k < weight.Column; k++) { gradient[k, q] = newX[ti, q] * f[ti, q]; //пересчитываем вес weight.Set(k, q, weight.Get(k, q) - n * gradient[k, q]); } //Console.WriteLine(weight); e += Math.Pow(y.Get(ti, q) - d[ti, q], 2); } eSum += e; } sko.Add(Math.Sqrt((eSum / (m * p - 1)))); //Console.WriteLine(sko.ElementAt(sko.Count - 1)); t++; } NOutputRBF(y); for (int y1 = 0; y1 < y.Row; y1++) { for (int y2 = 0; y2 < y.Column; y2++) { Console.Write(y.Get(y1, y2) + " "); } Console.WriteLine(t); } for (int y2 = 0; y2 < sko.Count(); y2++) { errPr = sko.ElementAt(y2); Console.WriteLine(errPr); } Console.WriteLine(sko.ElementAt(sko.Count - 1)); temp = t; errTrain = Math.Round(errPr, 10); RBFNetwork nr = new RBFNetwork(countRow, numInput, numHidden, numOutput, x, centroids, n, temp, errTrain); return(nr); }