Exemple #1
0
        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //IOptimize optimizer = new SGD();// SGD();//AdaGrad();// Momentum();// SGD();

            net = new TwoLayerNet(784, 50, 10);

            net.LoadWidgets("sampleWidget.td");

            OnClickImage(sender, e);
        }
Exemple #2
0
        public async void TestTwoLayerNet()
        {
            // TrainingData data = TrainingData.Load("sampleWidget.td");

            List <Matrix> matrices = await LoadMNISTAsync();

            Matrix x_train = matrices[0];
            Matrix t_train = matrices[1];

            // Matrix x_test = matrices[2];
            // Matrix y_test = matrices[3];

            int itersNum   = 3000;
            int batch_size = 100;


            IOptimize optimizer = new SGD();// SGD();//AdaGrad();// Momentum();// SGD();

            net = new TwoLayerNet(784, 50, 10);

            net.LossUpdated += loss => { this.LossUpdated?.Invoke(loss); };

            //  net.print += Net_print;
            Matrix x_batch = new Matrix(batch_size, 784);

            Matrix t_batch = new Matrix(batch_size, 10);


            int[] indexs = new int[batch_size];


            for (int i = 0; i < itersNum; i++)
            {
                for (int j = 0; j < batch_size; j++)
                {
                    indexs[j] = random.Next(TrainDataCount);
                }

                for (int row = 0; row < indexs.Length; row++)
                {
                    int index = indexs[row];

                    //  Console.WriteLine(index);

                    for (int col = 0; col < x_batch.Y; col++)
                    {
                        x_batch[row, col] = x_train[index, col];
                    }
                    for (int col = 0; col < t_batch.Y; col++)
                    {
                        t_batch[row, col] = t_train[index, col];//
                    }
                }

                // Matrix[] grads = net.NumericalGradient(x_batch, t_batch);

                Matrix[] grads = net.Gradient(x_batch, t_batch);

                optimizer.Update(net.Params, grads);

                net.Update();

                Console.WriteLine($"损失值:{net.Loss(x_batch, t_batch)} ");

                if (i % 300 == 0)
                {
                    Console.WriteLine("识别精度计算中...");
                    double accuracy = net.Accuracy(x_train, t_train);

                    AccuracyUpdated?.Invoke(accuracy);
                    accuracies.Add(accuracy);

                    Console.WriteLine($"识别精度:{accuracy}");
                }
            }

            net.Save("sampleWidget.td");
        }
Exemple #3
0
        private void Button_Train_Click(object sender, EventArgs e)
        {
            listBox_NetworkState.Items.Clear();
            NeuronNum    = Convert.ToInt32(textBox_NeuronNumber.Text);
            LearningRate = Convert.ToDouble(textBox_LearningRate.Text);
            epoch        = Convert.ToInt32(textBox_MaxEpoch.Text);
            NetOutput    = new double[OutputSize];
            Branch       = Convert.ToInt32(textBox_branch.Text);
            t            = new Thread(BackgroundWork);
            int DataNumber = InputData.Length / Branch;

            double[] NetInput   = new double[InputData[0].Length * Branch];
            double[] RealOutput = new double[OutputSize];
            TrainNetwork = new TwoLayerNet(NetInput.Length, NeuronNum, OutputSize);       //網路初始化


            for (int k = 0; k < epoch; k++)
            //for (int k = 0; k < 1; k++)
            {
                double errorEachepoch = 0;
                //對輸入進來的482筆資料做訓練,DataNumber = 482
                for (int i = 0; i < DataNumber; i++)
                //for (int i = 0; i < 3; i++)
                {
                    //處理要輸入進網路的資料NetInput
                    for (int j = 0; j < Branch; j++)
                    {
                        NetInput[0 + 4 * j] = InputData[i * Branch + j][0];
                        NetInput[1 + 4 * j] = InputData[i * Branch + j][1];
                        NetInput[2 + 4 * j] = InputData[i * Branch + j][2];
                        NetInput[3 + 4 * j] = InputData[i * Branch + j][3];
                    }
                    //處理該筆資料的RealOutput
                    if (DataOutput[i * Branch] == 1)
                    {
                        RealOutput[0] = 1;
                        RealOutput[1] = 0;
                    }
                    else
                    {
                        RealOutput[0] = 0;
                        RealOutput[1] = 1;
                    }
                    //進行forward
                    NetOutput = TrainNetwork.forward(NetInput);
                    TrainNetwork.backward(LearningRate, RealOutput, NetInput);
                    errorEachepoch += TrainNetwork.loss(NetOutput, RealOutput);
                    //listBox_NetworkState.Items.Add(TrainNetwork.loss(NetOutput, RealOutput));
                }
                errorEachepoch = errorEachepoch / DataNumber;
                listBox_NetworkState.Items.Add(k + " : " + errorEachepoch);


                /*
                 * listBox_NetworkState.Items.Add("Weight : ");
                 * for (int i = 0; i < TrainNetwork.Weight1.Length; i++)
                 * {
                 *  string weight_col = "";
                 *  for (int j = 0; j < TrainNetwork.Weight1[0].Length; j++)
                 *  {
                 *      weight_col += TrainNetwork.Weight1[i][j].ToString("f2") + "\t";
                 *  }
                 *  listBox_NetworkState.Items.Add(weight_col);
                 * }
                 *
                 * listBox_NetworkState.Items.Add("Bias : ");
                 * for (int i = 0; i < TrainNetwork.Bias1.Length; i++)
                 * {
                 *  listBox_NetworkState.Items.Add(TrainNetwork.Bias1[i].ToString("f2"));
                 * }
                 *
                 * //NetOutput = TrainNetwork.forward(NetInput);
                 *
                 *
                 * listBox_NetworkState.Items.Add("Sum : " + TestSum);
                 *
                 * listBox_NetworkState.Items.Add("loss : ");
                 * listBox_NetworkState.Items.Add(TrainNetwork.loss(NetInput, RealOutput));
                 * TrainNetwork.backward(LearningRate, RealOutput, NetInput);
                 */
            }
        }