(处理)梯度下降算法不起作用..(线性回归)

时间:2014-12-23 10:05:43

标签: processing gradient regression linear gradient-descent

我已经使用Gradient下降算法堆积了一周。 我使用Processing 2.2.1进行编码,我希望在收敛之前显示动画。 我不擅长英语,所以很难描述这个问题。 如果你在下面构建代码会很棒。

使用渐变下降与处理的线性回归

[步骤]

  1. 点击鼠标15次 - > x,y鼠标坐标成为线性回归的输入数据
  2. 按键盘上的按钮
  3. 开始线性回归
  4. [问题]

    成本函数的价值......没有收敛,除此之外我认为价值是大的。 参数theta0,1 ...需要花费很多时间来适应输入数据。

    这是我的代码。我的算法在draw()中有什么问题吗?

    int num = 15;
    double[] x = new double[num];
    double[] y = new double[num];
    float[] xx = new float[num];
    float[] yy = new float[num];
    double[] hy = new double[num];
    float[] hy1 = new float[num];
    float theta0=0;
    float theta1, theta2=0;
    float alpha=1.0E-4;
    float pd0, pd1, pdd= 0;
    int r=0;
    int width=600, height=600;
    int count, check= 0;
    float temp0, temp1, temp2 =0;
    boolean flag;
    float xmin, hymin, xmax, hymax, ymax, ymin, hy1max, hy1min=0;
    float xsum, ysum, xsd, ysd, xave, yave=0;
    
    void setup() {
      frameRate(300);
      size(width, height);
      // scale(1, -1);
      noStroke();
      smooth();
      fill(255);
      theta0=0;
      theta1=0;
      //  theta2=0;
      pd0=0;
      pd1=0;
      pdd=0;
    }
    
    
    void draw() {
    
      background(0);
      coordinates();
    
      //Center
      translate(width/2, height/2);
    
      for (int ii = 0; ii<num; ii++) {
        if (x[ii]!=0&&y[ii]!=0) {
          fill(255);
          ellipse((float)x[ii], (float)-y[ii], 5, 5);
        }
      }
    
      //noLoop();
      if (flag==true) {
    
        for (int i =0; i<num; i++) {
          hy[i]=theta0+theta1*x[i];
        }
    
        for (int i =0; i<num; i++) {
          pd0+=(hy[i]-y[i]); 
          pd1+=(hy[i]-y[i])*x[i];
          pdd+=(hy[i]-y[i])*(hy[i]-y[i]);
        }
    
        pdd=pdd/(2.00*num);
    
        if ( !((theta0-temp0)<=1.0E-4 && (theta0-temp0)>=-1.0E-4)||((theta1-temp1)<=1.0E-4 && (theta1-temp1)>=-1.0E-4)) {
          if (check!=0)
            if (check<=500) 
    
          temp0=theta0-(alpha/int(num))*pd0;
          temp1=theta1-(alpha/int(num))*pd1;
    
          theta0=temp0;
          theta1=temp1;
    
          check++;
          println(pdd+","+check);
    
          for (int i =0; i<num; i++) {
            fill(255, 0, 0);
            println("x"+ x[i] +","+"y:"+y[i]+ "hy"+hy[i]+"cost F:"+pd0, pd1+"theta0,1:"+theta0+","+theta1+":alpha"+alpha+"check:"+check+"pdd:"+","+pdd);
            ellipse((int)x[i], (int)-hy[i], 3, 3);
          }
        }//if fin
        else {
          println("fin: pd is "+pd0, pd1+"Theta0"+theta0+", Theta1:"+theta1);
          for (int i =0; i<num; i++) {
            fill(255, 0, 0);       
            ellipse((int)x[i], (int)-hy[i], 5, 5);
          }
    
          noLoop();
        }
      }
    }
    
    void coordinates() {
    
      for (int i=-width; i<width; i+=20) {
        for (int j=-height; j<height; j+=20) {
          stroke(255, 2);
          strokeWeight(1/2);
          line(0, height/2+j, width, height/2+j);
          line(i, 0, i, height);
        }
      }
      noStroke();
      stroke(255, 0, 0);
      strokeWeight(2);
      line(0, height/2, width, height/2);
      stroke(0, 255, 0);
      line(width/2, 0, width/2, height);
      noStroke();
    }
    
    void mousePressed() {
    
      y[count] =   -mouseY+height/2;
      x[count] =   constrain(mouseX-width/2, -width/2, width/2);
      count++;
      if (count==num) {
        //flag=true;
    
        //normalization false
        for (int i =0; i<num; i++) {
          hy[i]=theta0+theta1*x[i];
          xsum+=x[i];
          ysum+=y[i];
        }
    
        for (int i =0; i<num; i++) {
          println(x[i]+","+y[i]);
        }
    
        println("COSTf"+pd0+","+pd1);
        println("COMPLETE");
      }
      if (count>num) {
        count = 0;
        // initsa();
        flag=false;
      }
    }
    
    void keyPressed() {
      if (key=='s') {
        background(0);
        for (int i=0; i<num; i++) {
          println(x[i]+","+y[i]);
        }
      }
      if (key=='q') {
        background(0);
        // initsa();
      }
      if (key=='w') {
        flag=true;
      }
      if (key=='d') {
        flag=false;
      }
      if (keyCode==UP) {
        alpha+=0.00001;
      }
      if (keyCode==DOWN) {
        alpha-=0.00001;
      }
    }
    

1 个答案:

答案 0 :(得分:0)

关于使用梯度下降的多元线性回归的文章,http://melwin-jose.blogspot.com/2015/01/multivariate-linear-regression.html。来自HackerRank的sample code problem。我希望这会有所帮助。