First, download CodeBlocks-EP at http://codeblocks.codecutter.org/
Note that this is a different program than the regular CodeBlocks but I am pretty sure it has the same functionality. It seems to be a version to learn from. Install it and open it.
Step 2: Select "koolplot project" and click "Go"
Step 3: Select "Add Console" It is not the default so make sure that you select it and then hit next.
Step 4: Name your project "GradientDescent" and click next. On the next screen, click finish.
Step 5: Go to File -> New -> Empty file. Name it GradientDescent.c
Step 6: Right click and select edit and paste the following code:
#include "koolplot.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char**argv) {
/*training set1.2
1.1
2.4 2.2
3.1 3.3
4.4 4.3
5.3 5.4
6.3 6.7
7.6 7.7
8.7 8.6
9.7 9.8*/
float x[] = {1.2, 2.4, 3.1, 4.4, 5.3, 6.3, 7.6, 8.7, 9.7};
float y[] = {1.1, 2.2, 3.3, 4.3, 5.4, 6.7, 7.7, 8.6, 9.8};
/*number of examples*/
int m = 9;
/*thetas and temp thetas*/
float theta0, temp0, theta1, temp1;
theta0 = 0.0; temp0 = 0.0;
theta1 = 0.0; temp1 = 0.0;
/*iterations and learning rate HIGHLY VARIABLE*/
int iterations = 1000;
float alpha = 0.03;
int j = 0;
float h0 = 0.0;
float h1 = 0.0;
int i = 0;
for(i = 0; i < iterations; i++)
{
h0 = 0.0;
h1 = 0.0;
for(j = 0; j<m; j++)
{
h0 = h0 + ((theta0 + x[j]*theta1) - y[j]);
h1 = h1 + ((theta0 + x[j]*theta1) - y[j])*x[j];
}
//h0 = h0 / (float)m;
//h1 = h1 / (float)m;
temp0 = theta0 - (alpha*h0)/(float)m;
temp1 = theta1 - (alpha*h1)/(float)m;
theta0 = temp0;
theta1 = temp1;
}
printf("Theta0: %f\n", theta0);
printf("Theta1: %f\n", theta1);
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char**argv) {
/*training set1.2
1.1
2.4 2.2
3.1 3.3
4.4 4.3
5.3 5.4
6.3 6.7
7.6 7.7
8.7 8.6
9.7 9.8*/
float x[] = {1.2, 2.4, 3.1, 4.4, 5.3, 6.3, 7.6, 8.7, 9.7};
float y[] = {1.1, 2.2, 3.3, 4.3, 5.4, 6.7, 7.7, 8.6, 9.8};
/*number of examples*/
int m = 9;
/*thetas and temp thetas*/
float theta0, temp0, theta1, temp1;
theta0 = 0.0; temp0 = 0.0;
theta1 = 0.0; temp1 = 0.0;
/*iterations and learning rate HIGHLY VARIABLE*/
int iterations = 1000;
float alpha = 0.03;
int j = 0;
float h0 = 0.0;
float h1 = 0.0;
int i = 0;
for(i = 0; i < iterations; i++)
{
h0 = 0.0;
h1 = 0.0;
for(j = 0; j<m; j++)
{
h0 = h0 + ((theta0 + x[j]*theta1) - y[j]);
h1 = h1 + ((theta0 + x[j]*theta1) - y[j])*x[j];
}
//h0 = h0 / (float)m;
//h1 = h1 / (float)m;
temp0 = theta0 - (alpha*h0)/(float)m;
temp1 = theta1 - (alpha*h1)/(float)m;
theta0 = temp0;
theta1 = temp1;
}
printf("Theta0: %f\n", theta0);
printf("Theta1: %f\n", theta1);
Plotdata xx(0.0, 3.0), yy = theta0 + xx*theta1;
plot(xx, yy);
return 0;
}
Step 7: Select Build -> Build and then Build -> Run
Lastly, if you followed all of these steps it should run. The command console will show the values of theta0 and theta1 and the plot with plot out "y = theta0 + x*theta1" Note that I had to use "xx" and "yy" at the end because these are the variables that koolplot is using to plot and x and y were already used to store our training data. If you click on the plot, you will be able to see the x and y values above. Here is what the output should look like:
And that's it.
No comments:
Post a Comment