示例#1
0
        private void neural_network()
        {
            WorkBook  workbook  = WorkBook.Load("HW.csv");
            WorkSheet sheet     = workbook.WorkSheets.First();
            DataTable dataTable = sheet.ToDataTable(true);
            Random    r         = new Random();

            for (int i = 0; i < 8; i++)
            {
                ini_weight[i] = r.NextDouble() * (0.5 - (-0.5)) + (-0.5);
            }
            for (int i = 0; i < 3; i++)
            {
                ini_bias[i] = r.NextDouble() * (1 - (-1)) + (-1);
            }
            while (true)//epoch
            {
                int      j          = 2;
                double[] upd_weight = new double[8];
                double[] upd_bias   = new double[3];

                foreach (DataRow row in dataTable.Rows)
                {
                    // MessageBox.Show(row[0]+"");
                    for (int i = 0; i < 3; i++)
                    {
                        input[i] = Convert.ToDouble(row[i]);
                    }
                    target_output = Convert.ToDouble(row[3]);
                    feed_foward(ini_weight, ini_bias);
                    sheet["E" + j].Value = output[5];
                    // for c_error
                    c_error += Math.Abs(output_error);
                    j++;
                    for (int i = 0; i < 8; i++)
                    {
                        upd_weight[i] += _weight[i];
                    }
                    for (int i = 0; i < 3; i++)
                    {
                        upd_bias[i] += _bias[i];
                    }
                }
                //average biases and weight after completing one epoch
                for (int i = 0; i < 8; i++)
                {
                    upd_weight[i] = upd_weight[i] / dataTable.Rows.Count;
                }
                for (int i = 0; i < 3; i++)
                {
                    upd_bias[i] = upd_bias[i] / dataTable.Rows.Count;
                }
                c_error = c_error / dataTable.Rows.Count;

                if (c_error > t_threshold)
                {
                    ini_weight = upd_weight;
                    ini_bias   = upd_bias;
                    c++;
                }
                else
                {
                    // MessageBox.Show(c_error + " " + c);
                    workbook.SaveAs(@"C:\Users\Pc Mart\Desktop\NewExcelFile.xlsx");
                    WorkBook  wb     = WorkBook.Load(@"C:\Users\Pc Mart\Desktop\NewExcelFile.xlsx");
                    WorkSheet _sheet = wb.WorkSheets.First();
                    _sheet.CreateChart(IronXL.Drawing.Charts.ChartType.Scatter, 2, "D", 10, "E");
                    dataTable = _sheet.ToDataTable(true);
                    dataGridView1.DataSource = dataTable;
                    j = 2;

                    var chart = chart1.ChartAreas[0];
                    chart.AxisX.IntervalType                 = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Number;
                    chart.AxisX.LabelStyle.Format            = "";
                    chart.AxisY.LabelStyle.Format            = "";
                    chart.AxisX.LabelStyle.IsEndLabelVisible = true;
                    chart.AxisY.Minimum  = 0.4;
                    chart.AxisY.Interval = 0.1;
                    chart.AxisX.Enabled  = System.Windows.Forms.DataVisualization.Charting.AxisEnabled.False;
                    chart1.Series[0].IsVisibleInLegend = false;
                    chart1.Series.Add("Target Output");
                    chart1.Series["Target Output"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.FastLine;
                    chart1.Series["Target Output"].Color     = Color.SeaGreen;

                    chart.BackColor = Color.Black;
                    chart1.Series.Add("Calculated Output");
                    chart1.Series["Calculated Output"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.FastLine;
                    chart1.Series["Calculated Output"].Color     = Color.Red;

                    foreach (DataRow row in dataTable.Rows)
                    {
                        chart1.Series["Target Output"].Points.AddY(sheet["D" + j].Value);
                        chart1.Series["Calculated Output"].Points.AddY(sheet["E" + j].Value);
                        j++;
                    }


                    break;
                }
            }
        }