public void Demo_011_Compare_NN_SVM_LR() { Console.WriteLine("ニューラルネットワーク、サポートベクトルマシン、線形回帰を比較します。"); Console.WriteLine("WriteLineはPCの処理能力を使うので、裏ですべて計算してから、結果をお見せします。"); //計算時間 DateTime start_NN; DateTime finish_NN; TimeSpan span_NN_epoch_100k; DateTime start_SVM; DateTime finish_SVM; TimeSpan span_SVM; DateTime start_LR; DateTime finish_LR; TimeSpan span_LR; List <double[, ]> list_x = new List <double[, ]>(); List <double[, ]> list_t = new List <double[, ]>(); for (int j = 0; j < 9; j++) { double[,] x = new double[1, 1]; x[0, 0] = j; list_x.Add(x); double[,] t = new double[1, 1]; if (j < 3 || 5 < j) { t[0, 0] = -1; } else { t[0, 0] = 1; } list_t.Add(t); } //Console.WriteLine("次は、2層のNeural Networkで試してみます"); //Console.WriteLine("第1層の計数行列はwは2行1列の行列とします"); //Console.WriteLine("活性化関数はSigmoid関数とします"); double[,] w_1 = new double[2, 1]; w_1[0, 0] = 10; w_1[1, 0] = -10; //Console.WriteLine("第1層の計数行列 w"); //this.Show_Matrix_Element(w_1); double[,] b_1 = new double[2, 1]; b_1[0, 0] = -35; b_1[1, 0] = 55; //Console.WriteLine("第1層のバイアスベクトル b"); //this.Show_Matrix_Element(b_1); //第一層は隠れ層なので、別のクラスです Hidden_Layer hd_1 = new Hidden_Layer(); hd_1.Preset_1_4th_Set_w(w_1); hd_1.Preset_2_4th_Set_b(b_1); hd_1.Preset_3_4th_Set_Hyper_Parameter(0.01, 0, 0, 0); hd_1.Preset_4_4th_Set_activation_Function(new Sigmoid_IFunction()); //Console.WriteLine("\n"); //Console.WriteLine("第2層の計数行列はwは1行2列の行列とします"); //Console.WriteLine("活性化関数は、第2層はHyperbolic_Tangent関数とします"); double[,] w_2 = new double[1, 2]; w_2[0, 0] = 20; w_2[0, 1] = 20; //Console.WriteLine("第2層の計数行列 w"); //this.Show_Matrix_Element(w_2); double[,] b_2 = new double[1, 1]; b_2[0, 0] = -30; //Console.WriteLine("第2層のバイアスベクトル b"); //this.Show_Matrix_Element(b_2); //Console.WriteLine("\n\n"); Regression_Final_Layer rfl_2 = new Regression_Final_Layer(); rfl_2.Preset_1_4th_Set_w(w_2); rfl_2.Preset_2_4th_Set_b(b_2); rfl_2.Preset_3_4th_Set_Hyper_Parameter(0.001, 0, 0, 0); rfl_2.Preset_4_4th_Set_activation_Function(new Hyperbolic_Tangent_IFunction()); //Console.WriteLine("\n\n"); int epoch = 1000; double[] error = new double[list_x.Count]; //double max_error = 0; //int max_k = 0; start_NN = DateTime.Now; for (int j = 0; j < list_x.Count * epoch; j++) { /* * //Console.WriteLine("epoch" + "\t" + j); * for (int k = 0; k < list_x.Count; k++) * { * hd_1.Step_1_3rd_Forward_Propagation(list_x[k]); * rfl_2.Step_1_3rd_Forward_Propagation(hd_1.Get_f_wx_plus_b()); * error[k] = Math.Abs(rfl_2.Get_f_wx_plus_b()[0, 0] - list_t[k][0, 0]); * //Console.WriteLine(k + "\t" + error[k].ToString("G3") + "\t" + list_t[k][0, 0] + "\t" + rfl_2.Get_f_wx_plus_b()[0, 0].ToString("G3") + "\t" + hd_1.Get_f_wx_plus_b()[0, 0].ToString("G3") + "\t" + hd_1.Get_f_wx_plus_b()[1, 0].ToString("G3")); * } * * max_k = 0; * max_error = error[0]; * for (int k = 1; k < list_x.Count; k++) * { * if (max_error < error[k]) * { * max_error = error[k]; * max_k = k; * } * } * //Console.WriteLine("Max error is No." + max_k); * //Console.WriteLine(" "); */ //順伝搬 hd_1.Step_1_3rd_Forward_Propagation(list_x[j % list_x.Count]); rfl_2.Step_1_3rd_Forward_Propagation(hd_1.Get_f_wx_plus_b()); //逆伝搬 rfl_2.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j % list_t.Count]); //hd_1.Step_2_3rd_Calculate_Delta(rfl_2.Get_w(), rfl_2.Get_delta()); //パラメータの更新 rfl_2.Step_3_3rd_Update(); //hd_1.Step_3_3rd_Update(); } finish_NN = DateTime.Now; span_NN_epoch_100k = finish_NN - start_NN; double[,] y_NN = new double[list_x.Count, 1]; for (int j = 0; j < 9; j++) { //順伝搬 hd_1.Step_1_3rd_Forward_Propagation(list_x[j]); rfl_2.Step_1_3rd_Forward_Propagation(hd_1.Get_f_wx_plus_b()); y_NN[j, 0] = rfl_2.Get_f_wx_plus_b()[0, 0]; } //SVM、線形回帰の計画行列を定義します。 double[,] X = new double[9, 1]; double[,] t_vec = new double[9, 1]; //Console.WriteLine("\t" + "入力x\t" + "教師t"); for (int j = 0; j < 9; j++) { X[j, 0] = j; if (j < 3 || 5 < j) { t_vec[j, 0] = -1; } else { t_vec[j, 0] = 1; } //Console.WriteLine("\t" + X[j, 0] + "\t" + t_vec[j, 0] + ""); } //SVMの学習です start_SVM = DateTime.Now; double[,] variance_covariance = Design_Matrix.Variance_Covariance_Matrix(X); //係数Aを学習する double[,] Coefficient_A = Support_Vector_Machine.Learned_Coefficient_A(t_vec, X, new Power_of_10_IKernel(), variance_covariance); finish_SVM = DateTime.Now; span_SVM = finish_SVM - start_SVM; double[,] classified = Support_Vector_Machine.Classification_Design_Matrix(t_vec, X, new Power_of_10_IKernel(), variance_covariance, Coefficient_A, X); //線形回帰用の計画行列です。 double[,] phi_X = new double[9, 3]; //Console.WriteLine("\t" + "教師t" + "\t" + "exp(-(x-0.5)^2/2 )/√2π" + "\t" + "exp(-(x-2.5)^2/2 )/√2π" + "\t" + "exp(-(x-4.5)^2/2 )/√2π"); for (int j = 0; j < 9; j++) { phi_X[j, 0] = Math.Exp(-(X[j, 0] - 1) * (X[j, 0] - 1) / 2.0) / Math.Sqrt(2 * Math.PI); phi_X[j, 1] = Math.Exp(-(X[j, 0] - 4) * (X[j, 0] - 4) / 2.0) / Math.Sqrt(2 * Math.PI); phi_X[j, 2] = Math.Exp(-(X[j, 0] - 7) * (X[j, 0] - 7) / 2.0) / Math.Sqrt(2 * Math.PI); //Console.WriteLine("\t" + t_vec[j, 0] + "\t" + phi_X[j, 0].ToString("G2") + "\t\t\t\t" + phi_X[j, 1].ToString("G2") + "\t\t\t\t" + phi_X[j, 2].ToString("G2")); } start_LR = DateTime.Now; double[,] w = Liner_Regression.Learning_parameter_w_column_vector(phi_X, t_vec); finish_LR = DateTime.Now; span_LR = finish_LR - start_LR; double[,] y_LR = Liner_Regression.Regression_Design_Matrix(phi_X, w); Console.WriteLine("入力x" + "\t" + "答えt" + "\t" + "NN" + "\t" + "SVM" + "\t" + "LR"); for (int j = 0; j < 9; j++) { Console.Write(X[j, 0] + "\t"); Console.Write(t_vec[j, 0] + "\t"); Console.Write(y_NN[j, 0].ToString("G2") + "\t"); Console.Write(classified[j, 0].ToString("G2") + "\t"); Console.Write(y_LR[j, 0].ToString("G2") + "\t"); Console.WriteLine(" "); } Console.WriteLine(" "); Console.WriteLine("NNの計算時間\t\t" + span_NN_epoch_100k.Minutes + "分" + span_NN_epoch_100k.Seconds + "秒"); Console.WriteLine("SVMの計算時間\t\t" + span_SVM.Minutes + "分" + span_SVM.Seconds + "秒"); Console.WriteLine("LRの計算時間\t\t" + span_LR.Minutes + "分" + span_LR.Seconds + "秒"); Console.WriteLine("\n\n" + "NNの精度が良くないですね。"); Console.WriteLine("計数行列wの初期値や、学習回数、ハイパーパラメータなどを調整したのですが、これが限界でした。"); }
public void Demo_006_Neural_Network_2() { Console.WriteLine("Neural Networkのデモンストレーションです"); Console.WriteLine("前回は入力が1次元で、教師データが0と1でした。"); Console.WriteLine("今回は教師データを-1と1にした場合、どの程度学習に影響を及ぼすかを調べます。"); //教師データを設定します。 //簡単のためf(x)の一次元とします。 List <double[, ]> list_x = new List <double[, ]>(); List <double[, ]> list_t = new List <double[, ]>(); for (int j = 0; j < 4; j++) { double[,] x = new double[1, 1]; x[0, 0] = j * 1.0; list_x.Add(x); double[,] t = new double[1, 1]; if (j > 1) { t[0, 0] = 1; } else { t[0, 0] = -1; } list_t.Add(t); } Console.WriteLine("今回の入力値xと、答えtです"); Console.WriteLine("x > 1 のとき、答えが1になります。それ以外の場合、-1になります。"); Console.Write("x\t"); foreach (double[,] x in list_x) { Console.Write(x[0, 0] + "\t"); } Console.WriteLine("\t"); Console.Write("t\t"); foreach (double[,] t in list_t) { Console.Write(t[0, 0] + "\t"); } Console.WriteLine("\n"); Console.WriteLine("計数行列Wとバイアスベクトルbを設定します。"); Console.WriteLine("今回のNeural Networkの層は1層とします。"); double[,] W = new double[1, 1]; W[0, 0] = 3; double[,] b = new double[1, 1]; b[0, 0] = 2; Console.WriteLine("W[0,0] = " + W[0, 0] + "\t" + "b[0,0] = " + b[0, 0] + "\n\n"); Console.WriteLine("Neural Netoworkを設定しました。負の数を出力するため、活性化関数はHyperbolic_Tangent関数とします"); Console.WriteLine("ハイパーパラメータの値は0.01とします。\n"); Regression_Final_Layer rfl = new Regression_Final_Layer(); rfl.Preset_1_4th_Set_w(W); rfl.Preset_2_4th_Set_b(b); rfl.Preset_3_4th_Set_Hyper_Parameter(0.01, 0, 0, 0); rfl.Preset_4_4th_Set_activation_Function(new Hyperbolic_Tangent_IFunction()); Console.WriteLine("学習前のNeural Networkの出力を確認します。"); Console.WriteLine("f(wx + b)とtの値が近ければ、良い近似であると言えます"); List <double[, ]> list_wx_plus_b = new List <double[, ]>(); List <double[, ]> list_f_wx_plus_b = new List <double[, ]>(); List <double[, ]> list_delta = new List <double[, ]>(); for (int j = 0; j < list_x.Count; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j]); list_wx_plus_b.Add(rfl.Get_wx_plus_b()); list_f_wx_plus_b.Add(rfl.Get_f_wx_plus_b()); } Console.WriteLine("W " + rfl.Get_w()[0, 0] + "\t" + "b " + rfl.Get_b()[0, 0]); Console.Write("x\t\t"); foreach (double[,] x in list_x) { Console.Write(x[0, 0] + "\t"); } Console.WriteLine(" "); Console.Write("wx + b\t\t"); foreach (double[,] x in list_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("f(wx + b)\t"); foreach (double[,] x in list_f_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("t\t\t"); foreach (double[,] t in list_t) { Console.Write(t[0, 0] + "\t"); } Console.WriteLine("\n\n"); Console.WriteLine("1 epoch 学習しました。予測精度がどの程度変わったのかを確認しましょう"); for (int j = 0; j < list_x.Count; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j]); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j]); rfl.Step_3_3rd_Update(); } list_wx_plus_b = new List <double[, ]>(); list_f_wx_plus_b = new List <double[, ]>(); list_delta = new List <double[, ]>(); for (int j = 0; j < list_x.Count; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j]); list_wx_plus_b.Add(rfl.Get_wx_plus_b()); list_f_wx_plus_b.Add(rfl.Get_f_wx_plus_b()); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j]); list_delta.Add(rfl.Get_delta()); } Console.WriteLine("W " + rfl.Get_w()[0, 0] + "\t" + "b " + rfl.Get_b()[0, 0]); Console.Write("x\t\t"); foreach (double[,] x in list_x) { Console.Write(x[0, 0] + "\t"); } Console.WriteLine(" "); Console.Write("wx + b\t\t"); foreach (double[,] x in list_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("f(wx + b)\t"); foreach (double[,] x in list_f_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("t\t\t"); foreach (double[,] t in list_t) { Console.Write(t[0, 0] + "\t"); } Console.WriteLine(" "); Console.WriteLine("\n\n"); Console.WriteLine("10 epoch 学習しました。"); for (int j = 0; j < list_x.Count * 9; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j % list_x.Count]); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j % list_x.Count]); rfl.Step_3_3rd_Update(); } list_wx_plus_b = new List <double[, ]>(); list_f_wx_plus_b = new List <double[, ]>(); list_delta = new List <double[, ]>(); for (int j = 0; j < list_x.Count; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j]); list_wx_plus_b.Add(rfl.Get_wx_plus_b()); list_f_wx_plus_b.Add(rfl.Get_f_wx_plus_b()); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j]); list_delta.Add(rfl.Get_delta()); } Console.WriteLine("W " + rfl.Get_w()[0, 0] + "\t" + "b " + rfl.Get_b()[0, 0]); Console.Write("x\t\t"); foreach (double[,] x in list_x) { Console.Write(x[0, 0] + "\t"); } Console.WriteLine(" "); Console.Write("wx + b\t\t"); foreach (double[,] x in list_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("f(wx + b)\t"); foreach (double[,] x in list_f_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("t\t\t"); foreach (double[,] t in list_t) { Console.Write(t[0, 0] + "\t"); } Console.WriteLine(" "); Console.WriteLine("\n\n"); Console.WriteLine("100 epoch 学習しました。"); for (int j = 0; j < list_x.Count * 90; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j % list_x.Count]); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j % list_x.Count]); rfl.Step_3_3rd_Update(); } list_wx_plus_b = new List <double[, ]>(); list_f_wx_plus_b = new List <double[, ]>(); list_delta = new List <double[, ]>(); for (int j = 0; j < list_x.Count; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j]); list_wx_plus_b.Add(rfl.Get_wx_plus_b()); list_f_wx_plus_b.Add(rfl.Get_f_wx_plus_b()); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j]); list_delta.Add(rfl.Get_delta()); } Console.WriteLine("W " + rfl.Get_w()[0, 0] + "\t" + "b " + rfl.Get_b()[0, 0]); Console.Write("x\t\t"); foreach (double[,] x in list_x) { Console.Write(x[0, 0] + "\t"); } Console.WriteLine(" "); Console.Write("wx + b\t\t"); foreach (double[,] x in list_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("f(wx + b)\t"); foreach (double[,] x in list_f_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("t\t\t"); foreach (double[,] t in list_t) { Console.Write(t[0, 0] + "\t"); } Console.WriteLine(" "); Console.WriteLine("\n\n"); Console.WriteLine("1,000 epoch 学習しました。"); for (int j = 0; j < list_x.Count * 900; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j % list_x.Count]); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j % list_x.Count]); rfl.Step_3_3rd_Update(); } list_wx_plus_b = new List <double[, ]>(); list_f_wx_plus_b = new List <double[, ]>(); list_delta = new List <double[, ]>(); for (int j = 0; j < list_x.Count; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j]); list_wx_plus_b.Add(rfl.Get_wx_plus_b()); list_f_wx_plus_b.Add(rfl.Get_f_wx_plus_b()); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j]); list_delta.Add(rfl.Get_delta()); } Console.WriteLine("W " + rfl.Get_w()[0, 0] + "\t" + "b " + rfl.Get_b()[0, 0]); Console.Write("x\t\t"); foreach (double[,] x in list_x) { Console.Write(x[0, 0] + "\t"); } Console.WriteLine(" "); Console.Write("wx + b\t\t"); foreach (double[,] x in list_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("f(wx + b)\t"); foreach (double[,] x in list_f_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("t\t\t"); foreach (double[,] t in list_t) { Console.Write(t[0, 0] + "\t"); } Console.WriteLine(" "); Console.WriteLine("\n\n"); Console.WriteLine("5,000 epoch 学習しました。"); for (int j = 0; j < list_x.Count * 4000; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j % list_x.Count]); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j % list_x.Count]); rfl.Step_3_3rd_Update(); } list_wx_plus_b = new List <double[, ]>(); list_f_wx_plus_b = new List <double[, ]>(); list_delta = new List <double[, ]>(); for (int j = 0; j < list_x.Count; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j]); list_wx_plus_b.Add(rfl.Get_wx_plus_b()); list_f_wx_plus_b.Add(rfl.Get_f_wx_plus_b()); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j]); list_delta.Add(rfl.Get_delta()); } Console.WriteLine("W " + rfl.Get_w()[0, 0] + "\t" + "b " + rfl.Get_b()[0, 0]); Console.Write("x\t\t"); foreach (double[,] x in list_x) { Console.Write(x[0, 0] + "\t"); } Console.WriteLine(" "); Console.Write("wx + b\t\t"); foreach (double[,] x in list_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("f(wx + b)\t"); foreach (double[,] x in list_f_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("t\t\t"); foreach (double[,] t in list_t) { Console.Write(t[0, 0] + "\t"); } Console.WriteLine(" "); Console.WriteLine("\n\n"); Console.WriteLine("10,000 epoch 学習しました。"); for (int j = 0; j < list_x.Count * 5000; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j % list_x.Count]); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j % list_x.Count]); rfl.Step_3_3rd_Update(); } list_wx_plus_b = new List <double[, ]>(); list_f_wx_plus_b = new List <double[, ]>(); list_delta = new List <double[, ]>(); for (int j = 0; j < list_x.Count; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j]); list_wx_plus_b.Add(rfl.Get_wx_plus_b()); list_f_wx_plus_b.Add(rfl.Get_f_wx_plus_b()); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j]); list_delta.Add(rfl.Get_delta()); } Console.WriteLine("W " + rfl.Get_w()[0, 0] + "\t" + "b " + rfl.Get_b()[0, 0]); Console.Write("x\t\t"); foreach (double[,] x in list_x) { Console.Write(x[0, 0] + "\t"); } Console.WriteLine(" "); Console.Write("wx + b\t\t"); foreach (double[,] x in list_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("f(wx + b)\t"); foreach (double[,] x in list_f_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("t\t\t"); foreach (double[,] t in list_t) { Console.Write(t[0, 0] + "\t"); } Console.WriteLine(" "); Console.WriteLine("\n\n"); Console.WriteLine("20,000 epoch 学習しました。"); for (int j = 0; j < list_x.Count * 10000; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j % list_x.Count]); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j % list_x.Count]); rfl.Step_3_3rd_Update(); } list_wx_plus_b = new List <double[, ]>(); list_f_wx_plus_b = new List <double[, ]>(); list_delta = new List <double[, ]>(); for (int j = 0; j < list_x.Count; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j]); list_wx_plus_b.Add(rfl.Get_wx_plus_b()); list_f_wx_plus_b.Add(rfl.Get_f_wx_plus_b()); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j]); list_delta.Add(rfl.Get_delta()); } Console.WriteLine("W " + rfl.Get_w()[0, 0] + "\t" + "b " + rfl.Get_b()[0, 0]); Console.Write("x\t\t"); foreach (double[,] x in list_x) { Console.Write(x[0, 0] + "\t"); } Console.WriteLine(" "); Console.Write("wx + b\t\t"); foreach (double[,] x in list_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("f(wx + b)\t"); foreach (double[,] x in list_f_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("t\t\t"); foreach (double[,] t in list_t) { Console.Write(t[0, 0] + "\t"); } Console.WriteLine(" "); Console.WriteLine("\n\n"); Console.WriteLine("50,000 epoch 学習しました。"); for (int j = 0; j < list_x.Count * 30000; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j % list_x.Count]); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j % list_x.Count]); rfl.Step_3_3rd_Update(); } list_wx_plus_b = new List <double[, ]>(); list_f_wx_plus_b = new List <double[, ]>(); list_delta = new List <double[, ]>(); for (int j = 0; j < list_x.Count; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j]); list_wx_plus_b.Add(rfl.Get_wx_plus_b()); list_f_wx_plus_b.Add(rfl.Get_f_wx_plus_b()); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j]); list_delta.Add(rfl.Get_delta()); } Console.WriteLine("W " + rfl.Get_w()[0, 0] + "\t" + "b " + rfl.Get_b()[0, 0]); Console.Write("x\t\t"); foreach (double[,] x in list_x) { Console.Write(x[0, 0] + "\t"); } Console.WriteLine(" "); Console.Write("wx + b\t\t"); foreach (double[,] x in list_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("f(wx + b)\t"); foreach (double[,] x in list_f_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("t\t\t"); foreach (double[,] t in list_t) { Console.Write(t[0, 0] + "\t"); } Console.WriteLine(" "); Console.WriteLine("\n\n"); Console.WriteLine("100,000 epoch 学習しました。"); for (int j = 0; j < list_x.Count * 50000; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j % list_x.Count]); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j % list_x.Count]); rfl.Step_3_3rd_Update(); } list_wx_plus_b = new List <double[, ]>(); list_f_wx_plus_b = new List <double[, ]>(); list_delta = new List <double[, ]>(); for (int j = 0; j < list_x.Count; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j]); list_wx_plus_b.Add(rfl.Get_wx_plus_b()); list_f_wx_plus_b.Add(rfl.Get_f_wx_plus_b()); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j]); list_delta.Add(rfl.Get_delta()); } Console.WriteLine("W " + rfl.Get_w()[0, 0] + "\t" + "b " + rfl.Get_b()[0, 0]); Console.Write("x\t\t"); foreach (double[,] x in list_x) { Console.Write(x[0, 0] + "\t"); } Console.WriteLine(" "); Console.Write("wx + b\t\t"); foreach (double[,] x in list_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("f(wx + b)\t"); foreach (double[,] x in list_f_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("t\t\t"); foreach (double[,] t in list_t) { Console.Write(t[0, 0] + "\t"); } Console.WriteLine(" "); Console.WriteLine("\n\n"); Console.WriteLine("1,000,000 epoch 学習しました。"); for (int j = 0; j < list_x.Count * 900000; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j % list_x.Count]); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j % list_x.Count]); rfl.Step_3_3rd_Update(); } list_wx_plus_b = new List <double[, ]>(); list_f_wx_plus_b = new List <double[, ]>(); list_delta = new List <double[, ]>(); for (int j = 0; j < list_x.Count; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j]); list_wx_plus_b.Add(rfl.Get_wx_plus_b()); list_f_wx_plus_b.Add(rfl.Get_f_wx_plus_b()); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j]); list_delta.Add(rfl.Get_delta()); } Console.WriteLine("W " + rfl.Get_w()[0, 0] + "\t" + "b " + rfl.Get_b()[0, 0]); Console.Write("x\t\t"); foreach (double[,] x in list_x) { Console.Write(x[0, 0] + "\t"); } Console.WriteLine(" "); Console.Write("wx + b\t\t"); foreach (double[,] x in list_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("f(wx + b)\t"); foreach (double[,] x in list_f_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("t\t\t"); foreach (double[,] t in list_t) { Console.Write(t[0, 0] + "\t"); } Console.WriteLine(" "); Console.WriteLine("\n\n"); Console.WriteLine("教師データを-1と1にしたとたん、学習効率が格段に落ちました"); Console.WriteLine("この現象は、学習すべき計数行列とバイアスベクトルの条件が異なることが原因です。\n\n"); Console.WriteLine("Demo005ではx > 1 のとき1が出力されて、xが1以下のとき0が出力されるようパラメータを学習しました。 "); Console.WriteLine("すなわち、Sigmoid( w*1 + b )≒0、Sigmoid( w*2 + b )≒1となるw、bを学習しました。"); Console.WriteLine("これを満たすw、bの条件は下記となります。"); Console.WriteLine("01 : w + b << 0"); Console.WriteLine("02 : 2w + b >> 0"); Console.WriteLine("\n" + "Sigmoid関数の変数(wx+b)の符号がx=1、2で切り替わりその絶対値が大きいことが条件になります。"); Console.WriteLine("これを満たすw , bの条件は下記になります。"); Console.WriteLine("01 : w >> 0"); Console.WriteLine("02 : b << 0"); Console.WriteLine("03 : w < |b| < 2w \n\n"); Console.WriteLine("Demo006ではx > 1 のとき1が出力されて、xが1以下のとき-1が出力されるようパラメータを学習しました。 "); Console.WriteLine("すなわち、tanh( w*1 + b )≒-1、tanh( w*2 + b )≒1となるw、bを学習しました。"); Console.WriteLine("これを満たすw、bの条件は下記となります。"); Console.WriteLine("01 : w + b << 0"); Console.WriteLine("02 : 2w + b >> 0"); Console.WriteLine("03 : -( w + b ) ≒ 2w + b ∵ -tanh( w + b ) ≒ tanh( 2w + b )"); Console.WriteLine("\n" + "tanh(wx+b)は(wx+b)=1.5を堺に符号が反転することが求められます。"); Console.WriteLine("これを満たすw , bの条件は下記になります。"); Console.WriteLine("01 : w >> 0"); Console.WriteLine("02 : b << 0"); Console.WriteLine("03 : b ≒ -3w/2 \n\n"); Console.WriteLine("Demo005とDemo006との差異を下記に示します。"); Console.WriteLine("Demo005 : w < |b| < 2w"); Console.WriteLine("Demo006 : b ≒ -3w/2\n"); Console.WriteLine("Demo006の方がw , bの関係の制約が厳しいです。"); Console.WriteLine("しかし、w , bを学習するための手法(誤差逆伝搬法)は、それぞれのパラメータを独立に更新する手法です。"); Console.WriteLine("パラメータ同士の関係が考慮されないため、demo006は学習効率が低かったと考えられます。\n\n"); Console.WriteLine("学習データの前処理や層の設定などの工夫で、学習効率を上げることができると考えられます"); }
public void Demo_005_Neural_Network_1() { Console.WriteLine("Neural Networkのデモンストレーションです"); Console.WriteLine("学習結果がわかりやすいように、1次元のデータで紹介します。\n\n"); //教師データを設定します。 //簡単のためf(x)の一次元とします。 List <double[, ]> list_x = new List <double[, ]>(); List <double[, ]> list_t = new List <double[, ]>(); for (int j = 0; j < 4; j++) { double[,] x = new double[1, 1]; x[0, 0] = j * 1.0; list_x.Add(x); double[,] t = new double[1, 1]; if (j > 1) { t[0, 0] = 1; } else { t[0, 0] = 0; } list_t.Add(t); } Console.WriteLine("今回の入力値xと、答えtです"); Console.WriteLine("x > 1 のとき、答えが1になります。それ以外の場合、0になります。"); Console.Write("x\t"); foreach (double[,] x in list_x) { Console.Write(x[0, 0] + "\t"); } Console.WriteLine("\t"); Console.Write("t\t"); foreach (double[,] t in list_t) { Console.Write(t[0, 0] + "\t"); } Console.WriteLine("\n"); Console.WriteLine("計数行列Wとバイアスベクトルbを設定します。"); Console.WriteLine("今回のNeural Networkの層は1層とします。"); double[,] W = new double[1, 1]; W[0, 0] = 3; double[,] b = new double[1, 1]; b[0, 0] = 2; Console.WriteLine("W[0,0] = " + W[0, 0] + "\t" + "b[0,0] = " + b[0, 0] + "\n\n"); Console.WriteLine("Neural Netoworkを設定しました。今回の活性化関数はSigmoid関数とします"); Console.WriteLine("ハイパーパラメータの値は0.01とします。\n"); Regression_Final_Layer rfl = new Regression_Final_Layer(); rfl.Preset_1_4th_Set_w(W); rfl.Preset_2_4th_Set_b(b); rfl.Preset_3_4th_Set_Hyper_Parameter(0.01, 0, 0, 0); rfl.Preset_4_4th_Set_activation_Function(new Sigmoid_IFunction()); Console.WriteLine("学習前のNeural Networkの出力を確認します。"); Console.WriteLine("f(wx + b)とtの値が近ければ、良い近似であると言えます"); List <double[, ]> list_wx_plus_b = new List <double[, ]>(); List <double[, ]> list_f_wx_plus_b = new List <double[, ]>(); List <double[, ]> list_delta = new List <double[, ]>(); for (int j = 0; j < list_x.Count; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j]); list_wx_plus_b.Add(rfl.Get_wx_plus_b()); list_f_wx_plus_b.Add(rfl.Get_f_wx_plus_b()); } Console.WriteLine("W " + rfl.Get_w()[0, 0] + "\t" + "b " + rfl.Get_b()[0, 0]); Console.Write("x\t\t"); foreach (double[,] x in list_x) { Console.Write(x[0, 0] + "\t"); } Console.WriteLine(" "); Console.Write("wx + b\t\t"); foreach (double[,] x in list_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("f(wx + b)\t"); foreach (double[,] x in list_f_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("t\t\t"); foreach (double[,] t in list_t) { Console.Write(t[0, 0] + "\t"); } Console.WriteLine("\n\n"); Console.WriteLine("1 epoch 学習しました。予測精度がどの程度変わったのかを確認しましょう"); for (int j = 0; j < list_x.Count; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j]); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j]); rfl.Step_3_3rd_Update(); } list_wx_plus_b = new List <double[, ]>(); list_f_wx_plus_b = new List <double[, ]>(); list_delta = new List <double[, ]>(); for (int j = 0; j < list_x.Count; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j]); list_wx_plus_b.Add(rfl.Get_wx_plus_b()); list_f_wx_plus_b.Add(rfl.Get_f_wx_plus_b()); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j]); list_delta.Add(rfl.Get_delta()); } Console.WriteLine("W " + rfl.Get_w()[0, 0] + "\t" + "b " + rfl.Get_b()[0, 0]); Console.Write("x\t\t"); foreach (double[,] x in list_x) { Console.Write(x[0, 0] + "\t"); } Console.WriteLine(" "); Console.Write("wx + b\t\t"); foreach (double[,] x in list_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("f(wx + b)\t"); foreach (double[,] x in list_f_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("t\t\t"); foreach (double[,] t in list_t) { Console.Write(t[0, 0] + "\t"); } Console.WriteLine(" "); Console.WriteLine("\n\n"); Console.WriteLine("10 epoch 学習しました。"); for (int j = 0; j < list_x.Count * 9; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j % list_x.Count]); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j % list_x.Count]); rfl.Step_3_3rd_Update(); } list_wx_plus_b = new List <double[, ]>(); list_f_wx_plus_b = new List <double[, ]>(); list_delta = new List <double[, ]>(); for (int j = 0; j < list_x.Count; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j]); list_wx_plus_b.Add(rfl.Get_wx_plus_b()); list_f_wx_plus_b.Add(rfl.Get_f_wx_plus_b()); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j]); list_delta.Add(rfl.Get_delta()); } Console.WriteLine("W " + rfl.Get_w()[0, 0] + "\t" + "b " + rfl.Get_b()[0, 0]); Console.Write("x\t\t"); foreach (double[,] x in list_x) { Console.Write(x[0, 0] + "\t"); } Console.WriteLine(" "); Console.Write("wx + b\t\t"); foreach (double[,] x in list_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("f(wx + b)\t"); foreach (double[,] x in list_f_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("t\t\t"); foreach (double[,] t in list_t) { Console.Write(t[0, 0] + "\t"); } Console.WriteLine(" "); Console.WriteLine("\n\n"); Console.WriteLine("100 epoch 学習しました。"); for (int j = 0; j < list_x.Count * 90; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j % list_x.Count]); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j % list_x.Count]); rfl.Step_3_3rd_Update(); } list_wx_plus_b = new List <double[, ]>(); list_f_wx_plus_b = new List <double[, ]>(); list_delta = new List <double[, ]>(); for (int j = 0; j < list_x.Count; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j]); list_wx_plus_b.Add(rfl.Get_wx_plus_b()); list_f_wx_plus_b.Add(rfl.Get_f_wx_plus_b()); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j]); list_delta.Add(rfl.Get_delta()); } Console.WriteLine("W " + rfl.Get_w()[0, 0] + "\t" + "b " + rfl.Get_b()[0, 0]); Console.Write("x\t\t"); foreach (double[,] x in list_x) { Console.Write(x[0, 0] + "\t"); } Console.WriteLine(" "); Console.Write("wx + b\t\t"); foreach (double[,] x in list_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("f(wx + b)\t"); foreach (double[,] x in list_f_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("t\t\t"); foreach (double[,] t in list_t) { Console.Write(t[0, 0] + "\t"); } Console.WriteLine(" "); Console.WriteLine("\n\n"); Console.WriteLine("1,000 epoch 学習しました。"); for (int j = 0; j < list_x.Count * 900; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j % list_x.Count]); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j % list_x.Count]); rfl.Step_3_3rd_Update(); } list_wx_plus_b = new List <double[, ]>(); list_f_wx_plus_b = new List <double[, ]>(); list_delta = new List <double[, ]>(); for (int j = 0; j < list_x.Count; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j]); list_wx_plus_b.Add(rfl.Get_wx_plus_b()); list_f_wx_plus_b.Add(rfl.Get_f_wx_plus_b()); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j]); list_delta.Add(rfl.Get_delta()); } Console.WriteLine("W " + rfl.Get_w()[0, 0] + "\t" + "b " + rfl.Get_b()[0, 0]); Console.Write("x\t\t"); foreach (double[,] x in list_x) { Console.Write(x[0, 0] + "\t"); } Console.WriteLine(" "); Console.Write("wx + b\t\t"); foreach (double[,] x in list_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("f(wx + b)\t"); foreach (double[,] x in list_f_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("t\t\t"); foreach (double[,] t in list_t) { Console.Write(t[0, 0] + "\t"); } Console.WriteLine(" "); Console.WriteLine("\n\n"); Console.WriteLine("5,000 epoch 学習しました。"); for (int j = 0; j < list_x.Count * 4000; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j % list_x.Count]); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j % list_x.Count]); rfl.Step_3_3rd_Update(); } list_wx_plus_b = new List <double[, ]>(); list_f_wx_plus_b = new List <double[, ]>(); list_delta = new List <double[, ]>(); for (int j = 0; j < list_x.Count; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j]); list_wx_plus_b.Add(rfl.Get_wx_plus_b()); list_f_wx_plus_b.Add(rfl.Get_f_wx_plus_b()); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j]); list_delta.Add(rfl.Get_delta()); } Console.WriteLine("W " + rfl.Get_w()[0, 0] + "\t" + "b " + rfl.Get_b()[0, 0]); Console.Write("x\t\t"); foreach (double[,] x in list_x) { Console.Write(x[0, 0] + "\t"); } Console.WriteLine(" "); Console.Write("wx + b\t\t"); foreach (double[,] x in list_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("f(wx + b)\t"); foreach (double[,] x in list_f_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("t\t\t"); foreach (double[,] t in list_t) { Console.Write(t[0, 0] + "\t"); } Console.WriteLine(" "); Console.WriteLine("\n\n"); Console.WriteLine("10,000 epoch 学習しました。"); for (int j = 0; j < list_x.Count * 5000; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j % list_x.Count]); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j % list_x.Count]); rfl.Step_3_3rd_Update(); } list_wx_plus_b = new List <double[, ]>(); list_f_wx_plus_b = new List <double[, ]>(); list_delta = new List <double[, ]>(); for (int j = 0; j < list_x.Count; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j]); list_wx_plus_b.Add(rfl.Get_wx_plus_b()); list_f_wx_plus_b.Add(rfl.Get_f_wx_plus_b()); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j]); list_delta.Add(rfl.Get_delta()); } Console.WriteLine("W " + rfl.Get_w()[0, 0] + "\t" + "b " + rfl.Get_b()[0, 0]); Console.Write("x\t\t"); foreach (double[,] x in list_x) { Console.Write(x[0, 0] + "\t"); } Console.WriteLine(" "); Console.Write("wx + b\t\t"); foreach (double[,] x in list_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("f(wx + b)\t"); foreach (double[,] x in list_f_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("t\t\t"); foreach (double[,] t in list_t) { Console.Write(t[0, 0] + "\t"); } Console.WriteLine(" "); Console.WriteLine("\n\n"); Console.WriteLine("20,000 epoch 学習しました。"); for (int j = 0; j < list_x.Count * 10000; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j % list_x.Count]); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j % list_x.Count]); rfl.Step_3_3rd_Update(); } list_wx_plus_b = new List <double[, ]>(); list_f_wx_plus_b = new List <double[, ]>(); list_delta = new List <double[, ]>(); for (int j = 0; j < list_x.Count; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j]); list_wx_plus_b.Add(rfl.Get_wx_plus_b()); list_f_wx_plus_b.Add(rfl.Get_f_wx_plus_b()); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j]); list_delta.Add(rfl.Get_delta()); } Console.WriteLine("W " + rfl.Get_w()[0, 0] + "\t" + "b " + rfl.Get_b()[0, 0]); Console.Write("x\t\t"); foreach (double[,] x in list_x) { Console.Write(x[0, 0] + "\t"); } Console.WriteLine(" "); Console.Write("wx + b\t\t"); foreach (double[,] x in list_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("f(wx + b)\t"); foreach (double[,] x in list_f_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("t\t\t"); foreach (double[,] t in list_t) { Console.Write(t[0, 0] + "\t"); } Console.WriteLine(" "); Console.WriteLine("\n\n"); Console.WriteLine("50,000 epoch 学習しました。"); for (int j = 0; j < list_x.Count * 30000; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j % list_x.Count]); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j % list_x.Count]); rfl.Step_3_3rd_Update(); } list_wx_plus_b = new List <double[, ]>(); list_f_wx_plus_b = new List <double[, ]>(); list_delta = new List <double[, ]>(); for (int j = 0; j < list_x.Count; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j]); list_wx_plus_b.Add(rfl.Get_wx_plus_b()); list_f_wx_plus_b.Add(rfl.Get_f_wx_plus_b()); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j]); list_delta.Add(rfl.Get_delta()); } Console.WriteLine("W " + rfl.Get_w()[0, 0] + "\t" + "b " + rfl.Get_b()[0, 0]); Console.Write("x\t\t"); foreach (double[,] x in list_x) { Console.Write(x[0, 0] + "\t"); } Console.WriteLine(" "); Console.Write("wx + b\t\t"); foreach (double[,] x in list_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("f(wx + b)\t"); foreach (double[,] x in list_f_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("t\t\t"); foreach (double[,] t in list_t) { Console.Write(t[0, 0] + "\t"); } Console.WriteLine(" "); Console.WriteLine("\n\n"); Console.WriteLine("100,000 epoch 学習しました。"); for (int j = 0; j < list_x.Count * 50000; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j % list_x.Count]); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j % list_x.Count]); rfl.Step_3_3rd_Update(); } list_wx_plus_b = new List <double[, ]>(); list_f_wx_plus_b = new List <double[, ]>(); list_delta = new List <double[, ]>(); for (int j = 0; j < list_x.Count; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j]); list_wx_plus_b.Add(rfl.Get_wx_plus_b()); list_f_wx_plus_b.Add(rfl.Get_f_wx_plus_b()); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j]); list_delta.Add(rfl.Get_delta()); } Console.WriteLine("W " + rfl.Get_w()[0, 0] + "\t" + "b " + rfl.Get_b()[0, 0]); Console.Write("x\t\t"); foreach (double[,] x in list_x) { Console.Write(x[0, 0] + "\t"); } Console.WriteLine(" "); Console.Write("wx + b\t\t"); foreach (double[,] x in list_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("f(wx + b)\t"); foreach (double[,] x in list_f_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("t\t\t"); foreach (double[,] t in list_t) { Console.Write(t[0, 0] + "\t"); } Console.WriteLine(" "); Console.WriteLine("\n\n"); Console.WriteLine("1,000,000 epoch 学習しました。"); for (int j = 0; j < list_x.Count * 900000; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j % list_x.Count]); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j % list_x.Count]); rfl.Step_3_3rd_Update(); } list_wx_plus_b = new List <double[, ]>(); list_f_wx_plus_b = new List <double[, ]>(); list_delta = new List <double[, ]>(); for (int j = 0; j < list_x.Count; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j]); list_wx_plus_b.Add(rfl.Get_wx_plus_b()); list_f_wx_plus_b.Add(rfl.Get_f_wx_plus_b()); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j]); list_delta.Add(rfl.Get_delta()); } Console.WriteLine("W " + rfl.Get_w()[0, 0] + "\t" + "b " + rfl.Get_b()[0, 0]); Console.Write("x\t\t"); foreach (double[,] x in list_x) { Console.Write(x[0, 0] + "\t"); } Console.WriteLine(" "); Console.Write("wx + b\t\t"); foreach (double[,] x in list_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("f(wx + b)\t"); foreach (double[,] x in list_f_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("t\t\t"); foreach (double[,] t in list_t) { Console.Write(t[0, 0] + "\t"); } Console.WriteLine(" "); Console.WriteLine("\n\n"); }
public void Demo_007_Neural_Network_3() { Console.WriteLine("Neural Networkのデモンストレーションです"); Console.WriteLine("今回は1層では学習できない例を紹介します\n\n"); //教師データを設定します。 //簡単のためf(x)の一次元とします。 List <double[, ]> list_x = new List <double[, ]>(); List <double[, ]> list_t = new List <double[, ]>(); for (int j = 0; j < 6; j++) { double[,] x = new double[1, 1]; x[0, 0] = j * 1.0; list_x.Add(x); double[,] t = new double[1, 1]; if (j < 2 || 3 < j) { t[0, 0] = 1; } else { t[0, 0] = 0; } list_t.Add(t); } Console.WriteLine("今回の入力値xと、答えtです"); Console.WriteLine("x < 2 , 3 < x のとき、答えが1になります。それ以外の場合、0です。"); Console.Write("x\t"); foreach (double[,] x in list_x) { Console.Write(x[0, 0] + "\t"); } Console.WriteLine("\t"); Console.Write("t\t"); foreach (double[,] t in list_t) { Console.Write(t[0, 0] + "\t"); } Console.WriteLine("\n\n"); Console.WriteLine("まずは1層のNeural Networkで試してみます"); Console.WriteLine("計数行列Wとバイアスベクトルbを設定します。"); double[,] W_f = new double[1, 1]; W_f[0, 0] = 3; double[,] b_f = new double[1, 1]; b_f[0, 0] = 2; Console.WriteLine("W[0,0] = " + W_f[0, 0] + "\t" + "b[0,0] = " + b_f[0, 0] + "\n\n"); Console.WriteLine("Neural Netoworkを設定しました。今回の活性化関数はSigmoid関数とします"); Console.WriteLine("ハイパーパラメータの値は0.1とします。\n"); Regression_Final_Layer rfl = new Regression_Final_Layer(); rfl.Preset_1_4th_Set_w(W_f); rfl.Preset_2_4th_Set_b(b_f); rfl.Preset_3_4th_Set_Hyper_Parameter(0.3, 0, 0.01, 0); rfl.Preset_4_4th_Set_activation_Function(new Sigmoid_IFunction()); Console.WriteLine("学習前のNeural Networkの出力を確認します。"); List <double[, ]> list_wx_plus_b = new List <double[, ]>(); List <double[, ]> list_f_wx_plus_b = new List <double[, ]>(); List <double[, ]> list_delta = new List <double[, ]>(); for (int j = 0; j < list_x.Count; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j]); list_wx_plus_b.Add(rfl.Get_wx_plus_b()); list_f_wx_plus_b.Add(rfl.Get_f_wx_plus_b()); } Console.WriteLine("W " + rfl.Get_w()[0, 0] + "\t" + "b " + rfl.Get_b()[0, 0]); Console.Write("x\t\t"); foreach (double[,] x in list_x) { Console.Write(x[0, 0] + "\t"); } Console.WriteLine(" "); Console.Write("wx + b\t\t"); foreach (double[,] x in list_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("f(wx + b)\t"); foreach (double[,] x in list_f_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("t\t\t"); foreach (double[,] t in list_t) { Console.Write(t[0, 0] + "\t"); } Console.WriteLine("\n\n"); Console.WriteLine("100,000 epoch 学習しました。"); int epoch = 100000; for (int j = 0; j < list_x.Count * epoch; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j % list_x.Count]); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j % list_x.Count]); rfl.Step_3_3rd_Update(); } list_wx_plus_b = new List <double[, ]>(); list_f_wx_plus_b = new List <double[, ]>(); list_delta = new List <double[, ]>(); for (int j = 0; j < list_x.Count; j++) { rfl.Step_1_3rd_Forward_Propagation(list_x[j]); list_wx_plus_b.Add(rfl.Get_wx_plus_b()); list_f_wx_plus_b.Add(rfl.Get_f_wx_plus_b()); rfl.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j]); list_delta.Add(rfl.Get_delta()); } Console.WriteLine("W " + rfl.Get_w()[0, 0] + "\t" + "b " + rfl.Get_b()[0, 0]); Console.Write("x\t\t"); foreach (double[,] x in list_x) { Console.Write(x[0, 0] + "\t"); } Console.WriteLine(" "); Console.Write("wx + b\t\t"); foreach (double[,] x in list_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("f(wx + b)\t"); foreach (double[,] x in list_f_wx_plus_b) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("t\t\t"); foreach (double[,] t in list_t) { Console.Write(t[0, 0] + "\t"); } Console.WriteLine(" "); Console.WriteLine("\n\n\n"); Console.WriteLine("次は、2層のNeural Networkで試してみます"); Console.WriteLine("第1層の計数行列はwは2行1列の行列とします"); Console.WriteLine("活性化関数はSwish関数とします"); double[,] w_1 = new double[2, 1]; w_1[0, 0] = 3; w_1[1, 0] = -3; Console.WriteLine("第1層の計数行列 w"); this.Show_Matrix_Element(w_1); double[,] b_1 = new double[2, 1]; b_1[0, 0] = 2; b_1[1, 0] = -2; Console.WriteLine("第1層のバイアスベクトル b"); this.Show_Matrix_Element(b_1); //第一層は隠れ層なので、別のクラスです Hidden_Layer hd_1 = new Hidden_Layer(); hd_1.Preset_1_4th_Set_w(w_1); hd_1.Preset_2_4th_Set_b(b_1); hd_1.Preset_3_4th_Set_Hyper_Parameter(0.15, 0, 0, 0); hd_1.Preset_4_4th_Set_activation_Function(new Swish_IFunction()); Console.WriteLine("\n"); Console.WriteLine("第2層の計数行列はwは1行2列の行列とします"); Console.WriteLine("活性化関数は、第2層はSigmoid関数とします"); double[,] w_2 = new double[1, 2]; w_2[0, 0] = 3; w_2[0, 1] = -3; Console.WriteLine("第2層の計数行列 w"); this.Show_Matrix_Element(w_2); double[,] b_2 = new double[1, 1]; b_2[0, 0] = 2; Console.WriteLine("第2層のバイアスベクトル b"); this.Show_Matrix_Element(b_2); Console.WriteLine("\n\n"); Regression_Final_Layer rfl_2 = new Regression_Final_Layer(); rfl_2.Preset_1_4th_Set_w(w_2); rfl_2.Preset_2_4th_Set_b(b_2); rfl_2.Preset_3_4th_Set_Hyper_Parameter(0.15, 0, 0, 0); rfl_2.Preset_4_4th_Set_activation_Function(new Sigmoid_IFunction()); Console.WriteLine("\n\n"); Console.WriteLine("学習前のNeural Networkの出力を確認します。"); List <double[, ]> list_wx_plus_b_1 = new List <double[, ]>(); List <double[, ]> list_f_wx_plus_b_1 = new List <double[, ]>(); List <double[, ]> list_wx_plus_b_2 = new List <double[, ]>(); List <double[, ]> list_f_wx_plus_b_2 = new List <double[, ]>(); for (int j = 0; j < list_x.Count; j++) { //順伝搬 hd_1.Step_1_3rd_Forward_Propagation(list_x[j]); rfl_2.Step_1_3rd_Forward_Propagation(hd_1.Get_f_wx_plus_b()); list_wx_plus_b_1.Add(hd_1.Get_wx_plus_b()); list_f_wx_plus_b_1.Add(hd_1.Get_f_wx_plus_b()); list_wx_plus_b_2.Add(rfl_2.Get_wx_plus_b()); list_f_wx_plus_b_2.Add(rfl_2.Get_f_wx_plus_b()); } Console.WriteLine("第1層のW "); this.Show_Matrix_Element(hd_1.Get_w()); Console.WriteLine("第1層のb "); this.Show_Matrix_Element(hd_1.Get_b()); Console.Write("x\t\t\t"); foreach (double[,] x in list_x) { Console.Write(x[0, 0] + "\t"); } Console.WriteLine(" "); Console.Write("第1層の(wx + b)\t\t"); foreach (double[,] x in list_wx_plus_b_1) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.Write("\n \t\t\t"); foreach (double[,] x in list_wx_plus_b_1) { Console.Write(x[1, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("第1層のf(wx + b)\t"); foreach (double[,] x in list_f_wx_plus_b_1) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.Write("\n \t\t\t"); foreach (double[,] x in list_f_wx_plus_b_1) { Console.Write(x[1, 0].ToString("G3") + "\t"); } Console.WriteLine("\n"); Console.WriteLine("第2層のW "); this.Show_Matrix_Element(rfl_2.Get_w()); Console.WriteLine("第2層のb "); this.Show_Matrix_Element(rfl_2.Get_b()); Console.Write("第2層の(wx + b)\t\t"); foreach (double[,] x in list_wx_plus_b_2) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("第2層のf(wx + b)\t"); foreach (double[,] x in list_f_wx_plus_b_2) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("t\t\t\t"); foreach (double[,] t in list_t) { Console.Write(t[0, 0] + "\t"); } Console.WriteLine("\n\n"); Console.WriteLine("100,000 epoch 学習しました。"); epoch = 100000; for (int j = 0; j < list_x.Count * epoch; j++) { //順伝搬 hd_1.Step_1_3rd_Forward_Propagation(list_x[j % list_x.Count]); rfl_2.Step_1_3rd_Forward_Propagation(hd_1.Get_f_wx_plus_b()); //逆伝搬 rfl_2.Step_2_3rd_Calculate_Target_Function_and_Delta(list_t[j % list_x.Count]); hd_1.Step_2_3rd_Calculate_Delta(rfl_2.Get_w(), rfl_2.Get_delta()); //パラメータの更新 rfl_2.Step_3_3rd_Update(); hd_1.Step_3_3rd_Update(); } list_wx_plus_b_1 = new List <double[, ]>(); list_f_wx_plus_b_1 = new List <double[, ]>(); list_wx_plus_b_2 = new List <double[, ]>(); list_f_wx_plus_b_2 = new List <double[, ]>(); for (int j = 0; j < list_x.Count; j++) { //順伝搬 hd_1.Step_1_3rd_Forward_Propagation(list_x[j]); rfl_2.Step_1_3rd_Forward_Propagation(hd_1.Get_f_wx_plus_b()); list_wx_plus_b_1.Add(hd_1.Get_wx_plus_b()); list_f_wx_plus_b_1.Add(hd_1.Get_f_wx_plus_b()); list_wx_plus_b_2.Add(rfl_2.Get_wx_plus_b()); list_f_wx_plus_b_2.Add(rfl_2.Get_f_wx_plus_b()); } Console.WriteLine("第1層のW "); this.Show_Matrix_Element(hd_1.Get_w()); Console.WriteLine("第1層のb "); this.Show_Matrix_Element(hd_1.Get_b()); Console.Write("x\t\t\t"); foreach (double[,] x in list_x) { Console.Write(x[0, 0] + "\t"); } Console.WriteLine(" "); Console.Write("第1層の(wx + b)\t\t"); foreach (double[,] x in list_wx_plus_b_1) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.Write("\n \t\t\t"); foreach (double[,] x in list_wx_plus_b_1) { Console.Write(x[1, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("第1層のf(wx + b)\t"); foreach (double[,] x in list_f_wx_plus_b_1) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.Write("\n \t\t\t"); foreach (double[,] x in list_f_wx_plus_b_1) { Console.Write(x[1, 0].ToString("G3") + "\t"); } Console.WriteLine("\n"); Console.WriteLine("第2層のW "); this.Show_Matrix_Element(rfl_2.Get_w()); Console.WriteLine("第2層のb "); this.Show_Matrix_Element(rfl_2.Get_b()); Console.Write("第2層の(wx + b)\t\t"); foreach (double[,] x in list_wx_plus_b_2) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("第2層のf(wx + b)\t"); foreach (double[,] x in list_f_wx_plus_b_2) { Console.Write(x[0, 0].ToString("G3") + "\t"); } Console.WriteLine(" "); Console.Write("t\t\t\t"); foreach (double[,] t in list_t) { Console.Write(t[0, 0] + "\t"); } Console.WriteLine("\n\n"); }