Monday, June 13, 2016

Gradient Descent in Java fitting a quadratic function

Here we will use a modification to fit a quadratic line to our data set. The line will be of the form:

(theta0)x^2 + (theta1)x + (theta0)n

We will be using java and the source code is below. Simple make a class called GradDescent, paste the code and run.

public class GradDescent {

    public static void main(String[] args) {
   
         float x[] = {1f,2f,3f,4f,5f,6f,7f,8f,9f,10f};  
         float y[] = {2f,5f,10f,17f,26f,37f,50f,65f,82f,101f};  
      
         /*number of examples*/
         int m = 10;
        
          /*thetas and temp thetas*/
            float theta0, temp0, theta1, temp1, theta2, temp2;
            theta0 = 0.0f; temp0 = 0.0f;
            theta1 = 0.0f; temp1 = 0.0f;
            theta2 = 0.0f; temp2 = 0.0f;
        
        /*# of iterations and learning rate*/
            int iterations = 3430000;
            float alpha = 0.003f;

            int j = 0;
            float h0 = 0.0f;
            float h1 = 0.0f;
            float h2 = 0.0f;
            int i = 0;
            for(i = 0; i < iterations; i++)
            {
        
                h0 = 0.0f;
                h1 = 0.0f;
                h2 = 0.0f;
                for(j = 0; j<m; j++)
                {
                    h0 = h0 + ((theta0 + x[j]*theta1 +  x[j]*x[j]*theta2) - y[j]);
                    h1 = h1 + ((theta0 + x[j]*theta1 +  x[j]*x[j]*theta2) - y[j])*x[j];
                    h2 = h2 + ((theta0 + x[j]*theta1 +  x[j]*x[j]*theta2) - y[j])*x[j];
                }   
        temp0 = theta0 - (alpha*h0)/(float)m;
        temp1 = theta1 - (alpha*h1)/(float)m;
        temp2 = theta2 - (alpha*h2)/(float)m;
        theta0 = temp0;
        theta1 = temp1;
        theta2 = temp2;
        }
          
       System.out.println("" + theta2 + "x^2 + " + theta1 + "x + " + theta0);
     
       testGradientDescent(5f, theta0, theta1, theta2);

    }
   
    private static void testGradientDescent(float n, float theta0, float theta1, float theta2)
    {
        float result = theta0 + (theta1*n) + (theta2*n*n);
        System.out.println("Result: " + result);
    }

}


Running it with sample input shows that it fits quite nicely.

No comments:

Post a Comment