march alex's blog
          hello,I am march alex
          posts - 52,comments - 7,trackbacks - 0

          實(shí)現(xiàn)時(shí)遇到一個(gè)問(wèn)題,就是fi的值的設(shè)定問(wèn)題,因?yàn)槲覀儾捎秒S機(jī)梯度下降法,一方面這節(jié)省了時(shí)間,但是如果fi值亙古不變的話(huà)可能會(huì)面臨跳來(lái)跳去一直找不到答案的問(wèn)題,所以我這里設(shè)定他得知在每一輪之后都會(huì)按比例減小(fi *= 0.5;),大家可以按自己的喜好自由設(shè)定。
          import java.util.Scanner;


          public class Perceptron {
              private static int N = 3;
              private static int n = 2;
              private static double[][] X = null;
              private static double[] Y = null;
              private static double[] W = null;
              private static double B = 0;
              private static double fi = 0.5;
              
              private static boolean check(int id) {
                  double ans = B;
                  for(int i=0;i<n;i++)
                      ans += X[id][i] * W[i];
                  if(ans * Y[id] > 0) return true;
                  return false;
              }
              
              private static void debug() {
                  System.out.print("debug: W");
                  for(int i=0;i<n;i++) System.out.print(W[i] + " ");
                  System.out.println("/ B : " + B);
              }
              
              public static void solve() {
                  Scanner in = new Scanner(System.in);
                  System.out.print("input N:"); N = in.nextInt();
                  System.out.print("input n:"); n = in.nextInt();
                  
                  X = new double[N][n];
                  Y = new double[N];
                  W = new double[n];
                  
                  System.out.println("input N * n datas X[i][j]:");
                  for(int i=0;i<N;i++)
                      for(int j=0;j<n;j++)
                          X[i][j] = in.nextDouble();
                  System.out.println("input N datas Y[i]");
                  for(int i=0;i<N;i++) 
                      Y[i] = in.nextDouble();
                  
                  
                  for(int i=0;i<n;i++) W[i] = 0;
                  B = 0;
                  
                  boolean ok = true;
                  while(ok == true) {
                      ok = false;
                      //這里在原來(lái)算法的基礎(chǔ)上不斷地將fi縮小,以避免跳來(lái)跳去一直達(dá)不到要求的點(diǎn)的效果。
                      for(int i=0;i<N;i++) {
                          //System.out.println("here " + i);
                          while(check(i) == false) {
                              ok = true;
                              for(int j=0;j<n;j++)
                                  W[j] += fi * Y[i] * X[i][j];
                              B += fi * Y[i];
                              //debug();
                          }
                      }
                      fi *= 0.5;
                  }
              }
              
              public static void main(String[] args) {
                  solve();
                  System.out.print("W = [");
                  for(int i=0;i<n-1;i++) System.out.print(W[i] + ", ");
                  System.out.println(W[n-1] + "]");
                  System.out.println("B = " + B);
              }
          }

          posted on 2015-03-20 11:08 marchalex 閱讀(636) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): java小程序
          主站蜘蛛池模板: 额济纳旗| 长泰县| 余江县| 河津市| 兰州市| 烟台市| 镇雄县| 永善县| 澄城县| 安仁县| 龙口市| 佛教| 大理市| 开封市| 视频| 长垣县| 行唐县| 溆浦县| 拜城县| 枣庄市| 连平县| 江源县| 上饶县| 定日县| 车致| 商河县| 雷州市| 祥云县| 乌兰浩特市| 敦煌市| 闵行区| 雷波县| 麦盖提县| 饶河县| 绩溪县| 高碑店市| 达拉特旗| 抚宁县| 康定县| 滨海县| 健康|