private void SimplePerceptronTrain() { InitializeWeights(); decimal tempSum = 0; for (int i = 0; i < iterationLimit; i++) { foreach (Dots member in ExtractData.GetDataTrain()) { #region Compute Output for (int j = 0; j < numClusters; j++) { decimal a = Activation(member.X, ClusterCenters[j, 0], ClusterSpreads[j]); decimal w = Weights[j]; tempSum += a * w; } #endregion #region UpdateWeights decimal delta = 0; for (int j = 0; j < numClusters; j++) { delta = trainingCoefficient * (member.Y - tempSum) * Activation(member.X, ClusterCenters[j, 0], ClusterSpreads[j]); Weights[j] += delta; } #endregion tempSum = 0; } } }
private void FindSpreads() { ClusterSpreads = new decimal[numClusters]; for (int i = 0; i < numClusters; i++) { ClusterSpreads[i] = Utils.SimpleDistance(ClusterCenters[i, 0], ExtractData.GetDataTrain().Max(x => x.X)) / 2; } }
public RBF(int ParamNumClusters, int numOfIterations) { iterationLimit = numOfIterations; numClusters = ParamNumClusters; AssignClusterCenters(numClusters); InitialCluster(); FindSpreads(); SimplePerceptronTrain(); SimplePerceptronValidate(); List <Dots> asdasd = ExtractData.GetDataTrain(); }
private void ReCalculateCenters() { for (int i = 0; i < numClusters; i++) { decimal count = Convert.ToDecimal(ExtractData.GetDataTrain().Where(x => x.BelongingCluster[numClusters - 1] == i + 1).Count()); decimal summ = ExtractData.GetDataTrain().Where(x => x.BelongingCluster[numClusters - 1] == i + 1).Sum(y => y.X); if (count != 0) { ClusterCenters[i, 0] = summ / count; } } }
private void InitialCluster() { foreach (Dots item in ExtractData.GetDataTrain()) { decimal Dist = 500000m; for (int i = 0; i < numClusters; i++) { item.DistanceToCentersByX[i] = Utils.SimpleDistance(ClusterCenters[i, 0], item.X); } for (int i = 0; i < numClusters; i++) { if (item.DistanceToCentersByX[i] < Dist) { Dist = item.DistanceToCentersByX[i]; item.BelongingCluster[numClusters - 1] = i + 1; } } } ReCalculateCenters(); foreach (Dots item in ExtractData.GetDataValidation()) { decimal Dist = 500000m; for (int i = 0; i < numClusters; i++) { item.DistanceToCentersByX[i] = Utils.SimpleDistance(ClusterCenters[i, 0], item.X); } for (int i = 0; i < numClusters; i++) { if (item.DistanceToCentersByX[i] < Dist) { Dist = item.DistanceToCentersByX[i]; item.BelongingCluster[numClusters - 1] = i + 1; } } } }
public void SetupRBF() { ExtractData.SetDataTrain(); ExtractData.SetDataValidation(); Random rrr = new Random(); //Clustering ClusterData = new Clustering(); //ClusterData.Cluster(); //StringBuilder str = new StringBuilder(); //str.AppendLine("X\tY\t"); //foreach (Dots x in ExtractData.GetDataValidation()) //{ // str.AppendLine(String.Format("{0}\t{1}\t", x.X, x.Y)); //} //textBox1.Text = str.ToString(); for (int l = 0; l < 100; l++) { StringBuilder str = new StringBuilder(); StringBuilder str2 = new StringBuilder(); for (int k = 100; k < 10001; k *= 10) { for (int i = 1; i < 11; i++) // Cluster Number Iteration { string Key = MD5Sifrele(DateTime.Now.ToString() + rrr.Next().ToString()); int cluster_count = i; RBF rbf = new RBF(cluster_count, k); str.AppendLine(Key); str.AppendLine(String.Format("RBF with {0} Epoch, {1} Training Coefficient, {2} Clusters", k, 0.1, i)); str.AppendLine("X\tY\tResult\tCluster\t"); foreach (Dots x in ExtractData.GetDataValidation().OrderBy(x => x.BelongingCluster[cluster_count - 1])) { str.AppendLine(String.Format("{0}\t{1}\t{2}\t{3}\t", x.X, x.Y, x.outY, x.BelongingCluster[cluster_count - 1])); } str.AppendLine(""); str.AppendLine(""); str.AppendLine(""); str2.AppendLine(Key); str2.AppendLine(rbf.ToString()); } } System.IO.StreamWriter file = new System.IO.StreamWriter("d:\\Results\\OutputRBF_Stats_TestRun_#" + (l + 1) + ".txt"); file.Write(str2.ToString()); file.Close(); System.IO.StreamWriter file2 = new System.IO.StreamWriter("d:\\Results\\OutputRBF_Predictions_TestRun_#" + (l + 1) + ".txt"); file2.Write(str.ToString()); file2.Close(); } //Mean Error - Mean Square Error Stats Exporter //StringBuilder str = new StringBuilder(); //str.AppendLine("ID\tEpoch\tCluster\tMean Error\tMean Square Error\t"); //int id = 0; //for (int l = 0; l < 100; l++) //{ // for (int k = 100; k < 10001; k *= 10) // { // for (int i = 1; i < 11; i++) // Cluster Number Iteration // { // id++; // //string Key = MD5Sifrele(DateTime.Now.ToString() + rrr.Next().ToString()); // int cluster_count = i; // RBF rbf = new RBF(cluster_count, k); // str.AppendLine(String.Format("{0}\t{1}\t{2}\t{3}\t{4}",id, k, cluster_count, rbf.MeanError.ToString("#.##"), rbf.SumOfSquareErrorMean.ToString("#.##"))); // } // } //} //System.IO.StreamWriter file2 = new System.IO.StreamWriter("d:\\Results\\OutputRBF_Predictions_General_Plot" + ".txt"); //file2.Write(str.ToString()); //file2.Close(); //textBox1.Text = str.ToString() ; //textBox2.Text = str2.ToString(); }
private void SimplePerceptronValidate() { decimal LocalError = 0; CorrectCount = 0; FalseCount = 0; foreach (Dots member in ExtractData.GetDataValidation()) { #region Compute Output decimal output = 0; for (int j = 0; j < numClusters; j++) { output += Activation(member.X, ClusterCenters[j, 0], ClusterSpreads[j]) * Weights[j]; } member.outY = output; #endregion #region Information Collection LocalError = member.Y - output; if (Math.Abs(Convert.ToDouble(output)) <= Math.Abs(Convert.ToDouble(member.Y)) * 1.2 && Math.Abs(Convert.ToDouble(output)) >= Math.Abs(Convert.ToDouble(member.Y)) * 0.8) { CorrectCount++; } else { FalseCount++; } SumOfError += LocalError; SumOfSquareError += LocalError * LocalError; SumOfOutput += output; SumOfSquareOutput += output * output; SumOfValue += member.Y; SumOfSquareValue += member.Y * member.Y; #endregion } SumOfSquareErrorMean = SumOfSquareError / ExtractData.GetDataValidation().Count; MeanError = SumOfError / ExtractData.GetDataValidation().Count; ErrorStdDev = Convert.ToDecimal(Math.Sqrt(Convert.ToDouble((SumOfSquareError - (MeanError * MeanError * ExtractData.GetDataValidation().Count)) / ExtractData.GetDataValidation().Count))); SumOfSquareValueMean = SumOfSquareValue / ExtractData.GetDataValidation().Count; MeanValue = SumOfValue / ExtractData.GetDataValidation().Count; ValueStdDev = Convert.ToDecimal(Math.Sqrt(Convert.ToDouble((SumOfSquareValue - (MeanValue * MeanValue * ExtractData.GetDataValidation().Count)) / ExtractData.GetDataValidation().Count))); SumOfSquareOutputMean = SumOfSquareOutput / ExtractData.GetDataValidation().Count; MeanOutput = SumOfOutput / ExtractData.GetDataValidation().Count; OutputStdDev = Convert.ToDecimal(Math.Sqrt(Convert.ToDouble((SumOfSquareOutput - (MeanOutput * MeanOutput * ExtractData.GetDataValidation().Count)) / ExtractData.GetDataValidation().Count))); }