public static void ExportDatasetsAsCSV(List <DataSet> datasets) { var dialog = new SaveFileDialog { Title = "Save Dataset File", Filter = "Text File|*.txt;" }; if (dialog.ShowDialog() != true) { return; } ProgressDialogResult result = ProgressDialog.ProgressDialog.Execute(Application.Current.MainWindow, "Converting Dataset...", () => { using (var file = File.CreateText(dialog.FileName)) { //var serializer = new JsonSerializer { Formatting = Formatting.Indented }; //serializer.Serialize(file, datasets); foreach (DataSet ds in datasets) { string s = ""; foreach (double d in ds.Targets) { s += d.ToString() + ","; } foreach (double d in ds.Values) { s += d.ToString() + ","; } file.WriteLine(s.Substring(0, s.Length - 1)); } } }); }
void testOnce() { if (this.TestDataSets == null) { this.btnTest.IsEnabled = false; return; } int right = 0; int sum = 0; ProgressDialogResult result = ProgressDialog.ProgressDialog.Execute(this, "Testing Network", () => { for (int i = 0; i < TestDataSets.Count(); i++) { ProgressDialog.ProgressDialog.Current.Report("Testing Number {0}/{1}... \n ", i, TestDataSets.Count()); double[] a = this.Network.Compute(TestDataSets[i].Values); if (findMaxIndex(a) == findMaxIndex(TestDataSets[i].Targets)) { right++; } sum++; } }, ProgressDialogSettings.WithSubLabel); if (result.OperationFailed) { MessageBox.Show("Test failed."); } else { MessageBox.Show("Total test sample Number:" + sum.ToString() + "\n" + "Right number:" + right.ToString() + "\n" + "Wrong number:" + (sum - right).ToString() + "\n" + "Accuracy:" + (right / (double)sum).ToString()); } this.Dispatcher.BeginInvoke(new Action(() => { SeriesCollectionAccuracy[0].Values.Add(right / (double)sum); if (SeriesCollectionAccuracy[0].Values.Count > 50) { SeriesCollectionAccuracy[0].Values.RemoveAt(0); } })); }
private void BtnSaveAs_Click(object sender, RoutedEventArgs e) { var dialog = new SaveFileDialog { Title = "Save Dataset File", Filter = "Text File|*.txt;" }; if (dialog.ShowDialog() != true) { return; } try { ProgressDialogResult result = ProgressDialog.ProgressDialog.Execute(this, "Converting Dataset...", () => { using (var file = File.CreateText(dialog.FileName)) { //var serializer = new JsonSerializer { Formatting = Formatting.Indented }; //serializer.Serialize(file, datasets); foreach (DataSet ds in Datasets) { string s = ""; foreach (double d in ds.Targets) { s += d.ToString() + ","; } foreach (double d in ds.Values) { s += d.ToString() + ","; } file.WriteLine(s.Substring(0, s.Length - 1)); } } }); }catch (Exception) { } }
public static List <DataSet> ImportDatasetsFromCSV(int inputSize, int outputSize) { try { var dialog = new OpenFileDialog { Multiselect = false, Title = "Open Dataset File", Filter = "Text File|*.txt;" }; if (dialog.ShowDialog() != true) { return(null); } List <DataSet> dataSets = new List <DataSet>(); using (StreamReader sr = new StreamReader(dialog.FileName, Encoding.Default)) { ProgressDialogResult result = ProgressDialog.ProgressDialog.Execute(Application.Current.MainWindow, "Loading Train Data...", () => { while (sr.Peek() > 0) { string temp = sr.ReadLine(); double[] all = ToDoubleArray(temp, ','); if (all.Count() != (inputSize + outputSize)) { MessageBox.Show("The lenth of Network is not fit the length of Dataset"); break; } double[] tar = new double[outputSize]; double[] val = new double[inputSize]; for (int i = 0; i < outputSize; i++) { tar[i] = all[i]; } for (int j = 0; j < inputSize; j++) { val[j] = all[j + outputSize]; } DataSet ds = new DataSet(val, tar); dataSets.Add(ds); } }); if (result.OperationFailed) { MessageBox.Show("Loading Train Data failed."); } else { MessageBox.Show("Loading Train Data successfully."); } return(dataSets); } } catch (Exception) { return(null); } }
void trainEpoch(Object EpochNumber, bool isTest) { Stopwatch sw = new Stopwatch(); sw.Start(); this.Dispatcher.BeginInvoke(new Action(() => { TrainEpocNum.Content = "0"; TrainElapseTime.Content = "0"; })); ProgressDialogResult result = ProgressDialog.ProgressDialog.Execute(this, "Trainning Network", () => { for (int i = 0; i < (int)EpochNumber; i++) { ProgressDialog.ProgressDialog.Current.Report("Trained Epoch {0}/{1}... \n ", i, (int)EpochNumber); int j = 0; foreach (var dataSet in TrainDataSets) { if (this.stopTrain == true) { break; } this.Network.TrainOneStep(dataSet); j++; this.Dispatcher.BeginInvoke(new Action(() => { TrainNum.Content = j.ToString(); TrainEpocNum.Content = i.ToString(); TrainElapseTime.Content = (sw.ElapsedMilliseconds / 60.0).ToString("0.00") + "s"; TotalError.Content = string.Format("{0:N3}", this.Network.TotalError); })); } if (isTest) { if (this.TestDataSets == null) { this.btnTest.IsEnabled = false; return; } int right = 0; int sum = 0; for (int k = 0; k < TestDataSets.Count(); k++) { ProgressDialog.ProgressDialog.Current.Report("Testing {0}/{1}... \n ", k, (int)TestDataSets.Count()); double[] a = this.Network.Compute(TestDataSets[k].Values); if (findMaxIndex(a) == findMaxIndex(TestDataSets[k].Targets)) { right++; } sum++; } this.Dispatcher.BeginInvoke(new Action(() => { SeriesCollectionAccuracy[0].Values.Add(right / (double)sum); if (SeriesCollectionAccuracy[0].Values.Count > 50) { SeriesCollectionAccuracy[0].Values.RemoveAt(0); } })); } this.Dispatcher.BeginInvoke(new Action(() => { EpochNum.Add(i); SeriesCollectionEpoch[0].Values.Add(this.Network.TotalError); if (SeriesCollectionEpoch[0].Values.Count > 50) { SeriesCollectionEpoch[0].Values.RemoveAt(0); } TimeElapse.Add(DateTime.Now.ToString("mm-ss")); SeriesCollectionCPUUsage[0].Values.Add(theCPUCounter.NextValue() / Environment.ProcessorCount); if (SeriesCollectionCPUUsage[0].Values.Count > 50) { SeriesCollectionCPUUsage[0].Values.RemoveAt(0); } })); } }, ProgressDialogSettings.WithSubLabel); if (result.OperationFailed) { MessageBox.Show("Train failed."); } else { MessageBox.Show("Train successfully."); } sw.Stop(); this.Dispatcher.BeginInvoke(new Action(() => { resetAllButtonState(); this.btnTrainOneStep.IsEnabled = true; this.btnTrain.IsEnabled = true; this.btnLoadTestData.IsEnabled = true; this.btnSave.IsEnabled = true; if (this.TestDataSets != null) { this.btnTest.IsEnabled = true; } })); }
private void BtnConvert_Click(object sender, RoutedEventArgs e) { try { int outputSize = int.Parse(this.txtOutputSize.Text.ToString().Trim()); StreamReader sr = new StreamReader(originFile, Encoding.Default); double threshold = double.Parse(this.txtThreshold.Text.ToString().Trim()); double min = double.Parse(this.txtXmin.Text.ToString().Trim()); double max = double.Parse(this.txtXMax.Text.ToString().Trim()); double v = max - min; int k = 0; if (this.radioBtnThreshold.IsChecked == true) { k = 0; } else if (this.radioButtonMinMax.IsChecked == true) { k = 1; } this.Datasets.Clear(); ProgressDialogResult result = ProgressDialog.ProgressDialog.Execute(this, "Converting Dataset...", () => { while (sr.Peek() > 0) { string temp = sr.ReadLine(); List <double> s = ToDoubleArray(temp, ',').ToList(); double[] tar = new double[outputSize]; //初始化 tar[(int)s[0]] = 1; //把第一个数对应的数组中的值改成1,其它为0 s.RemoveAt(0); for (int i = 0; i < s.Count; i++) { if (k == 0) { if (s[i] > threshold) { s[i] = 1.0; } else { s[i] = 0.0; } } else if (k == 1) { s[i] = ((s[i] - min) / v) * 0.99 + 0.01; } } DataSet ds = new DataSet(s.ToArray(), tar); Datasets.Add(ds); } }); if (result.OperationFailed) { MessageBox.Show("Converting Dataset failed."); } else { MessageBox.Show("Converting Dataset successfully."); } sr.Close(); txtDatasetSize.Content = Datasets.Count().ToString(); } catch (Exception) { } }